001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.compapp.projects.jbi;
042:
043: import org.netbeans.modules.compapp.projects.jbi.ui.customizer.JbiProjectProperties;
044:
045: import org.netbeans.api.project.Project;
046: import org.netbeans.api.project.ProjectManager;
047: import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
048:
049: import org.netbeans.spi.project.support.ant.AntProjectHelper;
050: import org.netbeans.spi.project.support.ant.EditableProperties;
051: import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
052: import org.netbeans.spi.project.support.ant.ProjectGenerator;
053:
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.util.NbBundle;
057:
058: import org.w3c.dom.Document;
059: import org.w3c.dom.Element;
060:
061: import java.io.File;
062: import java.io.IOException;
063: import java.nio.charset.Charset;
064: import org.netbeans.api.queries.FileEncodingQuery;
065: import org.netbeans.modules.compapp.projects.jbi.ComponentInfoGenerator;
066: import org.netbeans.modules.compapp.jbiserver.JbiManager;
067: import org.netbeans.modules.compapp.projects.jbi.api.JbiProjectConstants;
068:
069: /**
070: * Create a fresh EjbProject from scratch or by importing and exisitng web module in one of the
071: * recognized directory structures.
072: *
073: * @author Pavel Buzek
074: */
075: public class JbiProjectGenerator {
076: private static final String DEFAULT_CONF_FOLDER = "conf"; // NOI18N
077: private static final String DEFAULT_SRC_FOLDER = "src"; // NOI18N
078: private static final String DEFAULT_JBIASA_FOLDER = JbiProjectConstants.FOLDER_JBIASA;
079: private static final String DEFAULT_COMPONENTASA_FOLDER = "jbiServiceUnits"; // NOI18N
080: private static final String DEFAULT_BUILD_DIR = "build"; // NOI18N
081: private static final String DEFAULT_JBI_ROUTING = "true"; // NOI18N
082: private static final String DEFAULT_JBI_SA_INTERNAL_ROUTING = "true"; // NOI18N
083: // Start Test Framework
084: private static final String DEFAULT_TEST_FOLDER = "test"; // NOI18N
085: private static final String DEFAULT_TEST_RESULTS_FOLDER = "${basedir}/test/results"; // NOI18N
086:
087: // End Test Framework
088:
089: private JbiProjectGenerator() {
090: }
091:
092: /**
093: * Create a new empty project.
094: *
095: * @param dir the top-level directory (need not yet exist but if it does it must be empty)
096: * @param name the code name for the project
097: * @param j2eeLevel DOCUMENT ME!
098: *
099: * @return the helper object permitting it to be further customized
100: *
101: * @throws IOException in case something went wrong
102: */
103: public static AntProjectHelper createProject(File dir, String name,
104: String j2eeLevel) throws IOException {
105: dir.mkdirs();
106:
107: // XXX clumsy way to refresh, but otherwise it doesn't work for new folders
108: File rootF = dir;
109:
110: while (rootF.getParentFile() != null) {
111: rootF = rootF.getParentFile();
112: }
113:
114: FileObject fo = FileUtil.toFileObject(rootF);
115: assert fo != null : "At least disk roots must be mounted! "
116: + rootF; // NOI18N
117: fo.getFileSystem().refresh(false);
118: fo = FileUtil.toFileObject(dir);
119:
120: // vlv # 113228
121: if (fo == null) {
122: throw new IOException("Can't create " + dir.getName());
123: }
124: assert fo.isFolder() : "Not really a dir: " + dir; // NOI18N
125: assert fo.getChildren().length == 0 : "Dir must have been empty: "
126: + dir; // NOI18N
127:
128: AntProjectHelper h = setupProject(fo, name, j2eeLevel);
129:
130: FileObject srcRoot = fo.createFolder(DEFAULT_SRC_FOLDER); // NOI18N
131: srcRoot.createFolder(DEFAULT_JBIASA_FOLDER);
132:
133: FileObject confRoot = srcRoot.createFolder(DEFAULT_CONF_FOLDER);
134:
135: // Start Test Framework
136: fo.createFolder(DEFAULT_TEST_FOLDER);
137: // End Test Framework
138:
139: // Create default component info config files
140: // 04/12/06, NB FO returns different path format for Unix and Windoz..
141: String confDir = FileUtil.toFile(confRoot).getPath();
142: new ComponentInfoGenerator(confDir).doIt();
143:
144: EditableProperties ep = h
145: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
146: ep.put(JbiProjectProperties.SOURCE_ROOT, DEFAULT_SRC_FOLDER);
147: ep.setProperty(JbiProjectProperties.META_INF, "${"
148: + JbiProjectProperties.SOURCE_ROOT + "}/"
149: + DEFAULT_CONF_FOLDER // NOI18N
150: );
151: ep.setProperty(JbiProjectProperties.SRC_DIR, "${"
152: + JbiProjectProperties.SOURCE_ROOT + "}/"
153: + DEFAULT_JBIASA_FOLDER // NOI18N
154: );
155: ep.setProperty(JbiProjectProperties.SRC_BUILD_DIR, "${"
156: + JbiProjectProperties.SOURCE_ROOT + "}/"
157: + DEFAULT_COMPONENTASA_FOLDER // NOI18N
158: );
159: Charset enc = FileEncodingQuery.getDefaultEncoding();
160: ep
161: .setProperty(JbiProjectProperties.SOURCE_ENCODING, enc
162: .name());
163: ep.setProperty(JbiProjectProperties.JBI_ROUTING,
164: DEFAULT_JBI_ROUTING);
165: ep.setProperty(JbiProjectProperties.JBI_SA_INTERNAL_ROUTING,
166: DEFAULT_JBI_SA_INTERNAL_ROUTING);
167: // Start Test Framework
168: ep.setProperty(JbiProjectProperties.TEST_DIR,
169: DEFAULT_TEST_FOLDER);
170: ep.setProperty(JbiProjectProperties.TEST_RESULTS_DIR,
171: DEFAULT_TEST_RESULTS_FOLDER);
172: // End Test Framework
173: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
174:
175: Project p = ProjectManager.getDefault().findProject(
176: h.getProjectDirectory());
177: ProjectManager.getDefault().saveProject(p);
178:
179: CasaHelper.createDefaultCasaFileObject((JbiProject) p);
180:
181: return h;
182: }
183:
184: /**
185: * DOCUMENT ME!
186: *
187: * @param dir DOCUMENT ME!
188: * @param name DOCUMENT ME!
189: * @param wmFO DOCUMENT ME!
190: * @param javaRoot DOCUMENT ME!
191: * @param configFilesBase DOCUMENT ME!
192: * @param j2eeLevel DOCUMENT ME!
193: * @param buildfile DOCUMENT ME!
194: *
195: * @return DOCUMENT ME!
196: *
197: * @throws IOException DOCUMENT ME!
198: */
199: public static AntProjectHelper importProject(File dir, String name,
200: FileObject wmFO, FileObject javaRoot,
201: FileObject configFilesBase, String j2eeLevel,
202: String buildfile) throws IOException {
203: dir.mkdirs();
204:
205: // XXX clumsy way to refresh, but otherwise it doesn't work for new folders
206: File rootF = dir;
207:
208: while (rootF.getParentFile() != null) {
209: rootF = rootF.getParentFile();
210: }
211:
212: // XXX add code to set meta inf directory (meta-inf and java src)
213: FileObject fo = FileUtil.toFileObject(rootF);
214: assert fo != null : "At least disk roots must be mounted! "
215: + rootF; // NOI18N
216: fo.getFileSystem().refresh(false);
217: fo = FileUtil.toFileObject(dir);
218:
219: // vlv # 113228
220: if (fo == null) {
221: throw new IOException("Can't create " + dir.getName()); // NOI18N
222: }
223: assert fo.isFolder() : "Not really a dir: " + dir; // NOI18N
224:
225: AntProjectHelper h = setupProject(fo, name, j2eeLevel);
226: EditableProperties ep = h
227: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
228:
229: if (FileUtil.isParentOf(fo, wmFO) || fo.equals(wmFO)) {
230: ep.put(JbiProjectProperties.SOURCE_ROOT, "."); // NOI18N
231: ep.setProperty(JbiProjectProperties.SRC_DIR, relativePath(
232: fo, javaRoot));
233: ep.setProperty(JbiProjectProperties.META_INF, relativePath(
234: fo, configFilesBase));
235: } else {
236: File wmRoot = FileUtil.toFile(wmFO);
237: ep.put(JbiProjectProperties.SOURCE_ROOT, wmRoot
238: .getAbsolutePath());
239:
240: String configFilesPath = relativePath(wmFO, configFilesBase);
241: configFilesPath = (configFilesPath.length() > 0) ? ("${"
242: + JbiProjectProperties.SOURCE_ROOT + "}/" + configFilesPath) // NOI18N
243: : ("${" + JbiProjectProperties.SOURCE_ROOT + "}"); // NOI18N
244:
245: String javaPath = relativePath(wmFO, javaRoot);
246: javaPath = (javaPath.length() > 0) ? ("${"
247: + JbiProjectProperties.SOURCE_ROOT + "}/" + javaPath) // NOI18N
248: : ("${" + JbiProjectProperties.SOURCE_ROOT + "}"); // NOI18N
249: ep.setProperty(JbiProjectProperties.SRC_DIR, javaPath);
250: ep.setProperty(JbiProjectProperties.META_INF,
251: configFilesPath);
252: }
253:
254: if (!GeneratedFilesHelper.BUILD_XML_PATH.equals(buildfile)) {
255: ep.setProperty(JbiProjectProperties.BUILD_FILE, buildfile);
256: }
257:
258: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
259:
260: Project p = ProjectManager.getDefault().findProject(
261: h.getProjectDirectory());
262: ProjectManager.getDefault().saveProject(p);
263:
264: return h;
265: }
266:
267: private static String relativePath(FileObject parent,
268: FileObject child) {
269: if (child.equals(parent)) {
270: return ""; // NOI18N
271: }
272:
273: if (!FileUtil.isParentOf(parent, child)) {
274: throw new IllegalArgumentException(NbBundle.getMessage(
275: JbiProjectGenerator.class,
276: "MSG_Cannot_find_relative_path", parent, child) // NOI18N
277: );
278: }
279:
280: return child.getPath().substring(parent.getPath().length() + 1);
281: }
282:
283: private static AntProjectHelper setupProject(FileObject dirFO,
284: String name, String j2eeLevel) throws IOException {
285: AntProjectHelper h = ProjectGenerator.createProject(dirFO,
286: JbiProjectType.TYPE);
287: Element data = h.getPrimaryConfigurationData(true);
288: Document doc = data.getOwnerDocument();
289: Element nameEl = doc.createElementNS(
290: JbiProjectType.PROJECT_CONFIGURATION_NAMESPACE, "name" // NOI18N
291: );
292: nameEl.appendChild(doc.createTextNode(name));
293: data.appendChild(nameEl);
294:
295: Element minant = doc.createElementNS(
296: JbiProjectType.PROJECT_CONFIGURATION_NAMESPACE,
297: "minimum-ant-version" // NOI18N
298: );
299: minant.appendChild(doc.createTextNode("1.6")); // NOI18N
300: data.appendChild(minant);
301: h.putPrimaryConfigurationData(data, true);
302:
303: EditableProperties ep = h
304: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
305:
306: // ep.setProperty(JbiProjectProperties.JAVAC_CLASSPATH, "${libs.j2ee14.classpath}");
307: ep.setProperty(JbiProjectProperties.DIST_DIR, "dist"); // NOI18N
308: ep.setProperty(JbiProjectProperties.DIST_JAR, "${"
309: + JbiProjectProperties.DIST_DIR + "}/" + name + ".zip" // NOI18N
310: );
311: ep.setProperty(JbiProjectProperties.J2EE_PLATFORM, j2eeLevel);
312: ep.setProperty(JbiProjectProperties.JAR_COMPRESS, "false"); // NOI18N
313:
314: Deployment deployment = Deployment.getDefault();
315:
316: ep.setProperty(JbiProjectProperties.JAVAC_SOURCE, "1.4"); // NOI18N
317: ep.setProperty(JbiProjectProperties.JAVAC_DEBUG, "true"); // NOI18N
318: ep.setProperty(JbiProjectProperties.JAVAC_DEPRECATION, "false"); // NOI18N
319:
320: ep.setProperty(JbiProjectProperties.JAVAC_TARGET, "1.4"); // NOI18N
321:
322: ep.setProperty(JbiProjectProperties.BUILD_DIR,
323: DEFAULT_BUILD_DIR);
324: ep.setProperty(JbiProjectProperties.BUILD_GENERATED_DIR, "${"
325: + JbiProjectProperties.BUILD_DIR + "}/generated" // NOI18N
326: );
327: ep.setProperty(JbiProjectProperties.BUILD_CLASSES_DIR, "${"
328: + JbiProjectProperties.BUILD_DIR + "}/jar" // NOI18N
329: );
330: ep.setProperty(JbiProjectProperties.BUILD_CLASSES_EXCLUDES,
331: "**/*.java,**/*.form,**/.nbattrs" // NOI18N
332: );
333: ep.setProperty(JbiProjectProperties.DIST_JAVADOC_DIR, "${"
334: + JbiProjectProperties.DIST_DIR + "}/javadoc" // NOI18N
335: );
336: ep.setProperty(JbiProjectProperties.JAVA_PLATFORM,
337: "default_platform"); // NOI18N
338: ep.setProperty(JbiProjectProperties.DEBUG_CLASSPATH, "${"
339: + JbiProjectProperties.JAVAC_CLASSPATH + "}:${" + // NOI18N
340: JbiProjectProperties.BUILD_CLASSES_DIR + "}" // NOI18N
341: );
342: ep.setProperty(JbiProjectProperties.JAVADOC_PRIVATE, "false"); // NOI18N
343: ep.setProperty(JbiProjectProperties.JAVADOC_NO_TREE, "false"); // NOI18N
344: ep.setProperty(JbiProjectProperties.JAVADOC_USE, "true"); // NOI18N
345: ep.setProperty(JbiProjectProperties.JAVADOC_NO_NAVBAR, "false"); // NOI18N
346: ep.setProperty(JbiProjectProperties.JAVADOC_NO_INDEX, "false"); // NOI18N
347: ep
348: .setProperty(JbiProjectProperties.JAVADOC_SPLIT_INDEX,
349: "true"); // NOI18N
350: ep.setProperty(JbiProjectProperties.JAVADOC_AUTHOR, "false"); // NOI18N
351: ep.setProperty(JbiProjectProperties.JAVADOC_VERSION, "false"); // NOI18N
352: ep.setProperty(JbiProjectProperties.JAVADOC_WINDOW_TITLE, ""); // NOI18N
353: ep.setProperty(JbiProjectProperties.JAVADOC_ENCODING, ""); // NOI18N
354: ep.setProperty(JbiProjectProperties.JAVADOC_PREVIEW, "true"); // NOI18N
355:
356: //============= Start of JBI ========================================//
357: ep.setProperty(JbiProjectProperties.SERVICE_ASSEMBLY_ID, name);
358: ep.setProperty(
359: JbiProjectProperties.SERVICE_ASSEMBLY_DESCRIPTION,
360: NbBundle.getMessage(JbiProjectGenerator.class,
361: "SERVICE_ASSEMBLY_DESCRIPTION", name)); // NOI18N
362: ep.setProperty(JbiProjectProperties.SERVICE_UNIT_DESCRIPTION,
363: NbBundle.getMessage(JbiProjectGenerator.class,
364: "DEFAULT_SERVICE_UNIT_DESCRIPTION")); // NOI18N
365: //============= End of JBI ========================================//
366: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
367:
368: ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
369:
370: String[] serverInstanceIDs = JbiManager.getAppServers();
371: if (serverInstanceIDs != null && serverInstanceIDs.length == 1) {
372: ep.setProperty(JbiProjectProperties.J2EE_SERVER_INSTANCE,
373: serverInstanceIDs[0]);
374: }
375:
376: h.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep);
377:
378: Project p = ProjectManager.getDefault().findProject(dirFO);
379: ProjectManager.getDefault().saveProject(p);
380:
381: return h;
382: }
383: }
|