2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.java.j2seplatform;
43
import java.io.IOException;
44
import java.util.concurrent.Callable;
45
import java.util.logging.Level;
46
import java.util.logging.Logger;
47
import java.util.logging.Logger;
48
import org.openide.cookies.InstanceCookie;
49
import org.openide.filesystems.FileObject;
50
import org.openide.filesystems.Repository;
51
import org.openide.loaders.DataObject;
52
import org.openide.modules.ModuleInstall;
53
import org.openide.util.Exceptions;
54
import org.openide.util.Exceptions;
55
import org.netbeans.api.java.platform.JavaPlatform;
56
import org.netbeans.api.java.platform.JavaPlatformManager;
57
import org.netbeans.api.java.platform.Specification;
58
import org.netbeans.api.project.ProjectManager;
59
import org.netbeans.spi.project.support.ant.EditableProperties;
60
import org.netbeans.spi.project.support.ant.PropertyUtils;
61
import org.netbeans.modules.java.j2seplatform.platformdefinition.PlatformConvertor;
62
import org.netbeans.modules.java.j2seplatform.platformdefinition.J2SEPlatformImpl;
65
public class J2SEPlatformModule extends ModuleInstall {
67
private static final String DEFAULT_PLATFORM = "Services/Platforms/org-netbeans-api-java-Platform/default_platform.xml"; //NOI18N
69
public void restored() {
71
// update source level and J2SE platforms in build.properties file
72
updateBuildProperties();
75
// implemented as separate method for simpler unit testing
76
public static void updateBuildProperties() {
77
ProjectManager.mutex().postWriteRequest(
81
recoverDefaultPlatform ();
82
EditableProperties ep = PropertyUtils.getGlobalProperties();
83
boolean save = updateSourceLevel(ep);
84
save |= updateBuildProperties (ep);
86
PropertyUtils.putGlobalProperties (ep);
88
} catch (IOException ioe) {
89
Exceptions.printStackTrace(ioe);
95
private static boolean updateSourceLevel(EditableProperties ep) {
96
JavaPlatform platform = JavaPlatformManager.getDefault().getDefaultPlatform();
97
String ver = platform.getSpecification().getVersion().toString();
98
if (!ver.equals(ep.getProperty("default.javac.source"))) { //NOI18N
99
ep.setProperty("default.javac.source", ver); //NOI18N
100
ep.setProperty("default.javac.target", ver); //NOI18N
108
private static boolean updateBuildProperties (EditableProperties ep) {
109
boolean changed = false;
110
JavaPlatform[] installedPlatforms = JavaPlatformManager.getDefault().getPlatforms(null, new Specification ("j2se",null)); //NOI18N
111
for (int i=0; i<installedPlatforms.length; i++) {
112
//Handle only platforms created by this module
113
if (!installedPlatforms[i].equals (JavaPlatformManager.getDefault().getDefaultPlatform()) && installedPlatforms[i] instanceof J2SEPlatformImpl) {
114
String systemName = ((J2SEPlatformImpl)installedPlatforms[i]).getAntName();
115
String key = PlatformConvertor.createName(systemName,"home"); //NOI18N
116
if (!ep.containsKey (key)) {
118
PlatformConvertor.generatePlatformProperties(installedPlatforms[i], systemName, ep);
120
} catch (PlatformConvertor.BrokenPlatformException b) {
121
Logger.getLogger(J2SEPlatformModule.class.getName()).info("Platform: " + installedPlatforms[i].getDisplayName() +" is missing: " + b.getMissingTool());
122
} catch (IOException ioe) {
123
Exceptions.printStackTrace(ioe);
131
private static void recoverDefaultPlatform () {
132
final FileObject defaultPlatform = Repository.getDefault().getDefaultFileSystem().findResource(DEFAULT_PLATFORM);
133
if (defaultPlatform != null) {
135
DataObject dobj = DataObject.find(defaultPlatform);
136
boolean valid = false;
137
InstanceCookie ic = (InstanceCookie) dobj.getCookie(InstanceCookie.class);
142
} catch (Exception e) {
143
//Ignore it, logged bellow
147
Logger.getLogger("global").log(Level.WARNING,"default_platform.xml is broken, regenerating.");
148
Object attr = defaultPlatform.getAttribute("removeWritables"); //NOI18N
149
if (attr instanceof Callable) {
150
((Callable)attr).call ();
153
} catch (Exception e) {
154
Exceptions.printStackTrace(e);
158
Logger.getLogger("global").log(Level.WARNING,"The default platform is hidden."); //NOI18N