1 19 20 package org.netbeans.test.j2ee.lib; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.Set ; 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.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.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 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 DEFAULT_J2EE_LEVEL 59 = WebModule.J2EE_14_LEVEL; 60 61 public static final String DEFAULT_APPSRV_ID 62 = "deployer:Sun:AppServer::localhost:4848"; 63 64 67 public static final String DEFAULT_SRC_STRUCTURE 68 = WebProjectUtilities.SRC_STRUCT_BLUEPRINTS; 69 70 71 private Util() { 72 throw new UnsupportedOperationException ("It is just a helper class."); 73 } 74 75 79 public static Object openProject(File projectDir) { 80 final ProjectOpenListener listener = new ProjectOpenListener(); 81 try { 82 final Project project = OpenProjectList.fileToProject(projectDir); 84 Mutex.EVENT.writeAccess(new Runnable () { 86 public void run() { 87 OpenProjectList.getDefault().addPropertyChangeListener(listener); 88 OpenProjectList.getDefault().open(project); 89 } 92 }); 93 Thread waitThread = new Thread (new Runnable () { 98 public void run() { 99 while(!listener.projectOpened) { 100 try { 101 Thread.sleep(50); 102 } catch (Exception e) { 103 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 104 } 105 } 106 } 107 }); 108 waitThread.start(); 109 try { 110 waitThread.join(60000L); } catch (InterruptedException iex) { 112 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, iex); 113 } 114 if (waitThread.isAlive()) { 115 ErrorManager.getDefault().log(ErrorManager.USER, "Project not opened in 60 second."); 117 waitThread.interrupt(); 118 } 119 waitScanFinished(); 122 return project; 123 } catch (Exception ex) { 124 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 125 return null; 126 } finally { 127 OpenProjectList.getDefault().removePropertyChangeListener(listener); 128 } 129 } 130 131 135 public static Object openProject(String projectPath) { 136 return openProject(new File (projectPath)); 137 } 138 139 146 public static Object createProject(String projectParentPath, String name) { 147 return createProject(new File (projectParentPath), name, J2SE_PROJECT, null); 148 } 149 150 158 public static Object createProject(File projectParentDir, String name, 159 int type, String [] params) { 160 String mainClass = null; 161 try { 162 File projectDir = new File (projectParentDir, name); 163 switch (type) { 164 case J2SE_PROJECT: 165 J2SEProjectGenerator.createProject(projectDir, name, mainClass, null); 166 break; 167 case WEB_PROJECT: 168 if (params == null){ 172 params = new String [] {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 if (params == null){ 180 params = new String [] {DEFAULT_J2EE_LEVEL, DEFAULT_APPSRV_ID}; 181 } 182 EjbJarProjectGenerator.createProject(projectDir, name, params[0], params[1]); 183 break; 184 case J2EE_PROJECT: 185 if (params == null){ 189 params = new String [] {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 ("Invalid project type."); 195 } 196 return openProject(projectDir); 197 } catch (IOException e) { 198 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 199 return null; 200 } 201 } 202 203 208 public static boolean closeProject(String 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 Mutex.EVENT.writeAccess(new Runnable () { 216 public void run() { 217 OpenProjectList.getDefault().close( new Project[] { project } ); 218 } 219 }); 220 return true; 221 } 222 } 223 return false; 225 } 226 227 228 public static boolean waitScanFinished() { 229 return ((JMManager)JMManager.getManager()).waitScanFinished(); 230 } 231 232 236 public static Set getFileSet(Project p) { 237 File f = FileUtil.toFile(p.getProjectDirectory()); 238 Set dummy = new HashSet (); 239 visitAllDirsAndFiles(f, dummy); 240 Set retVal = new HashSet (dummy.size()); 241 Iterator i = dummy.iterator(); 242 while (i.hasNext()) { 243 String s = ((String ) 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 getFileSet(String projectRoot) { 252 File f = new File (projectRoot); 253 Set dummy = new HashSet (); 254 visitAllDirsAndFiles(f, dummy); 255 Set retVal = new HashSet (dummy.size()); 256 Iterator i = dummy.iterator(); 257 while (i.hasNext()) { 258 String s = ((String ) 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 wd, String relativePath) throws Exception { 267 File f = new File (wd, relativePath); 268 f = f.getCanonicalFile(); 269 FileObject fo = FileUtil.toFileObject(f); 270 return ProjectManager.getDefault().findProject(fo); 271 } 272 273 private static void visitAllDirsAndFiles(File dir, Set s) { 275 s.add(dir.isDirectory() ? dir.getPath() + File.separatorChar : dir.getPath()); 276 if (dir.isDirectory()) { 277 String [] children = dir.list(); 278 for (int i=0; i<children.length; i++) { 279 visitAllDirsAndFiles(new File (dir, children[i]), s); 280 } 281 } 282 } 283 284 285 static class ProjectOpenListener implements PropertyChangeListener { 286 public boolean projectOpened = false; 287 288 289 public void propertyChange(PropertyChangeEvent evt) { 290 if(OpenProjectList.PROPERTY_OPEN_PROJECTS.equals(evt.getPropertyName())) { 291 projectOpened = true; 292 } 293 } 294 } 295 } 296 297 | Popular Tags |