KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > suite > SuiteProjectGenerator


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

19
20 package org.netbeans.modules.apisupport.project.suite;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import org.netbeans.api.project.ProjectManager;
26 import org.netbeans.modules.apisupport.project.ProjectXMLManager;
27 import org.netbeans.modules.apisupport.project.universe.ModuleList;
28 import org.netbeans.spi.project.support.ant.AntProjectHelper;
29 import org.netbeans.spi.project.support.ant.EditableProperties;
30 import org.openide.filesystems.FileLock;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.util.Mutex;
34 import org.openide.util.MutexException;
35
36 /**
37  * Servers for generating new NetBeans Modules templates.
38  *
39  * @author Martin Krauskopf
40  */

41 public class SuiteProjectGenerator {
42     
43     private static final String JavaDoc PLATFORM_PROPERTIES_PATH =
44             "nbproject/platform.properties"; // NOI18N
45
public static final String JavaDoc PROJECT_PROPERTIES_PATH = "nbproject/project.properties"; // NOI18N
46
public static final String JavaDoc PRIVATE_PROPERTIES_PATH = "nbproject/private/private.properties"; // NOI18N
47

48     /** Use static factory methods instead. */
49     private SuiteProjectGenerator() {/* empty constructor*/}
50     
51     /** Generates standalone NetBeans Module. */
52     public static void createSuiteProject(final File JavaDoc projectDir, final String JavaDoc platformID) throws IOException JavaDoc {
53         try {
54             ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() {
55                 public Object JavaDoc run() throws IOException JavaDoc {
56                     final FileObject dirFO = FileUtil.createFolder(projectDir);
57                     if (ProjectManager.getDefault().findProject(dirFO) != null) {
58                         throw new IllegalArgumentException JavaDoc("Already a project in " + dirFO); // NOI18N
59
}
60                     createSuiteProjectXML(dirFO);
61                     createPlatformProperties(dirFO, platformID);
62                     createProjectProperties(dirFO);
63                     ModuleList.refresh();
64                     ProjectManager.getDefault().clearNonProjectCache();
65                     return null;
66                 }
67             });
68         } catch (MutexException e) {
69             throw (IOException JavaDoc) e.getException();
70         }
71     }
72     
73     /**
74      * Creates basic <em>nbbuild/project.xml</em> or whatever
75      * <code>AntProjectHelper.PROJECT_XML_PATH</code> is pointing to for
76      * <em>Suite</em>.
77      */

78     private static void createSuiteProjectXML(FileObject projectDir) throws IOException JavaDoc {
79         ProjectXMLManager.generateEmptySuiteTemplate(
80                 createFileObject(projectDir, AntProjectHelper.PROJECT_XML_PATH),
81                 projectDir.getNameExt());
82     }
83     
84     private static void createPlatformProperties(FileObject projectDir, String JavaDoc platformID) throws IOException JavaDoc {
85         FileObject plafPropsFO = createFileObject(
86                 projectDir, PLATFORM_PROPERTIES_PATH);
87         EditableProperties props = new EditableProperties(true);
88         props.setProperty("nbplatform.active", platformID); // NOI18N
89
storeProperties(plafPropsFO, props);
90     }
91     
92     private static void createProjectProperties(FileObject projectDir) throws IOException JavaDoc {
93         // #60026: ${modules} has to be defined right away.
94
FileObject propsFO = createFileObject(projectDir, PROJECT_PROPERTIES_PATH);
95         EditableProperties props = new EditableProperties(true);
96         props.setProperty("modules", ""); // NOI18N
97
storeProperties(propsFO, props);
98     }
99     
100     /** Just utility method. */
101     private static void storeProperties(FileObject bundleFO, EditableProperties props) throws IOException JavaDoc {
102         FileLock lock = bundleFO.lock();
103         try {
104             OutputStream JavaDoc os = bundleFO.getOutputStream(lock);
105             try {
106                 props.store(os);
107             } finally {
108                 os.close();
109             }
110         } finally {
111             lock.releaseLock();
112         }
113     }
114     
115     /**
116      * Creates a new <code>FileObject</code>.
117      * Throws <code>IllegalArgumentException</code> if such an object already
118      * exists. Throws <code>IOException</code> if creation fails.
119      */

120     private static FileObject createFileObject(FileObject dir, String JavaDoc relToDir) throws IOException JavaDoc {
121         FileObject createdFO = dir.getFileObject(relToDir);
122         if (createdFO != null) {
123             throw new IllegalArgumentException JavaDoc("File " + createdFO + " already exists."); // NOI18N
124
}
125         createdFO = FileUtil.createData(dir, relToDir);
126         return createdFO;
127     }
128 }
129
130
131
Popular Tags