KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > api > AppClientProjectGenerator


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.clientproject.api;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.math.BigDecimal JavaDoc;
27 import java.util.Stack 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.java.project.JavaProjectConstants;
33 import org.netbeans.modules.j2ee.clientproject.AppClientProject;
34 import org.netbeans.modules.j2ee.clientproject.AppClientProjectType;
35 import org.netbeans.modules.j2ee.clientproject.AppClientProvider;
36 import org.netbeans.modules.j2ee.clientproject.UpdateHelper;
37 import org.netbeans.modules.j2ee.clientproject.Utils;
38 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties;
39 import org.netbeans.modules.j2ee.clientproject.ui.customizer.PlatformUiSupport;
40 import org.netbeans.modules.j2ee.dd.api.client.AppClient;
41 import org.netbeans.modules.j2ee.dd.api.client.DDProvider;
42 import org.netbeans.modules.j2ee.deployment.devmodules.api.AntDeploymentHelper;
43 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
44 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
45 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
46 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
47 import org.netbeans.modules.websvc.api.client.WebServicesClientConstants;
48 import org.netbeans.spi.project.support.ant.AntProjectHelper;
49 import org.netbeans.spi.project.support.ant.EditableProperties;
50 import org.netbeans.spi.project.support.ant.ProjectGenerator;
51 import org.netbeans.spi.project.support.ant.PropertyUtils;
52 import org.netbeans.spi.project.support.ant.ReferenceHelper;
53 import org.openide.filesystems.FileObject;
54 import org.openide.filesystems.FileUtil;
55 import org.openide.filesystems.Repository;
56 import org.openide.filesystems.FileStateInvalidException;
57 import org.openide.loaders.DataFolder;
58 import org.openide.loaders.DataObject;
59 import org.openide.util.Mutex;
60 import org.openide.util.MutexException;
61 import org.openide.ErrorManager;
62 import org.openide.filesystems.FileLock;
63 import org.openide.modules.SpecificationVersion;
64 import org.openide.util.NbBundle;
65 import org.w3c.dom.Document JavaDoc;
66 import org.w3c.dom.Element JavaDoc;
67 import org.w3c.dom.NodeList JavaDoc;
68
69 /**
70  * Creates a AppClientProject from scratch according to some initial configuration.
71  */

72 public class AppClientProjectGenerator {
73     
74     private static final String JavaDoc DEFAULT_CONF_FOLDER = "conf"; //NOI18N
75
private static final String JavaDoc DEFAULT_SRC_FOLDER = "src"; //NOI18N
76
private static final String JavaDoc DEFAULT_TEST_FOLDER = "test"; //NOI18N
77
private static final String JavaDoc DEFAULT_RESOURCE_FOLDER = "setup"; //NOI18N
78
private static final String JavaDoc DEFAULT_JAVA_FOLDER = "java"; //NOI18N
79
private static final String JavaDoc DEFAULT_BUILD_DIR = "build"; //NOI18N
80

81     public static final String JavaDoc MINIMUM_ANT_VERSION = "1.6.5"; // NOI18N
82

83     private static final String JavaDoc MANIFEST_FILE = "MANIFEST.MF"; // NOI18N
84

85     private AppClientProjectGenerator() {}
86     
87     /**
88      * Create a new Application client project.
89      *
90      * @param dir the top-level directory (need not yet exist but if it does it must be empty)
91      * @param name the name for the project
92      * @param mainClass the name for the main class
93      * @param j2eeLevel defined in <code>org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule<code>
94      * @param serverInstanceID provided by j2eeserver module
95      *
96      * @return the helper object permitting it to be further customized
97      * @throws IOException in case something went wrong
98      */

99     public static AntProjectHelper createProject(File JavaDoc dir, String JavaDoc name, String JavaDoc mainClass, String JavaDoc j2eeLevel, String JavaDoc serverInstanceID) throws IOException JavaDoc {
100         FileObject fo = createProjectDir(dir);
101         
102         FileObject srcRoot = fo.createFolder(DEFAULT_SRC_FOLDER);
103         FileObject javaRoot = srcRoot.createFolder(DEFAULT_JAVA_FOLDER);
104         FileObject confRoot = srcRoot.createFolder(DEFAULT_CONF_FOLDER);
105         fo.createFolder(DEFAULT_TEST_FOLDER);
106         
107         // create application-client.xml
108
String JavaDoc resource = (J2eeModule.JAVA_EE_5.equals(j2eeLevel)
109                 ? "org-netbeans-modules-j2ee-clientproject/application-client-5.xml" // NOI18N
110
: "org-netbeans-modules-j2ee-clientproject/application-client-1.4.xml"); // NOI18N
111
FileObject ddFile = FileUtil.copyFile(Repository.getDefault().getDefaultFileSystem().findResource(resource), confRoot, "application-client"); //NOI18N
112
AppClient appClient = DDProvider.getDefault().getDDRoot(ddFile);
113         appClient.setDisplayName(name);
114         appClient.write(ddFile);
115         
116         AntProjectHelper h = createProject(fo, name, DEFAULT_SRC_FOLDER, DEFAULT_TEST_FOLDER,
117                 null, null, null,mainClass, j2eeLevel, serverInstanceID);
118         
119         EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
120         ep.put(AppClientProjectProperties.SOURCE_ROOT, DEFAULT_SRC_FOLDER); //NOI18N
121
ep.setProperty(AppClientProjectProperties.META_INF, "${"+AppClientProjectProperties.SOURCE_ROOT+"}/"+DEFAULT_CONF_FOLDER); //NOI18N
122
ep.setProperty(AppClientProjectProperties.SRC_DIR, "${"+AppClientProjectProperties.SOURCE_ROOT+"}/"+DEFAULT_JAVA_FOLDER); //NOI18N
123
h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
124         
125         Project p = ProjectManager.getDefault().findProject(fo);
126         ProjectManager.getDefault().saveProject(p);
127         
128         if ( mainClass != null ) {
129             createMainClass( mainClass, javaRoot );
130         }
131         
132         createManifest(confRoot, MANIFEST_FILE);
133         
134         return h;
135     }
136     
137     /**
138      * Imports an existing Application client project into NetBeans project.
139      *
140      * @param dir the top-level directory (need not yet exist but if it does it must be empty) - "nbproject" location
141      * @param name the name for the project
142      * @param sourceFolders top-level location(s) of java sources - must not be null
143      * @param testFolders top-level location(s) of test(s) - must not be null
144      * @param confFolder top-level location of configuration file(s) folder - must not be null
145      * @param libFolder top-level location of libraries
146      * @param j2eeLevel defined in <code>org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule<code>
147      * @param serverInstanceID provided by j2eeserver module
148      *
149      * @return the helper object permitting it to be further customized
150      * @throws IOException in case something went wrong
151      */

152     public static AntProjectHelper importProject(final File JavaDoc dir, final String JavaDoc name,
153             final File JavaDoc[] sourceFolders, final File JavaDoc[] testFolders, final File JavaDoc confFolder, final File JavaDoc libFolder, String JavaDoc j2eeLevel, String JavaDoc serverInstanceID) throws IOException JavaDoc {
154         assert sourceFolders != null && testFolders != null: "Package roots can't be null"; //NOI18N
155
final FileObject dirFO = createProjectDir(dir);
156         final AntProjectHelper h = createProject(dirFO, name, null, null,
157                 confFolder.getAbsolutePath(), (libFolder == null ? null : libFolder.getAbsolutePath()),
158                 null, null, j2eeLevel, serverInstanceID);
159         final AppClientProject p = (AppClientProject) ProjectManager.getDefault().findProject(dirFO);
160         final ReferenceHelper refHelper = p.getReferenceHelper();
161         try {
162             ProjectManager.mutex().writeAccess( new Mutex.ExceptionAction() {
163                 public Object JavaDoc run() throws Exception JavaDoc {
164                     Element JavaDoc data = h.getPrimaryConfigurationData(true);
165                     Document JavaDoc doc = data.getOwnerDocument();
166                     NodeList JavaDoc nl = data.getElementsByTagNameNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); // NOI18N
167
assert nl.getLength() == 1;
168                     Element JavaDoc sourceRoots = (Element JavaDoc) nl.item(0);
169                     nl = data.getElementsByTagNameNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); //NOI18N
170
assert nl.getLength() == 1;
171                     Element JavaDoc testRoots = (Element JavaDoc) nl.item(0);
172                     for (int i=0; i<sourceFolders.length; i++) {
173                         String JavaDoc propName = "src.dir" + (i == 0 ? "" : Integer.toString(i+1)); //NOI18N
174
String JavaDoc srcReference = refHelper.createForeignFileReference(sourceFolders[i], JavaProjectConstants.SOURCES_TYPE_JAVA);
175                         Element JavaDoc root = doc.createElementNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
176
root.setAttribute("id",propName); //NOI18N
177
sourceRoots.appendChild(root);
178                         EditableProperties props = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
179                         props.put(propName,srcReference);
180                         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); // #47609
181
}
182                     
183                     if (testFolders.length == 0) {
184                         String JavaDoc testLoc = NbBundle.getMessage(AppClientProjectGenerator.class,"TXT_DefaultTestFolderName");
185                         File JavaDoc f = new File JavaDoc(dir,testLoc);
186                         f.mkdirs();
187                         String JavaDoc propName = "test.src.dir"; // NOI18N
188
Element JavaDoc root = doc.createElementNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
189
root.setAttribute("id",propName); //NOI18N
190
root.setAttribute("name",NbBundle.getMessage(AppClientProjectGenerator.class, "NAME_test.src.dir"));
191                         testRoots.appendChild(root);
192                         EditableProperties props = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
193                         props.put(propName,testLoc);
194                         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); // #47609
195
} else {
196                         for (int i=0; i<testFolders.length; i++) {
197                             if (!testFolders[i].exists()) {
198                                 testFolders[i].mkdirs();
199                             }
200                             String JavaDoc propName = "test.src.dir" + (i == 0 ? "" : Integer.toString(i+1)); //NOI18N
201
String JavaDoc testReference = refHelper.createForeignFileReference(testFolders[i], JavaProjectConstants.SOURCES_TYPE_JAVA);
202                             Element JavaDoc root = doc.createElementNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
203
root.setAttribute("id",propName); //NOI18N
204
testRoots.appendChild(root);
205                             EditableProperties props = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); // #47609
206
props.put(propName,testReference);
207                             h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
208                         }
209                     }
210                     h.putPrimaryConfigurationData(data,true);
211                     ProjectManager.getDefault().saveProject(p);
212                     return null;
213                 }
214             });
215         } catch (MutexException me ) {
216             ErrorManager.getDefault().notify(me);
217         }
218         // AB: fix for #53170: if j2eeLevel is 1.4 and application-client.xml is version 1.3, we upgrade it to version 1.4
219
FileObject confFolderFO = FileUtil.toFileObject(confFolder);
220         FileObject appClientXML = confFolderFO == null ? null
221                 : confFolderFO.getFileObject(AppClientProvider.FILE_DD);
222         if (appClientXML != null) {
223             try {
224                 //AppClient root = DDProvider.getDefault().getDDRoot(Car.getCar(appClientXML));
225
AppClient root = DDProvider.getDefault().getDDRoot(appClientXML);
226                 boolean writeDD = false;
227                 if (new BigDecimal JavaDoc(AppClient.VERSION_1_3).equals(root.getVersion()) && J2eeModule.J2EE_14.equals(j2eeLevel)) { // NOI18N
228
root.setVersion(new BigDecimal JavaDoc(AppClient.VERSION_1_4));
229                     writeDD = true;
230                 }
231                 // also set the display name if not set (#55733)
232
String JavaDoc dispName = root.getDefaultDisplayName();
233                 if (null == dispName || dispName.trim().length() == 0) {
234                     root.setDisplayName(name);
235                     writeDD = true;
236                 }
237                 if (writeDD) {
238                     root.write(appClientXML);
239                 }
240             } catch (IOException JavaDoc e) {
241                 ErrorManager.getDefault().notify(e);
242             }
243         } else {
244             // XXX just temporary, since now the import would fail due to another bug
245
String JavaDoc resource = (J2eeModule.JAVA_EE_5.equals(j2eeLevel)
246                     ? "org-netbeans-modules-j2ee-clientproject/application-client-5.xml" // NOI18N
247
: "org-netbeans-modules-j2ee-clientproject/application-client-1.4.xml"); // NOI18N
248
FileUtil.copyFile(Repository.getDefault().getDefaultFileSystem().findResource(resource),
249                     confFolderFO, "application-client"); //NOI18N
250
}
251         createManifest(confFolderFO, MANIFEST_FILE);
252         return h;
253     }
254     
255     /**
256      * Imports an existing Application client project into NetBeans project
257      * with a flag to specify whether the project contains java source files
258      * or was created from an exploded archive.
259      * @return the helper object permitting it to be further customized
260      * @param fromJavaSource indicate whether the project is "from" source or an exploded archive
261      * @param dir the top-level directory (need not yet exist but if it does it must be empty) - "nbproject" location
262      * @param name the name for the project
263      * @param sourceFolders top-level location(s) of java sources - must not be null
264      * @param testFolders top-level location(s) of test(s) - must not be null
265      * @param confFolder top-level location of configuration file(s) folder - must not be null
266      * @param libFolder top-level location of libraries
267      * @param j2eeLevel defined in <code>org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule<code>
268      * @param serverInstanceID provided by j2eeserver module
269      * @throws IOException in case something went wrong
270      */

271     public static AntProjectHelper importProject(final File JavaDoc dir, final String JavaDoc name,
272             final File JavaDoc[] sourceFolders, final File JavaDoc[] testFolders, final File JavaDoc confFolder,
273             final File JavaDoc libFolder, String JavaDoc j2eeLevel, String JavaDoc serverInstanceID,boolean fromJavaSource) throws IOException JavaDoc {
274         AntProjectHelper h = importProject(dir,name,sourceFolders,testFolders,
275                 confFolder,libFolder,j2eeLevel,serverInstanceID);
276         EditableProperties subEp = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
277         subEp.setProperty(AppClientProjectProperties.JAVA_SOURCE_BASED,fromJavaSource+""); // NOI18N
278
h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,subEp);
279         Project subP = ProjectManager.getDefault().findProject(h.getProjectDirectory());
280         ProjectManager.getDefault().saveProject(subP);
281         return h;
282     }
283     private static AntProjectHelper createProject(FileObject dirFO, String JavaDoc name,
284             String JavaDoc srcRoot, String JavaDoc testRoot, String JavaDoc configFiles, String JavaDoc libraries,
285             String JavaDoc resources, String JavaDoc mainClass, String JavaDoc j2eeLevel,
286             String JavaDoc serverInstanceID) throws IOException JavaDoc {
287         AntProjectHelper h = ProjectGenerator.createProject(dirFO, AppClientProjectType.TYPE);
288         Element JavaDoc data = h.getPrimaryConfigurationData(true);
289         Document JavaDoc doc = data.getOwnerDocument();
290         Element JavaDoc nameEl = doc.createElementNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE, "name"); // NOI18N
291
nameEl.appendChild(doc.createTextNode(name));
292         data.appendChild(nameEl);
293         Element JavaDoc minant = doc.createElementNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE, "minimum-ant-version"); // NOI18N
294
minant.appendChild(doc.createTextNode(MINIMUM_ANT_VERSION)); // NOI18N
295
data.appendChild(minant);
296         
297         //TODO: ma154696: not sure if needed
298
// Element addLibs = doc.createElementNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE, "ejb-module-additional-libraries"); //NOI18N
299
// data.appendChild(addLibs);
300

301         EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
302         Element JavaDoc sourceRoots = doc.createElementNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); //NOI18N
303
if (srcRoot != null) {
304             Element JavaDoc root = doc.createElementNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
305
root.setAttribute("id","src.dir"); //NOI18N
306
root.setAttribute("name",NbBundle.getMessage(AppClientProjectGenerator.class, "NAME_src.dir"));
307             sourceRoots.appendChild(root);
308             ep.setProperty("src.dir", srcRoot); // NOI18N
309
}
310         data.appendChild(sourceRoots);
311         Element JavaDoc testRoots = doc.createElementNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); //NOI18N
312
if (testRoot != null) {
313             Element JavaDoc root = doc.createElementNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
314
root.setAttribute("id","test.src.dir"); //NOI18N
315
root.setAttribute("name",NbBundle.getMessage(AppClientProjectGenerator.class, "NAME_test.src.dir"));
316             testRoots.appendChild(root);
317             ep.setProperty("test.src.dir", testRoot); // NOI18N
318
}
319         data.appendChild(testRoots);
320         h.putPrimaryConfigurationData(data, true);
321         
322         if (configFiles != null) {
323             ep.setProperty(AppClientProjectProperties.META_INF, configFiles);
324         }
325         if (libraries != null) {
326             ep.setProperty(AppClientProjectProperties.LIBRARIES_DIR, libraries);
327         }
328         
329         if (resources != null) {
330             ep.setProperty(AppClientProjectProperties.RESOURCE_DIR, resources);
331         } else {
332             ep.setProperty(AppClientProjectProperties.RESOURCE_DIR, DEFAULT_RESOURCE_FOLDER);
333         }
334         
335         //XXX the name of the dist.ear.jar file should be different, but now it cannot be since the name is used as a key in module provider mapping
336
ep.setProperty(AppClientProjectProperties.DIST_EAR_JAR, "${"+AppClientProjectProperties.DIST_DIR+"}/" + "${" + AppClientProjectProperties.JAR_NAME + "}"); // NOI18N
337
ep.setProperty(AppClientProjectProperties.JAR_NAME, PropertyUtils.getUsablePropertyName(name) + ".jar"); // NOI18N
338
ep.setProperty(AppClientProjectProperties.BUILD_EAR_CLASSES_DIR, "${"+AppClientProjectProperties.BUILD_DIR+"}/ear-module"); // NOI18N
339

340         ep.setProperty("dist.dir", "dist"); // NOI18N
341
ep.setComment("dist.dir", new String JavaDoc[] {"# " + NbBundle.getMessage(AppClientProjectGenerator.class, "COMMENT_dist.dir")}, false); // NOI18N
342
// ep.setProperty("dist.jar", "${dist.dir}/" + PropertyUtils.getUsablePropertyName(name) + ".jar"); // NOI18N
343
ep.setProperty(AppClientProjectProperties.DIST_JAR, "${"+AppClientProjectProperties.DIST_DIR+"}/" + "${" + AppClientProjectProperties.JAR_NAME + "}"); // NOI18N
344
ep.setProperty("javac.classpath", new String JavaDoc[0]); // NOI18N
345
ep.setProperty("build.sysclasspath", "ignore"); // NOI18N
346
ep.setComment("build.sysclasspath", new String JavaDoc[] {"# " + NbBundle.getMessage(AppClientProjectGenerator.class, "COMMENT_build.sysclasspath")}, false); // NOI18N
347
ep.setProperty("run.classpath", new String JavaDoc[] { // NOI18N
348
"${javac.classpath}:", // NOI18N
349
"${build.classes.dir}", // NOI18N
350
});
351         ep.setProperty("debug.classpath", new String JavaDoc[] { // NOI18N
352
"${run.classpath}", // NOI18N
353
});
354         ep.setProperty("jar.compress", "false"); // NOI18N
355
if (mainClass != null) {
356             ep.setProperty("main.class", mainClass); // NOI18N
357
}
358         
359         ep.setProperty("javac.compilerargs", ""); // NOI18N
360
ep.setComment("javac.compilerargs", new String JavaDoc[] { // NOI18N
361
"# " + NbBundle.getMessage(AppClientProjectGenerator.class, "COMMENT_javac.compilerargs"), // NOI18N
362
}, false);
363         
364         JavaPlatform defaultPlatform = JavaPlatformManager.getDefault().getDefaultPlatform();
365         SpecificationVersion v = defaultPlatform.getSpecification().getVersion();
366         String JavaDoc sourceLevel = v.toString();
367         // #89131: these levels are not actually distinct from 1.5.
368
if (sourceLevel.equals("1.6") || sourceLevel.equals("1.7"))
369             sourceLevel = "1.5";
370         ep.setProperty(AppClientProjectProperties.JAVAC_SOURCE, sourceLevel); // NOI18N
371
ep.setProperty(AppClientProjectProperties.JAVAC_TARGET, sourceLevel); // NOI18N
372

373         ep.setProperty("javac.deprecation", "false"); // NOI18N
374
ep.setProperty("javac.test.classpath", new String JavaDoc[] { // NOI18N
375
"${javac.classpath}:", // NOI18N
376
"${build.classes.dir}:", // NOI18N
377
"${libs.junit.classpath}", // NOI18N
378
});
379         ep.setProperty("run.test.classpath", new String JavaDoc[] { // NOI18N
380
"${javac.test.classpath}:", // NOI18N
381
"${build.test.classes.dir}", // NOI18N
382
});
383         ep.setProperty("debug.test.classpath", new String JavaDoc[] { // NOI18N
384
"${run.test.classpath}", // NOI18N
385
});
386         
387         ep.setProperty("build.generated.dir", "${build.dir}/generated"); // NOI18N
388
//ep.setProperty("meta.inf.dir", "${src.dir}/META-INF"); // NOI18N
389

390         ep.setProperty("build.dir", DEFAULT_BUILD_DIR); // NOI18N
391
ep.setComment("build.dir", new String JavaDoc[] {"# " + NbBundle.getMessage(AppClientProjectGenerator.class, "COMMENT_build.dir")}, false); // NOI18N
392
//ep.setProperty("build.classes.dir", "${build.dir}/classes"); // NOI18N
393
ep.setProperty("build.classes.dir", "${build.dir}/jar"); // NOI18N
394
ep.setProperty("build.test.classes.dir", "${build.dir}/test/classes"); // NOI18N
395
ep.setProperty("build.test.results.dir", "${build.dir}/test/results"); // NOI18N
396
ep.setProperty("build.classes.excludes", "**/*.java,**/*.form"); // NOI18N
397
ep.setProperty("dist.javadoc.dir", "${dist.dir}/javadoc"); // NOI18N
398
ep.setProperty(AppClientProjectProperties.JAVA_PLATFORM, "default_platform"); // NOI18N
399

400         ep.setProperty("run.jvmargs", ""); // NOI18N
401
ep.setComment("run.jvmargs", new String JavaDoc[] { // NOI18N
402
"# " + NbBundle.getMessage(AppClientProjectGenerator.class, "COMMENT_run.jvmargs"), // NOI18N
403
"# " + NbBundle.getMessage(AppClientProjectGenerator.class, "COMMENT_run.jvmargs_2"), // NOI18N
404
"# " + NbBundle.getMessage(AppClientProjectGenerator.class, "COMMENT_run.jvmargs_3"), // NOI18N
405
}, false);
406         
407         ep.setProperty(AppClientProjectProperties.JAVADOC_PRIVATE, "false"); // NOI18N
408
ep.setProperty(AppClientProjectProperties.JAVADOC_NO_TREE, "false"); // NOI18N
409
ep.setProperty(AppClientProjectProperties.JAVADOC_USE, "true"); // NOI18N
410
ep.setProperty(AppClientProjectProperties.JAVADOC_NO_NAVBAR, "false"); // NOI18N
411
ep.setProperty(AppClientProjectProperties.JAVADOC_NO_INDEX, "false"); // NOI18N
412
ep.setProperty(AppClientProjectProperties.JAVADOC_SPLIT_INDEX, "true"); // NOI18N
413
ep.setProperty(AppClientProjectProperties.JAVADOC_AUTHOR, "false"); // NOI18N
414
ep.setProperty(AppClientProjectProperties.JAVADOC_VERSION, "false"); // NOI18N
415
ep.setProperty(AppClientProjectProperties.JAVADOC_WINDOW_TITLE, ""); // NOI18N
416
ep.setProperty(AppClientProjectProperties.JAVADOC_ENCODING, ""); // NOI18N
417
ep.setProperty(AppClientProjectProperties.JAVADOC_ADDITIONALPARAM, ""); // NOI18N
418

419         Deployment deployment = Deployment.getDefault();
420         J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstanceID);
421         if (!j2eePlatform.getSupportedSpecVersions(J2eeModule.CLIENT).contains(j2eeLevel)) {
422             ErrorManager.getDefault().log(ErrorManager.WARNING,
423                     NbBundle.getMessage(AppClientProjectGenerator.class, "MSG_Warning_SpecLevelNotSupported", // NOI18N
424
new Object JavaDoc[] {j2eeLevel, Deployment.getDefault().getServerInstanceDisplayName(serverInstanceID)}));
425         }
426         ep.setProperty(AppClientProjectProperties.J2EE_SERVER_TYPE, deployment.getServerID(serverInstanceID));
427         ep.setProperty(AppClientProjectProperties.J2EE_PLATFORM, j2eeLevel);
428         ep.setProperty("manifest.file", "${" +AppClientProjectProperties.META_INF + "}/" + MANIFEST_FILE); // NOI18N
429

430         String JavaDoc mainClassArgs = j2eePlatform.getToolProperty(J2eePlatform.TOOL_APP_CLIENT_RUNTIME, J2eePlatform.TOOL_PROP_MAIN_CLASS_ARGS);
431         if (mainClassArgs != null && !mainClassArgs.equals("")) {
432             ep.put(AppClientProjectProperties.APPCLIENT_MAINCLASS_ARGS, mainClassArgs);
433         } else if ((mainClassArgs = j2eePlatform.getToolProperty(J2eePlatform.TOOL_APP_CLIENT_RUNTIME, AppClientProjectProperties.CLIENT_NAME))
434                         != null) {
435             ep.put(AppClientProjectProperties.CLIENT_NAME, mainClassArgs);
436         }
437         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
438         
439         //private.properties
440
ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
441         // set j2ee.appclient environment
442
File JavaDoc[] accrt = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_APP_CLIENT_RUNTIME);
443         ep.setProperty(AppClientProjectProperties.APPCLIENT_TOOL_RUNTIME, Utils.toClasspathString(accrt));
444         String JavaDoc acMain = j2eePlatform.getToolProperty(J2eePlatform.TOOL_APP_CLIENT_RUNTIME, J2eePlatform.TOOL_PROP_MAIN_CLASS);
445         if (acMain != null) {
446             ep.setProperty(AppClientProjectProperties.APPCLIENT_TOOL_MAINCLASS, acMain);
447         }
448         String JavaDoc jvmOpts = j2eePlatform.getToolProperty(J2eePlatform.TOOL_APP_CLIENT_RUNTIME, J2eePlatform.TOOL_PROP_JVM_OPTS);
449         if (jvmOpts != null) {
450             ep.setProperty(AppClientProjectProperties.APPCLIENT_TOOL_JVMOPTS, jvmOpts);
451         }
452         String JavaDoc args = j2eePlatform.getToolProperty(J2eePlatform.TOOL_APP_CLIENT_RUNTIME,
453                 AppClientProjectProperties.J2EE_PLATFORM_APPCLIENT_ARGS);
454         if (args != null) {
455             ep.setProperty(AppClientProjectProperties.APPCLIENT_TOOL_ARGS, args);
456         }
457         
458         ep.setProperty(AppClientProjectProperties.J2EE_SERVER_INSTANCE, serverInstanceID);
459         // set j2ee.platform.classpath
460
String JavaDoc classpath = Utils.toClasspathString(j2eePlatform.getClasspathEntries());
461         ep.setProperty(AppClientProjectProperties.J2EE_PLATFORM_CLASSPATH, classpath);
462         // set j2ee.platform.wscompile.classpath
463
if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSCOMPILE)) {
464             File JavaDoc[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSCOMPILE);
465             ep.setProperty(WebServicesClientConstants.J2EE_PLATFORM_WSCOMPILE_CLASSPATH,
466                     Utils.toClasspathString(wsClasspath));
467         }
468         
469         //WORKAROUND for --retrieve option in asadmin deploy command
470
//works only for local domains
471
//see also http://www.netbeans.org/issues/show_bug.cgi?id=82929
472
if ("J2EE".equals(deployment.getServerID(serverInstanceID))) { // NOI18N
473
File JavaDoc asRoot = j2eePlatform.getPlatformRoots()[0];
474             File JavaDoc exFile = new File JavaDoc(asRoot, "lib/javaee.jar"); // NOI18N
475
InstanceProperties ip = InstanceProperties.getInstanceProperties(serverInstanceID);
476             if (exFile.exists()) {
477                 ep.setProperty("wa.copy.client.jar.from", // NOI18N
478
new File JavaDoc(ip.getProperty("LOCATION"), ip.getProperty("DOMAIN") + "/generated/xml/j2ee-modules").getAbsolutePath()); // NOI18N
479
} else {
480                 ep.setProperty("wa.copy.client.jar.from", // NOI18N
481
new File JavaDoc(ip.getProperty("LOCATION"), ip.getProperty("DOMAIN") + "/applications/j2ee-modules").getAbsolutePath()); // NOI18N
482
}
483         } else {
484             ep.remove("wa.copy.client.jar.from"); // NOI18N
485
}
486         
487         // ant deployment support
488
File JavaDoc projectFolder = FileUtil.toFile(dirFO);
489         try {
490             AntDeploymentHelper.writeDeploymentScript(new File JavaDoc(projectFolder, AppClientProjectProperties.ANT_DEPLOY_BUILD_SCRIPT),
491                     J2eeModule.CLIENT, serverInstanceID);
492         } catch (IOException JavaDoc ioe) {
493             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
494         }
495         File JavaDoc deployAntPropsFile = AntDeploymentHelper.getDeploymentPropertiesFile(serverInstanceID);
496         if (deployAntPropsFile != null) {
497             ep.setProperty(AppClientProjectProperties.DEPLOY_ANT_PROPS_FILE, deployAntPropsFile.getAbsolutePath());
498         }
499         
500         h.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep);
501         
502         return h;
503     }
504     
505     private static FileObject createProjectDir(File JavaDoc dir) throws IOException JavaDoc {
506         Stack JavaDoc stack = new Stack JavaDoc ();
507         while (!dir.exists()) {
508             stack.push (dir.getName());
509             dir = dir.getParentFile();
510         }
511         FileObject dirFO = FileUtil.toFileObject (dir);
512         if (dirFO == null) {
513             refreshFileSystem(dir);
514             dirFO = FileUtil.toFileObject (dir);
515         }
516         assert dirFO != null;
517         while (!stack.isEmpty()) {
518             dirFO = dirFO.createFolder((String JavaDoc)stack.pop());
519         }
520         return dirFO;
521     }
522     
523     private static void createMainClass( String JavaDoc mainClassName, FileObject srcFolder ) throws IOException JavaDoc {
524         
525         int lastDotIdx = mainClassName.lastIndexOf( '.' );
526         String JavaDoc mName, pName;
527         if ( lastDotIdx == -1 ) {
528             mName = mainClassName.trim();
529             pName = null;
530         } else {
531             mName = mainClassName.substring( lastDotIdx + 1 ).trim();
532             pName = mainClassName.substring( 0, lastDotIdx ).trim();
533         }
534         
535         if ( mName.length() == 0 ) {
536             return;
537         }
538         
539         FileObject mainTemplate = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/Classes/Main.java" ); // NOI18N
540

541         if ( mainTemplate == null ) {
542             return; // Don't know the template
543
}
544         
545         DataObject mt = DataObject.find( mainTemplate );
546         
547         FileObject pkgFolder = srcFolder;
548         if ( pName != null ) {
549             String JavaDoc fName = pName.replace( '.', '/' ); // NOI18N
550
pkgFolder = FileUtil.createFolder( srcFolder, fName );
551         }
552         DataFolder pDf = DataFolder.findFolder( pkgFolder );
553         mt.createFromTemplate( pDf, mName );
554         
555     }
556     
557     
558     private static void refreshFileSystem(final File JavaDoc dir) throws FileStateInvalidException {
559         File JavaDoc rootF = dir;
560         while (rootF.getParentFile() != null) {
561             rootF = rootF.getParentFile();
562         }
563         FileObject dirFO = FileUtil.toFileObject(rootF);
564         assert dirFO != null : "At least disk roots must be mounted! " + rootF; // NOI18N
565
dirFO.getFileSystem().refresh(false);
566     }
567     
568     /**
569      * Set J2SE platform to be used.
570      *
571      * @param helper "reference" to project to be updated
572      * @param platformName the name of the J2SE platform
573      * @param sourceLevel the source level to be set
574      */

575     // AB: this method is also called from the enterprise application, so we can't pass UpdateHelper here
576
// well, actually we can, but let's not expose too many classes
577
public static void setPlatform(final AntProjectHelper helper, final String JavaDoc platformName, final String JavaDoc sourceLevel) {
578         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
579             public void run() {
580                 try {
581                     AppClientProject project = (AppClientProject)ProjectManager.getDefault().findProject(helper.getProjectDirectory());
582                     UpdateHelper updateHelper = project.getUpdateHelper();
583                     EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
584                     String JavaDoc finalPlatformName = platformName;
585                     if (finalPlatformName == null) {
586                         finalPlatformName = JavaPlatformManager.getDefault().getDefaultPlatform().getDisplayName();
587                     }
588                     
589                     // #89131: these levels are not actually distinct from 1.5.
590
String JavaDoc srcLevel = sourceLevel;
591                     if (sourceLevel.equals("1.6") || sourceLevel.equals("1.7"))
592                         srcLevel = "1.5";
593                     PlatformUiSupport.storePlatform(ep, updateHelper, finalPlatformName, srcLevel != null ? new SpecificationVersion(srcLevel) : null);
594                     helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
595                     ProjectManager.getDefault().saveProject(ProjectManager.getDefault().findProject(helper.getProjectDirectory()));
596                 } catch (IOException JavaDoc e) {
597                     ErrorManager.getDefault().notify(e);
598                 }
599             }
600         });
601     }
602     
603     /**
604      * Create a new application manifest file with minimal initial contents.
605      * @param dir the directory to create it in
606      * @param path the relative path of the file
607      * @throws IOException in case of problems
608      */

609     private static void createManifest(FileObject dir, String JavaDoc path) throws IOException JavaDoc {
610         if (dir.getFileObject(path) == null) {
611             FileObject manifest = FileUtil.createData(dir, path);
612             FileLock lock = manifest.lock();
613             try {
614                 OutputStream JavaDoc os = manifest.getOutputStream(lock);
615                 try {
616                     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(os);
617                     pw.println("Manifest-Version: 1.0"); // NOI18N
618
pw.println("X-COMMENT: Main-Class will be added automatically by build"); // NOI18N
619
pw.println(); // safest to end in \n\n due to JRE parsing bug
620
pw.flush();
621                 } finally {
622                     os.close();
623                 }
624             } finally {
625                 lock.releaseLock();
626             }
627         }
628     }
629     
630 }
631
632
633
Popular Tags