KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > JonasProject


1  package com.bull.eclipse.jonas;
2
3 /*
4  * (c) Copyright Bull SA 2003.
5  * All Rights Reserved.
6  */

7  
8 import java.io.File JavaDoc;
9 import java.io.FileInputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Arrays JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IProjectNature;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.resources.IResourceStatus;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.runtime.PlatformObject;
30 import org.eclipse.core.runtime.QualifiedName;
31 import org.eclipse.jdt.core.IClasspathEntry;
32 import org.eclipse.jdt.core.IJavaProject;
33 import org.eclipse.jdt.core.JavaCore;
34
35 import com.bull.eclipse.jonas.utils.FileUtil;
36 import com.bull.eclipse.jonas.utils.JDTUtil;
37 import com.bull.eclipse.jonas.utils.TemplateDirUtil;
38
39
40
41
42 public class JonasProject extends PlatformObject implements IProjectNature {
43
44     // Persistence properties of projects
45
private static final String JavaDoc PROPERTIES_FILENAME = ".jonasplugin";
46     private static final String JavaDoc KEY_WEBCONTEXT = "webContext";
47     private static final String JavaDoc KEY_EXPORTSOURCE = "exportSource";
48     private static final String JavaDoc KEY_RELOADABLE="reloadable";
49     private static final String JavaDoc KEY_REDIRECTLOGGER="redirectLogger";
50     private static final String JavaDoc KEY_WARLOCATION = "warLocation";
51     private static final String JavaDoc KEY_SRCDIR = "srcDir";
52     private static final String JavaDoc KEY_SRCEJBDIR = "srcEJBDir";
53     private static final String JavaDoc KEY_WEBDIRNAME = "webdirName";
54     private static final String JavaDoc KEY_EXTRARESOURCES = ExtraResourcesEntries.TAG_NAME;
55
56     private static final QualifiedName QN_WEBCONTEXT = new QualifiedName("JonasProject", KEY_WEBCONTEXT);
57     private static final QualifiedName QN_EXPORTSOURCE = new QualifiedName("JonasProject", KEY_EXPORTSOURCE);
58     private static final QualifiedName QN_RELOADABLE = new QualifiedName("JonasProject", KEY_RELOADABLE);
59     private static final QualifiedName QN_REDIRECTLOGGER = new QualifiedName("JonasProject", KEY_REDIRECTLOGGER);
60     private static final QualifiedName QN_WARLOCATION = new QualifiedName("JonasProject", KEY_WARLOCATION);
61     private static final QualifiedName QN_SRCDIR = new QualifiedName("JonasProject", KEY_SRCDIR);
62     private static final QualifiedName QN_SRCEJBDIR = new QualifiedName("JonasProject", KEY_SRCEJBDIR);
63     private static final QualifiedName QN_WEBDIRNAME = new QualifiedName("JonasProject", KEY_WEBDIRNAME);
64     private static final QualifiedName QN_EXTRARESOURCES = new QualifiedName("JonasProject", KEY_EXTRARESOURCES);
65     
66     /**
67      * The platform project this <code>JonasProject</code> is based on
68      */

69     protected IProject project;
70     protected IJavaProject javaProject;
71
72     protected String JavaDoc webContext = "";
73     protected String JavaDoc warLocation = "";
74     protected boolean exportSource;
75     protected boolean reloadable = true;
76     protected boolean redirectLogger = true;
77     protected String JavaDoc srcDir = "";
78     protected String JavaDoc srcEJBDir = "";
79     protected String JavaDoc srcPersistenceDir = "persistence";
80     protected String JavaDoc webdirName = "";
81     protected ExtraResourcesEntries extraResourcesEntries;
82     protected Vector JavaDoc packageEJB = null;
83     protected Vector JavaDoc jarEJBName = null;
84     
85     protected IFolder webDirFolder;
86     protected IFolder srcDirFolder;
87     protected IFolder srcEJBDirFolder;
88     protected boolean createSamples; // value from the proj creation wizard
89

90     /**
91      * Gets the project.
92      * @return Returns a IProject
93      */

94     public IProject getProject() {
95         return project;
96     }
97
98     /**
99      * Sets the project.
100      * @param project The project to set
101      */

102     public void setProject(IProject project) {
103         this.project = project;
104     }
105     
106     /*
107      * @see IProjectNature#configure()
108      */

109     public void configure() throws CoreException {
110     }
111
112     /*
113      * @see IProjectNature#deconfigure()
114      */

115     public void deconfigure() throws CoreException {
116     }
117
118     /*
119      * @see IProjectNature#getProject()
120      */

121     public IJavaProject getJavaProject() {
122         return javaProject;
123     }
124
125     /*
126      * @see IProjectNature#setProject(IProject)
127      */

128     public void setJavaProject(IJavaProject javaProject) {
129         this.javaProject = javaProject;
130         this.setProject(javaProject.getProject());
131     }
132
133
134     static public void addJonasNature(IJavaProject project) {
135         try {
136             JDTUtil.addNatureToProject(project.getProject(), JonasLauncherPlugin.NATURE_ID);
137         } catch(CoreException ex) {
138             JonasLauncherPlugin.log(ex.getMessage());
139         }
140     }
141
142     static public void removeJonasNature(IJavaProject project) {
143         try {
144             JDTUtil.removeNatureToProject(project.getProject(), JonasLauncherPlugin.NATURE_ID);
145         } catch(CoreException ex) {
146             JonasLauncherPlugin.log(ex.getMessage());
147         }
148     }
149
150     /**
151      * Return a JonasProject if this javaProject has the jonas nature
152      * Return null if Project has not jonas nature
153      */

154     static public JonasProject create(IJavaProject javaProject) {
155         JonasProject result = null;
156         try {
157             result = (JonasProject)javaProject.getProject().getNature(JonasLauncherPlugin.NATURE_ID);
158             if(result != null)
159                 result.setJavaProject(javaProject);
160         } catch(CoreException ex) {
161             JonasLauncherPlugin.log(ex.getMessage());
162         }
163         return result;
164     }
165
166     /**
167      * Return a JonasProject if this Project has the jonas nature
168      * Return null if Project has not jonas nature
169      */

170     static public JonasProject create(IProject project) {
171
172         IJavaProject javaProject = JavaCore.create(project);
173         if(javaProject != null) {
174             return JonasProject.create(javaProject);
175         } else {
176             return null;
177         }
178     }
179
180     private File JavaDoc getPropertiesFile() {
181         return (this.getProject().getLocation().append(PROPERTIES_FILENAME).toFile());
182     }
183     
184     private String JavaDoc readProperty(String JavaDoc key) {
185         String JavaDoc result = null;
186         
187         try {
188             result = FileUtil.readPropertyInXMLFile(getPropertiesFile(), key);
189         } catch (IOException JavaDoc e) {
190             try {
191                 result = getJavaProject().getCorrespondingResource().getPersistentProperty(new QualifiedName("JonasProject", key));
192             } catch (Exception JavaDoc e2) {
193                 JonasLauncherPlugin.log(e2);
194             }
195         }
196         
197         if(result == null) {
198             result = "";
199         }
200             
201         return result;
202     }
203
204         
205     /**
206      * Gets the webContext.
207      * @return Returns a String
208      */

209     public String JavaDoc getWebContext() {
210         return this.readProperty(KEY_WEBCONTEXT);
211     }
212
213     /**
214      * Sets the webContext.
215      * @param webContext The webContext to set
216      */

217     public void setWebContext(String JavaDoc wp) {
218         this.webContext = wp;
219     }
220
221     public void updateWebContext(String JavaDoc newContextPath) throws Exception JavaDoc {
222         setWebContext(newContextPath);
223     }
224         
225     /**
226      * Gets the warfile.
227      * @return Returns a String
228      */

229     public String JavaDoc getWarLocation() {
230         return this.readProperty(KEY_WARLOCATION);
231     }
232
233     /**
234      * Sets the warfile
235      * @param warfile The warfile to set
236      */

237     public void setWarLocation(String JavaDoc wl) {
238         this.warLocation = wl;
239     }
240     
241
242     /**
243      * Gets the exportSource.
244      * @return Returns a boolean
245      */

246     public boolean getExportSource() {
247         return new Boolean JavaDoc(this.readProperty(KEY_EXPORTSOURCE)).booleanValue();
248     }
249
250     /**
251      * Sets the exportSource.
252      * @param exportSource The exportSource to set
253      */

254     public void setExportSource(boolean exportSource) {
255         this.exportSource = exportSource;
256     }
257
258     /**
259      * Gets the reloadable
260      * @return Returns a boolean
261      */

262     public boolean getReloadable(){
263         String JavaDoc reloadableProperty = this.readProperty(KEY_RELOADABLE);
264         // Set default value to true
265
if(reloadableProperty.equals("")) {
266             reloadableProperty = "true";
267         }
268         return new Boolean JavaDoc(reloadableProperty).booleanValue();
269     }
270     
271     /**
272      * Sets the reloadable
273      * @param reloadable The reloadable to set
274      */

275     public void setReloadable(boolean reloadable){
276         this.reloadable = reloadable;
277     }
278
279     /**
280      * Gets the redirectLogger
281      * @return Returns a boolean
282      */

283     public boolean getRedirectLogger(){
284         String JavaDoc redirectLoggerProperty = this.readProperty(KEY_REDIRECTLOGGER);
285         // Set default value to false
286
if(redirectLoggerProperty.equals("")) {
287             redirectLoggerProperty = "false";
288         }
289         return new Boolean JavaDoc(redirectLoggerProperty).booleanValue();
290     }
291     
292     /**
293      * Sets the redirectLogger
294      * @param redirectLogger The redirectLogger to set
295      */

296     public void setRedirectLogger(boolean redirectLogger){
297         this.redirectLogger = redirectLogger;
298     }
299     
300
301
302     /**
303      * Gets the srcdir
304      * @return Returns a String
305      */

306     public String JavaDoc getSrcDir(){
307         return this.readProperty(KEY_SRCDIR);
308     }
309
310     public String JavaDoc getSrcEJBDir(){
311         return this.readProperty(KEY_SRCEJBDIR);
312     }
313
314     /**
315      * Sets the srcdir
316      * @param srcdir The srcdir to set
317      */

318     public void setSrcDir(String JavaDoc srcdir){
319         this.srcDir = srcdir;
320     }
321
322     
323     public void setSrcEJBDir(String JavaDoc srcejbdir){
324         this.srcEJBDir = srcejbdir;
325     }
326
327     /**
328      * Gets the webdirName
329      * @return Returns a String
330      */

331     public String JavaDoc getWebdirName(){
332         return this.readProperty(KEY_WEBDIRNAME);
333     }
334     
335     /**
336      * Sets the webdirName
337      * @param webdirName The webdirName to set
338      */

339     public void setWebdirName(String JavaDoc webdirName){
340         this.webdirName = webdirName;
341     }
342
343
344     /**
345      * set the extra resources entries which shall be loaded
346      * @param entries List of Extra Resources Entry objects
347      */

348     public void setExtraResourcesEntries(ExtraResourcesEntries entries) {
349         extraResourcesEntries = entries;
350     }
351     
352     /**
353      * return the webclasspath entries
354      */

355     public ExtraResourcesEntries getExtraResourcesEntries() {
356         try {
357             return ExtraResourcesEntries.xmlUnmarshal(FileUtil.readTextFile(getPropertiesFile()));
358         } catch(IOException JavaDoc ioEx) {
359             return null;
360         }
361     }
362
363
364     /*
365      * Store exportSource in project persistent properties
366      */

367     public void saveProperties() {
368         try {
369             StringBuffer JavaDoc fileContent = new StringBuffer JavaDoc();
370             fileContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
371             fileContent.append("<jonasProjectProperties>\n");
372             fileContent.append(" <exportSource>" + exportSource + "</exportSource>\n");
373             fileContent.append(" <reloadable>" + reloadable + "</reloadable>\n");
374             fileContent.append(" <redirectLogger>" + redirectLogger + "</redirectLogger>\n");
375             fileContent.append(" <warLocation>" + warLocation + "</warLocation>\n");
376             fileContent.append(" <srcDir>" + srcDir + "</srcDir>\n");
377             fileContent.append(" <srcEJBDir>" + srcEJBDir + "</srcEJBDir>\n");
378             fileContent.append(" <webdirName>" + webdirName + "</webdirName>\n");
379             fileContent.append(" <webContext>" + webContext + "</webContext>\n");
380             if (packageEJB != null) {
381                 int i = 1;
382                 for (Enumeration JavaDoc e = packageEJB.elements() ; e.hasMoreElements() ;) {
383                     fileContent.append(" <ejb" + i + ">" + e.nextElement() + "</ejb" + i + ">\n");
384                     i++;
385                 }
386             }
387             if (jarEJBName != null) {
388                 int i = 1;
389                 for (Enumeration JavaDoc e = jarEJBName.elements() ; e.hasMoreElements() ;) {
390                     fileContent.append(" <ejbjarname" + i + ">" + e.nextElement() + "</ejbjarname" + i + ">\n");
391                     i++;
392                 }
393             }
394
395             if (extraResourcesEntries != null) {
396                 fileContent.append(extraResourcesEntries.xmlMarshal(4));
397             }
398             fileContent.append("</jonasProjectProperties>\n");
399             FileUtil.toTextFile(getPropertiesFile(), fileContent.toString());
400         } catch (Exception JavaDoc ex) {
401             JonasLauncherPlugin.log(ex.getMessage());
402         }
403     }
404
405     public void saveEJBProperties() {
406         try {
407             StringBuffer JavaDoc fileContent = new StringBuffer JavaDoc();
408             fileContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
409             fileContent.append("<jonasProjectProperties>\n");
410             fileContent.append(" <exportSource>" + getExportSource() + "</exportSource>\n");
411             fileContent.append(" <reloadable>" + getReloadable() + "</reloadable>\n");
412             fileContent.append(" <redirectLogger>" + getRedirectLogger() + "</redirectLogger>\n");
413             fileContent.append(" <warLocation>" + getWarLocation() + "</warLocation>\n");
414             fileContent.append(" <srcDir>" + getSrcDir() + "</srcDir>\n");
415             fileContent.append(" <srcEJBDir>" + getSrcEJBDir() + "</srcEJBDir>\n");
416             fileContent.append(" <webdirName>" + getWebdirName() + "</webdirName>\n");
417             fileContent.append(" <webContext>" + getWebContext() + "</webContext>\n");
418             if (packageEJB != null) {
419                 int i = 1;
420                 for (Enumeration JavaDoc e = packageEJB.elements() ; e.hasMoreElements() ;) {
421                     fileContent.append(" <ejb" + i + ">" + e.nextElement() + "</ejb" + i + ">\n");
422                     i++;
423                 }
424             }
425             if (jarEJBName != null) {
426                 int i = 1;
427                 for (Enumeration JavaDoc e = jarEJBName.elements() ; e.hasMoreElements() ;) {
428                     fileContent.append(" <ejbjarname" + i + ">" + e.nextElement() + "</ejbjarname" + i + ">\n");
429                     i++;
430                 }
431             }
432
433             if (getExtraResourcesEntries() != null) {
434                 fileContent.append(getExtraResourcesEntries().xmlMarshal(4));
435             }
436             fileContent.append("</jonasProjectProperties>\n");
437             FileUtil.toTextFile(getPropertiesFile(), fileContent.toString());
438         } catch (Exception JavaDoc ex) {
439             JonasLauncherPlugin.log(ex.getMessage());
440         }
441     }
442
443     /**
444      * Run all the steps to configure a JavaProject as a JonasProject
445      */

446     public void fullConfiguration() throws CoreException, IOException JavaDoc {
447
448         this.createWEBINFFolder();
449         this.createSrcFolder();
450
451         this.createWebSample();
452
453         this.addJonasJarToProjectClasspath();
454         this.addWEBINFLibJarFilesToProjectClasspath();
455
456         this.clearDefaultSourceEntries();
457         this.setClassesAsOutputFolder();
458         if(classesContainsJavaFiles()) {
459             this.setClassesAsSourceFolder();
460         }
461         this.setSrcAsSourceFolder(srcDir);
462
463     }
464
465     public void clearDefaultSourceEntries() throws CoreException {
466
467         IClasspathEntry[] entries = javaProject.getRawClasspath();
468         List JavaDoc cp= new ArrayList JavaDoc(entries.length + 1);
469         for(int i=0; i<entries.length; i++) {
470             if(entries[i].getEntryKind() != IClasspathEntry.CPE_SOURCE) {
471                 cp.add(entries[i]);
472             }
473         }
474         javaProject.setRawClasspath((IClasspathEntry[])cp.toArray(new IClasspathEntry[cp.size()]), null);
475     }
476
477     /*
478      * Add Jonas_RMI.jar, Jonas_JEREMIE.jar and web_catalina.jar to project classpath
479      */

480     public void addJonasJarToProjectClasspath() throws CoreException {
481
482         Vector JavaDoc jonasJarPath = JonasLauncherPlugin.getDefault().getJonasBootstrap().getJonasJarPath();
483         Vector JavaDoc jonasWebJarPath = JonasLauncherPlugin.getDefault().getJonasBootstrap().getJonasWebJarPath();
484
485
486         IClasspathEntry[] entries = javaProject.getRawClasspath();
487         List JavaDoc cp= new ArrayList JavaDoc(entries.length + 1);
488         cp.addAll(Arrays.asList(entries));
489
490         if(jonasJarPath != null) {
491             for(int i = 0; i < jonasJarPath.size();i++) {
492                 cp.add(JavaCore.newVariableEntry(JonasLauncherPlugin.getDefault().getJonasIPath().append((IPath) jonasJarPath.elementAt(i)), null, null));
493             }
494         }
495         if(jonasWebJarPath != null) {
496             for(int j = 0; j < jonasWebJarPath.size();j++) {
497                 cp.add(JavaCore.newVariableEntry(JonasLauncherPlugin.getDefault().getJonasIPath().append((IPath) jonasWebJarPath.elementAt(j)), null, null));
498             }
499         }
500
501
502         javaProject.setRawClasspath((IClasspathEntry[])cp.toArray(new IClasspathEntry[cp.size()]), null);
503     }
504     
505     public void removeJonasJarToProjectClasspath() throws CoreException {
506
507         IClasspathEntry[] entries = javaProject.getRawClasspath();
508         List JavaDoc cp = new ArrayList JavaDoc(entries.length + 1);
509         cp.addAll(Arrays.asList(entries));
510         Iterator JavaDoc cpIte = cp.iterator();
511         List JavaDoc newCp = new ArrayList JavaDoc();
512         
513         while (cpIte.hasNext()) {
514             IClasspathEntry pathEntry = (IClasspathEntry) cpIte.next();
515             String JavaDoc lastSeg = pathEntry.getPath().lastSegment();
516             JonasLauncherPlugin.log("Last seg = " + lastSeg);
517             if (lastSeg.equals("jonas.jar") ||
518                 lastSeg.equals("web_catalina.jar") ||
519                 lastSeg.equals("mail.jar") ||
520                 lastSeg.equals("jts-spec1_0.jar") ||
521                 lastSeg.equals("jta-spec1_0_1.jar") ||
522                 lastSeg.equals("jms.jar") ||
523                 lastSeg.equals("servlet-2_4.jar") ||
524                 lastSeg.equals("jsp-2_0.jar") ||
525                 lastSeg.equals("jacc-1_0.jar") ||
526                 lastSeg.equals("javax77.jar") ||
527                 lastSeg.equals("ejb-2_1-api.jar") ||
528                 lastSeg.equals("connector-1_5.jar") ||
529                 lastSeg.equals("activation.jar") ||
530                 lastSeg.equals("RMI_jonas.jar") ||
531                 lastSeg.equals("JEREMIE_jonas.jar") ||
532                 lastSeg.equals("jaxrpc.jar") ||
533                 lastSeg.equals("commons-discovery.jar") ||
534                 lastSeg.equals("saaj.jar") ||
535                 lastSeg.equals("commons-discovery.jar") ||
536                 lastSeg.equals("commons-logging-api.jar") ||
537                 lastSeg.equals("ow_jonas.jar") ||
538                 lastSeg.equals("ow_carol_core.jar") ||
539                 lastSeg.equals("jotm.jar") ||
540                 lastSeg.equals("axis.jar"))
541                 continue;
542             else
543                 newCp.add(pathEntry);
544             
545         }
546         javaProject.setRawClasspath((IClasspathEntry[])newCp.toArray(new IClasspathEntry[newCp.size()]), null);
547     }
548
549     
550     /*
551      * Add all jar in WEB-INF/lib to project classpath
552      */

553     public void addWEBINFLibJarFilesToProjectClasspath() throws CoreException {
554         IFolder libFolder = this.getWebInfFolder().getFolder("lib");
555         IResource[] libFiles = libFolder.members();
556
557         IClasspathEntry[] entries = javaProject.getRawClasspath();
558         List JavaDoc cp= new ArrayList JavaDoc(entries.length + 1);
559         cp.addAll(Arrays.asList(entries));
560         
561         for(int i=0; i<libFiles.length; i++) {
562             if((libFiles[i].getType() == IResource.FILE) && (libFiles[i].getFileExtension().equalsIgnoreCase("jar"))) {
563                 cp.add(JavaCore.newLibraryEntry(libFiles[i].getFullPath(), null, null));
564             }
565         }
566
567         javaProject.setRawClasspath((IClasspathEntry[])cp.toArray(new IClasspathEntry[cp.size()]), null);
568     }
569     
570     public IFolder getWebInfFolder() {
571         if (getWebDirFolder() == null) {
572             return project.getFolder("WEB-INF");
573         } else {
574             return getWebDirFolder().getFolder("WEB-INF");
575         }
576     }
577
578
579     //public IFolder getProjectFolder() {
580
// return new IFolder()
581
//}
582
public IFolder getWebDirFolder() {
583         if (webDirFolder == null) {
584             this.initWebDirFolder(false);
585         }
586         return webDirFolder;
587     }
588     
589     public IFolder getSrcDirFolder() {
590         if (srcDirFolder == null) {
591             this.initSrcDirFolder(false);
592         }
593         return srcDirFolder;
594     }
595
596     public IFolder getSrcEJBDirFolder() {
597         if (srcEJBDirFolder == null) {
598             this.initSrcEJBDirFolder(false);
599         }
600         return srcEJBDirFolder;
601     }
602
603
604     
605     private IFolder initWebDirFolder(boolean create) {
606
607         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(this.getWebdirName(), "/\\:");
608         IFolder folder = null;
609         try {
610             while (tokenizer.hasMoreTokens()) {
611                 String JavaDoc each = tokenizer.nextToken();
612                 if (folder == null) {
613                     folder = project.getFolder(each);
614                 } else {
615                     folder = folder.getFolder(each);
616                 }
617                 if (create) {
618                     this.createFolder(folder);
619                 }
620             }
621         } catch (CoreException ex) {
622             JonasLauncherPlugin.log(ex);
623             folder = null;
624             setWebdirName("/");
625         }
626         this.webDirFolder = folder;
627         return folder;
628     }
629
630     private IFolder initSrcDirFolder(boolean create) {
631
632         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(this.getSrcDir(), "/\\:");
633         IFolder folder = null;
634         try {
635             while (tokenizer.hasMoreTokens()) {
636                 String JavaDoc each = tokenizer.nextToken();
637                 if (folder == null) {
638                     folder = project.getFolder(each);
639                 } else {
640                     folder = folder.getFolder(each);
641                 }
642                 if (create) {
643                     this.createFolder(folder);
644                 }
645             }
646         } catch (CoreException ex) {
647             JonasLauncherPlugin.log(ex);
648             folder = null;
649         }
650         this.srcDirFolder = folder;
651         return folder;
652     }
653
654     private IFolder initSrcEJBDirFolder(boolean create) {
655         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(this.getSrcEJBDir(), "/\\:");
656         IFolder folder = null;
657         try {
658             while (tokenizer.hasMoreTokens()) {
659                 String JavaDoc each = tokenizer.nextToken();
660                 if (folder == null) {
661                     folder = project.getFolder(each);
662                 } else {
663                     folder = folder.getFolder(each);
664                 }
665                 if (create) {
666                     this.createFolder(folder);
667                 }
668             }
669         } catch (CoreException ex) {
670             JonasLauncherPlugin.log(ex);
671             folder = null;
672         }
673         this.srcEJBDirFolder = folder;
674         return folder;
675     }
676
677     public void createWEBINFFolder() throws CoreException {
678         IFolder webinfFolder = this.getWebInfFolder();
679         if ((webDirFolder != null) && (!(webDirFolder.getName().trim()).equals(".")))
680             this.createFolder(webDirFolder);
681         this.createFolder(webinfFolder);
682         this.createFolder(webinfFolder.getFolder("classes"));
683         this.createFolder(webinfFolder.getFolder("lib"));
684     }
685
686     public void createSrcFolder() throws CoreException {
687
688         //this.createFolder(this.getWebInfFolder().getFolder("src"));
689
IFolder srcdirFolder = this.getSrcDirFolder();
690         if (srcDirFolder != null)
691             this.createFolder(srcdirFolder);
692     }
693     
694     /**
695      * create sample html and java src files.
696      * copy the skeletons from the template directory,
697      * editing them as necessary.
698      * @author steve krupanszky, august 2003
699      *
700      * NOTE: The creation of these sample files should be
701      * based on "Create a Web Application" and/or
702      * "Create an EJB" being set, too.
703     */

704     public void createWebSample()
705     {
706         String JavaDoc template = null;
707         String JavaDoc webxmlTemplate = null;
708         IFile webxml = null;
709         if( ! createSamples ) {
710             template = TemplateDirUtil.getTemplateDir();
711             webxmlTemplate = template + "/web.xml.empty";
712             webxml = getWebInfFolder().getFile("web.xml");
713             createFile( webxmlTemplate, webxml );
714             return;
715         }
716
717         template = TemplateDirUtil.getTemplateDir();
718         webxmlTemplate = template + "/web.xml";
719         webxml = getWebInfFolder().getFile("web.xml");
720         createFile( webxmlTemplate, webxml );
721
722         String JavaDoc indexhtmlTemplate = template + "/index.html";
723         IFile indexhtml = getWebDirFolder().getFile("index.html");
724         createFile( indexhtmlTemplate, indexhtml );
725
726         String JavaDoc samplejspTemplate = template + "/sample-jsp.jsp";
727         IFile samplejsp = getWebDirFolder().getFile("sample-jsp.jsp");
728         createFile( samplejspTemplate, samplejsp );
729
730         String JavaDoc sampleservletTemplate = template + "/SampleServlet-src";
731         IFile sampleservlet = getSrcDirFolder().getFile("SampleServlet.java");
732         createFile( sampleservletTemplate, sampleservlet );
733         
734         // and create a skeleton jonas-web.xml file
735
try {
736             StringBuffer JavaDoc fileContent = new StringBuffer JavaDoc();
737             fileContent.append(
738                 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
739                 + "<!DOCTYPE jonas-web-app PUBLIC\n"
740                 + " \"-//ObjectWeb//DTD JOnAS Web App 2.6//EN\"\n"
741                 + " \"http://www.objectweb.org/jonas/dtds/jonas-web-app_2_6.dtd\">\n"
742                 + "\n"
743                 + "<jonas-web-app>\n"
744                 + "\t<context-root>" + webContext + "</context-root>\n"
745                 + "</jonas-web-app>\n"
746             );
747             IFile jonaswebxml = getWebInfFolder().getFile("jonas-web.xml");
748             JonasLauncherPlugin.log("JOPE: creating " + jonaswebxml.getFullPath().toString() );
749             jonaswebxml.create(new java.io.ByteArrayInputStream JavaDoc(fileContent.toString().getBytes()),false,null);
750         }
751         catch( Exception JavaDoc e ) {
752             JonasLauncherPlugin.log("JOPE: exception creating file: " + e );
753         }
754     }
755     
756     
757     public void createFile( String JavaDoc from, IFile to )
758     {
759         JonasLauncherPlugin.log("JOPE: copying " + from + " to " + to.getFullPath().toString() );
760         try {
761             InputStream JavaDoc stream = new FileInputStream JavaDoc(from);
762             to.create( stream, false, null );
763         }
764         catch( Exception JavaDoc e ) {
765             JonasLauncherPlugin.log("JOPE: exception copying file: " + e );
766         }
767     }
768
769
770     public Vector JavaDoc getPackageProjectFolder() {
771         readEJBPkgName();
772         return packageEJB;
773     }
774
775     private void readEJBPkgName() {
776         if ((packageEJB == null) || (jarEJBName == null)) {
777             int i = 1;
778             boolean isEJB = true;
779             String JavaDoc packEJB = null;
780             String JavaDoc jarName = null;
781             while (isEJB) {
782                 packEJB = this.readProperty("ejb" + i);
783                 jarName = this.readProperty("ejbjarname" + i);
784                 if (packEJB.equals("")) {
785                     isEJB = false;
786                 } else {
787                     if ((new File JavaDoc(this.getProject().getLocation().append(getSrcEJBDir()).append(packEJB).toOSString()).exists())) {
788                         if (packageEJB == null)
789                             packageEJB = new Vector JavaDoc();
790                         if (jarEJBName == null)
791                             jarEJBName = new Vector JavaDoc();
792                         packageEJB.add(packEJB);
793                         jarEJBName.add(jarName);
794                     }
795                 }
796                 i++;
797             }
798         } else {
799             for (Enumeration JavaDoc e = packageEJB.elements() ; e.hasMoreElements() ;) {
800                 String JavaDoc pkgEJB = (String JavaDoc)e.nextElement();
801                 if (!(new File JavaDoc(this.getProject().getLocation().append(getSrcEJBDir()).append(pkgEJB).toOSString()).exists())) {
802                     int i = packageEJB.indexOf(pkgEJB);
803                     packageEJB.remove(i);
804                     jarEJBName.remove(i);
805                 }
806             }
807         }
808         saveEJBProperties();
809     }
810     public Vector JavaDoc getJarEjbName() {
811         readEJBPkgName();
812         return jarEJBName;
813     }
814
815     public void setPackageProjectFolder(String JavaDoc packageDir) {
816         if (packageEJB == null)
817             packageEJB = new Vector JavaDoc();
818         packageDir = packageDir.replace(".".charAt(0),File.separatorChar);
819         JonasLauncherPlugin.log("package = " + packageDir);
820         packageEJB.add(packageDir);
821         saveEJBProperties();
822     }
823
824     public void setJarEjbName(String JavaDoc jarejbName) {
825         if (jarEJBName == null)
826             jarEJBName = new Vector JavaDoc();
827         JonasLauncherPlugin.log("jarEJBName = " + jarejbName);
828         jarEJBName.add(jarejbName);
829         saveEJBProperties();
830     }
831
832     public void setSrcAsSourceFolder(String JavaDoc srcFolder) throws CoreException {
833         this.setFolderAsSourceEntry(project.getFolder(srcFolder));
834     }
835
836     public String JavaDoc getOutputFolder() {
837         try {
838             String JavaDoc rootPath = project.getLocation().toOSString();
839             String JavaDoc classesPath = rootPath + File.separator + ".." + File.separator + javaProject.getOutputLocation().toOSString();
840             return classesPath;
841         } catch (CoreException ce) {
842             JonasLauncherPlugin.log("JOPE: exception getOutputFolder: " + ce );
843         }
844         return null;
845     }
846     
847     public void setClassesAsOutputFolder() throws CoreException {
848 // IFolder classesFolder = this.getWebInfFolder().getFolder("classes");
849
// javaProject.setOutputLocation(classesFolder.getFullPath(),null);
850

851         //IFolder prjFolder = project.getFolder(project.getLocation().toOSString());
852
//JonasLauncherPlugin.log("Project location = " + project.getLocation().toOSString());
853

854         this.createFolder(project.getFolder("classes"));
855         IFolder classesFolder = project.getFolder("classes");
856         javaProject.setOutputLocation(classesFolder.getFullPath(),null);
857
858         }
859
860     public void setClassesAsSourceFolder() throws CoreException {
861         IFolder classesFolder = this.getWebInfFolder().getFolder("classes");
862         this.setFolderAsSourceEntry(classesFolder);
863     }
864
865
866     private void createFolder(IFolder folderHandle) throws CoreException {
867         try {
868             // Create the folder resource in the workspace
869
folderHandle.create(false, true, null); //new SubProgressMonitor(monitor, 500));
870
}
871         catch (CoreException e) {
872             // If the folder already existed locally, just refresh to get contents
873
if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED)
874                 folderHandle.refreshLocal(IResource.DEPTH_INFINITE, null); //new SubProgressMonitor(monitor, 500));
875
else
876                 throw e;
877         }
878     }
879         
880     public void createSrcEJBFolder() throws CoreException {
881
882         //this.createFolder(this.getWebInfFolder().getFolder("src"));
883
IFolder srcEJBDirFolder = this.getSrcEJBDirFolder();
884         if (srcEJBDirFolder != null)
885             this.createFolder(srcEJBDirFolder);
886         setSrcAsSourceFolder(srcEJBDir);
887     }
888
889     private void setFolderAsSourceEntry(IFolder folderHandle) throws CoreException {
890         IClasspathEntry[] entries= javaProject.getRawClasspath();
891         IClasspathEntry[] newEntries= new IClasspathEntry[entries.length + 1];
892         System.arraycopy(entries,0, newEntries,0, entries.length);
893         newEntries[entries.length] = JavaCore.newSourceEntry(folderHandle.getFullPath());
894         javaProject.setRawClasspath(newEntries, null);
895     }
896
897
898
899     public void hotDeploy() throws CoreException {
900         String JavaDoc baseDir = JonasLauncherPlugin.getDefault().getBaseDir();
901         File JavaDoc directoryWeb = null;
902         if (getWebdirName().trim().equals(""))
903             directoryWeb = new File JavaDoc(project.getDescription().getLocation().toOSString());
904         else
905             directoryWeb = new File JavaDoc(getWebDirFolder().getLocation().toOSString());
906         File JavaDoc directorySrc = null;
907         if (this.getSrcDir().trim().equals(""))
908                 directorySrc = new File JavaDoc(project.getDescription().getLocation().toOSString());
909             else
910                 directorySrc = new File JavaDoc(getSrcDirFolder().getLocation().toOSString());
911                                                         
912         HotDeploy hot = new HotDeploy(baseDir, new Path(directoryWeb.toString()),getWebContext());
913         hot.copyFileByAnt();
914         
915     }
916
917     /*
918      * if WEB-INF classes contains Java files add it to source folders
919      * Otherwise Eclipse will delete all those files
920      */

921     private boolean classesContainsJavaFiles() {
922         IFolder webinfFolder = this.getWebInfFolder();
923         IFolder classesFolder = webinfFolder.getFolder("classes");
924         File JavaDoc f = classesFolder.getLocation().toFile();
925                  
926         if (!f.exists()) return false;
927         if (!f.isDirectory()) return false;
928     
929         return FileUtil.dirContainsFiles(f, "java", true);
930     }
931
932     public void setCreateSamples( boolean b )
933     {
934         createSamples = b;
935     }
936
937     public String JavaDoc getSrcPersistenceDir() {
938         return srcPersistenceDir;
939     }
940     public void setSrcPersistenceDir(String JavaDoc srcPersistenceDir) {
941         this.srcPersistenceDir = srcPersistenceDir;
942     }
943 }
944
Popular Tags