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.junit.ide.ProjectSupport; 33 import org.netbeans.modules.j2ee.deployment.impl.ServerRegistry; 34 import org.netbeans.modules.j2ee.earproject.EarProjectGenerator; 35 import org.netbeans.modules.j2ee.ejbjarproject.api.EjbJarProjectGenerator; 36 import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator; 37 import org.netbeans.modules.project.ui.OpenProjectList; 38 import org.netbeans.modules.web.api.webmodule.WebModule; 39 import org.netbeans.modules.web.project.api.WebProjectUtilities; 40 import org.openide.ErrorManager; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.util.Mutex; 44 45 49 public class J2eeProjectSupport { 50 51 public static final int J2SE_PROJECT = 0; 52 public static final int WEB_PROJECT = 1; 53 public static final int EJB_PROJECT = 2; 54 public static final int J2EE_PROJECT = 3; 55 56 public static final String DEFAULT_J2EE_LEVEL 57 = WebModule.J2EE_14_LEVEL; 58 59 public static final String DEFAULT_APPSRV_ID; 60 static { 61 String location = System.getProperty("com.sun.aas.installRoot"); 62 if (location != null) { 63 location = new File (location).getAbsolutePath(); 64 } 65 DEFAULT_APPSRV_ID = "[" + location + "]deployer:Sun:AppServer::localhost:4848"; 66 } 67 68 public static final String DEFAULT_SRC_STRUCTURE 69 = WebProjectUtilities.SRC_STRUCT_BLUEPRINTS; 70 71 public static final int WAIT_APPSRV_INSTALL = 30000; 72 73 74 private J2eeProjectSupport() { 75 throw new UnsupportedOperationException ("It is just a helper class."); 76 } 77 78 public static boolean waitForAppServerInstall() { 79 long t1 = System.currentTimeMillis(); 80 while (ServerRegistry.getInstance().getServerInstance(DEFAULT_APPSRV_ID) == null && System.currentTimeMillis() - t1 < WAIT_APPSRV_INSTALL) { 81 try { 82 Thread.sleep(500); 83 } catch (InterruptedException e) {} 84 } 85 return ServerRegistry.getInstance().getServerInstance(DEFAULT_APPSRV_ID) != null; 86 } 87 88 92 public static Object openProject(File projectDir) { 93 final ProjectOpenListener listener = new ProjectOpenListener(); 94 try { 95 final Project project = OpenProjectList.fileToProject(projectDir); 97 Mutex.EVENT.writeAccess(new Runnable () { 99 public void run() { 100 OpenProjectList.getDefault().addPropertyChangeListener(listener); 101 OpenProjectList.getDefault().open(project); 102 } 105 }); 106 Thread waitThread = new Thread (new Runnable () { 111 public void run() { 112 while(!listener.projectOpened) { 113 try { 114 Thread.sleep(50); 115 } catch (Exception e) { 116 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 117 } 118 } 119 } 120 }); 121 waitThread.start(); 122 try { 123 waitThread.join(60000L); } catch (InterruptedException iex) { 125 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, iex); 126 } 127 if (waitThread.isAlive()) { 128 ErrorManager.getDefault().log(ErrorManager.USER, "Project not opened in 60 second."); 130 waitThread.interrupt(); 131 } 132 waitScanFinished(); 135 return project; 136 } catch (Exception ex) { 137 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex); 138 return null; 139 } finally { 140 OpenProjectList.getDefault().removePropertyChangeListener(listener); 141 } 142 } 143 144 148 public static Object openProject(String projectPath) { 149 return openProject(new File (projectPath)); 150 } 151 152 159 public static Object createProject(String projectParentPath, String name) { 160 return createProject(new File (projectParentPath), name, J2SE_PROJECT, null); 161 } 162 163 171 public static Object createProject(File projectParentDir, String name, 172 int type, String [] params) { 173 String mainClass = null; 174 try { 175 File projectDir = new File (projectParentDir, name); 176 switch (type) { 177 case J2SE_PROJECT: 178 J2SEProjectGenerator.createProject(projectDir, name, mainClass, null); 179 break; 180 case WEB_PROJECT: 181 if (params == null){ 185 params = new String [] {DEFAULT_APPSRV_ID, DEFAULT_SRC_STRUCTURE, DEFAULT_J2EE_LEVEL}; 186 } 187 WebProjectUtilities.createProject(projectDir, name, params[0], params[1], params[2], name); 188 break; 189 case EJB_PROJECT: 190 if (params == null){ 193 params = new String [] {DEFAULT_J2EE_LEVEL, DEFAULT_APPSRV_ID}; 194 } 195 EjbJarProjectGenerator.createProject(projectDir, name, params[0], params[1]); 196 break; 197 case J2EE_PROJECT: 198 if (params == null){ 202 params = new String [] {DEFAULT_J2EE_LEVEL, DEFAULT_APPSRV_ID, null}; 203 } 204 EarProjectGenerator.createProject(projectDir, name, params[0], params[1], params[2]); 205 break; 206 default: 207 throw new IllegalArgumentException ("Invalid project type."); 208 } 209 return openProject(projectDir); 210 } catch (IOException e) { 211 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 212 return null; 213 } 214 } 215 216 221 public static boolean closeProject(String name) { 222 Project[] projects = OpenProjectList.getDefault().getOpenProjects(); 223 for(int i=0;i<projects.length;i++) { 224 final Project project = projects[i]; 225 if(ProjectUtils.getInformation(project).getDisplayName().equals(name) || 226 ProjectUtils.getInformation(project).getName().equals(name)) { 227 Mutex.EVENT.writeAccess(new Runnable () { 229 public void run() { 230 OpenProjectList.getDefault().close( new Project[] { project }, true); 231 } 232 }); 233 return true; 234 } 235 } 236 return false; 238 } 239 240 241 public static void waitScanFinished() { 242 ProjectSupport.waitScanFinished(); 243 } 244 245 249 public static Set getFileSet(Project p) { 250 File f = FileUtil.toFile(p.getProjectDirectory()); 251 Set dummy = new HashSet (); 252 visitAllDirsAndFiles(f, dummy); 253 Set retVal = new HashSet (dummy.size()); 254 Iterator i = dummy.iterator(); 255 while (i.hasNext()) { 256 String s = ((String ) i.next()).substring(f.getAbsolutePath().length() + 1); 257 if (s.length() > 2) { 258 retVal.add(s); 259 } 260 } 261 return retVal; 262 } 263 264 public static Set getFileSet(String projectRoot) { 265 File f = new File (projectRoot); 266 Set dummy = new HashSet (); 267 visitAllDirsAndFiles(f, dummy); 268 Set retVal = new HashSet (dummy.size()); 269 Iterator i = dummy.iterator(); 270 while (i.hasNext()) { 271 String s = ((String ) i.next()).substring(f.getAbsolutePath().length() + 1); 272 if (s.length() > 2) { 273 retVal.add(s); 274 } 275 } 276 return retVal; 277 } 278 279 public static Project getProject(File wd, String relativePath) throws Exception { 280 File f = new File (wd, relativePath); 281 f = f.getCanonicalFile(); 282 FileObject fo = FileUtil.toFileObject(f); 283 return ProjectManager.getDefault().findProject(fo); 284 } 285 286 private static void visitAllDirsAndFiles(File dir, Set s) { 288 s.add(dir.isDirectory() ? dir.getPath() + File.separatorChar : dir.getPath()); 289 if (dir.isDirectory()) { 290 String [] children = dir.list(); 291 for (int i=0; i<children.length; i++) { 292 visitAllDirsAndFiles(new File (dir, children[i]), s); 293 } 294 } 295 } 296 297 298 static class ProjectOpenListener implements PropertyChangeListener { 299 public boolean projectOpened = false; 300 301 302 public void propertyChange(PropertyChangeEvent evt) { 303 if(OpenProjectList.PROPERTY_OPEN_PROJECTS.equals(evt.getPropertyName())) { 304 projectOpened = true; 305 } 306 } 307 } 308 } 309 | Popular Tags |