KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ejb > JonasDeploymentTool


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.ejb;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import javax.xml.parsers.SAXParser JavaDoc;
25 import org.apache.tools.ant.AntClassLoader;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.taskdefs.Java;
29 import org.apache.tools.ant.types.Path;
30
31 /**
32  * The deployment tool to add the jonas specific deployment descriptors to the
33  * ejb JAR file. JONAS only requires one additional file jonas-ejb-jar.xml.
34  *
35  * @version 1.0
36  * @see EjbJar#createJonas
37  */

38 public class JonasDeploymentTool extends GenericDeploymentTool {
39
40     /** Public Id of the standard deployment descriptor DTD. */
41     protected static final String JavaDoc EJB_JAR_1_1_PUBLIC_ID
42         = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN";
43     protected static final String JavaDoc EJB_JAR_2_0_PUBLIC_ID
44         = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN";
45
46     /** Public Id of the JOnAS-specific deployment descriptor DTD. */
47     protected static final String JavaDoc JONAS_EJB_JAR_2_4_PUBLIC_ID
48         = "-//ObjectWeb//DTD JOnAS 2.4//EN";
49     protected static final String JavaDoc JONAS_EJB_JAR_2_5_PUBLIC_ID
50         = "-//ObjectWeb//DTD JOnAS 2.5//EN";
51
52     /** RMI ORB. */
53     protected static final String JavaDoc RMI_ORB = "RMI";
54
55     /** JEREMIE ORB. */
56     protected static final String JavaDoc JEREMIE_ORB = "JEREMIE";
57
58     /** DAVID ORB. */
59     protected static final String JavaDoc DAVID_ORB = "DAVID";
60
61     /**
62      * Name of the standard deployment descriptor DTD (these files are stored in
63      * the ${JONAS_ROOT}/xml directory).
64      */

65     protected static final String JavaDoc EJB_JAR_1_1_DTD = "ejb-jar_1_1.dtd";
66     protected static final String JavaDoc EJB_JAR_2_0_DTD = "ejb-jar_2_0.dtd";
67
68     /**
69      * Name of the JOnAS-specific deployment descriptor DTD (these files are
70      * stored in the ${JONAS_ROOT}/xml directory).
71      */

72     protected static final String JavaDoc JONAS_EJB_JAR_2_4_DTD
73         = "jonas-ejb-jar_2_4.dtd";
74     protected static final String JavaDoc JONAS_EJB_JAR_2_5_DTD
75         = "jonas-ejb-jar_2_5.dtd";
76
77     /** Default JOnAS deployment descriptor name. */
78     protected static final String JavaDoc JONAS_DD = "jonas-ejb-jar.xml";
79
80     /** GenIC class name (JOnAS 2.5) */
81     protected static final String JavaDoc GENIC_CLASS =
82     "org.objectweb.jonas_ejb.genic.GenIC";
83
84     /** Old GenIC class name (JOnAS 2.4.x). */
85     protected static final String JavaDoc OLD_GENIC_CLASS_1 =
86         "org.objectweb.jonas_ejb.tools.GenWholeIC";
87
88     /** Old GenIC class name. */
89     protected static final String JavaDoc OLD_GENIC_CLASS_2 =
90         "org.objectweb.jonas_ejb.tools.GenIC";
91
92     /**
93      * Filename of the standard EJB descriptor (which is passed to this class
94      * from the parent "ejbjar" task). This file is relative to the directory
95      * specified by the "srcdir" attribute in the ejbjar task.
96      */

97     private String JavaDoc descriptorName;
98
99     /**
100      * Filename of the JOnAS-specific EJB descriptor (which is passed to this
101      * class from the parent "ejbjar" task). This file is relative to the
102      * directory specified by the "srcdir" attribute in the ejbjar task.
103      */

104     private String JavaDoc jonasDescriptorName;
105
106     /* ------------- */
107     /* GenIC options */
108     /* ------------- */
109
110     /**
111      * Temporary output directory used by GenIC.
112      */

113     private File JavaDoc outputdir;
114
115     /**
116      * <code>true</code> if the intermediate Java source files generated by
117      * GenIC must be deleted or not. The default is <code>false</code>
118      */

119     private boolean keepgenerated = false;
120
121     /**
122      * <code>true</code> if the generated source files must not be compiled via
123      * the java and rmi compilers. The default is <code>false</code>.
124      */

125     private boolean nocompil = false;
126
127     /**
128      * <code>true</code> if the XML deployment descriptors must be parsed
129      * without validation. The default is <code>false</code>.
130      */

131     private boolean novalidation = false;
132
133     /**
134      * Java compiler to use. The default is the value of
135      * <code>build.compiler</code> property.
136      */

137     private String JavaDoc javac;
138
139     /** Options to pass to the java compiler. */
140     private String JavaDoc javacopts;
141
142     /** Options to pass to the rmi compiler. */
143     private String JavaDoc rmicopts;
144
145     /**
146      * Whether or not the RMI skeleton and stub must be modified to
147      * implement the implicit propagation of the security context (the
148      * transactional context is always provided). The default is
149      * <code>false</code>.
150      */

151     private boolean secpropag = false;
152
153     /**
154      * <code>true</code> if the GenIC call must be verbose. The default
155      * is <code>false</code>.
156      */

157     private boolean verbose = false;
158
159     /** Additional args to send to GenIC. */
160     private String JavaDoc additionalargs;
161
162     /* ------------- */
163     /* other options */
164     /* ------------- */
165
166     /** JOnAS root directory. */
167     private File JavaDoc jonasroot;
168
169     /**
170      * <code>true</code> if the generic JAR file used as input to GenIC must be
171      * retained. The default is <code>false</code>.
172      */

173     private boolean keepgeneric = false;
174
175     /** Stores the suffix for the JOnAS JAR file. The default is '.jar'. */
176     private String JavaDoc suffix = ".jar";
177
178     /**
179      * ORB to use (RMI, JEREMIE or DAVID). If omitted, it defaults to the one
180      * present in classpath. If specified, the corresponding JOnAS JAR is
181      * automatically added to the classpath.
182      */

183     private String JavaDoc orb;
184
185     /**
186      * <code>true</code> if GenIC must not be run on the EJB JAR.
187      * The default is <code>false</code>.
188      */

189     private boolean nogenic = false;
190
191     /* -------------------- */
192     /* GenIC options setter */
193     /* -------------------- */
194
195     /**
196      * Sets the <code>keepgenerated</code> flag.
197      *
198      * @param aBoolean <code>true</code> if the flag must be set.
199      */

200     public void setKeepgenerated(boolean aBoolean) {
201         keepgenerated = aBoolean;
202     }
203
204     /**
205      * Sets the additional arguments.
206      *
207      * @param aString additional args.
208      */

209     public void setAdditionalargs(String JavaDoc aString) {
210         additionalargs = aString;
211     }
212
213     /**
214      * Sets the <code>nocompil</code> flag.
215      *
216      * @param aBoolean <code>true</code> if the flag must be set.
217      */

218     public void setNocompil(boolean aBoolean) {
219         nocompil = aBoolean;
220     }
221
222     /**
223      * Sets the <code>novalidation</code> flag.
224      *
225      * @param aBoolean <code>true</code> if the flag must be set.
226      */

227     public void setNovalidation(boolean aBoolean) {
228         novalidation = aBoolean;
229     }
230
231     /**
232      * Sets the java compiler to use.
233      *
234      * @param aString the java compiler.
235      */

236     public void setJavac(String JavaDoc aString) {
237         javac = aString;
238     }
239
240     /**
241      * Set the options to pass to the java compiler.
242      *
243      * @param aString the options.
244      */

245     public void setJavacopts(String JavaDoc aString) {
246         javacopts = aString;
247     }
248
249     /**
250      * Set the options to pass to the rmi compiler.
251      *
252      * @param aString the options.
253      */

254     public void setRmicopts(String JavaDoc aString) {
255         rmicopts = aString;
256     }
257
258     /**
259      * Sets the <code>secpropag</code> flag.
260      *
261      * @param aBoolean <code>true</code> if the flag must be set.
262      */

263     public void setSecpropag(boolean aBoolean) {
264         secpropag = aBoolean;
265     }
266
267     /**
268      * Sets the <code>verbose</code> flag.
269      *
270      * @param aBoolean <code>true</code> if the flag must be set.
271      */

272     public void setVerbose(boolean aBoolean) {
273         verbose = aBoolean;
274     }
275
276     /* -------------------- */
277     /* other options setter */
278     /* -------------------- */
279
280     /**
281      * Set the JOnAS root directory.
282      *
283      * @param aFile the JOnAS root directory.
284      */

285     public void setJonasroot(File JavaDoc aFile) {
286         jonasroot = aFile;
287     }
288
289     /**
290      * Sets the <code>keepgeneric</code> flag.
291      *
292      * @param aBoolean <code>true</code> if the flag must be set.
293      */

294     public void setKeepgeneric(boolean aBoolean) {
295         keepgeneric = aBoolean;
296     }
297
298     /**
299      * Sets the jar suffix.
300      *
301      * @param aString the string to use as the suffix.
302      */

303     public void setJarsuffix(String JavaDoc aString) {
304         suffix = aString;
305     }
306
307     /**
308      * Sets the <code>orb</code> to construct classpath.
309      *
310      * @param aString 'RMI', 'JEREMIE', or 'DAVID'.
311      */

312     public void setOrb(String JavaDoc aString) {
313         orb = aString;
314     }
315
316     /**
317      * Sets the <code>nogenic</code> flag.
318      *
319      * @param aBoolean <code>true</code> if the flag must be set.
320      */

321     public void setNogenic(boolean aBoolean) {
322         nogenic = aBoolean;
323     }
324
325     /* ------------- */
326     /* other methods */
327     /* ------------- */
328
329     /** {@inheritDoc}. */
330     public void processDescriptor(String JavaDoc aDescriptorName, SAXParser JavaDoc saxParser) {
331
332         descriptorName = aDescriptorName;
333
334         log("JOnAS Deployment Tool processing: " + descriptorName,
335             Project.MSG_VERBOSE);
336
337         super.processDescriptor(descriptorName, saxParser);
338
339         if (outputdir != null) {
340             // the method deleteOnExit() do not work because the directory is not empty
341
log("Deleting temp output directory '" + outputdir + "'.", Project.MSG_VERBOSE);
342             deleteAllFiles(outputdir);
343         }
344     }
345
346     /** {@inheritDoc}. */
347     protected void writeJar(String JavaDoc baseName, File JavaDoc jarfile, Hashtable JavaDoc ejbFiles, String JavaDoc publicId)
348     throws BuildException {
349
350         // create the generic jar first
351
File JavaDoc genericJarFile = super.getVendorOutputJarFile(baseName);
352         super.writeJar(baseName, genericJarFile, ejbFiles, publicId);
353
354         // GenIC call on generic jar
355
addGenICGeneratedFiles(genericJarFile, ejbFiles);
356
357         // create the real jar
358
super.writeJar(baseName, getVendorOutputJarFile(baseName), ejbFiles, publicId);
359
360         if (!keepgeneric) {
361             log("Deleting generic JAR " + genericJarFile.toString(), Project.MSG_VERBOSE);
362             genericJarFile.delete();
363         }
364     }
365
366     /** {@inheritDoc}. */
367     protected void addVendorFiles(Hashtable JavaDoc ejbFiles, String JavaDoc ddPrefix) {
368
369     // JOnAS-specific descriptor deployment
370
jonasDescriptorName = getJonasDescriptorName();
371         File JavaDoc jonasDD = new File JavaDoc(getConfig().descriptorDir, jonasDescriptorName);
372
373         if (jonasDD.exists()) {
374             ejbFiles.put(META_DIR + JONAS_DD, jonasDD);
375         } else {
376             log("Unable to locate the JOnAS deployment descriptor. It was expected to be in: "
377                 + jonasDD.getPath() + ".", Project.MSG_WARN);
378         }
379     }
380
381     /** {@inheritDoc}. */
382     protected File JavaDoc getVendorOutputJarFile(String JavaDoc baseName) {
383         return new File JavaDoc(getDestDir(), baseName + suffix);
384     }
385
386     /**
387      * Determines the name of the JOnAS-specific EJB descriptor using the
388      * specified standard EJB descriptor name. In general, the standard
389      * descriptor will be named "[basename]-ejb-jar.xml", and this method will
390      * return "[basename]-jonas-ejb-jar.xml" or "jonas-[basename].xml"
391      *
392      * @return The name of the JOnAS-specific EJB descriptor file.
393      */

394     private String JavaDoc getJonasDescriptorName() {
395
396         // descriptorName = <path><basename><basenameterminator><remainder>
397
// examples = /org/objectweb/fooAppli/foo/Foo-ejb-jar.xml
398
// examples = /org/objectweb/fooAppli/foo/Foo.xml (JOnAS convention)
399

400         String JavaDoc jonasDN; // JOnAS-specific DD
401
boolean jonasConvention = false; // true if the JOnAS convention is used for the DD
402
String JavaDoc path; // Directory path of the EJB descriptor
403
String JavaDoc fileName; // EJB descriptor file name
404
String JavaDoc baseName; // Filename appearing before name terminator
405
String JavaDoc remainder; // Filename appearing after the name terminator
406

407         int startOfFileName = descriptorName.lastIndexOf(File.separatorChar);
408         if (startOfFileName != -1) {
409             // extract path info
410
path = descriptorName.substring(0, startOfFileName + 1);
411             fileName = descriptorName.substring(startOfFileName + 1);
412         } else {
413             // descriptorName is just a file without path
414
path = "";
415             fileName = descriptorName;
416         }
417
418         if (fileName.startsWith(EJB_DD)) {
419             return path + JONAS_DD;
420         }
421
422         int endOfBaseName = descriptorName.indexOf(getConfig().baseNameTerminator, startOfFileName);
423
424         /*
425          * Check for the odd case where the terminator and/or filename
426          * extension aren't found. These will ensure "jonas-" appears at the
427          * end of the name and before the '.' (if present).
428          */

429         if (endOfBaseName < 0) {
430             // baseNameTerminator not found: the descriptor use the
431
// JOnAS naming convention, ie [Foo.xml,jonas-Foo.xml] and
432
// not [Foo<baseNameTerminator>-ejb-jar.xml,
433
// Foo<baseNameTerminator>-jonas-ejb-jar.xml].
434
endOfBaseName = descriptorName.lastIndexOf('.') - 1;
435             if (endOfBaseName < 0) {
436                 // no . found
437
endOfBaseName = descriptorName.length() - 1;
438             }
439
440             jonasConvention = true;
441         }
442
443         baseName = descriptorName.substring(startOfFileName + 1, endOfBaseName + 1);
444         remainder = descriptorName.substring(endOfBaseName + 1);
445
446         if (jonasConvention) {
447             jonasDN = path + "jonas-" + baseName + ".xml";
448         } else {
449             jonasDN = path + baseName + "jonas-" + remainder;
450         }
451
452         log("Standard EJB descriptor name: " + descriptorName, Project.MSG_VERBOSE);
453         log("JOnAS-specific descriptor name: " + jonasDN, Project.MSG_VERBOSE);
454
455         return jonasDN;
456     }
457
458     /** {@inheritDoc}. */
459     protected String JavaDoc getJarBaseName(String JavaDoc descriptorFileName) {
460
461         String JavaDoc baseName = null;
462
463         if (getConfig().namingScheme.getValue().equals(EjbJar.NamingScheme.DESCRIPTOR)) {
464
465             // try to find JOnAS specific convention name
466
if (descriptorFileName.indexOf(getConfig().baseNameTerminator) == -1) {
467
468                 // baseNameTerminator not found: the descriptor use the
469
// JOnAS naming convention, ie [Foo.xml,jonas-Foo.xml] and
470
// not [Foo<baseNameTerminator>-ejb-jar.xml,
471
// Foo<baseNameTerminator>-jonas-ejb-jar.xml].
472

473                 String JavaDoc aCanonicalDescriptor = descriptorFileName.replace('\\', '/');
474                 int lastSeparatorIndex = aCanonicalDescriptor.lastIndexOf('/');
475                 int endOfBaseName;
476
477                 if (lastSeparatorIndex != -1) {
478                     endOfBaseName = descriptorFileName.indexOf(".xml", lastSeparatorIndex);
479                 } else {
480                     endOfBaseName = descriptorFileName.indexOf(".xml");
481                 }
482
483                 if (endOfBaseName != -1) {
484                     baseName = descriptorFileName.substring(0, endOfBaseName);
485                 }
486             }
487         }
488
489         if (baseName == null) {
490             // else get standard baseName
491
baseName = super.getJarBaseName(descriptorFileName);
492         }
493
494         log("JAR base name: " + baseName, Project.MSG_VERBOSE);
495
496         return baseName;
497     }
498
499     /** {@inheritDoc}. */
500     protected void registerKnownDTDs(DescriptorHandler handler) {
501         handler.registerDTD(EJB_JAR_1_1_PUBLIC_ID,
502                     jonasroot + File.separator + "xml" + File.separator + EJB_JAR_1_1_DTD);
503         handler.registerDTD(EJB_JAR_2_0_PUBLIC_ID,
504                     jonasroot + File.separator + "xml" + File.separator + EJB_JAR_2_0_DTD);
505
506         handler.registerDTD(JONAS_EJB_JAR_2_4_PUBLIC_ID,
507                     jonasroot + File.separator + "xml" + File.separator + JONAS_EJB_JAR_2_4_DTD);
508         handler.registerDTD(JONAS_EJB_JAR_2_5_PUBLIC_ID,
509                     jonasroot + File.separator + "xml" + File.separator + JONAS_EJB_JAR_2_5_DTD);
510     }
511
512     /**
513      * Add to the given hashtable all the file generated by GenIC.
514      *
515      * @param genericJarFile jar file.
516      * @param ejbFiles the hashtable.
517      */

518     private void addGenICGeneratedFiles(
519         File JavaDoc genericJarFile, Hashtable JavaDoc ejbFiles) {
520         Java genicTask = null; // GenIC task
521
String JavaDoc genicClass = null; // GenIC class (3 are supported for various
522
// versions
523
if (nogenic) {
524             return;
525         }
526
527         genicTask = new Java(getTask());
528         genicTask.setTaskName("genic");
529         genicTask.setFork(true);
530
531         // jonasroot
532
genicTask.createJvmarg().setValue("-Dinstall.root=" + jonasroot);
533
534         // java policy file
535
String JavaDoc jonasConfigDir = jonasroot + File.separator + "config";
536         File JavaDoc javaPolicyFile = new File JavaDoc(jonasConfigDir, "java.policy");
537         if (javaPolicyFile.exists()) {
538             genicTask.createJvmarg().setValue("-Djava.security.policy="
539                               + javaPolicyFile.toString());
540         }
541
542         // outputdir
543
try {
544             outputdir = createTempDir();
545         } catch (IOException JavaDoc aIOException) {
546             String JavaDoc msg = "Cannot create temp dir: " + aIOException.getMessage();
547             throw new BuildException(msg, aIOException);
548         }
549         log("Using temporary output directory: " + outputdir, Project.MSG_VERBOSE);
550
551         genicTask.createArg().setValue("-d");
552         genicTask.createArg().setFile(outputdir);
553
554         // work around a bug of GenIC 2.5
555
String JavaDoc key;
556         File JavaDoc f;
557         Enumeration JavaDoc keys = ejbFiles.keys();
558         while (keys.hasMoreElements()) {
559             key = (String JavaDoc) keys.nextElement();
560             f = new File JavaDoc(outputdir + File.separator + key);
561             f.getParentFile().mkdirs();
562         }
563         log("Worked around a bug of GenIC 2.5.", Project.MSG_VERBOSE);
564
565         // classpath
566
Path classpath = getCombinedClasspath();
567         if (classpath == null) {
568             classpath = new Path(getTask().getProject());
569         }
570         classpath.append(new Path(classpath.getProject(), jonasConfigDir));
571         classpath.append(new Path(classpath.getProject(), outputdir.toString()));
572
573         // try to create the classpath for the correct ORB
574
if (orb != null) {
575             String JavaDoc orbJar = jonasroot + File.separator + "lib"
576                 + File.separator + orb + "_jonas.jar";
577             classpath.append(new Path(classpath.getProject(), orbJar));
578         }
579         log("Using classpath: " + classpath.toString(), Project.MSG_VERBOSE);
580         genicTask.setClasspath(classpath);
581
582         // class name (search in the classpath provided for the ejbjar element)
583
genicClass = getGenicClassName(classpath);
584         if (genicClass == null) {
585             log("Cannot find GenIC class in classpath.", Project.MSG_ERR);
586             throw new BuildException("GenIC class not found, please check the classpath.");
587         } else {
588             log("Using '" + genicClass + "' GenIC class." , Project.MSG_VERBOSE);
589             genicTask.setClassname(genicClass);
590         }
591
592         // keepgenerated
593
if (keepgenerated) {
594             genicTask.createArg().setValue("-keepgenerated");
595         }
596
597         // nocompil
598
if (nocompil) {
599             genicTask.createArg().setValue("-nocompil");
600         }
601
602         // novalidation
603
if (novalidation) {
604             genicTask.createArg().setValue("-novalidation");
605         }
606
607         // javac
608
if (javac != null) {
609             genicTask.createArg().setValue("-javac");
610             genicTask.createArg().setLine(javac);
611         }
612
613         // javacopts
614
if (javacopts != null && !javacopts.equals("")) {
615             genicTask.createArg().setValue("-javacopts");
616             genicTask.createArg().setLine(javacopts);
617         }
618
619         // rmicopts
620
if (rmicopts != null && !rmicopts.equals("")) {
621             genicTask.createArg().setValue("-rmicopts");
622             genicTask.createArg().setLine(rmicopts);
623         }
624
625         // secpropag
626
if (secpropag) {
627             genicTask.createArg().setValue("-secpropag");
628         }
629
630         // verbose
631
if (verbose) {
632             genicTask.createArg().setValue("-verbose");
633         }
634
635         // additionalargs
636
if (additionalargs != null) {
637             genicTask.createArg().setValue(additionalargs);
638         }
639
640         // the generated classes must not be added in the generic JAR!
641
// is that buggy on old JOnAS (2.4) ??
642
genicTask.createArg().setValue("-noaddinjar");
643
644         // input file to process by GenIC
645
genicTask.createArg().setValue(genericJarFile.getPath());
646
647         // calling GenIC task
648
log("Calling " + genicClass + " for " + getConfig().descriptorDir
649             + File.separator + descriptorName + ".", Project.MSG_VERBOSE);
650
651         if (genicTask.executeJava() != 0) {
652
653             // the method deleteOnExit() do not work because the directory is not empty
654
log("Deleting temp output directory '" + outputdir + "'.", Project.MSG_VERBOSE);
655             deleteAllFiles(outputdir);
656
657             if (!keepgeneric) {
658                 log("Deleting generic JAR " + genericJarFile.toString(),
659                     Project.MSG_VERBOSE);
660                 genericJarFile.delete();
661             }
662
663             throw new BuildException("GenIC reported an error.");
664         }
665
666         // add the generated files to the ejbFiles
667
addAllFiles(outputdir, "", ejbFiles);
668     }
669
670     /**
671      * Get the GenIC class name to use in the given classpath.
672      *
673      * @param classpath classpath where the GenIC class must be searched.
674      * @return the GenIC class name. Return <code>null</code> if the class name
675      * cannot be found.
676      */

677     String JavaDoc getGenicClassName(Path classpath) {
678
679         log("Looking for GenIC class in classpath: "
680             + classpath.toString(), Project.MSG_VERBOSE);
681
682         AntClassLoader cl = classpath.getProject().createClassLoader(classpath);
683
684         try {
685             cl.loadClass(JonasDeploymentTool.GENIC_CLASS);
686             log("Found GenIC class '" + JonasDeploymentTool.GENIC_CLASS
687                 + "' in classpath.", Project.MSG_VERBOSE);
688             return JonasDeploymentTool.GENIC_CLASS;
689
690         } catch (ClassNotFoundException JavaDoc cnf1) {
691             log("GenIC class '" + JonasDeploymentTool.GENIC_CLASS
692                 + "' not found in classpath.",
693             Project.MSG_VERBOSE);
694         }
695
696         try {
697             cl.loadClass(JonasDeploymentTool.OLD_GENIC_CLASS_1);
698             log("Found GenIC class '" + JonasDeploymentTool.OLD_GENIC_CLASS_1
699                 + "' in classpath.", Project.MSG_VERBOSE);
700             return JonasDeploymentTool.OLD_GENIC_CLASS_1;
701
702         } catch (ClassNotFoundException JavaDoc cnf2) {
703             log("GenIC class '" + JonasDeploymentTool.OLD_GENIC_CLASS_1
704                 + "' not found in classpath.",
705             Project.MSG_VERBOSE);
706         }
707
708         try {
709             cl.loadClass(JonasDeploymentTool.OLD_GENIC_CLASS_2);
710             log("Found GenIC class '" + JonasDeploymentTool.OLD_GENIC_CLASS_2
711                 + "' in classpath.", Project.MSG_VERBOSE);
712             return JonasDeploymentTool.OLD_GENIC_CLASS_2;
713
714         } catch (ClassNotFoundException JavaDoc cnf3) {
715             log("GenIC class '" + JonasDeploymentTool.OLD_GENIC_CLASS_2
716                 + "' not found in classpath.",
717             Project.MSG_VERBOSE);
718         }
719         return null;
720     }
721
722     /**
723      * Verify the configuration.
724      * @param descriptorFileName the name of the descriptor file.
725      * @param saxParser not used.
726      * @throws BuildException if there is an error.
727      */

728     protected void checkConfiguration(String JavaDoc descriptorFileName,
729                       SAXParser JavaDoc saxParser) throws BuildException {
730
731         // jonasroot
732
if (jonasroot == null) {
733             throw new BuildException("The jonasroot attribut is not set.");
734         } else if (!jonasroot.isDirectory()) {
735             throw new BuildException("The jonasroot attribut '" + jonasroot
736                 + "' is not a valid directory.");
737         }
738
739         // orb
740
if (orb != null && !orb.equals(RMI_ORB) && !orb.equals(JEREMIE_ORB)
741             && !orb.equals(DAVID_ORB)) {
742             throw new BuildException("The orb attribut '" + orb
743                 + "' is not valid (must be either "
744                 + RMI_ORB + ", " + JEREMIE_ORB + " or " + DAVID_ORB + ").");
745         }
746
747         // additionalargs
748
if (additionalargs != null && additionalargs.equals("")) {
749             throw new BuildException("Empty additionalargs attribut.");
750         }
751
752         // javac
753
if (javac != null && javac.equals("")) {
754             throw new BuildException("Empty javac attribut.");
755         }
756     }
757
758     /* ----------------------------------------------------------------------------------- */
759     /* utilitary methods */
760     /* ----------------------------------------------------------------------------------- */
761
762     /**
763      * Create a temporary directory for GenIC output.
764      *
765      * @return the temp directory.
766      * @throws BuildException if a temp directory cannot be created.
767      */

768     private File JavaDoc createTempDir() throws IOException JavaDoc {
769         File JavaDoc tmpDir = File.createTempFile("genic", null, null);
770         tmpDir.delete();
771         if (!tmpDir.mkdir()) {
772             throw new IOException JavaDoc("Cannot create the temporary directory '" + tmpDir + "'.");
773         }
774         return tmpDir;
775     }
776
777     /**
778      * Delete a file. If the file is a directory, delete recursivly all the
779      * files inside.
780      *
781      * @param aFile file to delete.
782      */

783     private void deleteAllFiles(File JavaDoc aFile) {
784         if (aFile.isDirectory()) {
785             File JavaDoc[] someFiles = aFile.listFiles();
786
787             for (int i = 0; i < someFiles.length; i++) {
788                 deleteAllFiles(someFiles[i]);
789             }
790         }
791         aFile.delete();
792     }
793
794     /**
795      * Add a file to the a given hashtable. If the file is a directory, add
796      * recursivly all the files inside to the hashtable.
797      *
798      * @param file the file to add.
799      * @param rootDir the current sub-directory to scan.
800      * @param hashtable the hashtable where to add the files.
801      */

802     private void addAllFiles(File JavaDoc file, String JavaDoc rootDir, Hashtable JavaDoc hashtable) {
803
804         if (!file.exists()) {
805             throw new IllegalArgumentException JavaDoc();
806         }
807
808         String JavaDoc newRootDir;
809         if (file.isDirectory()) {
810             File JavaDoc[] files = file.listFiles();
811             for (int i = 0; i < files.length; i++) {
812                 if (rootDir.length() > 0) {
813                     newRootDir = rootDir + File.separator + files[i].getName();
814                 } else {
815                     newRootDir = files[i].getName();
816                 }
817                 addAllFiles(files[i], newRootDir, hashtable);
818             }
819         } else {
820             hashtable.put(rootDir, file);
821         }
822     }
823 }
824
Popular Tags