KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > api > WebProjectUtilities


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.web.project.api;
21
22 import org.netbeans.api.java.project.JavaProjectConstants;
23 import org.netbeans.api.project.Project;
24 import org.netbeans.api.project.ProjectManager;
25 import org.netbeans.modules.j2ee.deployment.devmodules.api.AntDeploymentHelper;
26 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
27 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
28 import org.netbeans.modules.web.api.webmodule.WebModule;
29 import org.netbeans.modules.web.project.*;
30 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
31 import org.netbeans.spi.project.support.ant.AntProjectHelper;
32 import org.netbeans.spi.project.support.ant.EditableProperties;
33 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
34 import org.netbeans.spi.project.support.ant.ProjectGenerator;
35 import org.netbeans.spi.project.support.ant.PropertyUtils;
36 import org.netbeans.spi.project.support.ant.ReferenceHelper;
37 import org.openide.ErrorManager;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileSystem;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.filesystems.FileLock;
42 import org.openide.filesystems.Repository;
43 import org.openide.loaders.DataFolder;
44 import org.openide.loaders.DataObject;
45 import org.openide.util.Mutex;
46 import org.openide.util.MutexException;
47 import org.openide.util.NbBundle;
48 import org.w3c.dom.Document JavaDoc;
49 import org.w3c.dom.Element JavaDoc;
50
51 import java.io.*;
52 import java.util.Enumeration JavaDoc;
53 import java.util.LinkedList JavaDoc;
54 import java.util.List JavaDoc;
55
56 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
57 import org.netbeans.modules.web.project.classpath.ClassPathSupport;
58 import org.netbeans.modules.web.project.classpath.WebProjectClassPathExtender;
59 import org.netbeans.modules.web.project.ui.customizer.PlatformUiSupport;
60 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants;
61 import org.openide.modules.SpecificationVersion;
62 import org.w3c.dom.NodeList JavaDoc;
63
64 import org.netbeans.modules.web.project.ui.wizards.FileSearchUtility;
65
66 /**
67  * Create a fresh WebProject from scratch or by importing and exisitng web module
68  * in one of the recognized directory structures.
69  *
70  * @author Pavel Buzek
71  */

72 public class WebProjectUtilities {
73     
74     /**
75      * BluePrints source structure
76      */

77     public static final String JavaDoc SRC_STRUCT_BLUEPRINTS = "BluePrints"; //NOI18N
78

79     /**
80      * Jakarta source structure
81      */

82     public static final String JavaDoc SRC_STRUCT_JAKARTA = "Jakarta"; //NOI18N
83

84     private static final String JavaDoc DEFAULT_DOC_BASE_FOLDER = "web"; //NOI18N
85
private static final String JavaDoc DEFAULT_SRC_FOLDER = "src"; //NOI18N
86
private static final String JavaDoc DEFAULT_RESOURCE_FOLDER = "setup"; //NOI18N
87
private static final String JavaDoc DEFAULT_JAVA_FOLDER = "java"; //NOI18N
88
private static final String JavaDoc DEFAULT_CONF_FOLDER = "conf"; //NOI18N
89
private static final String JavaDoc DEFAULT_BUILD_DIR = "build"; //NOI18N
90

91     private static final String JavaDoc DEFAULT_TEST_FOLDER = "test"; //NOI18N
92

93     private static final String JavaDoc WEB_INF = "WEB-INF"; //NOI18N
94
private static final String JavaDoc SOURCE_ROOT_REF = "${" + WebProjectProperties.SOURCE_ROOT + "}"; //NOI18N
95

96     public static final String JavaDoc MINIMUM_ANT_VERSION = "1.6";
97
98     private WebProjectUtilities() {}
99
100     /**
101      * Create a new empty web project.
102      *
103      * @deprecated Use {@link #createProject(WebProjectCreateData)}
104      *
105      * @return the helper object permitting it to be further customized
106      * @throws IOException in case something went wrong
107      */

108     @Deprecated JavaDoc
109     public static AntProjectHelper createProject(File dir, String JavaDoc name, String JavaDoc serverInstanceID, String JavaDoc sourceStructure, String JavaDoc j2eeLevel, String JavaDoc contextPath)
110             throws IOException {
111         WebProjectCreateData createData = new WebProjectCreateData();
112         createData.setProjectDir(dir);
113         createData.setName(name);
114         createData.setServerInstanceID(serverInstanceID);
115         createData.setSourceStructure(sourceStructure);
116         createData.setJavaEEVersion(j2eeLevel);
117         createData.setContextPath(contextPath);
118         return createProject(createData);
119     }
120         
121     /**
122      * Creates a new empty web project.
123      * @param createData the object encapsulating necessary data to create the project
124      * @return the helper object permitting it to be further customized
125      * @throws IOException in case something went wrong
126      */

127     public static AntProjectHelper createProject(final WebProjectCreateData createData) throws IOException {
128         File dir = createData.getProjectDir();
129         String JavaDoc name = createData.getName();
130         String JavaDoc serverInstanceID = createData.getServerInstanceID();
131         String JavaDoc sourceStructure = createData.getSourceStructure();
132         String JavaDoc j2eeLevel = createData.getJavaEEVersion();
133         String JavaDoc contextPath = createData.getContextPath();
134         String JavaDoc javaPlatformName = createData.getJavaPlatformName();
135         String JavaDoc sourceLevel = createData.getSourceLevel();
136         
137         assert dir != null: "Project folder can't be null"; //NOI18N
138
assert name != null: "Project name can't be null"; //NOI18N
139
assert serverInstanceID != null: "Server instance ID can't be null"; //NOI18N
140
assert sourceStructure != null: "Source structure can't be null"; //NOI18N
141
assert j2eeLevel != null: "Java EE version can't be null"; //NOI18N
142

143         final boolean createBluePrintsStruct = SRC_STRUCT_BLUEPRINTS.equals(sourceStructure);
144         final boolean createJakartaStructure = SRC_STRUCT_JAKARTA.equals(sourceStructure);
145         
146         final FileObject fo = Utils.getValidEmptyDir(dir);
147         AntProjectHelper h = setupProject (fo, name, serverInstanceID, j2eeLevel);
148         
149         FileObject srcFO = fo.createFolder(DEFAULT_SRC_FOLDER);
150         FileObject confFolderFO = null;
151         
152         if (createBluePrintsStruct) {
153             srcFO.createFolder(DEFAULT_JAVA_FOLDER);
154             confFolderFO = srcFO.createFolder(DEFAULT_CONF_FOLDER);
155         }
156         
157         if(createJakartaStructure) {
158             confFolderFO = fo.createFolder(DEFAULT_CONF_FOLDER);
159         }
160         
161         //create default manifest
162
if(confFolderFO != null) {
163             FileUtil.copyFile(Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-project/MANIFEST.MF"), confFolderFO, "MANIFEST"); //NOI18N
164
}
165         
166         //test folder
167
FileUtil.createFolder(fo, DEFAULT_TEST_FOLDER);
168
169         FileObject webFO = fo.createFolder(DEFAULT_DOC_BASE_FOLDER);
170         final FileObject webInfFO = webFO.createFolder(WEB_INF);
171         // create web.xml
172
// PENDING : should be easier to define in layer and copy related FileObject (doesn't require systemClassLoader)
173
String JavaDoc webXMLContent = null;
174         if (J2eeModule.JAVA_EE_5.equals(j2eeLevel))
175             webXMLContent = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-project/web-2.5.xml").getInputStream ()); //NOI18N
176
else if (WebModule.J2EE_14_LEVEL.equals(j2eeLevel))
177             webXMLContent = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-project/web-2.4.xml").getInputStream ()); //NOI18N
178
else if (WebModule.J2EE_13_LEVEL.equals(j2eeLevel))
179             webXMLContent = readResource (Repository.getDefault().getDefaultFileSystem().findResource("org-netbeans-modules-web-project/web-2.3.xml").getInputStream ()); //NOI18N
180
assert webXMLContent != null : "Cannot find web.xml template for J2EE specification level:" + j2eeLevel;
181         final String JavaDoc webXmlText = webXMLContent;
182         FileSystem fs = webInfFO.getFileSystem();
183         fs.runAtomicAction(new FileSystem.AtomicAction() {
184             public void run() throws IOException {
185                 FileObject webXML = FileUtil.createData(webInfFO, "web.xml");//NOI18N
186
FileLock lock = webXML.lock();
187                 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(webXML.getOutputStream(lock)));
188                 try {
189                     bw.write(webXmlText);
190                 }
191                 finally {
192                     bw.close();
193                     lock.releaseLock();
194                 }
195             }
196         });
197         
198         EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
199         Element JavaDoc data = h.getPrimaryConfigurationData(true);
200         Document JavaDoc doc = data.getOwnerDocument();
201         Element JavaDoc sourceRoots = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); //NOI18N
202

203         Element JavaDoc rootSrc = doc.createElementNS (WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
204
rootSrc.setAttribute ("id",WebProjectProperties.SRC_DIR); //NOI18N
205
rootSrc.setAttribute ("name",NbBundle.getMessage(WebProjectUtilities.class, "NAME_src.dir")); //NOI18N
206
sourceRoots.appendChild(rootSrc);
207         if (createBluePrintsStruct)
208             ep.setProperty(WebProjectProperties.SRC_DIR, DEFAULT_SRC_FOLDER + "/" + DEFAULT_JAVA_FOLDER); // NOI18N
209
else
210             ep.setProperty(WebProjectProperties.SRC_DIR, DEFAULT_SRC_FOLDER); // NOI18N
211

212         data.appendChild (sourceRoots);
213         Element JavaDoc testRoots = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); //NOI18N
214

215         Element JavaDoc rootTest = doc.createElementNS (WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
216
rootTest.setAttribute ("id",WebProjectProperties.TEST_SRC_DIR); //NOI18N
217
rootTest.setAttribute ("name",NbBundle.getMessage(WebProjectUtilities.class, "NAME_test.src.dir")); //NOI18N
218
testRoots.appendChild (rootTest);
219         ep.setProperty(WebProjectProperties.TEST_SRC_DIR, DEFAULT_TEST_FOLDER); // NOI18N
220

221         data.appendChild (testRoots);
222         h.putPrimaryConfigurationData(data, true);
223         
224         ep.put (WebProjectProperties.SOURCE_ROOT, createBluePrintsStruct ? DEFAULT_SRC_FOLDER : "."); //NOI18N
225

226         ep.setProperty(WebProjectProperties.WEB_DOCBASE_DIR, DEFAULT_DOC_BASE_FOLDER);
227         if (createBluePrintsStruct) {
228             ep.setProperty(WebProjectProperties.SRC_DIR, "${" + WebProjectProperties.SOURCE_ROOT + "}/" + DEFAULT_JAVA_FOLDER);
229             ep.setProperty(WebProjectProperties.CONF_DIR, "${" + WebProjectProperties.SOURCE_ROOT + "}/" + DEFAULT_CONF_FOLDER);
230         } else {
231             ep.setProperty(WebProjectProperties.SRC_DIR, DEFAULT_SRC_FOLDER);
232         }
233         
234         if(createJakartaStructure) {
235             ep.setProperty(WebProjectProperties.CONF_DIR, DEFAULT_CONF_FOLDER);
236         }
237         
238         ep.setProperty(WebProjectProperties.RESOURCE_DIR, DEFAULT_RESOURCE_FOLDER);
239         ep.setProperty(WebProjectProperties.LIBRARIES_DIR, "${" + WebProjectProperties.WEB_DOCBASE_DIR + "}/WEB-INF/lib"); //NOI18N
240

241         Project p = ProjectManager.getDefault().findProject(h.getProjectDirectory ());
242         UpdateHelper updateHelper = ((WebProject) p).getUpdateHelper();
243         
244         // #89131: these levels are not actually distinct from 1.5.
245
if (sourceLevel != null && (sourceLevel.equals("1.6") || sourceLevel.equals("1.7")))
246             sourceLevel = "1.5";
247         PlatformUiSupport.storePlatform(ep, updateHelper, javaPlatformName, sourceLevel != null ? new SpecificationVersion(sourceLevel) : null);
248         
249         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
250         
251         ProjectManager.getDefault().saveProject(p);
252
253         //create default index.jsp
254
createIndexJSP(webFO);
255         
256         ProjectWebModule pwm = (ProjectWebModule) p.getLookup ().lookup (ProjectWebModule.class);
257         if (pwm != null) //should not be null
258
pwm.setContextPath(contextPath);
259
260         return h;
261     }
262
263     /**
264      * Creates a web project from esisting sources.
265      *
266      * @deprecated Use {@link #importProject(WebProjectCreateData)}
267      *
268      * @return the helper object permitting it to be further customized
269      * @throws IOException in case something went wrong
270      */

271     @Deprecated JavaDoc
272     public static AntProjectHelper importProject(File dir, String JavaDoc name, FileObject wmFO, FileObject javaRoot, FileObject docBase, FileObject libFolder, String JavaDoc j2eeLevel, String JavaDoc serverInstanceID, String JavaDoc buildfile) throws IOException {
273         WebProjectCreateData createData = new WebProjectCreateData();
274         createData.setProjectDir(dir);
275         createData.setName(name);
276         createData.setWebModuleFO(wmFO);
277         createData.setSourceFolders(new File[] {FileUtil.toFile(javaRoot)});
278         createData.setTestFolders(null);
279         createData.setDocBase(docBase);
280         createData.setLibFolder(libFolder);
281         createData.setJavaEEVersion(j2eeLevel);
282         createData.setServerInstanceID(serverInstanceID);
283         createData.setBuildfile(buildfile);
284         return importProject(createData);
285     }
286     
287     /**
288      * Creates a web project from esisting sources.
289      *
290      * @deprecated Use {@link #importProject(WebProjectCreateData)}
291      *
292      * @return the helper object permitting it to be further customized
293      * @throws IOException in case something went wrong
294      */

295     @Deprecated JavaDoc
296     public static AntProjectHelper importProject(final File dir, String JavaDoc name, FileObject wmFO, final File[] sourceFolders, File[] tstFolders, FileObject docBase, FileObject libFolder, String JavaDoc j2eeLevel, String JavaDoc serverInstanceID, String JavaDoc buildfile) throws IOException {
297         WebProjectCreateData createData = new WebProjectCreateData();
298         createData.setProjectDir(dir);
299         createData.setName(name);
300         createData.setWebModuleFO(wmFO);
301         createData.setSourceFolders(sourceFolders);
302         createData.setTestFolders(tstFolders);
303         createData.setDocBase(docBase);
304         createData.setLibFolder(libFolder);
305         createData.setJavaEEVersion(j2eeLevel);
306         createData.setServerInstanceID(serverInstanceID);
307         createData.setBuildfile(buildfile);
308         return importProject(createData);
309     }
310
311     /**
312      * Creates a web project from esisting sources.
313      * @param createData the object encapsulating necessary data to create the project
314      * @return the helper object permitting it to be further customized
315      * @throws IOException in case something went wrong
316      */

317     public static AntProjectHelper importProject(final WebProjectCreateData createData) throws IOException {
318         final File dir = createData.getProjectDir();
319         String JavaDoc name = createData.getName();
320         FileObject wmFO = createData.getWebModuleFO();
321         final File[] sourceFolders = createData.getSourceFolders();
322         File[] tstFolders = createData.getTestFolders();
323         FileObject docBase = createData.getDocBase();
324         FileObject libFolder = createData.getLibFolder();
325         String JavaDoc j2eeLevel = createData.getJavaEEVersion();
326         String JavaDoc serverInstanceID = createData.getServerInstanceID();
327         String JavaDoc buildfile = createData.getBuildfile();
328         String JavaDoc javaPlatformName = createData.getJavaPlatformName();
329         String JavaDoc sourceLevel = createData.getSourceLevel();
330         boolean javaSourceBased = createData.getJavaSourceBased();
331         
332         assert dir != null: "Project folder can't be null"; //NOI18N
333
assert name != null: "Project name can't be null"; //NOI18N
334
assert wmFO != null: "File object representation of the imported web project location can't be null"; //NOI18N
335
assert sourceFolders != null: "Source package root can't be null"; //NOI18N
336
assert docBase != null: "Web Pages folder can't be null"; //NOI18N
337
assert serverInstanceID != null: "Server instance ID can't be null"; //NOI18N
338
assert j2eeLevel != null: "Java EE version can't be null"; //NOI18N
339

340         FileObject fo = Utils.getValidDir(dir);
341         
342         final AntProjectHelper antProjectHelper = setupProject(fo, name, serverInstanceID, j2eeLevel);
343         final WebProject p = (WebProject) ProjectManager.getDefault().findProject(antProjectHelper.getProjectDirectory());
344         final ReferenceHelper referenceHelper = p.getReferenceHelper();
345         EditableProperties ep = new EditableProperties(true);
346         
347         if (FileUtil.isParentOf(fo, wmFO) || fo.equals(wmFO)) {
348             ep.setProperty(WebProjectProperties.SOURCE_ROOT, "."); //NOI18N
349
} else {
350             ep.setProperty(WebProjectProperties.SOURCE_ROOT,
351                     referenceHelper.createForeignFileReference(FileUtil.toFile(wmFO), null));
352         }
353         ep.setProperty(WebProjectProperties.WEB_DOCBASE_DIR, createFileReference(referenceHelper, fo, wmFO, docBase));
354         
355         final File[] testFolders = tstFolders;
356         try {
357         ProjectManager.mutex().writeAccess( new Mutex.ExceptionAction () {
358             public Object JavaDoc run() throws Exception JavaDoc {
359                 Element JavaDoc data = antProjectHelper.getPrimaryConfigurationData(true);
360                 Document JavaDoc doc = data.getOwnerDocument();
361                 
362                 Element JavaDoc sourceRoots = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); //NOI18N
363
data.appendChild (sourceRoots);
364                 Element JavaDoc testRoots = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); //NOI18N
365
data.appendChild (testRoots);
366         
367                 NodeList JavaDoc nl = data.getElementsByTagNameNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots");
368                 assert nl.getLength() == 1;
369                 sourceRoots = (Element JavaDoc) nl.item(0);
370                 nl = data.getElementsByTagNameNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"test-roots"); //NOI18N
371
assert nl.getLength() == 1;
372                 testRoots = (Element JavaDoc) nl.item(0);
373                 for (int i=0; i<sourceFolders.length; i++) {
374                     String JavaDoc propName = "src.dir" + (i == 0 ? "" : Integer.toString (i+1)); //NOI18N
375
String JavaDoc srcReference = referenceHelper.createForeignFileReference(sourceFolders[i], JavaProjectConstants.SOURCES_TYPE_JAVA);
376                     Element JavaDoc root = doc.createElementNS (WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
377
root.setAttribute ("id",propName); //NOI18N
378
root.setAttribute ("name",NbBundle.getMessage(WebProjectUtilities.class, "NAME_src.dir")); //NOI18N
379
sourceRoots.appendChild(root);
380                     EditableProperties props = antProjectHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
381                     props.put(propName,srcReference);
382                     antProjectHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); // #47609
383
}
384
385                 if (testFolders == null || testFolders.length == 0) {
386                     EditableProperties props = antProjectHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
387                     props.put("test.src.dir", "");
388                     antProjectHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); // #47609
389
}
390                 else {
391                     for (int i=0; i<testFolders.length; i++) {
392                         if (!testFolders[i].exists()) {
393                             testFolders[i].mkdirs();
394                         }
395
396                         String JavaDoc name = testFolders[i].getName();
397                         String JavaDoc propName = "test." + name + ".dir"; //NOI18N
398
int rootIndex = 1;
399                         EditableProperties props = antProjectHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
400                         while (props.containsKey(propName)) {
401                             rootIndex++;
402                             propName = "test." + name + rootIndex + ".dir"; //NOI18N
403
}
404                         String JavaDoc testReference = referenceHelper.createForeignFileReference(testFolders[i], JavaProjectConstants.SOURCES_TYPE_JAVA);
405                         Element JavaDoc root = doc.createElementNS (WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N
406
root.setAttribute ("id",propName); //NOI18N
407
root.setAttribute ("name",NbBundle.getMessage(WebProjectUtilities.class, "NAME_test.src.dir")); //NOI18N
408
testRoots.appendChild(root);
409                         props = antProjectHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); // #47609
410
props.put(propName,testReference);
411                         antProjectHelper.putProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
412                     }
413                 }
414                 antProjectHelper.putPrimaryConfigurationData(data,true);
415                 ProjectManager.getDefault().saveProject (p);
416                 return null;
417             }
418         });
419         } catch (MutexException me ) {
420             ErrorManager.getDefault().notify (me);
421         }
422
423         if (libFolder != null) {
424             ep.setProperty(WebProjectProperties.LIBRARIES_DIR, createFileReference(referenceHelper, fo, wmFO, libFolder));
425             
426             //add libraries from the specified folder in the import wizard
427
if (libFolder.isFolder()) {
428                 FileObject children [] = libFolder.getChildren ();
429                 List JavaDoc libs = new LinkedList JavaDoc();
430                 for (int i = 0; i < children.length; i++) {
431                     if (FileUtil.isArchiveFile(children[i]))
432                         libs.add(children[i]);
433                 }
434                 FileObject[] libsArray = new FileObject[libs.size()];
435                 libs.toArray(libsArray);
436                 WebProjectClassPathExtender classPathExtender = (WebProjectClassPathExtender) p.getLookup().lookup(WebProjectClassPathExtender.class);
437                 classPathExtender.addArchiveFiles(WebProjectProperties.JAVAC_CLASSPATH, libsArray, ClassPathSupport.TAG_WEB_MODULE_LIBRARIES);
438                 //do we really need to add the listener? commenting it out
439
//libFolder.addFileChangeListener(p);
440
}
441         }
442         
443         if (!GeneratedFilesHelper.BUILD_XML_PATH.equals(buildfile)) {
444             ep.setProperty(WebProjectProperties.BUILD_FILE, buildfile);
445         }
446         
447         //creates conf.dir property and tries to simply guess it
448
//(it would be nice to have a possibily to set this property in the wizard)
449
Enumeration JavaDoc ch = FileSearchUtility.getChildrenToDepth(fo, 4, true);
450         String JavaDoc confDir = ""; //NOI18N
451
while (ch.hasMoreElements ()) {
452             FileObject f = (FileObject) ch.nextElement ();
453             if (f.isFolder() && f.getName().equalsIgnoreCase("conf")) { //NOI18N
454
confDir = FileUtil.getRelativePath(fo, f);
455                 break;
456             }
457         }
458         if (confDir.equals("")) { //NOI18N
459
// if no conf directory was found, create default directory (#82147)
460
fo.createFolder(DEFAULT_CONF_FOLDER);
461             ep.setProperty(WebProjectProperties.CONF_DIR, DEFAULT_CONF_FOLDER);
462         } else
463             ep.setProperty(WebProjectProperties.CONF_DIR, confDir); //NOI18N
464

465         //create resource.dir property, by default set to "setup"
466
//(it would be nice to have a possibily to set this property in the wizard)
467
ep.setProperty(WebProjectProperties.RESOURCE_DIR, DEFAULT_RESOURCE_FOLDER);
468         
469         ep.setProperty(WebProjectProperties.JAVA_SOURCE_BASED,javaSourceBased+"");
470
471         UpdateHelper updateHelper = ((WebProject) p).getUpdateHelper();
472         // #89131: these levels are not actually distinct from 1.5.
473
if (sourceLevel != null && (sourceLevel.equals("1.6") || sourceLevel.equals("1.7")))
474             sourceLevel = "1.5";
475         PlatformUiSupport.storePlatform(ep, updateHelper, javaPlatformName, sourceLevel != null ? new SpecificationVersion(sourceLevel) : null);
476         
477         // Utils.updateProperties() prevents problems caused by modification of properties in AntProjectHelper
478
// (e.g. during createForeignFileReference()) when local copy of properties is concurrently modified
479
Utils.updateProperties(antProjectHelper, AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
480         
481         ProjectManager.getDefault().saveProject(p);
482         
483         return antProjectHelper;
484     }
485
486     private static String JavaDoc createFileReference(ReferenceHelper refHelper, FileObject projectFO, FileObject sourceprojectFO, FileObject referencedFO) {
487         if (FileUtil.isParentOf(projectFO, referencedFO)) {
488             return relativePath(projectFO, referencedFO);
489         } else if (FileUtil.isParentOf(sourceprojectFO, referencedFO)) {
490             String JavaDoc s = relativePath(sourceprojectFO, referencedFO);
491             return s.length() > 0 ? SOURCE_ROOT_REF + "/" + s : SOURCE_ROOT_REF; //NOI18N
492
} else {
493             return refHelper.createForeignFileReference(FileUtil.toFile(referencedFO), null);
494         }
495     }
496
497     private static String JavaDoc relativePath (FileObject parent, FileObject child) {
498         if (child.equals (parent))
499             return ""; // NOI18N
500
if (!FileUtil.isParentOf (parent, child))
501             throw new IllegalArgumentException JavaDoc ("Cannot find relative path, " + parent + " is not parent of " + child);
502         return child.getPath ().substring (parent.getPath ().length () + 1);
503     }
504
505     private static AntProjectHelper setupProject (FileObject dirFO, String JavaDoc name, String JavaDoc serverInstanceID, String JavaDoc j2eeLevel) throws IOException {
506         AntProjectHelper h = ProjectGenerator.createProject(dirFO, WebProjectType.TYPE);
507         Element JavaDoc data = h.getPrimaryConfigurationData(true);
508         Document JavaDoc doc = data.getOwnerDocument();
509         Element JavaDoc nameEl = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, "name"); // NOI18N
510
nameEl.appendChild(doc.createTextNode(name));
511         data.appendChild(nameEl);
512         Element JavaDoc minant = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, "minimum-ant-version"); // NOI18N
513
minant.appendChild(doc.createTextNode(MINIMUM_ANT_VERSION)); // NOI18N
514
data.appendChild(minant);
515         
516         Element JavaDoc wmLibs = doc.createElementNS (WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, "web-module-libraries"); //NOI18N
517
data.appendChild (wmLibs);
518         
519         Element JavaDoc addLibs = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, "web-module-additional-libraries"); //NOI18N
520
data.appendChild(addLibs);
521         
522         h.putPrimaryConfigurationData(data, true);
523         EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
524         // XXX the following just for testing, TBD:
525
ep.setProperty(WebProjectProperties.DIST_DIR, "dist"); // NOI18N
526
ep.setProperty(WebProjectProperties.DIST_WAR, "${"+WebProjectProperties.DIST_DIR+"}/${" + WebProjectProperties.WAR_NAME + "}"); // NOI18N
527
ep.setProperty(WebProjectProperties.DIST_WAR_EAR, "${" + WebProjectProperties.DIST_DIR+"}/${" + WebProjectProperties.WAR_EAR_NAME + "}"); //NOI18N
528

529         ep.setProperty(WebProjectProperties.JAVAC_CLASSPATH, ""); // NOI18N
530

531         ep.setProperty(WebProjectProperties.JSPCOMPILATION_CLASSPATH, "${jspc.classpath}:${javac.classpath}");
532             
533         ep.setProperty(WebProjectProperties.J2EE_PLATFORM, j2eeLevel);
534         
535         ep.setProperty(WebProjectProperties.WAR_NAME, PropertyUtils.getUsablePropertyName(name) + ".war"); // NOI18N
536
//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
537
ep.setProperty(WebProjectProperties.WAR_EAR_NAME, PropertyUtils.getUsablePropertyName(name) + ".war"); // NOI18N
538

539         ep.setProperty(WebProjectProperties.WAR_COMPRESS, "false"); // NOI18N
540
ep.setProperty(WebProjectProperties.WAR_PACKAGE, "true"); // NOI18N
541
ep.setProperty(WebProjectProperties.WAR_CONTENT_ADDITIONAL, ""); // NOI18N
542

543         ep.setProperty(WebProjectProperties.LAUNCH_URL_RELATIVE, ""); // NOI18N
544
ep.setProperty(WebProjectProperties.DISPLAY_BROWSER, "true"); // NOI18N
545
Deployment deployment = Deployment.getDefault ();
546         ep.setProperty(WebProjectProperties.J2EE_SERVER_TYPE, deployment.getServerID (serverInstanceID));
547             
548         ep.setProperty(WebProjectProperties.JAVAC_DEBUG, "true"); // NOI18N
549
ep.setProperty(WebProjectProperties.JAVAC_DEPRECATION, "false"); // NOI18N
550
ep.setProperty("javac.compilerargs", ""); // NOI18N
551
ep.setComment("javac.compilerargs", new String JavaDoc[] { // NOI18N
552
"# " + NbBundle.getMessage(WebProjectUtilities.class, "COMMENT_javac.compilerargs"), // NOI18N
553
}, false);
554
555         ep.setProperty(WebProjectProperties.JAVAC_TEST_CLASSPATH, new String JavaDoc[] {
556             "${javac.classpath}:", // NOI18N
557
"${build.classes.dir}:", // NOI18N
558
"${libs.junit.classpath}", // NOI18N
559
});
560         ep.setProperty(WebProjectProperties.RUN_TEST_CLASSPATH, new String JavaDoc[] {
561             "${javac.test.classpath}:", // NOI18N
562
"${build.test.classes.dir}", // NOI18N
563
});
564         ep.setProperty(WebProjectProperties.DEBUG_TEST_CLASSPATH, new String JavaDoc[] {
565             "${run.test.classpath}", // NOI18N
566
});
567
568         ep.setProperty(WebProjectProperties.BUILD_DIR, DEFAULT_BUILD_DIR);
569         ep.setProperty(WebProjectProperties.BUILD_TEST_CLASSES_DIR, "${build.dir}/test/classes"); // NOI18N
570
ep.setProperty(WebProjectProperties.BUILD_TEST_RESULTS_DIR, "${build.dir}/test/results"); // NOI18N
571
ep.setProperty(WebProjectProperties.BUILD_WEB_DIR, "${"+WebProjectProperties.BUILD_DIR+"}/web"); // NOI18N
572
ep.setProperty(WebProjectProperties.BUILD_EAR_WEB_DIR, "${"+WebProjectProperties.BUILD_DIR+"}/ear-module"); // NOI18N
573
ep.setProperty(WebProjectProperties.BUILD_GENERATED_DIR, "${"+WebProjectProperties.BUILD_DIR+"}/generated"); // NOI18N
574
ep.setProperty(WebProjectProperties.BUILD_CLASSES_DIR, "${"+WebProjectProperties.BUILD_WEB_DIR+"}/WEB-INF/classes"); // NOI18N
575
ep.setProperty(WebProjectProperties.BUILD_EAR_CLASSES_DIR, "${"+WebProjectProperties.BUILD_EAR_WEB_DIR+"}/WEB-INF/classes"); // NOI18N
576
ep.setProperty(WebProjectProperties.BUILD_CLASSES_EXCLUDES, "**/*.java,**/*.form"); // NOI18N
577
ep.setProperty(WebProjectProperties.BUILD_WEB_EXCLUDES, "${"+ WebProjectProperties.BUILD_CLASSES_EXCLUDES +"}"); //NOI18N
578
ep.setProperty(WebProjectProperties.DIST_JAVADOC_DIR, "${"+WebProjectProperties.DIST_DIR+"}/javadoc"); // NOI18N
579
ep.setProperty(WebProjectProperties.NO_DEPENDENCIES, "false"); // NOI18N
580
ep.setProperty(WebProjectProperties.JAVA_PLATFORM, "default_platform"); // NOI18N
581
ep.setProperty(WebProjectProperties.DEBUG_CLASSPATH, "${"+WebProjectProperties.JAVAC_CLASSPATH+"}:${"+WebProjectProperties.BUILD_CLASSES_DIR+"}:${"+WebProjectProperties.BUILD_EAR_CLASSES_DIR+"}"); // NOI18N
582

583         ep.setProperty("runmain.jvmargs", ""); // NOI18N
584
ep.setComment("runmain.jvmargs", new String JavaDoc[] { // NOI18N
585
"# " + NbBundle.getMessage(WebProjectUtilities.class, "COMMENT_runmain.jvmargs"), // NOI18N
586
"# " + NbBundle.getMessage(WebProjectUtilities.class, "COMMENT_runmain.jvmargs_2"), // NOI18N
587
}, false);
588         
589         ep.setProperty(WebProjectProperties.JAVADOC_PRIVATE, "false"); // NOI18N
590
ep.setProperty(WebProjectProperties.JAVADOC_NO_TREE, "false"); // NOI18N
591
ep.setProperty(WebProjectProperties.JAVADOC_USE, "true"); // NOI18N
592
ep.setProperty(WebProjectProperties.JAVADOC_NO_NAVBAR, "false"); // NOI18N
593
ep.setProperty(WebProjectProperties.JAVADOC_NO_INDEX, "false"); // NOI18N
594
ep.setProperty(WebProjectProperties.JAVADOC_SPLIT_INDEX, "true"); // NOI18N
595
ep.setProperty(WebProjectProperties.JAVADOC_AUTHOR, "false"); // NOI18N
596
ep.setProperty(WebProjectProperties.JAVADOC_VERSION, "false"); // NOI18N
597
ep.setProperty(WebProjectProperties.JAVADOC_WINDOW_TITLE, ""); // NOI18N
598
ep.setProperty(WebProjectProperties.JAVADOC_ENCODING, ""); // NOI18N
599
ep.setProperty(WebProjectProperties.JAVADOC_PREVIEW, "true"); // NOI18N
600
ep.setProperty(WebProjectProperties.JAVADOC_ADDITIONALPARAM, ""); // NOI18N
601

602         ep.setProperty(WebProjectProperties.COMPILE_JSPS, "false"); // NOI18N
603

604         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
605         
606         ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
607         ep.setProperty(WebProjectProperties.J2EE_SERVER_INSTANCE, serverInstanceID);
608         
609         // set j2ee.platform.classpath
610
J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstanceID);
611         if (!j2eePlatform.getSupportedSpecVersions(J2eeModule.WAR).contains(j2eeLevel)) {
612             ErrorManager.getDefault().log(ErrorManager.WARNING, "J2EE level:" + j2eeLevel + " not supported by server " + Deployment.getDefault().getServerInstanceDisplayName(serverInstanceID) + " for module type WAR");
613         }
614         String JavaDoc classpath = Utils.toClasspathString(j2eePlatform.getClasspathEntries());
615         ep.setProperty(WebProjectProperties.J2EE_PLATFORM_CLASSPATH, classpath);
616         
617         // set j2ee.platform.wscompile.classpath
618
if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSCOMPILE)) {
619             File[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSCOMPILE);
620             ep.setProperty(WebServicesConstants.J2EE_PLATFORM_WSCOMPILE_CLASSPATH,
621                     Utils.toClasspathString(wsClasspath));
622         }
623         
624         // set j2ee.platform.wsimport.classpath
625
if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSIMPORT)) {
626             File[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSIMPORT);
627             ep.setProperty(WebServicesConstants.J2EE_PLATFORM_WSIMPORT_CLASSPATH,
628                     Utils.toClasspathString(wsClasspath));
629         }
630
631         // set j2ee.platform.wsgen.classpath
632
if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSGEN)) {
633             File[] wsClasspath = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_WSGEN);
634             ep.setProperty(WebServicesConstants.J2EE_PLATFORM_WSGEN_CLASSPATH,
635                     Utils.toClasspathString(wsClasspath));
636         }
637
638         // set j2ee.platform.jsr109 support
639
if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_JSR109)) {
640             ep.setProperty(WebServicesConstants.J2EE_PLATFORM_JSR109_SUPPORT,
641                     "true"); //NOI18N
642
}
643         
644         // ant deployment support
645
File projectFolder = FileUtil.toFile(dirFO);
646         try {
647             AntDeploymentHelper.writeDeploymentScript(new File(projectFolder, WebProjectProperties.ANT_DEPLOY_BUILD_SCRIPT),
648                     J2eeModule.WAR, serverInstanceID);
649         } catch (IOException ioe) {
650             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
651         }
652         File deployAntPropsFile = AntDeploymentHelper.getDeploymentPropertiesFile(serverInstanceID);
653         if (deployAntPropsFile != null) {
654             ep.setProperty(WebProjectProperties.DEPLOY_ANT_PROPS_FILE, deployAntPropsFile.getAbsolutePath());
655         }
656         
657         h.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep);
658         
659         return h;
660     }
661
662     private static void createIndexJSP(FileObject webFolder) throws IOException {
663         FileObject jspTemplate = Repository.getDefault().getDefaultFileSystem().findResource( "Templates/JSP_Servlet/JSP.jsp" ); // NOI18N
664

665         if (jspTemplate == null)
666             return; // Don't know the template
667

668         DataObject mt = DataObject.find(jspTemplate);
669         DataFolder webDf = DataFolder.findFolder(webFolder);
670         mt.createFromTemplate(webDf, "index"); // NOI18N
671
}
672
673     private static String JavaDoc readResource(InputStream is) throws IOException {
674         // read the config from resource first
675
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
676         String JavaDoc lineSep = System.getProperty("line.separator");//NOI18N
677
BufferedReader br = new BufferedReader(new InputStreamReader(is));
678         String JavaDoc line = br.readLine();
679         while (line != null) {
680             sb.append(line);
681             sb.append(lineSep);
682             line = br.readLine();
683         }
684         br.close();
685         return sb.toString();
686     }
687     
688 }
689
Popular Tags