KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > jbuilder > node > PrimeProject


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.jbuilder.node;
24
25 // ToolBox
26
import org.enhydra.tool.ToolBoxInfo;
27 import org.enhydra.tool.common.PathHandle;
28 import org.enhydra.tool.common.ToolException;
29
30 // JBuilder
31
import com.borland.jbuilder.node.JBProject;
32 import com.borland.jbuilder.node.HTMLFileNode;
33 import com.borland.jbuilder.paths.PathSet;
34 import com.borland.jbuilder.paths.PathSetManager;
35 import com.borland.jbuilder.paths.ProjectPathSet;
36 import com.borland.primetime.ide.Browser;
37 import com.borland.primetime.node.Node;
38 import com.borland.primetime.node.Project;
39 import com.borland.primetime.node.FileNode;
40 import com.borland.primetime.node.TextFileNode;
41 import com.borland.primetime.vfs.Url;
42 import com.borland.jbuilder.paths.PathSetManager;
43
44
45 // AddinCore
46
import org.enhydra.kelp.common.Constants;
47 import org.enhydra.kelp.common.PathUtil;
48 import org.enhydra.kelp.common.deployer.RunParameters;
49 import org.enhydra.kelp.common.node.OtterDocumentNode;
50 import org.enhydra.kelp.common.node.OtterFileNode;
51 import org.enhydra.kelp.common.node.OtterNode;
52 import org.enhydra.kelp.common.node.OtterTextFileNode;
53 import org.enhydra.kelp.common.node.OtterProject;
54 import org.enhydra.kelp.common.node.OtterNodeFactory;
55 import org.enhydra.kelp.common.node.PropertyKeys;
56
57 // JDK
58
import java.io.File JavaDoc;
59 import java.util.ArrayList JavaDoc;
60 import java.lang.reflect.Method JavaDoc;
61
62 //import org.enhydra.dods.generator.DODSGenerator;
63

64 /**
65  * Class declaration
66  *
67  *
68  * @author
69  */

70 public class PrimeProject extends OtterProject {
71     private JBProject nativeProject = null;
72     private PrimeNodeFactory factory = null;
73
74     /**
75      * Constructor declaration
76      *
77      *
78      * @param project
79      */

80     public PrimeProject(JBProject p) {
81         nativeProject = p;
82         factory = new PrimeNodeFactory();
83 // generator = new DODSGenerator();
84
}
85
86     public boolean isDefaultProject() {
87         boolean def = false;
88
89         if (getNativeProject() == null) {
90
91             //
92
} else {
93             def = Browser.getDefaultProject().equals(getNativeProject());
94         }
95         return def;
96     }
97
98     public String JavaDoc getCodeGenDefaultDestination() {
99         PathHandle ph = null;
100         File JavaDoc file = null;
101
102         file = new File JavaDoc(getRootPath());
103         if (file.getParentFile() == null) {
104             ph = PathHandle.createPathHandle(file);
105         } else {
106             ph = PathHandle.createPathHandle(file.getParentFile());
107         }
108         return ph.getPath();
109     }
110
111     /**
112      * Method declaration
113      *
114      *
115      * @return
116      */

117     public OtterTextFileNode getFirstPolicy() {
118         PolicyNodeReader reader = new PolicyNodeReader();
119
120         return reader.getFirstNode(nativeProject);
121     }
122
123     public String JavaDoc getWorkingPath() {
124         String JavaDoc path = new String JavaDoc();
125         File JavaDoc file = null;
126
127         path = getRootPath();
128         path = nativeProject.getProperty(PropertyKeys.NAME_WORKING_DIR, path);
129         if (path.indexOf("%|") == 1) { // nores
130
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
131
132             buf.append(path.charAt(0));
133             buf.append(':');
134             buf.append(path.substring(3));
135             path = buf.toString();
136         }
137         path = PathUtil.expandPathRelativeToProject(this, path);
138         path = PathHandle.createPathString(path);
139         return path;
140     }
141
142     public void setWorkingPath(String JavaDoc inPath) {
143         PathHandle handle = null;
144         ProjectPathSet pathSet = null;
145         Method JavaDoc setWD = null;
146         Method JavaDoc save = null;
147         Class JavaDoc[] paramClasses = new Class JavaDoc[0];
148         Url[] paramValues = new Url[0];
149
150         handle = PathHandle.createPathHandle(inPath);
151         pathSet = nativeProject.getPaths();
152         try {
153             paramValues = new Url[1];
154             paramValues[0] = new Url(handle.getFile());
155             paramClasses = new Class JavaDoc[1];
156             paramClasses[0] = paramValues[0].getClass();
157             setWD = pathSet.getClass().getMethod("setWorkingDirectory",
158                                                  paramClasses); // nores
159
setWD.invoke(pathSet, paramValues);
160             paramClasses = new Class JavaDoc[0];
161             paramValues = new Url[0];
162             save = pathSet.getClass().getMethod("save", paramClasses); // nores
163
save.invoke(pathSet, paramValues);
164         } catch (Exception JavaDoc e) {
165
166             // if exception, assume jb3
167
String JavaDoc compress = new String JavaDoc();
168
169             compress = PathUtil.compressPathRelativeToProject(this,
170                     handle.getPath());
171             nativeProject.setProperty(PropertyKeys.NAME_WORKING_DIR,
172                                       compress);
173         }
174     }
175
176     /**
177      * Get all nodes from input directory.
178      * implements OtterProject
179      */

180     public OtterFileNode[] getAllInput() {
181         String JavaDoc[] ext = new String JavaDoc[0];
182         OtterFileNode[] input = new OtterFileNode[0];
183         OtterFileNode[] nodes = new OtterFileNode[0];
184         ArrayList JavaDoc list = new ArrayList JavaDoc();
185         PathHandle inputPath = null;
186         PathHandle cursorPath = null;
187
188         inputPath = PathHandle.createPathHandle(getDeployInputPath());
189         if (isDeployInputFilter()) {
190             ext = new String JavaDoc[1];
191             ext[0] = Constants.TYPE_IN;
192         }
193         nodes = findFileNodesByType(ext);
194         for (int i = 0; i < nodes.length; i++) {
195             cursorPath = PathHandle.createPathHandle(nodes[i].getFilePath());
196             if (inputPath.parentOf(cursorPath)) {
197                 list.add(nodes[i]);
198             }
199         }
200         input = new OtterFileNode[list.size()];
201         input = (OtterFileNode[]) list.toArray(input);
202         list.clear();
203         return input;
204     }
205
206     /**
207      * Method declaration
208      *
209      *
210      * @param extensions
211      *
212      * @return
213      */

214     public OtterFileNode[] findFileNodesByType(String JavaDoc[] extensions) {
215         NodeReader reader = new NodeReader(extensions);
216
217         return reader.getNodes(nativeProject);
218     }
219
220     /**
221      * Method declaration
222      *
223      *
224      * @return
225      */

226     public OtterTextFileNode[] getAllDeploymentDescs() {
227         DeploymentNodeReader reader = new DeploymentNodeReader();
228
229         return reader.getNodes(nativeProject);
230     }
231
232     /**
233      * Method declaration
234      *
235      *
236      * @return
237      */

238     public OtterDocumentNode[] getAllDocuments() {
239         DocumentNodeReader reader = new DocumentNodeReader();
240
241         return reader.getNodes(this);
242     }
243
244     /**
245      * Method declaration
246      *
247      *
248      * @return
249      */

250     public Object JavaDoc getNativeProject() {
251         return nativeProject;
252     }
253
254     /**
255      * Method declaration
256      *
257      *
258      * @param o
259      */

260     public void setNativeProject(Object JavaDoc o) {
261         nativeProject = (JBProject) o;
262     }
263
264     /**
265      * Method declaration
266      *
267      *
268      * @return
269      */

270     public Object JavaDoc getNativeNode() {
271         return getNativeProject();
272     }
273
274     /**
275      * Method declaration
276      *
277      *
278      * @param o
279      */

280     public void setNativeNode(Object JavaDoc o) {
281         setNativeProject(o);
282     }
283
284     /**
285      * Method declaration
286      *
287      *
288      * @return
289      */

290     public OtterProject getProject() {
291         return this;
292     }
293
294     public OtterNode getParent() {
295         return null;
296     }
297
298     /**
299      * Method declaration
300      *
301      *
302      * @return
303      */

304     public OtterNodeFactory getNodeFactory() {
305         return factory;
306     }
307
308     /**
309      * Method declaration
310      *
311      *
312      * @return
313      */

314     public boolean isOpenBuild() {
315         return true;
316     }
317
318     /**
319      * Method declaration
320      *
321      *
322      * @return
323      */

324     public String JavaDoc getClassOutputPath() {
325         String JavaDoc out = File.separator + Constants.DIR_CLASSES;
326
327         if (nativeProject != null) {
328             out = nativeProject.getPaths().getOutPath().getFile();
329         }
330
331         // Make match current OS.
332
out.replace('\\', File.separatorChar);
333         out.replace('/', File.separatorChar);
334         return out;
335     }
336
337     public void setClassOutputPath(String JavaDoc p) {
338         Url classOut = null;
339
340         classOut = new Url(new File JavaDoc(p));
341         nativeProject.getPaths().setOutPath(classOut);
342     }
343
344     /**
345      * Method declaration
346      *
347      *
348      * @return
349      */

350     public String JavaDoc getSourcePath() {
351         StringBuffer JavaDoc path = new StringBuffer JavaDoc();
352         Url urlSet[];
353
354         if (nativeProject != null) {
355             urlSet = nativeProject.getPaths().getSourcePath();
356             for (int i = 0; i < urlSet.length; i++) {
357                 if (path.length() > 0) {
358                     path.append('/');
359                 }
360                 if (i > 0) {
361                     path.append(';');
362                 }
363                 path.append(PathHandle.createPathHandle(urlSet[i].getFile()));
364             }
365         }
366         return path.toString();
367     }
368
369     /**
370      * Method declaration
371      *
372      *
373      * @param pathAsArray
374      */

375     public void setSourcePathArray(String JavaDoc[] pathAsArray) {
376         Url urlSet[] = new Url[pathAsArray.length];
377
378         for (int i = 0; i < pathAsArray.length; i++) {
379             urlSet[i] = new Url(new File JavaDoc(pathAsArray[i]));
380         }
381         nativeProject.getPaths().setSourcePath(urlSet);
382     }
383
384     /**
385      * Method declaration
386      *
387      *
388      * @return
389      */

390     public String JavaDoc[] getSourcePathArray() {
391         String JavaDoc[] pathAsArray = null;
392         Url urlSet[];
393
394         if (nativeProject != null) {
395             urlSet = nativeProject.getPaths().getSourcePath();
396             pathAsArray = new String JavaDoc[urlSet.length];
397             for (int i = 0; i < urlSet.length; i++) {
398                 pathAsArray[i] =
399                     PathHandle.createPathString(urlSet[i].getFile());
400             }
401         }
402         return pathAsArray;
403     }
404
405     /**
406      * Method declaration
407      *
408      *
409      * @return
410      */

411     public String JavaDoc getClassPath() {
412         PathSet pathSet;
413         Url urlSet[];
414         String JavaDoc path = null;
415         String JavaDoc libs = null;
416
417         if (nativeProject != null) {
418             libs = getProperty(JBProject.PROPERTY_LIBRARIES);
419             pathSet = PathSetManager.getLibrary(libs);
420             urlSet = pathSet.getFullClassPath();
421             path = Url.getPath(urlSet);
422         }
423         return path;
424     }
425
426     /**
427      * Method declaration
428      *
429      *
430      * @param property
431      *
432      * @return
433      */

434     public String JavaDoc getProperty(String JavaDoc property) {
435         String JavaDoc out = null;
436
437         if (nativeProject != null) {
438             out = nativeProject.getProperty(property);
439         }
440         return out;
441     }
442
443     /**
444      * Method declaration
445      *
446      *
447      * @param property
448      * @param value
449      */

450     public void setProperty(String JavaDoc property, String JavaDoc value) {
451         if (nativeProject != null) {
452             if (property.trim().equalsIgnoreCase(PropertyKeys.NAME_LIBRARIES)
453                     && value.trim().equalsIgnoreCase(PropertyKeys.VALUE_ENHYDRA)) {
454                 nativeProject.getPaths().putClassOnFullPath(ToolBoxInfo.getEnhydraMainClass());
455             } else {
456                 nativeProject.setProperty(property, value);
457             }
458         }
459     }
460
461     /**
462      * Method declaration
463      *
464      *
465      * @param property
466      * @param i
467      */

468     public void setProperty(String JavaDoc property, int i) {
469         String JavaDoc value = Integer.toString(i);
470
471         setProperty(property, value);
472     }
473
474     /**
475      * Method declaration
476      *
477      *
478      * @return
479      */

480     public String JavaDoc getRootPath() {
481         PathHandle ph = null;
482
483         ph =
484             PathHandle.createPathHandle(nativeProject.getUrl().getFileObject());
485         ph = ph.getParent();
486         return ph.getPath();
487     }
488
489     /**
490      * Method declaration
491      *
492      *
493      * @return
494      */

495     public char getFileSeparatorChar() {
496         return '/';
497     }
498
499     /**
500      * Method declaration
501      *
502      */

503     public void configureRunClass() {
504         RunParameters param = new RunParameters(this);
505
506         // strings not to be resourced
507
nativeProject.setProperty("runtime.0", "RunnableType", // nores
508
"com.borland.jbuilder.runtime.ApplicationRunner"); // nores
509
nativeProject.setProperty("runtime.0", "application.class", // nores
510
ToolBoxInfo.getEnhydraMainClass());
511         nativeProject.setProperty("runtime.0",
512                                   "application.parameters", // nores
513
param.getAppParameters());
514         //DACHA 17.05.2003
515
nativeProject.setProperty("runtime.0", // nores
516
"application.vmparameters", // nores
517
param.getVMParameters());
518         //END DACHA
519
if (!ToolBoxInfo.isEnhydra3()) {
520             nativeProject.setProperty("runtime.0", // nores
521
"application.vmparameters", // nores
522
param.getVMParameters());
523         }
524     }
525
526     public void save() {
527         Project project = null;
528
529         if (getNativeNode() instanceof Project) {
530             project = (Project) getNativeNode();
531             try {
532                 project.save();
533             } catch (Exception JavaDoc e) {
534                 e.printStackTrace();
535             }
536         }
537     }
538
539     /**
540      * Method declaration
541      *
542      *
543      * @return
544      */

545     public void setProjectJavaPath(JBProject jbproj) {
546         String JavaDoc javaPath =
547           ""+PathSetManager.getJDK(jbproj.getProperty("JDK")).
548             getHomePath().getFile();
549 //add comment Sinisa - Linux problem
550
// javaPath = javaPath.substring(javaPath.indexOf("%|")-1,
551
// javaPath.length());
552
// javaPath = javaPath.replace('%',':');
553
// javaPath = javaPath.substring(0, javaPath.indexOf("|"))+
554
// javaPath.substring(javaPath.indexOf("|")+1, javaPath.length());
555
setProperty(DODS_PROJECT_JAVA_PATH, javaPath);
556     }
557
558     /**
559      * Method declaration
560      *
561      *
562      * @return
563      */

564     public void setEnhydraPath(JBProject jbproj) {
565         String JavaDoc javaPath =
566           PathSetManager.getLibrary("Enhydra").getClassPath()[0].getFile();
567         javaPath = javaPath.substring(1, javaPath.indexOf("/lib"));
568         setProperty(DODS_ENHYDRA_PATH, javaPath);
569     }
570
571     }
572
573 /**
574  * Class declaration
575  *
576  *
577  * @author Paul Mahar
578  */

579 class DeploymentNodeReader {
580     private ArrayList JavaDoc list = new ArrayList JavaDoc();
581
582     /**
583      * Constructor declaration
584      *
585      */

586     protected DeploymentNodeReader() {}
587
588     /**
589      * Method declaration
590      *
591      *
592      * @param nativeProject
593      *
594      * @return
595      */

596     protected OtterTextFileNode[] getNodes(JBProject nativeProject) {
597         OtterTextFileNode[] nodes;
598
599         recursiveRead(nativeProject);
600         nodes = new OtterTextFileNode[list.size()];
601         for (int i = 0; i < nodes.length; i++) {
602             nodes[i] = (OtterTextFileNode) list.get(i);
603         }
604         return nodes;
605     }
606
607     /**
608      * Method declaration
609      *
610      *
611      * @param container
612      */

613     private void recursiveRead(Node container) {
614         String JavaDoc filename = null;
615         Node[] nativeNodes = container.getChildren();
616         PathHandle current = null;
617
618         for (int i = 0; i < nativeNodes.length; i++) {
619             TextFileNode nativeFile = null;
620
621             if (nativeNodes[i] instanceof TextFileNode) {
622                 nativeFile = (TextFileNode) nativeNodes[i];
623                 filename = nativeFile.getUrl().getFile();
624                 current = PathHandle.createPathHandle(filename);
625                 if (current.isFile()
626                         && (current.endsWith(File.separator + Constants.FILE_WEB_XML)
627                             || current.endsWith(File.separator
628                                                 + Constants.FILE_EJB_JAR_XML))) {
629                     PrimeTextFileNode primeNode = null;
630
631                     primeNode = new PrimeTextFileNode(nativeFile);
632                     list.add(primeNode);
633                 }
634             }
635             recursiveRead(nativeNodes[i]);
636         }
637     }
638
639 }
640
641 /**
642  * Class declaration
643  *
644  *
645  * @author Paul Mahar
646  */

647 class DocumentNodeReader {
648     private ArrayList JavaDoc list = new ArrayList JavaDoc();
649     private OtterProject otterProject = null;
650     private String JavaDoc[] docTypes = new String JavaDoc[0];
651
652     /**
653      * Constructor declaration
654      *
655      */

656     protected DocumentNodeReader() {}
657
658     /**
659      * Method declaration
660      *
661      *
662      * @param nativeProject
663      *
664      * @return
665      */

666     protected OtterDocumentNode[] getNodes(OtterProject p) {
667         OtterDocumentNode[] nodes = new OtterDocumentNode[0];
668
669         otterProject = p;
670         docTypes = otterProject.getDocTypes();
671         list.clear();
672         recursiveRead((JBProject) otterProject.getNativeProject());
673         nodes = new OtterDocumentNode[list.size()];
674         nodes = (OtterDocumentNode[]) list.toArray(nodes);
675         list.clear();
676         return nodes;
677     }
678
679     /**
680      * Method declaration
681      *
682      *
683      * @param container
684      */

685     private void recursiveRead(Node container) {
686         Node[] nativeNodes = container.getChildren();
687         PathHandle current = null;
688
689         for (int i = 0; i < nativeNodes.length; i++) {
690             FileNode nativeFile = null;
691
692             if (nativeNodes[i] instanceof FileNode) {
693                 nativeFile = (FileNode) nativeNodes[i];
694                 current =
695                     PathHandle.createPathHandle(nativeFile.getUrl().getFileObject());
696                 if (current.getFile().exists()
697                         && current.hasExtension(docTypes)) {
698                     OtterDocumentNode docNode = null;
699
700                     docNode = new PrimeDocumentNode(nativeFile);
701                     list.add(docNode);
702                 }
703             }
704             recursiveRead(nativeNodes[i]);
705         }
706     }
707
708 }
709 class NodeReader {
710     private ArrayList JavaDoc list = new ArrayList JavaDoc();
711     private String JavaDoc[] extensions = new String JavaDoc[0];
712
713     /**
714      * Constructor declaration
715      *
716      *
717      * @param exts
718      */

719     protected NodeReader(String JavaDoc[] exts) {
720         extensions = exts;
721     }
722
723     /**
724      * Method declaration
725      *
726      *
727      * @param nativeProject
728      *
729      * @return
730      */

731     protected OtterFileNode[] getNodes(JBProject nativeProject) {
732         OtterFileNode[] nodes;
733
734         recursiveRead(nativeProject);
735         nodes = new OtterFileNode[list.size()];
736         for (int i = 0; i < nodes.length; i++) {
737             if (list.get(i) instanceof OtterTextFileNode) {
738                 nodes[i] = (OtterTextFileNode) list.get(i);
739             } else {
740                 nodes[i] = (OtterFileNode) list.get(i);
741             }
742         }
743         return nodes;
744     }
745
746     /**
747      * Method declaration
748      *
749      *
750      * @param container
751      */

752     private void recursiveRead(Node container) {
753         String JavaDoc filename = null;
754         Node[] nativeNodes = container.getChildren();
755         PathHandle current = null;
756
757         for (int i = 0; i < nativeNodes.length; i++) {
758             FileNode nativeFile = null;
759
760             if (nativeNodes[i] instanceof FileNode) {
761                 nativeFile = (FileNode) nativeNodes[i];
762                 filename = nativeFile.getUrl().getFile();
763                 current = PathHandle.createPathHandle(filename);
764                 if (current.getFile().exists()) {
765                     if (current.hasExtension(extensions)
766                             || (extensions.length == 0)) {
767                         PrimeNode primeNode = null;
768
769                         if (nativeFile instanceof TextFileNode) {
770                             primeNode =
771                                 new PrimeTextFileNode((TextFileNode) nativeFile);
772                         } else {
773                             primeNode = new PrimeNode(nativeFile);
774                         }
775                         list.add(primeNode);
776                     }
777                 }
778             }
779             recursiveRead(nativeNodes[i]);
780         }
781     }
782
783 }
784
785 /**
786  * Class declaration
787  *
788  *
789  * @author Paul Mahar
790  */

791 class PolicyNodeReader {
792     private OtterTextFileNode node = null;
793
794     /**
795      * Method declaration
796      *
797      *
798      * @param nativeProject
799      *
800      * @return
801      */

802     protected OtterTextFileNode getFirstNode(JBProject nativeProject) {
803         recursiveRead(nativeProject);
804         return node;
805     }
806
807     /**
808      * Method declaration
809      *
810      *
811      * @param container
812      */

813     private void recursiveRead(Node container) {
814         String JavaDoc filename = null;
815         Node[] nativeNodes = container.getChildren();
816         PathHandle current = null;
817
818         for (int i = 0; i < nativeNodes.length; i++) {
819             TextFileNode nativeFile = null;
820
821             if (nativeNodes[i] instanceof TextFileNode) {
822                 nativeFile = (TextFileNode) nativeNodes[i];
823                 filename = nativeFile.getUrl().getFile();
824                 current = PathHandle.createPathHandle(filename);
825                 if (current.isFile()
826                         && current.endsWith(Constants.FILE_JAVA_POLICY)) {
827                     node = new PrimeTextFileNode(nativeFile);
828                     break;
829                 }
830             }
831             recursiveRead(nativeNodes[i]);
832         }
833     }
834
835 }
836
Popular Tags