KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > jdkselection > JdkConfiguration


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
20 package org.netbeans.modules.java.freeform.jdkselection;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.netbeans.api.java.platform.JavaPlatform;
29 import org.netbeans.api.java.platform.JavaPlatformManager;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectManager;
32 import org.netbeans.api.project.ProjectUtils;
33 import org.netbeans.modules.ant.freeform.spi.support.Util;
34 import org.netbeans.modules.java.freeform.JavaProjectGenerator;
35 import org.netbeans.spi.project.support.ant.AntProjectHelper;
36 import org.netbeans.spi.project.support.ant.EditableProperties;
37 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
38 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
39 import org.openide.filesystems.FileLock;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileSystem;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.xml.XMLUtil;
44 import org.w3c.dom.Document JavaDoc;
45 import org.w3c.dom.Element JavaDoc;
46 import org.w3c.dom.NodeList JavaDoc;
47
48 /**
49  * Manages JDK configuration on disk.
50  * @see "issue #64160"
51  * @author Jesse Glick
52  */

53 public class JdkConfiguration {
54
55     private static final String JavaDoc NS_GENERAL = "http://www.netbeans.org/ns/project/1"; // NOI18N
56
private static final String JavaDoc NBJDK_PROPERTIES = "nbproject/nbjdk.properties"; // NOI18N
57
private static final String JavaDoc NBJDK_ACTIVE = "nbjdk.active"; // NOI18N
58
public static final String JavaDoc NBJDK_XML = "nbproject/nbjdk.xml"; // NOI18N
59
public static final String JavaDoc JDK_XML = "nbproject/jdk.xml"; // NOI18N
60
private static final String JavaDoc PLATFORM_ID_DEFAULT = "default_platform"; // NOI18N
61

62     private final Project project;
63     private final AntProjectHelper helper;
64     private final PropertyEvaluator evaluator;
65
66     public JdkConfiguration(Project project, AntProjectHelper helper, PropertyEvaluator evaluator) {
67         this.project = project;
68         this.helper = helper;
69         this.evaluator = evaluator;
70     }
71
72     /**
73      * Initialize a project to use a selected JDK.
74      * No-op for projects already initialized in this way.
75      * Initially does not select a particular JDK (i.e. uses default).
76      * @param p a freeform project to initialize with jdk.xml and so on
77      * @param helper its helper
78      * @throws IOException if writing anything fails
79      */

80     private void initialize() throws IOException JavaDoc {
81         project.getProjectDirectory().getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
82             public void run() throws IOException JavaDoc {
83                 createJdkXml();
84                 if (project.getProjectDirectory().getFileObject(NBJDK_XML) != null) {
85                     return;
86                 }
87                 Element JavaDoc generalDataE = Util.getPrimaryConfigurationData(helper);
88                 Document JavaDoc nbjdkDoc = createNbjdkXmlSkeleton();
89                 rebindAllActions(generalDataE, nbjdkDoc);
90                 writeXML(nbjdkDoc, NBJDK_XML);
91                 Util.putPrimaryConfigurationData(helper, generalDataE);
92                 ProjectManager.getDefault().saveProject(project);
93             }
94         });
95     }
96
97     private void createJdkXml() throws IOException JavaDoc {
98         ProjectManager.getDefault().saveProject(project); // GFH requires it
99
new GeneratedFilesHelper(helper).refreshBuildScript(JDK_XML, JdkConfiguration.class.getResource("jdk.xsl"), true);
100     }
101
102     /**
103      * Inserts an import to jdk.xml, as well as associated property definitions, into
104      * an Ant script. The script should reside in the nbproject/ dir and have basedir="..".
105      * Will not insert a duplicate import if one already exists.
106      */

107     public static void insertJdkXmlImport(Document JavaDoc doc) {
108         NodeList JavaDoc nl = doc.getElementsByTagName("import"); // NOI18N
109
for (int i = 0; i < nl.getLength(); i++) {
110             if (((Element JavaDoc) nl.item(i)).getAttribute("file").equals("jdk.xml")) { // NOI18N
111
return;
112             }
113         }
114         Element JavaDoc projectE = doc.getDocumentElement();
115         Element JavaDoc propE = doc.createElement("property"); // NOI18N
116
propE.setAttribute("file", NBJDK_PROPERTIES); // NOI18N
117
projectE.appendChild(propE);
118         propE = doc.createElement("property"); // NOI18N
119
propE.setAttribute("name", "user.properties.file"); // NOI18N
120
propE.setAttribute("location", "${netbeans.user}/build.properties"); // NOI18N
121
projectE.appendChild(propE);
122         propE = doc.createElement("property"); // NOI18N
123
propE.setAttribute("file", "${user.properties.file}"); // NOI18N
124
projectE.appendChild(propE);
125         Element JavaDoc importE = doc.createElement("import"); // NOI18N
126
importE.setAttribute("file", "jdk.xml"); // NOI18N
127
projectE.appendChild(importE);
128     }
129
130     private Document JavaDoc createNbjdkXmlSkeleton() {
131         Document JavaDoc nbjdkDoc = XMLUtil.createDocument("project", null, null, null); // NOI18N
132
Element JavaDoc projectE = nbjdkDoc.getDocumentElement();
133         // XXX for better fidelity would use ${ant.script}#/project[@name]
134
projectE.setAttribute("name", ProjectUtils.getInformation(project).getName()); // NOI18N
135
projectE.setAttribute("basedir", ".."); // NOI18N
136
insertJdkXmlImport(nbjdkDoc);
137         return nbjdkDoc;
138     }
139
140     private void rebindAllActions(Element JavaDoc generalDataE, Document JavaDoc nbjdkDoc) {
141         Element JavaDoc projectE = nbjdkDoc.getDocumentElement();
142         Set JavaDoc<String JavaDoc> targetsCreated = new HashSet JavaDoc<String JavaDoc>();
143         // XXX remove any definition of ${ant.script}, which will by now be obsolete
144
Element JavaDoc ideActionsE = Util.findElement(generalDataE, "ide-actions", Util.NAMESPACE);
145         if (ideActionsE != null) {
146             for (Element JavaDoc actionE : Util.findSubElements(ideActionsE)) {
147                 rebindAction(actionE, projectE, targetsCreated);
148             }
149         }
150         Element JavaDoc viewE = Util.findElement(generalDataE, "ide-actions", Util.NAMESPACE);
151         if (viewE != null) {
152             Element JavaDoc contextMenuE = Util.findElement(viewE, "context-menu", Util.NAMESPACE);
153             if (contextMenuE != null) {
154                 for (Element JavaDoc actionE : Util.findSubElements(contextMenuE)) {
155                     if (!actionE.getLocalName().equals("action")) {
156                         continue; // ignore <ide-action> here
157
}
158                     rebindAction(actionE, projectE, targetsCreated);
159                 }
160             }
161         }
162         // XXX need to change the customizer to also rebind actions added later!
163
// Tricky however because TargetMappingPanel is in ant/freeform and knows nothing of jdk.xml.
164
// Dangerous to simply rebind every action which is added later via customizer, probably.
165
}
166
167     private void rebindAction(Element JavaDoc actionE, Element JavaDoc projectE, Set JavaDoc<String JavaDoc> targetsCreated) {
168         Element JavaDoc scriptE = Util.findElement(actionE, "script", Util.NAMESPACE); // NOI18N
169
String JavaDoc script;
170         if (scriptE != null) {
171             script = Util.findText(scriptE);
172             actionE.removeChild(scriptE);
173         } else {
174             script = "build.xml"; // NOI18N
175
}
176         scriptE = actionE.getOwnerDocument().createElementNS(Util.NAMESPACE, "script"); // NOI18N
177
scriptE.appendChild(actionE.getOwnerDocument().createTextNode(NBJDK_XML));
178         actionE.insertBefore(scriptE, actionE.getFirstChild());
179         List JavaDoc<String JavaDoc> targetNames = new ArrayList JavaDoc<String JavaDoc>();
180         for (Element JavaDoc targetE : Util.findSubElements(actionE)) {
181             if (!targetE.getLocalName().equals("target")) { // NOI18N
182
continue;
183             }
184             targetNames.add(Util.findText(targetE));
185         }
186         if (targetNames.isEmpty()) {
187             targetNames.add(null);
188         }
189         String JavaDoc scriptPath = evaluator.evaluate(script);
190         for (String JavaDoc target : targetNames) {
191             if (targetsCreated.add(target)) {
192                 createOverride(projectE, target, scriptPath);
193             }
194         }
195     }
196
197     private static void createOverride(Element JavaDoc projectE, String JavaDoc target, String JavaDoc script) {
198         Element JavaDoc targetE = projectE.getOwnerDocument().createElement("target"); // NOI18N
199
if (target != null) {
200             targetE.setAttribute("name", target); // NOI18N
201
}
202         String JavaDoc depends;
203         if (target != null && /*XXX not very precise*/target.indexOf("debug") != -1) {
204             depends = "-jdk-init,-jdk-presetdef-nbjpdastart"; // NOI18N
205
} else {
206             // XXX what about profiler?
207
depends = "-jdk-init"; // NOI18N
208
}
209         targetE.setAttribute("depends", depends); // NOI18N
210
Element JavaDoc antE = projectE.getOwnerDocument().createElement("ant"); // NOI18N
211
if (target != null) {
212             antE.setAttribute("target", target); // NOI18N
213
}
214         if (!script.equals("build.xml")) { // NOI18N
215
antE.setAttribute("antfile", script); // NOI18N
216
}
217         antE.setAttribute("inheritall", "false"); // NOI18N
218
targetE.appendChild(antE);
219         projectE.appendChild(targetE);
220     }
221
222     private void writeXML(Document JavaDoc doc, String JavaDoc path) throws IOException JavaDoc {
223         FileObject fo = FileUtil.createData(project.getProjectDirectory(), path);
224         FileLock lock = fo.lock();
225         try {
226             OutputStream JavaDoc os = fo.getOutputStream(lock);
227             try {
228                 XMLUtil.write(doc, os, "UTF-8"); // NOI18N
229
} finally {
230                 os.close();
231             }
232         } finally {
233             lock.releaseLock();
234         }
235     }
236
237     /**
238      * Tries to find the selected Java platform for a project.
239      * May return null.
240      */

241     public JavaPlatform getSelectedPlatform() {
242         EditableProperties ep = helper.getProperties(NBJDK_PROPERTIES);
243         String JavaDoc plaf = ep.getProperty(NBJDK_ACTIVE);
244         if (plaf != null) {
245             for (JavaPlatform p : JavaPlatformManager.getDefault().getInstalledPlatforms()) {
246                 if (plaf.equals(getPlatformID(p))) {
247                     return p;
248                 }
249             }
250         }
251         return null;
252     }
253
254     /**
255      * Tries to set the Java platform for a project.
256      */

257     public void setSelectedPlatform(JavaPlatform jdk) throws IOException JavaDoc {
258         assert jdk != null;
259         initialize();
260         EditableProperties ep = helper.getProperties(NBJDK_PROPERTIES);
261         ep.setProperty(NBJDK_ACTIVE, getPlatformID(jdk));
262         helper.putProperties(NBJDK_PROPERTIES, ep);
263         ProjectManager.getDefault().saveProject(project);
264     }
265
266     private static String JavaDoc getPlatformID(JavaPlatform platform) {
267         String JavaDoc s = (String JavaDoc) platform.getProperties().get("platform.ant.name"); // NOI18N
268
if (s != null) {
269             return s;
270         } else {
271             return PLATFORM_ID_DEFAULT;
272         }
273     }
274
275 }
276
Popular Tags