001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.bpel.project;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.nio.charset.Charset;
024:
025: import org.openide.filesystems.FileObject;
026: import org.openide.filesystems.FileUtil;
027: import org.openide.filesystems.Repository;
028:
029: import org.netbeans.api.project.Project;
030: import org.netbeans.api.project.ProjectManager;
031: import org.netbeans.api.queries.FileEncodingQuery;
032: import org.netbeans.spi.project.support.ant.AntProjectHelper;
033: import org.netbeans.spi.project.support.ant.EditableProperties;
034: import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
035: import org.netbeans.spi.project.support.ant.ProjectGenerator;
036:
037: import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
038: import org.netbeans.modules.compapp.projects.base.ui.customizer.IcanproProjectProperties;
039: import org.openide.ErrorManager;
040: import org.openide.filesystems.FileStateInvalidException;
041: import org.openide.filesystems.FileSystem;
042: import org.openide.util.Mutex;
043: import org.openide.util.MutexException;
044: import org.openide.util.NbBundle;
045:
046: import org.w3c.dom.Document;
047: import org.w3c.dom.Element;
048:
049: /**
050: * Create a fresh EjbProject from scratch or by importing and exisitng web module
051: * in one of the recognized directory structures.
052: *
053: * @author Pavel Buzek
054: */
055: public class BpelproProjectGenerator {
056:
057: private static final String DEFAULT_DOC_BASE_FOLDER = "conf"; //NOI18N
058: private static final String DEFAULT_SRC_FOLDER = "src"; //NOI18N
059: private static final String DEFAULT_RESOURCE_FOLDER = "setup"; //NOI18N
060: private static final String DEFAULT_BPELASA_FOLDER = "bpelasa"; //NOI18N
061: private static final String DEFAULT_BUILD_DIR = "build"; //NOI18N
062:
063: private BpelproProjectGenerator() {
064: }
065:
066: /**
067: * Create a new empty J2SE project.
068: * @param dir the top-level directory (need not yet exist but if it does it must be empty)
069: * @param name the code name for the project
070: * @return the helper object permitting it to be further customized
071: * @throws IOException in case something went wrong
072: */
073: public static AntProjectHelper createProject(File dir,
074: final String name) throws IOException {
075: final FileObject fo = createProjectDir(dir);
076: final AntProjectHelper[] h = new AntProjectHelper[1];
077:
078: fo.getFileSystem().runAtomicAction(
079: new FileSystem.AtomicAction() {
080: public void run() throws IOException {
081: h[0] = setupProject(fo, name);
082: FileObject srcRoot = fo
083: .createFolder(DEFAULT_SRC_FOLDER); // NOI18N
084: createCatalogXml(h[0].getProjectDirectory());
085:
086: try {
087: ProjectManager.mutex().writeAccess(
088: new Mutex.ExceptionAction<Void>() {
089:
090: public Void run()
091: throws Exception {
092: EditableProperties ep = h[0]
093: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
094: ep
095: .put(
096: IcanproProjectProperties.SOURCE_ROOT,
097: DEFAULT_SRC_FOLDER); //NOI18N
098: ep
099: .setProperty(
100: IcanproProjectProperties.META_INF,
101: "${"
102: + IcanproProjectProperties.SOURCE_ROOT
103: + "}/"
104: + DEFAULT_DOC_BASE_FOLDER); //NOI18N
105: ep
106: .setProperty(
107: IcanproProjectProperties.SRC_DIR,
108: "${"
109: + IcanproProjectProperties.SOURCE_ROOT
110: + "}"); //NOI18N
111: ep
112: .setProperty(
113: IcanproProjectProperties.RESOURCE_DIR,
114: DEFAULT_RESOURCE_FOLDER);
115: h[0]
116: .putProperties(
117: AntProjectHelper.PROJECT_PROPERTIES_PATH,
118: ep);
119:
120: Project p = ProjectManager
121: .getDefault()
122: .findProject(
123: h[0]
124: .getProjectDirectory());
125: ProjectManager.getDefault()
126: .saveProject(p);
127:
128: return null;
129: }
130: });
131: } catch (MutexException me) {
132: ErrorManager.getDefault().notify(me);
133: }
134: }
135: });
136:
137: return h[0];
138: }
139:
140: private static FileObject createProjectDir(File dir)
141: throws IOException {
142: FileObject dirFO;
143: if (!dir.exists()) {
144: //Refresh before mkdir not to depend on window focus
145: refreshFileSystem(dir);
146: dirFO = FileUtil.createFolder(dir);
147: } else {
148: dirFO = FileUtil.toFileObject(dir);
149: }
150:
151: if (dirFO == null) {
152: throw new IOException("Can't create " + dir.getName());
153: }
154: assert dirFO.isFolder() : "Not really a dir: " + dir; // NOI18N
155: assert dirFO.getChildren().length == 0 : "Dir must have been empty: "
156: + dir;
157: return dirFO;
158: }
159:
160: private static void refreshFileSystem(final File dir)
161: throws FileStateInvalidException {
162: File rootF = dir;
163: while (rootF.getParentFile() != null) {
164: rootF = rootF.getParentFile();
165: }
166: FileObject dirFO = FileUtil.toFileObject(rootF);
167: assert dirFO != null : "At least disk roots must be mounted! "
168: + rootF; // NOI18N
169: dirFO.getFileSystem().refresh(false);
170: }
171:
172: // vlv # 111020
173: private static void createCatalogXml(FileObject project) {
174: try {
175: FileObject resource = Repository.getDefault()
176: .getDefaultFileSystem().findResource(
177: "bpel-project-resources/catalog.xml");
178: FileUtil.copyFile(resource, project, "catalog", "xml"); // NOI18N
179: } catch (IOException e) {
180: }
181: }
182:
183: public static AntProjectHelper importProject(File dir, String name,
184: FileObject wmFO, FileObject javaRoot,
185: FileObject configFilesBase, String j2eeLevel,
186: String buildfile) throws IOException {
187: dir.mkdirs();
188: File rootF = dir;
189:
190: while (rootF.getParentFile() != null) {
191: rootF = rootF.getParentFile();
192: }
193: FileObject fo = FileUtil.toFileObject(rootF);
194: assert fo != null : "At least disk roots must be mounted! "
195: + rootF;
196: fo.getFileSystem().refresh(false);
197: fo = FileUtil.toFileObject(dir);
198:
199: // vlv # 113228
200: if (fo == null) {
201: throw new IOException("Can't create " + dir.getName());
202: }
203: assert fo.isFolder() : "Not really a dir: " + dir;
204: AntProjectHelper h = setupProject(fo, name);
205: EditableProperties ep = h
206: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
207:
208: if (FileUtil.isParentOf(fo, wmFO) || fo.equals(wmFO)) {
209: ep.put(IcanproProjectProperties.SOURCE_ROOT, "."); //NOI18N
210: ep.setProperty(IcanproProjectProperties.SRC_DIR,
211: relativePath(fo, javaRoot)); //NOI18N
212: ep.setProperty(IcanproProjectProperties.META_INF,
213: relativePath(fo, configFilesBase)); //NOI18N
214: } else {
215: File wmRoot = FileUtil.toFile(wmFO);
216: ep.put(IcanproProjectProperties.SOURCE_ROOT, wmRoot
217: .getAbsolutePath());
218: String configFilesPath = relativePath(wmFO, configFilesBase);
219: configFilesPath = configFilesPath.length() > 0 ? "${"
220: + IcanproProjectProperties.SOURCE_ROOT + "}/"
221: + configFilesPath : "${"
222: + IcanproProjectProperties.SOURCE_ROOT + "}"; //NOI18N
223: String javaPath = relativePath(wmFO, javaRoot);
224: javaPath = javaPath.length() > 0 ? "${"
225: + IcanproProjectProperties.SOURCE_ROOT + "}/"
226: + javaPath : "${"
227: + IcanproProjectProperties.SOURCE_ROOT + "}"; //NOI18N
228: ep.setProperty(IcanproProjectProperties.SRC_DIR, javaPath);
229: ep.setProperty(IcanproProjectProperties.META_INF,
230: configFilesPath);
231: }
232: if (!GeneratedFilesHelper.BUILD_XML_PATH.equals(buildfile)) {
233: ep.setProperty(IcanproProjectProperties.BUILD_FILE,
234: buildfile);
235: }
236: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
237: Project p = ProjectManager.getDefault().findProject(
238: h.getProjectDirectory());
239: ProjectManager.getDefault().saveProject(p);
240:
241: return h;
242: }
243:
244: private static String relativePath(FileObject parent,
245: FileObject child) {
246: if (child.equals(parent))
247: return "";
248: if (!FileUtil.isParentOf(parent, child))
249: throw new IllegalArgumentException(
250: "Cannot find relative path, " + parent
251: + " is not parent of " + child);
252: return child.getPath().substring(parent.getPath().length() + 1);
253: }
254:
255: private static AntProjectHelper setupProject(FileObject dirFO,
256: String name) throws IOException {
257: AntProjectHelper h = ProjectGenerator.createProject(dirFO,
258: BpelproProjectType.TYPE);
259: Element data = h.getPrimaryConfigurationData(true);
260: Document doc = data.getOwnerDocument();
261: Element nameEl = doc.createElementNS(
262: BpelproProjectType.PROJECT_CONFIGURATION_NAMESPACE,
263: "name"); // NOI18N
264: nameEl.appendChild(doc.createTextNode(name));
265: data.appendChild(nameEl);
266: Element minant = doc.createElementNS(
267: BpelproProjectType.PROJECT_CONFIGURATION_NAMESPACE,
268: "minimum-ant-version"); // NOI18N
269: minant.appendChild(doc.createTextNode("1.6")); // NOI18N
270: data.appendChild(minant);
271: h.putPrimaryConfigurationData(data, true);
272:
273: EditableProperties ep = h
274: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
275: ep.setProperty(IcanproProjectProperties.DIST_DIR, "dist");
276: ep.setProperty(IcanproProjectProperties.DIST_JAR, "${"
277: + IcanproProjectProperties.DIST_DIR + "}/" + name
278: + ".zip");
279: ep
280: .setProperty(IcanproProjectProperties.JAR_NAME, name
281: + ".jar");
282: ep.setProperty(IcanproProjectProperties.JAR_COMPRESS, "false");
283:
284: Deployment deployment = Deployment.getDefault();
285: ep.setProperty(IcanproProjectProperties.JAVAC_SOURCE, "1.4");
286: ep.setProperty(IcanproProjectProperties.JAVAC_DEBUG, "true");
287: ep.setProperty(IcanproProjectProperties.JAVAC_DEPRECATION,
288: "false");
289: ep.setProperty(ProjectConstants.VALIDATION_FLAG, "false");
290: ep.setProperty(IcanproProjectProperties.JAVAC_TARGET, "1.4");
291:
292: ep.setProperty(IcanproProjectProperties.BUILD_DIR,
293: DEFAULT_BUILD_DIR);
294: ep.setProperty(IcanproProjectProperties.BUILD_GENERATED_DIR,
295: "${" + IcanproProjectProperties.BUILD_DIR
296: + "}/generated");
297: ep.setProperty(IcanproProjectProperties.BUILD_CLASSES_DIR, "${"
298: + IcanproProjectProperties.BUILD_DIR + "}/jar");
299: ep.setProperty(IcanproProjectProperties.BUILD_CLASSES_EXCLUDES,
300: "**/*.java,**/*.form,**/.nbattrs");
301: ep.setProperty(IcanproProjectProperties.DIST_JAVADOC_DIR, "${"
302: + IcanproProjectProperties.DIST_DIR + "}/javadoc");
303: ep.setProperty(IcanproProjectProperties.JAVA_PLATFORM,
304: "default_platform");
305: ep.setProperty(IcanproProjectProperties.DEBUG_CLASSPATH, "${"
306: + IcanproProjectProperties.JAVAC_CLASSPATH + "}:${"
307: + IcanproProjectProperties.BUILD_CLASSES_DIR + "}");
308: ep.setProperty(IcanproProjectProperties.WSDL_CLASSPATH, "");
309: Charset enc = FileEncodingQuery.getDefaultEncoding();
310: ep.setProperty(IcanproProjectProperties.SOURCE_ENCODING, enc
311: .name());
312:
313: //============= Start of IcanPro========================================//
314: ep.setProperty(IcanproProjectProperties.JBI_SE_TYPE,
315: "sun-bpel-engine"); // NOI18N
316: ep.setProperty(
317: IcanproProjectProperties.SERVICE_UNIT_DESCRIPTION,
318: NbBundle.getMessage(BpelproProjectGenerator.class,
319: "TXT_Service_Unit_Description")); // NOI18N
320:
321: // vlv # 109451 todo r
322: ep.setProperty("jbi.se.type", "sun-bpel-engine"); // NOI18N
323: ep.setProperty("jbi.service-unit.description", NbBundle
324: .getMessage(BpelproProjectGenerator.class,
325: "TXT_Service_Unit_Description")); // NOI18N
326:
327: ep.setProperty(IcanproProjectProperties.BC_DEPLOYMENT_JAR, "${"
328: + IcanproProjectProperties.BUILD_DIR + "}/"
329: + "BCDeployment.jar");
330: ep.setProperty(IcanproProjectProperties.SE_DEPLOYMENT_JAR, "${"
331: + IcanproProjectProperties.BUILD_DIR + "}/"
332: + "SEDeployment.jar");
333: //============= End of IcanPro========================================//
334:
335: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
336:
337: ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
338: //============= Start of IcanPro========================================//
339: ep.setProperty(
340: IcanproProjectProperties.JBI_COMPONENT_CONF_FILE,
341: "ComponentInformation.xml"); // NOI18N
342: ep.setProperty(
343: IcanproProjectProperties.JBI_DEPLOYMENT_CONF_FILE,
344: "default.xml"); // NOI18N
345: //============= End of IcanPro========================================//
346:
347: h.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep);
348: Project p = ProjectManager.getDefault().findProject(dirFO);
349: ProjectManager.getDefault().saveProject(p);
350: return h;
351: }
352: }
|