KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seplatform > J2SEPlatformModule


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.j2seplatform;
20
21 import java.io.IOException JavaDoc;
22 import java.util.concurrent.Callable JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import org.openide.cookies.InstanceCookie;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.Repository;
29 import org.openide.loaders.DataObject;
30 import org.openide.modules.ModuleInstall;
31 import org.openide.util.Exceptions;
32 import org.openide.util.Exceptions;
33 import org.netbeans.api.java.platform.JavaPlatform;
34 import org.netbeans.api.java.platform.JavaPlatformManager;
35 import org.netbeans.api.java.platform.Specification;
36 import org.netbeans.api.project.ProjectManager;
37 import org.netbeans.spi.project.support.ant.EditableProperties;
38 import org.netbeans.spi.project.support.ant.PropertyUtils;
39 import org.netbeans.modules.java.j2seplatform.platformdefinition.PlatformConvertor;
40 import org.netbeans.modules.java.j2seplatform.platformdefinition.J2SEPlatformImpl;
41
42
43 public class J2SEPlatformModule extends ModuleInstall {
44     
45     private static final String JavaDoc DEFAULT_PLATFORM = "Services/Platforms/org-netbeans-api-java-Platform/default_platform.xml"; //NOI18N
46

47     public void restored() {
48         super.restored();
49         // update source level and J2SE platforms in build.properties file
50
updateBuildProperties();
51     }
52
53     // implemented as separate method for simpler unit testing
54
public static void updateBuildProperties() {
55         ProjectManager.mutex().postWriteRequest(
56             new Runnable JavaDoc () {
57                 public void run () {
58                     try {
59                         recoverDefaultPlatform ();
60                         EditableProperties ep = PropertyUtils.getGlobalProperties();
61                         boolean save = updateSourceLevel(ep);
62                         save |= updateBuildProperties (ep);
63                         if (save) {
64                             PropertyUtils.putGlobalProperties (ep);
65                         }
66                     } catch (IOException JavaDoc ioe) {
67                         Exceptions.printStackTrace(ioe);
68                     }
69                 }
70             });
71     }
72     
73     private static boolean updateSourceLevel(EditableProperties ep) {
74         JavaPlatform platform = JavaPlatformManager.getDefault().getDefaultPlatform();
75         String JavaDoc ver = platform.getSpecification().getVersion().toString();
76         if (!ver.equals(ep.getProperty("default.javac.source"))) { //NOI18N
77
ep.setProperty("default.javac.source", ver); //NOI18N
78
ep.setProperty("default.javac.target", ver); //NOI18N
79
return true;
80         } else {
81             return false;
82         }
83     }
84
85
86     private static boolean updateBuildProperties (EditableProperties ep) {
87         boolean changed = false;
88         JavaPlatform[] installedPlatforms = JavaPlatformManager.getDefault().getPlatforms(null, new Specification ("j2se",null)); //NOI18N
89
for (int i=0; i<installedPlatforms.length; i++) {
90             //Handle only platforms created by this module
91
if (!installedPlatforms[i].equals (JavaPlatformManager.getDefault().getDefaultPlatform()) && installedPlatforms[i] instanceof J2SEPlatformImpl) {
92                 String JavaDoc systemName = ((J2SEPlatformImpl)installedPlatforms[i]).getAntName();
93                 String JavaDoc key = PlatformConvertor.createName(systemName,"home"); //NOI18N
94
if (!ep.containsKey (key)) {
95                     try {
96                         PlatformConvertor.generatePlatformProperties(installedPlatforms[i], systemName, ep);
97                         changed = true;
98                     } catch (PlatformConvertor.BrokenPlatformException b) {
99                         Logger.getLogger(J2SEPlatformModule.class.getName()).info("Platform: " + installedPlatforms[i].getDisplayName() +" is missing: " + b.getMissingTool());
100                     } catch (IOException JavaDoc ioe) {
101                         Exceptions.printStackTrace(ioe);
102                   }
103                 }
104             }
105         }
106         return changed;
107     }
108
109     private static void recoverDefaultPlatform () {
110         final FileObject defaultPlatform = Repository.getDefault().getDefaultFileSystem().findResource(DEFAULT_PLATFORM);
111         if (defaultPlatform != null) {
112             try {
113                 DataObject dobj = DataObject.find(defaultPlatform);
114                 boolean valid = false;
115                 InstanceCookie ic = (InstanceCookie) dobj.getCookie(InstanceCookie.class);
116                 if (ic != null) {
117                     try {
118                         ic.instanceCreate();
119                         valid = true;
120                     } catch (Exception JavaDoc e) {
121                         //Ignore it, logged bellow
122
}
123                 }
124                 if (!valid) {
125                     Logger.getLogger("global").log(Level.WARNING,"default_platform.xml is broken, regenerating.");
126                     Object JavaDoc attr = defaultPlatform.getAttribute("removeWritables"); //NOI18N
127
if (attr instanceof Callable JavaDoc) {
128                         ((Callable JavaDoc)attr).call ();
129                     }
130                 }
131             } catch (Exception JavaDoc e) {
132                 Exceptions.printStackTrace(e);
133             }
134         }
135         else {
136             Logger.getLogger("global").log(Level.WARNING,"The default platform is hidden."); //NOI18N
137
}
138     }
139 }
140
Popular Tags