1 19 20 package org.netbeans.modules.j2ee.archive.wizard; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.text.MessageFormat ; 33 import java.util.Collections ; 34 import java.util.Enumeration ; 35 import java.util.NoSuchElementException ; 36 import java.util.Set ; 37 import java.util.jar.Attributes ; 38 import java.util.jar.JarFile ; 39 import java.util.jar.Manifest ; 40 import java.util.zip.ZipEntry ; 41 import java.util.zip.ZipInputStream ; 42 import javax.swing.JComponent ; 43 import javax.swing.event.ChangeListener ; 44 import org.netbeans.api.java.project.JavaProjectConstants; 45 import org.netbeans.api.progress.ProgressHandle; 46 import org.netbeans.api.progress.ProgressHandleFactory; 47 import org.netbeans.api.project.Project; 48 import org.netbeans.api.project.ProjectManager; 49 import org.netbeans.api.project.ui.OpenProjects; 50 import org.netbeans.modules.j2ee.archive.Util; 51 import org.netbeans.modules.j2ee.archive.project.ArchiveProject; 52 import org.netbeans.modules.j2ee.archive.project.ArchiveProjectProperties; 53 import org.netbeans.modules.j2ee.archive.project.ArchiveProjectType; 54 import org.netbeans.modules.j2ee.archive.ui.JavaEePlatformUiSupport; 55 import org.netbeans.modules.j2ee.clientproject.api.AppClientProjectGenerator; 56 import org.netbeans.modules.j2ee.dd.api.application.Application; 57 import org.netbeans.modules.j2ee.dd.api.application.Module; 58 import org.netbeans.modules.j2ee.dd.api.application.Web; 59 import org.netbeans.modules.j2ee.dd.api.client.AppClient; 60 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar; 61 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 62 import org.netbeans.modules.j2ee.deployment.devmodules.api.AntDeploymentHelper; 63 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 64 import org.netbeans.modules.j2ee.ejbjarproject.api.EjbJarProjectGenerator; 65 import org.netbeans.modules.web.project.api.WebProjectCreateData; 66 import org.netbeans.modules.web.project.api.WebProjectUtilities; 67 import org.netbeans.spi.project.support.ant.AntProjectHelper; 68 import org.netbeans.spi.project.support.ant.EditableProperties; 69 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper; 70 import org.netbeans.spi.project.support.ant.ProjectGenerator; 71 import org.netbeans.spi.project.support.ant.PropertyUtils; 72 import org.netbeans.spi.project.support.ant.ReferenceHelper; 73 import org.netbeans.spi.project.ui.support.ProjectChooser; 74 import org.openide.DialogDescriptor; 75 import org.openide.DialogDisplayer; 76 import org.openide.ErrorManager; 77 import org.openide.NotifyDescriptor; 78 import org.openide.WizardDescriptor; 79 import org.openide.filesystems.FileLock; 80 import org.openide.filesystems.FileObject; 81 import org.openide.filesystems.FileSystem; 82 import org.openide.filesystems.FileUtil; 83 import org.openide.util.NbBundle; 84 import org.openide.util.RequestProcessor; 85 import org.w3c.dom.Document ; 86 import org.w3c.dom.Element ; 87 import org.xml.sax.InputSource ; 88 import org.xml.sax.SAXException ; 89 90 public class DeployableWizardIterator implements WizardDescriptor.InstantiatingIterator { 91 92 private static final long serialVersionUID = 1L; 93 94 private transient int index; 95 private transient WizardDescriptor.Panel[] panels; 96 private transient WizardDescriptor wiz; 97 private static final String STEP_NAME_ONE = "LBL_CreateProjectStep"; public static final String PROJECT_DIR_PROP = "projdir"; public static final String PROJECT_NAME_PROP = "name"; private static final String ANT_VERSION_PROP = "minimum-ant-version"; private static final String MINIMUM_ANT_VERSION = "1.6"; public static final String PROJECT_TARGET_PROP = "targetServer"; public static final String PROJECT_ARCHIVE_PROP = "sourceArchive"; public static final String PROJECT_TYPE_PROP = "type"; public static final String SOURCE_JAR_CONST = "source-jar"; 107 static final String PROJECT_WAR_SUBARCHIVES_PROP = "warchives"; static final String PROJECT_JAR_SUBARCHIVES_PROP = "jarchives"; static final String PROJECT_RAR_SUBARCHIVES_PROP = "rarchives"; static final String PROJECT_CAR_SUBARCHIVES_PROP = "carchives"; 112 public static final String PROJECT_HAS_DESCRIPTOR = "has.descriptor"; 114 public DeployableWizardIterator() { wiz = null; } 115 116 public static DeployableWizardIterator createIterator() { 117 return new DeployableWizardIterator(); 118 } 119 120 private WizardDescriptor.Panel[] createPanels() { 121 return new WizardDescriptor.Panel[] { 122 new DeployableWizardPanel(), 123 }; 124 } 125 126 private String [] createSteps() { 127 return new String [] { 128 NbBundle.getMessage(DeployableWizardIterator.class, STEP_NAME_ONE), 129 }; 130 } 131 132 133 public Set instantiate() throws IOException { 134 return instantiate(null, null); 135 } 136 137 private Set instantiate(String distArchive, StatusPanel sp) throws IOException { 138 final File archiveFile = (File ) wiz.getProperty(PROJECT_ARCHIVE_PROP); 140 Object type = wiz.getProperty(PROJECT_TYPE_PROP); 141 Set retVal = null; 142 if (ArchiveProjectProperties.PROJECT_TYPE_VALUE_UNKNOWN.equals(type)) { 143 JarFile jf = new JarFile (archiveFile); 145 boolean isJar = isEjbJar(jf); 146 boolean isCar = hasMain(jf); 147 if (!isJar && !isCar) { 148 NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(DeployableWizardIterator.class, 149 "ERR_Cannot_Deteremine_Type"), NotifyDescriptor.WARNING_MESSAGE); 150 DialogDisplayer.getDefault().notify(nd); 151 retVal = Collections.EMPTY_SET; 152 } 153 } 154 if (null == retVal) { 155 File dirF = FileUtil.normalizeFile((File ) wiz.getProperty(PROJECT_DIR_PROP)); 156 dirF.mkdirs(); 157 158 final FileObject dir = FileUtil.toFileObject(dirF); 159 160 final AntProjectHelper h = ProjectGenerator.createProject(dir, 161 ArchiveProjectType.TYPE); 162 ReferenceHelper rH = new ReferenceHelper(h, 163 h.createAuxiliaryConfiguration(),h.getStandardPropertyEvaluator()); 164 165 Element data = h.getPrimaryConfigurationData(true); 169 Document doc = data.getOwnerDocument(); 170 Element nameEl = doc.createElementNS(ArchiveProjectType.PROJECT_CONFIGURATION_NS, PROJECT_NAME_PROP); nameEl.appendChild(doc.createTextNode(wiz.getProperty(PROJECT_NAME_PROP).toString())); 172 data.appendChild(nameEl); 173 Element minant = doc.createElementNS(ArchiveProjectType.PROJECT_CONFIGURATION_NS, ANT_VERSION_PROP); minant.appendChild(doc.createTextNode(MINIMUM_ANT_VERSION)); data.appendChild(minant); 176 Element sourceRoots = doc.createElementNS(ArchiveProjectType.PROJECT_CONFIGURATION_NS, SOURCE_JAR_CONST); data.appendChild(sourceRoots); 178 String jarReference = rH.createForeignFileReference(FileUtil.normalizeFile(archiveFile), JavaProjectConstants.ARTIFACT_TYPE_JAR); 179 sourceRoots.appendChild(doc.createTextNode(jarReference)); 181 h.putPrimaryConfigurationData(data, true); 182 EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 186 Deployment deployment = Deployment.getDefault(); 187 final Object serverInstanceID = wiz.getProperty(PROJECT_TARGET_PROP); 188 189 ep.setProperty(ArchiveProjectProperties.J2EE_SERVER_TYPE, 190 deployment.getServerID(JavaEePlatformUiSupport.getServerInstanceID(serverInstanceID))); 191 ep.setProperty(ArchiveProjectProperties.SOURCE_ARCHIVE, 192 jarReference); 193 ep.setProperty(ArchiveProjectProperties.DIST_DIR,ArchiveProjectProperties.DIST_DIR_VALUE); 194 ep.setProperty(ArchiveProjectProperties.WAR_NAME,archiveFile.getName()); 195 ep.setProperty(ArchiveProjectProperties.ARCHIVE_TYPE, (String ) type); 196 ep.setProperty(ArchiveProjectProperties.PROXY_PROJECT_DIR, ArchiveProjectProperties.TMP_PROJ_DIR_VALUE); 197 if (null == distArchive) { 199 ep.setProperty(ArchiveProjectProperties.DIST_ARCHIVE, "${dist.dir}/${war.name}"); } else { 201 ep.setProperty(ArchiveProjectProperties.DIST_ARCHIVE, distArchive); 202 } 203 if (ArchiveProjectProperties.PROJECT_TYPE_VALUE_WAR.equals(ep.getProperty(ArchiveProjectProperties.ARCHIVE_TYPE))) { 204 ep.setProperty(ArchiveProjectProperties.CONTENT_DIR,"${proxy.project.dir}/web"); ep.setProperty(ArchiveProjectProperties.CONF_DIR, "${proxy.project.dir}/web/WEB-INF"); } else { 207 ep.setProperty(ArchiveProjectProperties.CONTENT_DIR,"${proxy.project.dir}/src/java"); ep.setProperty(ArchiveProjectProperties.CONF_DIR, "${proxy.project.dir}/src/conf"); } 210 211 h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 212 213 ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH); 214 File projectFolder = dirF; try { 217 AntDeploymentHelper.writeDeploymentScript(new File (projectFolder, ArchiveProjectProperties.ANT_DEPLOY_BUILD_SCRIPT), 218 ArchiveProjectProperties.mapType((String ) wiz.getProperty(PROJECT_TYPE_PROP)), 219 JavaEePlatformUiSupport.getServerInstanceID(serverInstanceID)); 220 } catch (IOException ioe) { 221 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 222 } 223 File deployAntPropsFile = AntDeploymentHelper.getDeploymentPropertiesFile(JavaEePlatformUiSupport.getServerInstanceID(serverInstanceID)); 224 if (deployAntPropsFile != null) { 225 ep.setProperty(ArchiveProjectProperties.DEPLOY_ANT_PROPS_FILE, deployAntPropsFile.getAbsolutePath()); 226 } 227 ep.setProperty(ArchiveProjectProperties.J2EE_SERVER_INSTANCE, JavaEePlatformUiSupport.getServerInstanceID(serverInstanceID)); 228 h.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep); 229 230 final Project p = ProjectManager.getDefault().findProject(dir); 231 ProjectManager.getDefault().saveProject(p); 232 233 final ArchiveProject ap = (ArchiveProject) p.getLookup().lookup(ArchiveProject.class); 235 if (null != ap) { 236 if (null == distArchive) { 237 Runnable t = new Runnable () { 239 public void run() { 240 ProgressHandle ph = ProgressHandleFactory.createHandle("DISPLAY NAME"); 241 StatusPanel sp = new StatusPanel(); 242 sp.setIndicator(ProgressHandleFactory.createProgressComponent(ph)); 243 final Thread t = Thread.currentThread(); 244 final DialogDescriptor phDD = new DialogDescriptor(sp, 245 NbBundle.getMessage(DeployableWizardIterator.class, 246 "TITLE_PROJECT_CREATE_STATUS"), false, 247 new Object [] { DialogDescriptor.CANCEL_OPTION }, 248 DialogDescriptor.CANCEL_OPTION, 249 DialogDescriptor.BOTTOM_ALIGN, 250 null, 251 new ActionListener () { 252 public void actionPerformed(ActionEvent e) { 253 t.interrupt(); 254 } 255 256 }); 257 Dialog phd = DialogDisplayer.getDefault().createDialog(phDD); 258 phd.setVisible(true); 259 ph.start(); 260 boolean cleanup = false; 261 try { 262 explodeTheProject(dir, archiveFile, ap, (String ) wiz.getProperty(PROJECT_TYPE_PROP), 263 JavaEePlatformUiSupport.getServerInstanceID(serverInstanceID),sp); 264 sp.setActionDescription( 265 NbBundle.getMessage(DeployableWizardIterator.class, 266 "MESS_OPEN_PROJECT")); 267 OpenProjects.getDefault().open(new Project[] {p},false); 268 } catch (java.nio.channels.ClosedByInterruptException cbie) { 269 cleanup = true; 272 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 273 cbie); 274 } catch (IOException ioe) { 275 cleanup = true; 277 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 278 ioe); 279 } catch (SAXException saxe) { 280 cleanup = true; 282 ErrorManager.getDefault().notify(ErrorManager.WARNING, 283 saxe); 284 } catch (RuntimeException rte) { 285 cleanup = true; 286 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 287 rte); 288 } finally { 289 if (cleanup) { 290 sp.setActionDescription( 291 NbBundle.getMessage(DeployableWizardIterator.class, 292 "MESS_CLEAN_UP")); try { 294 Thread.currentThread().sleep(500); 295 sp.setActionDescription( 296 NbBundle.getMessage(DeployableWizardIterator.class, 297 "MESS_CLEAN_UP2")); dir.delete(); 299 Thread.currentThread().sleep(500); 300 ap.getAntProjectHelper().notifyDeleted(); 301 } catch (InterruptedException ie) { 302 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 303 ie); 304 } catch (IOException ex) { 305 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 306 ex); 307 } 308 } 309 ph.finish(); 310 phd.setVisible(false); 311 wiz.putProperty(PROJECT_DIR_PROP,null); wiz.putProperty(PROJECT_NAME_PROP,null); wiz = null; 314 } 315 } 316 }; 317 RequestProcessor.getDefault().post(t); 318 } else { 319 try { 320 explodeTheProject(dir, archiveFile, ap, (String ) wiz.getProperty(PROJECT_TYPE_PROP), 321 JavaEePlatformUiSupport.getServerInstanceID(serverInstanceID), 322 sp); 323 } catch (SAXException saxe) { 324 IOException ioe = new IOException (); 325 ioe.initCause(saxe); 326 throw ioe; 327 } 328 } 329 } 330 331 File parent = dirF.getParentFile(); 332 if (parent != null && parent.exists()) { 333 ProjectChooser.setProjectsFolder(parent); 334 } 335 retVal = Collections.EMPTY_SET; 336 } 337 return retVal; 338 } 339 340 341 private static FileObject saveZipEntryAsFileObject(final ZipInputStream str ,final FileObject projectRoot, String name) throws IOException { 342 FileObject fo = FileUtil.createData(projectRoot, name); 343 FileLock lock = fo.lock(); 344 try { 345 OutputStream out = fo.getOutputStream(lock); 346 try { 347 FileUtil.copy(str, out); 348 } finally { 349 out.close(); 350 } 351 } finally { 352 lock.releaseLock(); 353 } 354 return fo; 355 } 356 357 public void initialize(WizardDescriptor wiz) { 358 this.wiz = wiz; 359 index = 0; 360 panels = createPanels(); 361 String [] steps = createSteps(); 363 for (int i = 0; i < panels.length; i++) { 364 Component c = panels[i].getComponent(); 365 if (steps[i] == null) { 366 steps[i] = c.getName(); 370 } 371 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 373 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 378 } 379 } 380 381 public void uninitialize(WizardDescriptor wiz) { 382 panels = null; 383 } 384 385 public String name() { 386 return MessageFormat.format(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/wizard/Bundle").getString("{0}_of_{1}"), new Object [] {new Integer (index + 1), new Integer (panels.length)}); 388 } 389 390 public boolean hasNext() { 391 return index < panels.length - 1; 392 } 393 394 public boolean hasPrevious() { 395 return index > 0; 396 } 397 398 public void nextPanel() { 399 if (!hasNext()) { 400 throw new NoSuchElementException (); 401 } 402 index++; 403 } 404 405 public void previousPanel() { 406 if (!hasPrevious()) { 407 throw new NoSuchElementException (); 408 } 409 index--; 410 } 411 412 public WizardDescriptor.Panel current() { 413 return panels[index]; 414 } 415 416 public final void addChangeListener(ChangeListener l) {} 418 public final void removeChangeListener(ChangeListener l) {} 419 420 void explodeTheProject(final FileObject dir, final File sourceArchive, 421 final ArchiveProject p, final String type, final String sid, 422 StatusPanel sp) throws IOException , SAXException { 423 FileObject subDir = dir.createFolder(ArchiveProjectProperties.TMP_PROJ_DIR_VALUE); 425 FileObject srcArchive = FileUtil.toFileObject(sourceArchive); 426 Unzipper t = new Unzipper(srcArchive, subDir, p, type, 427 sid, sp); 428 t.run(); 429 } 430 431 static private boolean isEjbJar(JarFile jf) throws IOException { 432 boolean retVal = false; 433 if (jf.getEntry("META-INF/ejb-jar.xml") != null) { 434 retVal = true; 435 } else { 436 retVal = EJBAnnotationDetector.containsSomeAnnotatedEJBs(jf); 437 } 438 return retVal; 439 } 440 441 static private boolean hasMain(JarFile jf) { 442 boolean retVal = false; 443 try { 445 Manifest mf = jf.getManifest(); 446 if (null != mf) { 447 Attributes attrs = mf.getMainAttributes(); 448 retVal = attrs.getValue("Main-Class") != null; 449 } 450 } catch (IOException ioe) { 451 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 452 } 453 return retVal; 454 } 455 456 static private int count = 0; 457 458 static private class Unzipper { 459 FileObject srcArchive, subDir; 460 461 ArchiveProject p = null; 462 463 String typeProp, sid; 464 StatusPanel sp; 465 466 Unzipper(FileObject srcArchive, FileObject subDir, ArchiveProject p, 467 String typeProp, String sid, StatusPanel sp) { 468 this.srcArchive = srcArchive; 469 this.subDir = subDir; 470 this.p = p; 471 this.typeProp = typeProp; 472 this.sid = sid; 473 this.sp = sp; 474 } 475 476 public void run() throws IOException , SAXException { 477 sp.setActionDescription(NbBundle.getMessage(DeployableWizardIterator.class, 478 "MESS_EXPAND_ARCHIVE",srcArchive.getNameExt())); if (ArchiveProjectProperties.PROJECT_TYPE_VALUE_EAR.equals(typeProp)) { ZipInputStream zstr = null; 482 FileObject copiedAppXml = null; 483 try { 484 485 zstr = new ZipInputStream (new FileInputStream (FileUtil.toFile(srcArchive))); ZipEntry entry; 487 while ((entry = zstr.getNextEntry()) != null && 488 copiedAppXml == null) { 489 String ename = entry.getName(); 490 if (ename.endsWith("META-INF/application.xml")) { copiedAppXml = saveZipEntryAsFileObject(zstr,p.getProjectDirectory(),"nbproject/application.xml"); } 494 } 495 } catch (java.nio.channels.ClosedByInterruptException cbie) { 496 throw cbie; 499 } catch (java.io.FileNotFoundException fnfe) { 500 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fnfe); 502 } catch (java.io.IOException ioe) { 503 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 505 } finally { 506 if (null != zstr) { 507 try { 508 zstr.close(); 509 } catch (IOException ex) { 510 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 511 } 512 } 513 } 514 } 515 boolean isWar = ArchiveProjectProperties.PROJECT_TYPE_VALUE_WAR.equals(typeProp); 516 boolean isEar = ArchiveProjectProperties.PROJECT_TYPE_VALUE_EAR.equals(typeProp); 517 boolean isCar = ArchiveProjectProperties.PROJECT_TYPE_VALUE_CAR.equals(typeProp); 518 boolean isJar = ArchiveProjectProperties.PROJECT_TYPE_VALUE_JAR.equals(typeProp); 519 boolean isRar = ArchiveProjectProperties.PROJECT_TYPE_VALUE_RAR.equals(typeProp); 520 FileObject srcDir = subDir.createFolder("src"); FileObject javaDir = srcDir.createFolder("java"); if (isWar) { 523 handleWarFile(javaDir); 524 } else { 525 handleJavaEeArchiveFile(srcDir, isCar, isEar, isJar, javaDir, isRar); 526 } 527 } 528 529 private void handleJavaEeArchiveFile(final FileObject srcDir, 530 boolean isCar, final boolean isEar, boolean isJar, 531 final FileObject javaDir, boolean isRar) throws FileNotFoundException , IOException , SAXException { 532 FileObject confDir; 533 confDir = srcDir.createFolder("conf"); 535 if (!isEar && !isRar && !isCar && !isJar) { 537 JarFile jf = new JarFile (FileUtil.toFile(srcArchive)); 538 isJar = isEjbJar(jf); 539 isCar = hasMain(jf); 540 } 541 542 unZipFile(srcArchive.getInputStream(), javaDir); 543 544 FileObject metaInf = javaDir.getFileObject("META-INF"); FileObject[] filesToMove = metaInf.getChildren(); 546 FileObject ddFile = metaInf.getFileObject("ejb-jar.xml"); if (null == ddFile){ 548 ddFile = metaInf.getFileObject("application-client.xml"); } 550 if (null == ddFile){ 551 ddFile = metaInf.getFileObject("application.xml"); } 553 if (null == ddFile){ 554 if (isEar) { 555 determineSubarchiveTypes(javaDir); 557 } 558 ddFile = metaInf.getFileObject("ra.xml"); } 560 String versionVal = getJavaEEVersion(ddFile); 561 int len = 0; 562 if (null != filesToMove) { 563 len = filesToMove.length; 564 } 565 for (int i = 0; i < len; i++) { 566 if (filesToMove[i].isData()) { 567 FileUtil.moveFile(filesToMove[i],confDir, 568 filesToMove[i].getName()); 569 } } 571 EditableProperties ep = p.getAntProjectHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 572 if (isJar) { 573 ep.setProperty(ArchiveProjectProperties.ARCHIVE_TYPE, 574 ArchiveProjectProperties.PROJECT_TYPE_VALUE_JAR); 575 p.getAntProjectHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 576 ProjectManager.getDefault().saveProject(p); 577 AntProjectHelper h = EjbJarProjectGenerator.importProject(FileUtil.toFile(subDir), 578 srcArchive.getName(), new File [] {FileUtil.toFile(srcDir.getFileObject("java"))}, new File [0],FileUtil.toFile(confDir),null,versionVal, 580 sid,false); 581 } else if (isCar) { 587 ep.setProperty(ArchiveProjectProperties.ARCHIVE_TYPE, 588 ArchiveProjectProperties.PROJECT_TYPE_VALUE_CAR); 589 p.getAntProjectHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 590 ProjectManager.getDefault().saveProject(p); 591 AntProjectHelper h = AppClientProjectGenerator.importProject(FileUtil.toFile(subDir), 592 "tmpProjName"+count, new File [] {FileUtil.toFile(srcDir)}, new File [0],FileUtil.toFile(confDir), null, versionVal, 594 sid,false); 595 } else if (isEar) { 601 createArchiveProjects(); 602 } 603 } 604 605 private void determineSubarchiveTypes(FileObject rootDir) throws IOException , SAXException { 606 InputStream is = DeployableWizardIterator.class.getResourceAsStream("template-application.xml"); InputSource saxIs = new InputSource (is); 608 Application app = null; 609 FileLock outLock = null; 610 OutputStream outStream = null; 611 try { 612 app = org.netbeans.modules.j2ee.dd.api.application.DDProvider.getDefault().getDDRoot(saxIs); 614 if (null != app) { 615 Enumeration files = rootDir.getData(true); 616 Module m = null; 617 while (files.hasMoreElements()) { 618 FileObject fo = (FileObject) files.nextElement(); 619 String pathInEar = PropertyUtils.relativizeFile(FileUtil.toFile(rootDir), 620 FileUtil.toFile(fo)); 621 if (pathInEar.startsWith("lib/")) { continue; 623 } 624 if (pathInEar.endsWith("war")) { Web w = null; 626 m = app.newModule(); 627 w = m.newWeb(); 628 w.setWebUri(pathInEar); 629 m.setWeb(w); 632 app.addModule(m); 633 continue; 634 } 635 if (pathInEar.endsWith("rar")) { m = app.newModule(); 637 m.setConnector(pathInEar); 638 app.addModule(m); 639 continue; 640 } 641 if (pathInEar.endsWith("jar")) { JarFile jf = new JarFile (FileUtil.toFile(fo)); 644 if (isEjbJar(jf)) { 645 m = app.newModule(); 646 m.setEjb(pathInEar); 647 app.addModule(m); 648 continue; 649 } 650 if (hasMain(jf)) { m = app.newModule(); 652 m.setJava(pathInEar); 653 app.addModule(m); 654 continue; 655 } 656 } 657 } 658 FileObject out = FileUtil.createData(p.getProjectDirectory(),"nbproject/application.xml"); outLock = out.lock(); 662 outStream = out.getOutputStream(outLock); 663 app.write(outStream); 664 } 665 } finally { 666 if (null != outStream) { 667 try { 668 outStream.close(); 669 } catch (IOException ioe) { 670 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 671 ioe); 672 } 673 } 674 if (null != outLock) { 675 outLock.releaseLock(); 676 } 677 if (null != is) { 678 try { 679 is.close(); 680 } catch (IOException ioe) { 681 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 682 ioe); 683 } 684 } 685 686 } 687 } 688 689 690 private void handleWarFile(final FileObject javaDir) throws FileNotFoundException , IOException { 691 FileObject webDir = subDir.createFolder("web"); 692 unZipFile(srcArchive.getInputStream(), webDir); 694 FileObject webInf = webDir.getFileObject("WEB-INF"); 695 if (null == webInf) { 697 throw new FileNotFoundException (NbBundle.getMessage(DeployableWizardIterator.class, 698 "ERROR_MISSING_WEB_INF",webDir, srcArchive)); 699 } 700 FileObject webDotXml = null; 701 if (webInf.isFolder()) { 702 webDotXml = webInf.getFileObject("web.xml"); 703 } 704 WebProjectCreateData createData = new WebProjectCreateData(); 705 createData.setProjectDir(FileUtil.toFile(subDir)); 706 createData.setName("tmpProjName"+count++); createData.setWebModuleFO(webInf); 708 createData.setSourceFolders(new File [] {FileUtil.toFile(javaDir)}); 709 createData.setTestFolders(null); 710 createData.setDocBase(webDir); createData.setLibFolder(null); 712 createData.setJavaEEVersion(getJavaEEVersion(webDotXml)); 713 createData.setServerInstanceID(sid); createData.setBuildfile(GeneratedFilesHelper.BUILD_XML_PATH); 715 createData.setJavaSourceBased(false); 716 AntProjectHelper h = WebProjectUtilities.importProject(createData); 717 } 723 724 725 private void createArchiveProjects() throws IOException { 726 FileObject appXml = p.getProjectDirectory().getFileObject("nbproject").getFileObject("application.xml"); if (null != appXml) { 729 Application app = org.netbeans.modules.j2ee.dd.api.application.DDProvider.getDefault().getDDRoot(appXml); 730 Module ms[] = app.getModule(); 731 for (Module m : ms) { 733 String archivePath = m.getEjb(); 734 if (null != archivePath) { 735 createArchiveProject(ArchiveProjectProperties.PROJECT_TYPE_VALUE_JAR, 736 archivePath); 737 } else { 738 archivePath = m.getJava(); 739 if (null != archivePath) { 740 createArchiveProject(ArchiveProjectProperties.PROJECT_TYPE_VALUE_CAR, 741 archivePath); 742 } else { 743 archivePath = m.getConnector(); 744 if (null != archivePath) { 745 createArchiveProject(ArchiveProjectProperties.PROJECT_TYPE_VALUE_RAR, 746 archivePath); 747 } else { 748 Web w = m.getWeb(); 749 if (null != w) { 750 archivePath = w.getWebUri(); 751 if (null != archivePath) { 752 createArchiveProject(ArchiveProjectProperties.PROJECT_TYPE_VALUE_WAR, 753 archivePath); 754 } 755 } 756 } 757 } 758 } 759 760 } 761 } else { 762 ErrorManager.getDefault().log(ErrorManager.WARNING, NbBundle.getMessage(ArchiveProject.class,"WARN_EAR_ARCH_MISSING_APPLICATION_XML")); 763 } 764 } 765 766 private void createArchiveProject(String type, String pathInEar) throws IOException { 767 FileObject root = p.getProjectDirectory(); 768 FileObject subprojRoot = FileUtil.createFolder(root,"subarchives"); String subprojkey = Util.getKey(pathInEar); 770 FileObject projDest = subprojRoot.createFolder(subprojkey); 771 DeployableWizardIterator dwi = new DeployableWizardIterator(); 773 WizardDescriptor wizDesc = new WizardDescriptor(new WizardDescriptor.Panel[0]); 774 File oldVal = ProjectChooser.getProjectsFolder(); 775 wizDesc.putProperty(PROJECT_DIR_PROP, 776 FileUtil.toFile(projDest)); 777 FileObject archive = root.getFileObject(ArchiveProjectProperties.TMP_PROJ_DIR_VALUE). 778 getFileObject("src").getFileObject("java").getFileObject(pathInEar); 779 wizDesc.putProperty(PROJECT_ARCHIVE_PROP, 780 FileUtil.toFile(archive)); 781 wizDesc.putProperty(PROJECT_NAME_PROP, 782 subprojkey); 783 784 wizDesc.putProperty(PROJECT_TARGET_PROP, 785 JavaEePlatformUiSupport.getServerInstanceID(sid)); 786 wizDesc.putProperty(PROJECT_TYPE_PROP, type); 787 String distArchive = 788 PropertyUtils.relativizeFile(FileUtil.toFile(archive.getParent().getParent()),FileUtil.toFile(root))+ 789 "/tmpproj/src/java/"+pathInEar; dwi.initialize(wizDesc); 791 dwi.instantiate(distArchive, sp); 792 if (null != oldVal) { 793 ProjectChooser.setProjectsFolder(oldVal); 794 } 795 } 796 797 private void unZipFile(InputStream source, final FileObject projectRoot) throws IOException { 798 try { 799 final ZipInputStream str = new ZipInputStream (source); 800 ZipEntry entry; 801 while ((entry = str.getNextEntry()) != null) { 802 if (entry.isDirectory()) { 803 FileUtil.createFolder(projectRoot, entry.getName()); 804 } else { 805 final ZipEntry fentry = entry; 806 if (fentry.getName().endsWith(".xml")){ FileSystem fs = projectRoot.getFileSystem(); 808 fs.runAtomicAction(new FileSystem.AtomicAction() { 809 public void run() throws IOException { 810 saveZipEntryAsFileObject( str , projectRoot, fentry.getName()); 811 } 812 }); 813 } else { 814 saveZipEntryAsFileObject( str , projectRoot, fentry.getName()); 815 816 } 817 } 818 } 819 } finally { 820 source.close(); 821 } 822 823 } 824 825 private String getJavaEEVersion(FileObject f) throws IOException { 826 String versionVal = "1.5"; if (null != f) { 828 if (f.getName().startsWith("web")) { WebApp wa = org.netbeans.modules.j2ee.dd.api.web.DDProvider.getDefault().getDDRoot(f); 830 String t = wa.getVersion(); 831 if (!"2.5".equals(t)) { versionVal = "1."+t.charAt(t.length()-1); 833 } 834 } 835 if (f.getName().startsWith("ejb")) { EjbJar ej = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(f); 838 if (null!=ej){ 839 versionVal = ej.getJ2eePlatformVersion(); 840 } 841 } 842 if (f.getName().startsWith("application-")) { AppClient ac = org.netbeans.modules.j2ee.dd.api.client.DDProvider.getDefault().getDDRoot(f); 844 String t = ac.getVersion().toString(); 845 if (!"5".equals(t)) { 846 versionVal = t; 847 } 848 } 849 if (f.getName().equals("application")) { Application ap = org.netbeans.modules.j2ee.dd.api.application.DDProvider.getDefault().getDDRoot(f); 851 versionVal = ap.getVersion().toString(); 852 } 853 } 857 return versionVal; 858 } 859 } 860 } 861 | Popular Tags |