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-2006 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:
042: package org.netbeans.modules.apisupport.project.suite;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.OutputStream;
047: import org.netbeans.api.project.ProjectManager;
048: import org.netbeans.modules.apisupport.project.ProjectXMLManager;
049: import org.netbeans.modules.apisupport.project.universe.ModuleList;
050: import org.netbeans.spi.project.support.ant.AntProjectHelper;
051: import org.netbeans.spi.project.support.ant.EditableProperties;
052: import org.openide.filesystems.FileLock;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileUtil;
055: import org.openide.util.Mutex;
056: import org.openide.util.MutexException;
057:
058: /**
059: * Servers for generating new NetBeans Modules templates.
060: *
061: * @author Martin Krauskopf
062: */
063: public class SuiteProjectGenerator {
064:
065: private static final String PLATFORM_PROPERTIES_PATH = "nbproject/platform.properties"; // NOI18N
066: public static final String PROJECT_PROPERTIES_PATH = "nbproject/project.properties"; // NOI18N
067: public static final String PRIVATE_PROPERTIES_PATH = "nbproject/private/private.properties"; // NOI18N
068:
069: /** Use static factory methods instead. */
070: private SuiteProjectGenerator() {/* empty constructor*/
071: }
072:
073: /** Generates standalone NetBeans Module. */
074: public static void createSuiteProject(final File projectDir,
075: final String platformID) throws IOException {
076: try {
077: ProjectManager.mutex().writeAccess(
078: new Mutex.ExceptionAction<Void>() {
079: public Void run() throws IOException {
080: final FileObject dirFO = FileUtil
081: .createFolder(projectDir);
082: if (ProjectManager.getDefault()
083: .findProject(dirFO) != null) {
084: throw new IllegalArgumentException(
085: "Already a project in " + dirFO); // NOI18N
086: }
087: createSuiteProjectXML(dirFO);
088: createPlatformProperties(dirFO, platformID);
089: createProjectProperties(dirFO);
090: ModuleList.refresh();
091: ProjectManager.getDefault()
092: .clearNonProjectCache();
093: return null;
094: }
095: });
096: } catch (MutexException e) {
097: throw (IOException) e.getException();
098: }
099: }
100:
101: /**
102: * Creates basic <em>nbbuild/project.xml</em> or whatever
103: * <code>AntProjectHelper.PROJECT_XML_PATH</code> is pointing to for
104: * <em>Suite</em>.
105: */
106: private static void createSuiteProjectXML(FileObject projectDir)
107: throws IOException {
108: ProjectXMLManager.generateEmptySuiteTemplate(createFileObject(
109: projectDir, AntProjectHelper.PROJECT_XML_PATH),
110: projectDir.getNameExt());
111: }
112:
113: private static void createPlatformProperties(FileObject projectDir,
114: String platformID) throws IOException {
115: FileObject plafPropsFO = createFileObject(projectDir,
116: PLATFORM_PROPERTIES_PATH);
117: EditableProperties props = new EditableProperties(true);
118: props.setProperty("nbplatform.active", platformID); // NOI18N
119: storeProperties(plafPropsFO, props);
120: }
121:
122: private static void createProjectProperties(FileObject projectDir)
123: throws IOException {
124: // #60026: ${modules} has to be defined right away.
125: FileObject propsFO = createFileObject(projectDir,
126: PROJECT_PROPERTIES_PATH);
127: EditableProperties props = new EditableProperties(true);
128: props.setProperty("modules", ""); // NOI18N
129: storeProperties(propsFO, props);
130: }
131:
132: /** Just utility method. */
133: private static void storeProperties(FileObject bundleFO,
134: EditableProperties props) throws IOException {
135: FileLock lock = bundleFO.lock();
136: try {
137: OutputStream os = bundleFO.getOutputStream(lock);
138: try {
139: props.store(os);
140: } finally {
141: os.close();
142: }
143: } finally {
144: lock.releaseLock();
145: }
146: }
147:
148: /**
149: * Creates a new <code>FileObject</code>.
150: * Throws <code>IllegalArgumentException</code> if such an object already
151: * exists. Throws <code>IOException</code> if creation fails.
152: */
153: private static FileObject createFileObject(FileObject dir,
154: String relToDir) throws IOException {
155: FileObject createdFO = dir.getFileObject(relToDir);
156: if (createdFO != null) {
157: throw new IllegalArgumentException("File " + createdFO
158: + " already exists."); // NOI18N
159: }
160: createdFO = FileUtil.createData(dir, relToDir);
161: return createdFO;
162: }
163: }
|