KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > archive > wizard > DeployableWizardIterator


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.j2ee.archive.wizard;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.text.MessageFormat JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.NoSuchElementException JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.jar.Attributes JavaDoc;
38 import java.util.jar.JarFile JavaDoc;
39 import java.util.jar.Manifest JavaDoc;
40 import java.util.zip.ZipEntry JavaDoc;
41 import java.util.zip.ZipInputStream JavaDoc;
42 import javax.swing.JComponent JavaDoc;
43 import javax.swing.event.ChangeListener JavaDoc;
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 JavaDoc;
86 import org.w3c.dom.Element JavaDoc;
87 import org.xml.sax.InputSource JavaDoc;
88 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc STEP_NAME_ONE = "LBL_CreateProjectStep"; // NOI18N
98
public static final String JavaDoc PROJECT_DIR_PROP = "projdir"; // NOI18N
99
public static final String JavaDoc PROJECT_NAME_PROP = "name"; // NOI18N
100
private static final String JavaDoc ANT_VERSION_PROP = "minimum-ant-version"; // NOI18N
101
private static final String JavaDoc MINIMUM_ANT_VERSION = "1.6"; // NOI18N
102
public static final String JavaDoc PROJECT_TARGET_PROP = "targetServer"; // NOI18N
103
public static final String JavaDoc PROJECT_ARCHIVE_PROP = "sourceArchive"; // NOI18N
104
public static final String JavaDoc PROJECT_TYPE_PROP = "type"; // NOI18N
105
public static final String JavaDoc SOURCE_JAR_CONST = "source-jar"; // NOI18N
106

107     static final String JavaDoc PROJECT_WAR_SUBARCHIVES_PROP = "warchives"; //NOI18N
108
static final String JavaDoc PROJECT_JAR_SUBARCHIVES_PROP = "jarchives"; //NOI18N
109
static final String JavaDoc PROJECT_RAR_SUBARCHIVES_PROP = "rarchives"; //NOI18N
110
static final String JavaDoc PROJECT_CAR_SUBARCHIVES_PROP = "carchives"; //NOI18N
111

112     public static final String JavaDoc PROJECT_HAS_DESCRIPTOR = "has.descriptor"; //NOI18N
113

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 JavaDoc[] createSteps() {
127         return new String JavaDoc[] {
128             NbBundle.getMessage(DeployableWizardIterator.class, STEP_NAME_ONE),
129         };
130     }
131     
132     
133     public Set JavaDoc/*<FileObject>*/ instantiate() throws IOException JavaDoc {
134         return instantiate(null, null);
135     }
136     
137     private Set JavaDoc/*<FileObject>*/ instantiate(String JavaDoc distArchive, StatusPanel sp) throws IOException JavaDoc {
138         // test to see if we should really continue for a jar HERE...
139
final File JavaDoc archiveFile = (File JavaDoc) wiz.getProperty(PROJECT_ARCHIVE_PROP);
140         Object JavaDoc type = wiz.getProperty(PROJECT_TYPE_PROP);
141         Set JavaDoc retVal = null;
142         if (ArchiveProjectProperties.PROJECT_TYPE_VALUE_UNKNOWN.equals(type)) {
143             // do a bit of digging...
144
JarFile JavaDoc jf = new JarFile JavaDoc(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 JavaDoc dirF = FileUtil.normalizeFile((File JavaDoc) 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             //
166
// persist the project attributes
167
//
168
Element JavaDoc data = h.getPrimaryConfigurationData(true);
169             Document JavaDoc doc = data.getOwnerDocument();
170             Element JavaDoc nameEl = doc.createElementNS(ArchiveProjectType.PROJECT_CONFIGURATION_NS, PROJECT_NAME_PROP); // NOI18N
171
nameEl.appendChild(doc.createTextNode(wiz.getProperty(PROJECT_NAME_PROP).toString()));
172             data.appendChild(nameEl);
173             Element JavaDoc minant = doc.createElementNS(ArchiveProjectType.PROJECT_CONFIGURATION_NS, ANT_VERSION_PROP); // NOI18N
174
minant.appendChild(doc.createTextNode(MINIMUM_ANT_VERSION)); // NOI18N
175
data.appendChild(minant);
176             Element JavaDoc sourceRoots = doc.createElementNS(ArchiveProjectType.PROJECT_CONFIGURATION_NS, SOURCE_JAR_CONST); //NOI18N
177
data.appendChild(sourceRoots);
178             String JavaDoc jarReference = rH.createForeignFileReference(FileUtil.normalizeFile(archiveFile), JavaProjectConstants.ARTIFACT_TYPE_JAR);
179             //Element root = doc.createElementNS (ArchiveProjectType.PROJECT_CONFIGURATION_NS,"root"); //NOI18N
180
sourceRoots.appendChild(doc.createTextNode(jarReference));
181             h.putPrimaryConfigurationData(data, true);
182             //
183
// write the properties file
184
//
185
EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
186             Deployment deployment = Deployment.getDefault();
187             final Object JavaDoc 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 JavaDoc) type);
196             ep.setProperty(ArchiveProjectProperties.PROXY_PROJECT_DIR, ArchiveProjectProperties.TMP_PROJ_DIR_VALUE);
197             //String distArchive = (String) wiz.getProperty(DIST_ARCHIVE_PROP);
198
if (null == distArchive) {
199                 ep.setProperty(ArchiveProjectProperties.DIST_ARCHIVE, "${dist.dir}/${war.name}");//NO18N
200
} 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");//NO18N
205
ep.setProperty(ArchiveProjectProperties.CONF_DIR, "${proxy.project.dir}/web/WEB-INF");//NO18N
206
} else {
207                 ep.setProperty(ArchiveProjectProperties.CONTENT_DIR,"${proxy.project.dir}/src/java");//NO18N
208
ep.setProperty(ArchiveProjectProperties.CONF_DIR, "${proxy.project.dir}/src/conf"); //NO18N
209
}
210             
211             h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
212             
213             ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
214             // ant deployment support
215
File JavaDoc projectFolder = dirF; // FileUtil.toFile(dirFO);
216
try {
217                 AntDeploymentHelper.writeDeploymentScript(new File JavaDoc(projectFolder, ArchiveProjectProperties.ANT_DEPLOY_BUILD_SCRIPT),
218                         ArchiveProjectProperties.mapType((String JavaDoc) wiz.getProperty(PROJECT_TYPE_PROP)),
219                         JavaEePlatformUiSupport.getServerInstanceID(serverInstanceID));
220             } catch (IOException JavaDoc ioe) {
221                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
222             }
223             File JavaDoc 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             // explode the project here....
234
final ArchiveProject ap = (ArchiveProject) p.getLookup().lookup(ArchiveProject.class);
235             if (null != ap) {
236                 if (null == distArchive) {
237                     // start thread to unzip the project tree
238
Runnable JavaDoc t = new Runnable JavaDoc() {
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 JavaDoc t = Thread.currentThread();
244                             final DialogDescriptor phDD = new DialogDescriptor(sp,
245                                     NbBundle.getMessage(DeployableWizardIterator.class,
246                                     "TITLE_PROJECT_CREATE_STATUS"), false,
247                                     new Object JavaDoc[] { DialogDescriptor.CANCEL_OPTION },
248                                     DialogDescriptor.CANCEL_OPTION,
249                                     DialogDescriptor.BOTTOM_ALIGN,
250                                     null,
251                                     new ActionListener JavaDoc() {
252                                 public void actionPerformed(ActionEvent JavaDoc e) {
253                                     t.interrupt();
254                                 }
255                                 
256                             });
257                             Dialog JavaDoc phd = DialogDisplayer.getDefault().createDialog(phDD);
258                             phd.setVisible(true);
259                             ph.start();
260                             boolean cleanup = false;
261                             try {
262                                 explodeTheProject(dir, archiveFile, ap, (String JavaDoc) 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 JavaDoc cbie) {
269                                 // I see this when i shoot the thread...
270
// so I will just clean up...
271
cleanup = true;
272                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
273                                         cbie);
274                             } catch (IOException JavaDoc ioe) {
275                                 // I don't know exactly what is up here
276
cleanup = true;
277                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
278                                         ioe);
279                             } catch (SAXException JavaDoc saxe) {
280                                 // could not parse the application.xml file...
281
cleanup = true;
282                                 ErrorManager.getDefault().notify(ErrorManager.WARNING,
283                                         saxe);
284                             } catch (RuntimeException JavaDoc 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")); // NOI18N
293
try {
294                                         Thread.currentThread().sleep(500);
295                                         sp.setActionDescription(
296                                                 NbBundle.getMessage(DeployableWizardIterator.class,
297                                                 "MESS_CLEAN_UP2")); // NOI18N
298
dir.delete();
299                                         Thread.currentThread().sleep(500);
300                                         ap.getAntProjectHelper().notifyDeleted();
301                                     } catch (InterruptedException JavaDoc ie) {
302                                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
303                                                 ie);
304                                     } catch (IOException JavaDoc 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);//NO18N
312
wiz.putProperty(PROJECT_NAME_PROP,null);//NO18N
313
wiz = null;
314                             }
315                         }
316                     };
317                     RequestProcessor.getDefault().post(t);
318                 } else {
319                     try {
320                         explodeTheProject(dir, archiveFile, ap, (String JavaDoc) wiz.getProperty(PROJECT_TYPE_PROP),
321                                 JavaEePlatformUiSupport.getServerInstanceID(serverInstanceID),
322                                 sp);
323                     } catch (SAXException JavaDoc saxe) {
324                         IOException JavaDoc ioe = new IOException JavaDoc();
325                         ioe.initCause(saxe);
326                         throw ioe;
327                     }
328                 }
329             }
330             
331             File JavaDoc 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 JavaDoc str ,final FileObject projectRoot, String JavaDoc name) throws IOException JavaDoc{
342         FileObject fo = FileUtil.createData(projectRoot, name);
343         FileLock lock = fo.lock();
344         try {
345             OutputStream JavaDoc 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         // Make sure list of steps is accurate.
362
String JavaDoc[] steps = createSteps();
363         for (int i = 0; i < panels.length; i++) {
364             Component JavaDoc c = panels[i].getComponent();
365             if (steps[i] == null) {
366                 // Default step name to component name of panel.
367
// Mainly useful for getting the name of the target
368
// chooser to appear in the list of steps.
369
steps[i] = c.getName();
370             }
371             if (c instanceof JComponent JavaDoc) { // assume Swing components
372
JComponent JavaDoc jc = (JComponent JavaDoc) c;
373                 // Step #.
374
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i));//NO18N
375
// Step name (actually the whole list for reference).
376
jc.putClientProperty("WizardPanel_contentData", steps);//NO18N
377
}
378         }
379     }
380     
381     public void uninitialize(WizardDescriptor wiz) {
382         panels = null;
383     }
384     
385     public String JavaDoc name() {
386         return MessageFormat.format(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/wizard/Bundle").getString("{0}_of_{1}"),//NO18N
387
new Object JavaDoc[] {new Integer JavaDoc(index + 1), new Integer JavaDoc(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 JavaDoc();
401         }
402         index++;
403     }
404     
405     public void previousPanel() {
406         if (!hasPrevious()) {
407             throw new NoSuchElementException JavaDoc();
408         }
409         index--;
410     }
411     
412     public WizardDescriptor.Panel current() {
413         return panels[index];
414     }
415     
416     // If nothing unusual changes in the middle of the wizard, simply:
417
public final void addChangeListener(ChangeListener JavaDoc l) {}
418     public final void removeChangeListener(ChangeListener JavaDoc l) {}
419     
420     void explodeTheProject(final FileObject dir, final File JavaDoc sourceArchive,
421             final ArchiveProject p, final String JavaDoc type, final String JavaDoc sid,
422             StatusPanel sp) throws IOException JavaDoc, SAXException JavaDoc {
423         // explode the archive file
424
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 JavaDoc jf) throws IOException JavaDoc {
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 JavaDoc jf) {
442         boolean retVal = false;
443         // throws IOE
444
try {
445             Manifest JavaDoc mf = jf.getManifest();
446             if (null != mf) {
447                 Attributes JavaDoc attrs = mf.getMainAttributes();
448                 retVal = attrs.getValue("Main-Class") != null;
449             }
450         } catch (IOException JavaDoc 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 JavaDoc typeProp, sid;
464         StatusPanel sp;
465         
466         Unzipper(FileObject srcArchive, FileObject subDir, ArchiveProject p,
467                 String JavaDoc typeProp, String JavaDoc 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 JavaDoc, SAXException JavaDoc {
477             sp.setActionDescription(NbBundle.getMessage(DeployableWizardIterator.class,
478                     "MESS_EXPAND_ARCHIVE",srcArchive.getNameExt())); // NOI18N
479
if (ArchiveProjectProperties.PROJECT_TYPE_VALUE_EAR.equals(typeProp)) { // ep.getProperty(ArchiveProjectProperties.ARCHIVE_TYPE))) {
480
// save real application.xml to nbproject directory
481
ZipInputStream JavaDoc zstr = null;
482                 FileObject copiedAppXml = null;
483                 try {
484                     
485                     zstr = new ZipInputStream JavaDoc(new FileInputStream JavaDoc(FileUtil.toFile(srcArchive))); // archiveFile));
486
ZipEntry JavaDoc entry;
487                     while ((entry = zstr.getNextEntry()) != null &&
488                             copiedAppXml == null) {
489                         String JavaDoc ename = entry.getName();
490                         if (ename.endsWith("META-INF/application.xml")) { // NOI18N
491
// found a real application.xml
492
copiedAppXml = saveZipEntryAsFileObject(zstr,p.getProjectDirectory(),"nbproject/application.xml"); // NOI18N
493
}
494                     }
495                 } catch (java.nio.channels.ClosedByInterruptException JavaDoc cbie) {
496                     // I see this when i shoot the thread...
497
// so I will just clean up...
498
throw cbie;
499                 } catch (java.io.FileNotFoundException JavaDoc fnfe) {
500                     // the file got deleted while we were in the wizard
501
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, fnfe);
502                 } catch (java.io.IOException JavaDoc ioe) {
503                     // the file couldn't be saved
504
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
505                 } finally {
506                     if (null != zstr) {
507                         try {
508                             zstr.close();
509                         } catch (IOException JavaDoc 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"); // NOI18N
521
FileObject javaDir = srcDir.createFolder("java"); // NOI18N
522
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 JavaDoc, IOException JavaDoc, SAXException JavaDoc {
532             FileObject confDir;
533             // this should work for ejb-jar/rar/car archives...
534
confDir = srcDir.createFolder("conf");
535             // run the annotation detector here for jar/car descrimination
536
if (!isEar && !isRar && !isCar && !isJar) {
537                 JarFile JavaDoc jf = new JarFile JavaDoc(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"); // NOI18N
545
FileObject[] filesToMove = metaInf.getChildren();
546             FileObject ddFile = metaInf.getFileObject("ejb-jar.xml"); // NOI18N
547
if (null == ddFile){
548                 ddFile = metaInf.getFileObject("application-client.xml"); // NOI18N
549
}
550             if (null == ddFile){
551                 ddFile = metaInf.getFileObject("application.xml"); // NOI18N
552
}
553             if (null == ddFile){
554                 if (isEar) {
555                     // build the nbproject/application.xml file here.
556
determineSubarchiveTypes(javaDir);
557                 }
558                 ddFile = metaInf.getFileObject("ra.xml"); // NOI18N
559
}
560             String JavaDoc 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                 } // else -- this is a folder
570
}
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 JavaDoc[] {FileUtil.toFile(srcDir.getFileObject("java"))}, // NOI18N
579
new File JavaDoc[0],FileUtil.toFile(confDir),null,versionVal,
580                         sid,false);
581 // EditableProperties subEp = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
582
// subEp.setProperty(SourceTypeConstants.PROJECT_SOURCES_TYPE,SourceTypeConstants.SOURCES_VALUE_CLASS); // NOI18N
583
// h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,subEp);
584
// Project subP = ProjectManager.getDefault().findProject(h.getProjectDirectory());
585
// ProjectManager.getDefault().saveProject(subP);
586
} 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 JavaDoc[] {FileUtil.toFile(srcDir)}, // NOI18N
593
new File JavaDoc[0],FileUtil.toFile(confDir), null, versionVal,
594                         sid,false);
595 // EditableProperties subEp = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
596
// subEp.setProperty(SourceTypeConstants.PROJECT_SOURCES_TYPE,SourceTypeConstants.SOURCES_VALUE_CLASS); // NOI18N
597
// h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,subEp);
598
// Project subP = ProjectManager.getDefault().findProject(h.getProjectDirectory());
599
// ProjectManager.getDefault().saveProject(subP);
600
} else if (isEar) {
601                 createArchiveProjects();
602             }
603         }
604         
605         private void determineSubarchiveTypes(FileObject rootDir) throws IOException JavaDoc, SAXException JavaDoc {
606             InputStream JavaDoc is = DeployableWizardIterator.class.getResourceAsStream("template-application.xml"); // NOI18N
607
InputSource JavaDoc saxIs = new InputSource JavaDoc(is);
608             Application app = null;
609             FileLock outLock = null;
610             OutputStream JavaDoc outStream = null;
611             try {
612                 // throws IOE or SAXEx
613
app = org.netbeans.modules.j2ee.dd.api.application.DDProvider.getDefault().getDDRoot(saxIs);
614                 if (null != app) {
615                     Enumeration JavaDoc files = rootDir.getData(true);
616                     Module m = null;
617                     while (files.hasMoreElements()) {
618                         FileObject fo = (FileObject) files.nextElement();
619                         String JavaDoc pathInEar = PropertyUtils.relativizeFile(FileUtil.toFile(rootDir),
620                                 FileUtil.toFile(fo));
621                         if (pathInEar.startsWith("lib/")) { // NOI18N
622
continue;
623                         }
624                         if (pathInEar.endsWith("war")) { // NOI18N
625
Web w = null;
626                             m = app.newModule();
627                             w = m.newWeb();
628                             w.setWebUri(pathInEar);
629                             // I don't need this
630
//w.setContextRoot(pathToCR(aPath));
631
m.setWeb(w);
632                             app.addModule(m);
633                             continue;
634                         }
635                         if (pathInEar.endsWith("rar")) { // NOI18N
636
m = app.newModule();
637                             m.setConnector(pathInEar);
638                             app.addModule(m);
639                             continue;
640                         }
641                         if (pathInEar.endsWith("jar")) { // NOI18N
642
// throws IOE
643
JarFile JavaDoc jf = new JarFile JavaDoc(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)) { // this may be a bit course
651
m = app.newModule();
652                                 m.setJava(pathInEar);
653                                 app.addModule(m);
654                                 continue;
655                             }
656                         }
657                     }
658                     // outstream needs to write to nbproject/application.xml
659
// throws IOE
660
FileObject out = FileUtil.createData(p.getProjectDirectory(),"nbproject/application.xml"); // NOI18N
661
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 JavaDoc 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 JavaDoc 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 JavaDoc, IOException JavaDoc {
691             FileObject webDir = subDir.createFolder("web");
692             //
693
unZipFile(srcArchive.getInputStream(), webDir);
694             FileObject webInf = webDir.getFileObject("WEB-INF");
695             // The web-inf is a MUST HAVE
696
if (null == webInf) {
697                 throw new FileNotFoundException JavaDoc(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++); //NOI18N
707
createData.setWebModuleFO(webInf);
708             createData.setSourceFolders(new File JavaDoc[] {FileUtil.toFile(javaDir)});
709             createData.setTestFolders(null);
710             createData.setDocBase(webDir); //NOI18N
711
createData.setLibFolder(null);
712             createData.setJavaEEVersion(getJavaEEVersion(webDotXml));
713             createData.setServerInstanceID(sid); // (String) app.get(ArchiveProjectProperties.J2EE_SERVER_INSTANCE)
714
createData.setBuildfile(GeneratedFilesHelper.BUILD_XML_PATH);
715             createData.setJavaSourceBased(false);
716             AntProjectHelper h = WebProjectUtilities.importProject(createData);
717 // EditableProperties subEp = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
718
// subEp.setProperty(SourceTypeConstants.PROJECT_SOURCES_TYPE,SourceTypeConstants.SOURCES_VALUE_CLASS); // NOI18N
719
// h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,subEp);
720
// Project subP = ProjectManager.getDefault().findProject(h.getProjectDirectory());
721
// ProjectManager.getDefault().saveProject(subP);
722
}
723         
724         
725         private void createArchiveProjects() throws IOException JavaDoc {
726             // get the application.xml file
727
FileObject appXml = p.getProjectDirectory().getFileObject("nbproject").getFileObject("application.xml"); // NOI18N
728
if (null != appXml) {
729                 Application app = org.netbeans.modules.j2ee.dd.api.application.DDProvider.getDefault().getDDRoot(appXml);
730                 Module ms[] = app.getModule();
731                 //int subprojIndex = 0;
732
for (Module m : ms) {
733                     String JavaDoc 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 JavaDoc type, String JavaDoc pathInEar) throws IOException JavaDoc {
767             FileObject root = p.getProjectDirectory();
768             FileObject subprojRoot = FileUtil.createFolder(root,"subarchives"); // NOI18N
769
String JavaDoc subprojkey = Util.getKey(pathInEar);
770             FileObject projDest = subprojRoot.createFolder(subprojkey);
771             //p.setEarPath(subprojkey, pathInEar);
772
DeployableWizardIterator dwi = new DeployableWizardIterator();
773             WizardDescriptor wizDesc = new WizardDescriptor(new WizardDescriptor.Panel[0]);
774             File JavaDoc 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 JavaDoc distArchive =
788                     PropertyUtils.relativizeFile(FileUtil.toFile(archive.getParent().getParent()),FileUtil.toFile(root))+
789                     "/tmpproj/src/java/"+pathInEar; // NOI18N
790
dwi.initialize(wizDesc);
791             dwi.instantiate(distArchive, sp);
792             if (null != oldVal) {
793                 ProjectChooser.setProjectsFolder(oldVal);
794             }
795         }
796         
797         private void unZipFile(InputStream JavaDoc source, final FileObject projectRoot) throws IOException JavaDoc {
798             try {
799                 final ZipInputStream JavaDoc str = new ZipInputStream JavaDoc(source);
800                 ZipEntry JavaDoc entry;
801                 while ((entry = str.getNextEntry()) != null) {
802                     if (entry.isDirectory()) {
803                         FileUtil.createFolder(projectRoot, entry.getName());
804                     } else {
805                         final ZipEntry JavaDoc fentry = entry;
806                         if (fentry.getName().endsWith(".xml")){ //need atomic action because we do not want half complete xml file later one in other threads
807
FileSystem fs = projectRoot.getFileSystem();
808                             fs.runAtomicAction(new FileSystem.AtomicAction() {
809                                 public void run() throws IOException JavaDoc {
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 JavaDoc getJavaEEVersion(FileObject f) throws IOException JavaDoc {
826             String JavaDoc versionVal = "1.5"; //NOI18N
827
if (null != f) {
828                 if (f.getName().startsWith("web")) { //NOI18N
829
WebApp wa = org.netbeans.modules.j2ee.dd.api.web.DDProvider.getDefault().getDDRoot(f);
830                     String JavaDoc t = wa.getVersion();
831                     if (!"2.5".equals(t)) { // NOI18N
832
versionVal = "1."+t.charAt(t.length()-1);
833                     }
834                 }
835                 if (f.getName().startsWith("ejb")) { //NOI18N
836
// TODO -- this doesn't work right... I always get a null here..
837
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-")) { //NOI18N
843
AppClient ac = org.netbeans.modules.j2ee.dd.api.client.DDProvider.getDefault().getDDRoot(f);
844                     String JavaDoc t = ac.getVersion().toString();
845                     if (!"5".equals(t)) {
846                         versionVal = t;
847                     }
848                 }
849                 if (f.getName().equals("application")) { //NOI18N
850
Application ap = org.netbeans.modules.j2ee.dd.api.application.DDProvider.getDefault().getDDRoot(f);
851                     versionVal = ap.getVersion().toString();
852                 }
853 // if (f.getName().equals("ra")) {
854
// // let's just stick to 1.5...
855
// }
856
}
857             return versionVal;
858         }
859     }
860 }
861
Popular Tags