KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > lib > Util


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.test.j2ee.lib;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.api.project.ProjectUtils;
32 import org.netbeans.modules.j2ee.earproject.EarProjectGenerator;
33 //import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProjectGenerator;
34
import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator;
35 import org.netbeans.modules.javacore.JMManager;
36 import org.netbeans.modules.project.ui.OpenProjectList;
37 import org.netbeans.modules.web.api.webmodule.WebModule;
38 //import org.netbeans.modules.web.project.WebProjectGenerator;
39
import org.openide.ErrorManager;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.util.Mutex;
43 import org.netbeans.modules.web.project.api.WebProjectUtilities;
44 import org.netbeans.modules.j2ee.ejbjarproject.api.EjbJarProjectGenerator;
45
46
47 /**
48  *
49  * @author jungi
50  */

51 public class Util {
52     
53     public static final int J2SE_PROJECT = 0;
54     public static final int WEB_PROJECT = 1;
55     public static final int EJB_PROJECT = 2;
56     public static final int J2EE_PROJECT = 3;
57     
58     public static final String JavaDoc DEFAULT_J2EE_LEVEL
59             = WebModule.J2EE_14_LEVEL;
60     
61     public static final String JavaDoc DEFAULT_APPSRV_ID
62             = "deployer:Sun:AppServer::localhost:4848";
63     
64 // public static final String DEFAULT_SRC_STRUCTURE
65
// = WebProjectGenerator.SRC_STRUCT_BLUEPRINTS;
66

67     public static final String JavaDoc DEFAULT_SRC_STRUCTURE
68             = WebProjectUtilities.SRC_STRUCT_BLUEPRINTS;
69     
70     /** Creates a new instance of J2eeProjectSupport */
71     private Util() {
72         throw new UnsupportedOperationException JavaDoc("It is just a helper class.");
73     }
74     
75     /** Opens project in specified directory.
76      * @param projectDir a directory with project to open
77      * @return Project instance of opened project
78      */

79     public static Object JavaDoc openProject(File JavaDoc projectDir) {
80         final ProjectOpenListener listener = new ProjectOpenListener();
81         try {
82             // open project
83
final Project project = OpenProjectList.fileToProject(projectDir);
84             // posting the to AWT event thread
85
Mutex.EVENT.writeAccess(new Runnable JavaDoc() {
86                 public void run() {
87                     OpenProjectList.getDefault().addPropertyChangeListener(listener);
88                     OpenProjectList.getDefault().open(project);
89                     // Set main? Probably user should do this if he wants.
90
// OpenProjectList.getDefault().setMainProject(project);
91
}
92             });
93             // WAIT PROJECT OPEN - start
94
// We need to wait until project is open and then we can start to
95
// wait when scanning finishes. If we don't wait, scanning is started
96
// too early and finishes immediatelly.
97
Thread JavaDoc waitThread = new Thread JavaDoc(new Runnable JavaDoc() {
98                 public void run() {
99                     while(!listener.projectOpened) {
100                         try {
101                             Thread.sleep(50);
102                         } catch (Exception JavaDoc e) {
103                             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
104                         }
105                     }
106                 }
107             });
108             waitThread.start();
109             try {
110                 waitThread.join(60000L); // wait 1 minute at the most
111
} catch (InterruptedException JavaDoc iex) {
112                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, iex);
113             }
114             if (waitThread.isAlive()) {
115                 // time-out expired, project not opened -> interrupt the wait thread
116
ErrorManager.getDefault().log(ErrorManager.USER, "Project not opened in 60 second.");
117                 waitThread.interrupt();
118             }
119             // WAIT PROJECT OPEN - end
120
// wait until metadata scanning is finished
121
waitScanFinished();
122             return project;
123         } catch (Exception JavaDoc ex) {
124             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex);
125             return null;
126         } finally {
127             OpenProjectList.getDefault().removePropertyChangeListener(listener);
128         }
129     }
130     
131     /** Opens project on specified path.
132      * @param projectPath path to a directory with project to open
133      * @return Project instance of opened project
134      */

135     public static Object JavaDoc openProject(String JavaDoc projectPath) {
136         return openProject(new File JavaDoc(projectPath));
137     }
138     
139     /** Creates an empty Java project in specified directory and opens it.
140      * Its name is defined by name parameter.
141      * @param projectParentPath path to directory where to create name subdirectory and
142      * new project structure in that subdirectory.
143      * @param name name of the project
144      * @return Project instance of created project
145      */

146     public static Object JavaDoc createProject(String JavaDoc projectParentPath, String JavaDoc name) {
147         return createProject(new File JavaDoc(projectParentPath), name, J2SE_PROJECT, null);
148     }
149     
150     /** Creates an empty project in specified directory and opens it.
151      * Its name is defined by name parameter.
152      * @param projectParentDir directory where to create name subdirectory and
153      * new project structure in that subdirectory.
154      * @param name name of the project
155      * @param type type of project
156      * @param params parameters passed to created project
157      */

158     public static Object JavaDoc createProject(File JavaDoc projectParentDir, String JavaDoc name,
159             int type, String JavaDoc[] params) {
160         String JavaDoc mainClass = null;
161         try {
162             File JavaDoc projectDir = new File JavaDoc(projectParentDir, name);
163             switch (type) {
164                 case J2SE_PROJECT:
165                     J2SEProjectGenerator.createProject(projectDir, name, mainClass, null);
166                     break;
167                 case WEB_PROJECT:
168                     //params[0] = serverInstanceID
169
//params[1] = sourceStructure
170
//params[2] = j2eeLevel
171
if (params == null){
172                         params = new String JavaDoc[] {DEFAULT_APPSRV_ID, DEFAULT_SRC_STRUCTURE, DEFAULT_J2EE_LEVEL};
173                     }
174                     WebProjectUtilities.createProject(projectDir, name, params[0], params[1], params[2], name);
175                     break;
176                 case EJB_PROJECT:
177                     //params[0] = j2eeLevel
178
//params[1] = serverInstanceID
179
if (params == null){
180                         params = new String JavaDoc[] {DEFAULT_J2EE_LEVEL, DEFAULT_APPSRV_ID};
181                     }
182                     EjbJarProjectGenerator.createProject(projectDir, name, params[0], params[1]);
183                     break;
184                 case J2EE_PROJECT:
185                     //params[0] = j2eeLevel
186
//params[1] = serverInstanceID
187
//params[2] = sourceLevel
188
if (params == null){
189                         params = new String JavaDoc[] {DEFAULT_J2EE_LEVEL, DEFAULT_APPSRV_ID, null};
190                     }
191                     EarProjectGenerator.createProject(projectDir, name, params[0], params[1], name, params[2]);
192                     break;
193                 default:
194                     throw new IllegalArgumentException JavaDoc("Invalid project type.");
195             }
196             return openProject(projectDir);
197         } catch (IOException JavaDoc e) {
198             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
199             return null;
200         }
201     }
202     
203     /** Closes project with system or display name equals to given name.
204      * @param name system or display name of project to be closed.
205      * @return true if project is closed, false otherwise (i.e. project was
206      * not found).
207      */

208     public static boolean closeProject(String JavaDoc name) {
209         Project[] projects = OpenProjectList.getDefault().getOpenProjects();
210         for(int i=0;i<projects.length;i++) {
211             final Project project = projects[i];
212             if(ProjectUtils.getInformation(project).getDisplayName().equals(name) ||
213                     ProjectUtils.getInformation(project).getName().equals(name)) {
214                 // posting the to AWT event thread
215
Mutex.EVENT.writeAccess(new Runnable JavaDoc() {
216                     public void run() {
217                         OpenProjectList.getDefault().close( new Project[] { project } );
218                     }
219                 });
220                 return true;
221             }
222         }
223         // project not found
224
return false;
225     }
226     
227     /** Waits until metadata scanning is finished. */
228     public static boolean waitScanFinished() {
229         return ((JMManager)JMManager.getManager()).waitScanFinished();
230     }
231     
232     /**
233      *
234      * @return set of file names under the project root
235      */

236     public static Set JavaDoc getFileSet(Project p) {
237         File JavaDoc f = FileUtil.toFile(p.getProjectDirectory());
238         Set JavaDoc dummy = new HashSet JavaDoc();
239         visitAllDirsAndFiles(f, dummy);
240         Set JavaDoc retVal = new HashSet JavaDoc(dummy.size());
241         Iterator JavaDoc i = dummy.iterator();
242         while (i.hasNext()) {
243             String JavaDoc s = ((String JavaDoc) i.next()).substring(f.getAbsolutePath().length() + 1);
244             if (s.length() > 2) {
245                 retVal.add(s);
246             }
247         }
248         return retVal;
249     }
250     
251     public static Set JavaDoc getFileSet(String JavaDoc projectRoot) {
252         File JavaDoc f = new File JavaDoc(projectRoot);
253         Set JavaDoc dummy = new HashSet JavaDoc();
254         visitAllDirsAndFiles(f, dummy);
255         Set JavaDoc retVal = new HashSet JavaDoc(dummy.size());
256         Iterator JavaDoc i = dummy.iterator();
257         while (i.hasNext()) {
258             String JavaDoc s = ((String JavaDoc) i.next()).substring(f.getAbsolutePath().length() + 1);
259             if (s.length() > 2) {
260                 retVal.add(s);
261             }
262         }
263         return retVal;
264     }
265     
266     public static Project getProject(File JavaDoc wd, String JavaDoc relativePath) throws Exception JavaDoc {
267         File JavaDoc f = new File JavaDoc(wd, relativePath);
268         f = f.getCanonicalFile();
269         FileObject fo = FileUtil.toFileObject(f);
270         return ProjectManager.getDefault().findProject(fo);
271     }
272     
273     // Process all files and directories under dir and put their names to given set
274
private static void visitAllDirsAndFiles(File JavaDoc dir, Set JavaDoc s) {
275         s.add(dir.isDirectory() ? dir.getPath() + File.separatorChar : dir.getPath());
276         if (dir.isDirectory()) {
277             String JavaDoc[] children = dir.list();
278             for (int i=0; i<children.length; i++) {
279                 visitAllDirsAndFiles(new File JavaDoc(dir, children[i]), s);
280             }
281         }
282     }
283     
284     /** Listener for project open. */
285     static class ProjectOpenListener implements PropertyChangeListener JavaDoc {
286         public boolean projectOpened = false;
287         
288         /** Listen for property which changes when project is hopefully opened. */
289         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
290             if(OpenProjectList.PROPERTY_OPEN_PROJECTS.equals(evt.getPropertyName())) {
291                 projectOpened = true;
292             }
293         }
294     }
295 }
296
297
Popular Tags