KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > ant > JonasDeploymentTool


1 /**
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "The Jakarta Project", "Ant", and "Apache Software
27  * Foundation" must not be used to endorse or promote products derived
28  * from this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  */

54 package org.objectweb.jonas.ant;
55
56 import java.io.BufferedReader JavaDoc;
57 import java.io.BufferedWriter JavaDoc;
58 import java.io.File JavaDoc;
59 import java.io.FileReader JavaDoc;
60 import java.io.FileWriter JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.util.Hashtable JavaDoc;
63
64 import javax.xml.parsers.SAXParser JavaDoc;
65
66 import org.apache.tools.ant.AntClassLoader;
67 import org.apache.tools.ant.BuildException;
68 import org.apache.tools.ant.Project;
69 import org.apache.tools.ant.taskdefs.Java;
70 import org.apache.tools.ant.types.Path;
71 import org.apache.tools.ant.types.Reference;
72
73 /**
74  * The deployment tool to add the jonas specific deployment descriptors to the
75  * ejb JAR file. JONAS only requires one additional file jonas-ejb-jar.xml.
76  * @author <a HREF="mailto:cmorvan@ingenosya.com">Cyrille Morvan </a>, <a
77  * HREF="http://www.ingenosya.com">Ingenosya France </a>,
78  * Contributor(s): <a HREF="mailto:mathieu.peltier@inrialpes.fr">Mathieu
79  * Peltier </a> Adriana Danes: change configuration directory name from
80  * "config" to "conf"
81  * @version 1.0
82  * @see EjbJar#createJonas
83  */

84 public class JonasDeploymentTool extends GenericDeploymentTool {
85
86     /** Public Id of the standard deployment descriptor DTD. */
87     protected static final String JavaDoc EJB_JAR_1_1_PUBLIC_ID = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN";
88     protected static final String JavaDoc EJB_JAR_2_0_PUBLIC_ID = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN";
89
90     /** Public Id of the JOnAS-specific deployment descriptor DTD. */
91     protected static final String JavaDoc JONAS_EJB_JAR_2_4_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 2.4//EN";
92     protected static final String JavaDoc JONAS_EJB_JAR_2_5_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 2.5//EN";
93     protected static final String JavaDoc JONAS_EJB_JAR_3_0_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 3.0//EN";
94     protected static final String JavaDoc JONAS_EJB_JAR_3_2_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 3.2//EN";
95     protected static final String JavaDoc JONAS_EJB_JAR_3_3_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 3.3//EN";
96     protected static final String JavaDoc JONAS_EJB_JAR_3_3_2_PUBLIC_ID = "-//ObjectWeb//DTD JOnAS 3.3.2//EN";
97
98     /**
99      * Name of the standard deployment descriptor DTD (these files are stored in
100      * the ${JONAS_ROOT}/xml directory).
101      */

102     protected static final String JavaDoc EJB_JAR_1_1_DTD = "ejb-jar_1_1.dtd";
103     protected static final String JavaDoc EJB_JAR_2_0_DTD = "ejb-jar_2_0.dtd";
104
105     /**
106      * Name of the JOnAS-specific deployment descriptor DTD (these files are
107      * stored in the ${JONAS_ROOT}/xml directory).
108      */

109     protected static final String JavaDoc JONAS_EJB_JAR_2_4_DTD = "jonas-ejb-jar_2_4.dtd";
110     protected static final String JavaDoc JONAS_EJB_JAR_2_5_DTD = "jonas-ejb-jar_2_5.dtd";
111     protected static final String JavaDoc JONAS_EJB_JAR_3_0_DTD = "jonas-ejb-jar_3_0.dtd";
112     protected static final String JavaDoc JONAS_EJB_JAR_3_2_DTD = "jonas-ejb-jar_3_2.dtd";
113     protected static final String JavaDoc JONAS_EJB_JAR_3_3_DTD = "jonas-ejb-jar_3_3.dtd";
114     protected static final String JavaDoc JONAS_EJB_JAR_3_3_2_DTD = "jonas-ejb-jar_3_3_2.dtd";
115
116     /** Default JOnAS deployment descriptor name. */
117     protected static final String JavaDoc JONAS_DD = "jonas-ejb-jar.xml";
118
119     /** Bootstrap class name. */
120     protected static final String JavaDoc BOOTSTRAP_CLASS = "org.objectweb.jonas.server.Bootstrap";
121
122     /** GenIC class name (JOnAS 2.6). */
123     protected static final String JavaDoc GENIC_CLASS = "org.objectweb.jonas_ejb.genic.GenIC";
124
125     /**
126      * Filename of the standard EJB descriptor (which is passed to this class
127      * from the parent "ejbjar" task). This file is relative to the directory
128      * specified by the "srcdir" attribute in the ejbjar task.
129      */

130     private String JavaDoc descriptorName;
131
132     /**
133      * Filename of the JOnAS-specific EJB descriptor (which is passed to this
134      * class from the parent "ejbjar" task). This file is relative to the
135      * directory specified by the "srcdir" attribute in the ejbjar task.
136      */

137     private String JavaDoc jonasDescriptorName;
138
139     /* ------------- */
140     /* GenIC options */
141     /* ------------- */
142
143     /**
144      * Temporary output directory used by GenIC.
145      */

146     private File JavaDoc outputdir;
147
148     /**
149      * <code>true</code> if the intermediate Java source files generated by
150      * GenIC must be deleted or not. The default is <code>false</code>
151      */

152     private boolean keepgenerated = false;
153
154     /**
155      * <code>true</code> if the generated source files must not be compiled
156      * via the java and rmi compilers. The default is <code>false</code>.
157      */

158     private boolean nocompil = false;
159
160     /**
161      * Equivalent to the <code>-invokecmd</code> GenIC option. The default is
162      * <code>false</code>.
163      */

164     private boolean invokeCmd = false;
165
166     /**
167      * <code>true</code> if the XML deployment descriptors must be parsed
168      * without validation. The default is <code>false</code>.
169      */

170     private boolean novalidation = false;
171
172     /**
173      * Java compiler to use. The default is the value of
174      * <code>build.compiler</code> property.
175      */

176     private String JavaDoc javac;
177
178     /** Options to pass to the java compiler. */
179     private String JavaDoc javacopts;
180
181     /** Options to pass to the rmi compiler. */
182     private String JavaDoc rmicopts;
183
184     /**
185      * <code>true</code> if the GenIC call must be verbose. The default is
186      * <code>false</code>.
187      */

188     private boolean verbose = false;
189
190     /** Additional args to send to GenIC. */
191     private String JavaDoc additionalargs;
192
193     /** Additional args to pass to the GenIC JVM. */
194     private String JavaDoc jvmopts = null;
195
196     /** List of protocols (used for Carol) */
197     private String JavaDoc protocols = null;
198
199     /* ------------- */
200     /* other options */
201     /* ------------- */
202
203     /** JOnAS root directory. */
204     private File JavaDoc jonasroot;
205
206     /** JOnAS base directory. */
207     private File JavaDoc jonasbase;
208
209     /**
210      * <code>true</code> if the generic JAR file used as input to GenIC must
211      * be retained. The default is <code>false</code>.
212      */

213     private boolean keepgeneric = false;
214
215     /** Stores the suffix for the JOnAS JAR file. The default is '.jar'. */
216     private String JavaDoc suffix = ".jar";
217
218     /**
219      * <code>true</code> if GenIC must not be run on the EJB JAR. The default
220      * is <code>false</code>.
221      */

222     private boolean nogenic = false;
223
224     /* -------------------- */
225     /* GenIC options setter */
226     /* -------------------- */
227
228     /**
229      * Set the {@link #keepgenerated}flag.
230      * @param aBoolean <code>true</code> if the flag must be set.
231      */

232     public void setKeepgenerated(boolean aBoolean) {
233         keepgenerated = aBoolean;
234     }
235
236     /**
237      * Do nothing because the mappernames attribute is deprecated
238      * @param aString list of mapper names separed by ,
239      */

240     public void setMappernames(String JavaDoc aString) {
241         log("'mappernames' attribute ignored (deprecated)", Project.MSG_WARN);
242     }
243
244     /**
245      * Set the protocols list
246      * @param aString list of the protocol names separed by ,
247      */

248     public void setProtocols(String JavaDoc aString) {
249         protocols = aString;
250     }
251
252     /**
253      * Set the classpath as a Reference on another path
254      * @param r Referenced Classpath
255      */

256     public void setClasspathref(Reference r) {
257         createClasspath().setRefid(r);
258     }
259     /**
260      * Set the {@link #additionalargs}.
261      * @param aString additional args.
262      */

263     public void setAdditionalargs(String JavaDoc aString) {
264         additionalargs = aString;
265     }
266
267     /**
268      * Set the {@link #nocompil}flag.
269      * @param aBoolean <code>true</code> if the flag must be set.
270      */

271     public void setNocompil(boolean aBoolean) {
272         nocompil = aBoolean;
273     }
274
275     /**
276      * Set the {@link #invokecmd}flag.
277      * @param aBoolean <code>true</code> if the flag must be set.
278      */

279     public void setInvokecmd(boolean aBoolean) {
280         invokeCmd = aBoolean;
281     }
282
283     /**
284      * Set the {@link #novalidation}flag.
285      * @param aBoolean <code>true</code> if the flag must be set.
286      */

287     public void setNovalidation(boolean aBoolean) {
288         novalidation = aBoolean;
289     }
290
291     /**
292      * Set the java compiler {@link #javac}to use.
293      * @param aString the java compiler.
294      */

295     public void setJavac(String JavaDoc aString) {
296         javac = aString;
297     }
298
299     /**
300      * Set the options to pass to the java compiler.
301      * @param aString the options.
302      */

303     public void setJavacopts(String JavaDoc aString) {
304         javacopts = aString;
305     }
306
307     /**
308      * Set the options to pass to the rmi compiler.
309      * @param aString the options.
310      */

311     public void setRmicopts(String JavaDoc aString) {
312         rmicopts = aString;
313     }
314
315     /**
316      * Set the {@link #verbose}flag.
317      * @param aBoolean <code>true</code> if the flag must be set.
318      */

319     public void setVerbose(boolean aBoolean) {
320         verbose = aBoolean;
321     }
322
323     /* -------------------- */
324     /* other options setter */
325     /* -------------------- */
326
327     /**
328      * Set the JOnAS root directory.
329      * @param aFile the JOnAS root directory.
330      */

331     public void setJonasroot(File JavaDoc aFile) {
332         jonasroot = aFile;
333     }
334
335     /**
336      * Set the JOnAS base directory.
337      * @param aFile the JOnAS base directory.
338      */

339     public void setJonasbase(File JavaDoc aFile) {
340         jonasbase = aFile;
341     }
342
343     /**
344      * Set the {@link #keepgeneric}flag.
345      * @param aBoolean <code>true</code> if the flag must be set.
346      */

347     public void setKeepgeneric(boolean aBoolean) {
348         keepgeneric = aBoolean;
349     }
350
351     /**
352      * Set the {@link #jarsuffix}.
353      * @param aString the string to use as the suffix.
354      */

355     public void setJarsuffix(String JavaDoc aString) {
356         suffix = aString;
357     }
358
359     /**
360      * Set the {@link #nogenic}flag.
361      * @param aBoolean <code>true</code> if the flag must be set.
362      */

363     public void setNogenic(boolean aBoolean) {
364         nogenic = aBoolean;
365     }
366
367     /**
368      * Set the additional args to pass to the GenIC JVM.
369      * @param aString the options.
370      */

371     public void setJvmopts(String JavaDoc aString) {
372         jvmopts = aString;
373     }
374
375     /* ------------- */
376     /* other methods */
377     /* ------------- */
378
379     public void processDescriptor(String JavaDoc aDescriptorName, SAXParser JavaDoc saxParser) {
380
381         descriptorName = aDescriptorName;
382
383         log("JOnAS Deployment Tool processing for JOnAS: " + descriptorName, Project.MSG_VERBOSE);
384
385         super.processDescriptor(descriptorName, saxParser);
386
387         if (outputdir != null) {
388             // the method deleteOnExit() do not work because the directory is
389
// not empty
390
log("Deleting temp output directory '" + outputdir + "'.", Project.MSG_VERBOSE);
391             deleteAllFiles(outputdir);
392         }
393     }
394
395     protected void writeJar(String JavaDoc baseName, File JavaDoc jarfile, Hashtable JavaDoc ejbFiles, String JavaDoc publicId,
396             boolean includeInnerClasses) throws BuildException {
397
398         // create the generic jar first
399
File JavaDoc genericJarFile = super.getVendorOutputJarFile(baseName);
400
401         super.writeJar(baseName, genericJarFile, ejbFiles, publicId, true);
402
403
404         // GenIC call on generic jar
405
addGenICGeneratedFiles(genericJarFile, ejbFiles);
406
407         // merge Manifest give by user if exist with this generated by Genic
408
if (this.getConfig() != null && this.getConfig().manifest != null
409                 && this.getConfig().manifest.exists()) {
410             File JavaDoc filePathGenicManifest = (File JavaDoc) ejbFiles.get("META-INF/MANIFEST.MF");
411             if (filePathGenicManifest != null)
412                 mergeManifest(this.getConfig().manifest, filePathGenicManifest);
413             else
414                 ejbFiles.put("META-INF/MANIFEST.MF", this.getConfig().manifest);
415         }
416
417         // create the real jar
418
super.writeJar(baseName, getVendorOutputJarFile(baseName), ejbFiles, publicId, false);
419
420         //log("genericJarFile: " + genericJarFile, Project.MSG_VERBOSE);
421
//log("getVendorOutputJarFile(baseName): " +
422
//getVendorOutputJarFile(baseName), Project.MSG_VERBOSE);
423
//log("baseName: " + baseName, Project.MSG_VERBOSE);
424

425         if (!keepgeneric) {
426             log("Deleting generic JAR " + genericJarFile.toString(), Project.MSG_VERBOSE);
427             genericJarFile.delete();
428         }
429     }
430
431     /**
432      * Merge Manifest file
433      * @param manifestUserFile : input file
434      * @param manifestGenicFile : output file
435      */

436     protected void mergeManifest(File JavaDoc manifestUserFile, File JavaDoc manifestGenicFile) {
437         try {
438             BufferedReader JavaDoc tempManifestUserReader = new BufferedReader JavaDoc(new FileReader JavaDoc(manifestUserFile));
439             BufferedWriter JavaDoc tempManifestGenicWriter = new BufferedWriter JavaDoc(new FileWriter JavaDoc(manifestGenicFile, true));
440             String JavaDoc ligneRead;
441
442             while ((ligneRead = tempManifestUserReader.readLine()) != null) {
443                 tempManifestGenicWriter.write(ligneRead);
444                 tempManifestGenicWriter.newLine();
445             }
446             tempManifestGenicWriter.close();
447             tempManifestUserReader.close();
448         } catch (IOException JavaDoc e) {
449             log("Unable to update Manifest with user manifest :" + e.getMessage(), Project.MSG_WARN);
450         }
451     }
452
453     protected void addVendorFiles(Hashtable JavaDoc ejbFiles, String JavaDoc ddPrefix) {
454
455         // JOnAS-specific descriptor deployment
456
jonasDescriptorName = getJonasDescriptorName();
457         File JavaDoc jonasDD = new File JavaDoc(getConfig().descriptorDir, jonasDescriptorName);
458
459         if (jonasDD.exists()) {
460             ejbFiles.put(META_DIR + JONAS_DD, jonasDD);
461         } else {
462             log("Unable to locate the JOnAS deployment descriptor. It was expected to be in: " + jonasDD.getPath()
463                     + ".", Project.MSG_WARN);
464         }
465     }
466
467     protected File JavaDoc getVendorOutputJarFile(String JavaDoc baseName) {
468         return new File JavaDoc(getDestDir(), baseName + suffix);
469     }
470
471     /**
472      * Determines the name of the JOnAS-specific EJB descriptor using the
473      * specified standard EJB descriptor name. In general, the standard
474      * descriptor will be named "[basename]-ejb-jar.xml", and this method will
475      * return "[basename]-jonas-ejb-jar.xml" or "jonas-[basename].xml"
476      * @return The name of the JOnAS-specific EJB descriptor file.
477      */

478     private String JavaDoc getJonasDescriptorName() {
479
480         // descriptorName = <path><basename><basenameterminator><remainder>
481
// examples = /org/objectweb/fooAppli/foo/Foo-ejb-jar.xml
482
// examples = /org/objectweb/fooAppli/foo/Foo.xml (JOnAS convention)
483

484         String JavaDoc jonasDescriptorName; // JOnAS-specific DD
485
boolean jonasConvention = false; // true if the JOnAS convention is used
486
// for the DD
487
String JavaDoc path; // Directory path of the EJB descriptor
488
String JavaDoc fileName; // EJB descriptor file name
489
String JavaDoc baseName; // Filename appearing before name terminator
490
String JavaDoc remainder; // Filename appearing after the name terminator
491

492         int startOfFileName = descriptorName.lastIndexOf(File.separatorChar);
493         if (startOfFileName != -1) {
494             // extract path info
495
path = descriptorName.substring(0, startOfFileName + 1);
496             fileName = descriptorName.substring(startOfFileName + 1);
497         } else {
498             // descriptorName is just a file without path
499
path = "";
500             fileName = descriptorName;
501         }
502
503         if (fileName.startsWith(EJB_DD)) {
504             return path + JONAS_DD;
505         }
506
507         int endOfBaseName = descriptorName.indexOf(getConfig().baseNameTerminator, startOfFileName);
508
509         /*
510          * Check for the odd case where the terminator and/or filename extension
511          * aren't found. These will ensure "jonas-" appears at the end of the
512          * name and before the '.' (if present).
513          */

514         if (endOfBaseName < 0) {
515             // baseNameTerminator not found: the descriptor use the
516
// JOnAS naming convention, ie [Foo.xml,jonas-Foo.xml] and
517
// not [Foo<baseNameTerminator>-ejb-jar.xml,
518
// Foo<baseNameTerminator>-jonas-ejb-jar.xml].
519
endOfBaseName = descriptorName.lastIndexOf('.') - 1;
520             if (endOfBaseName < 0) {
521                 // no . found
522
endOfBaseName = descriptorName.length() - 1;
523             }
524
525             jonasConvention = true;
526         }
527
528         baseName = descriptorName.substring(startOfFileName + 1, endOfBaseName + 1);
529         remainder = descriptorName.substring(endOfBaseName + 1);
530
531         if (jonasConvention) {
532             jonasDescriptorName = path + "jonas-" + baseName + ".xml";
533         } else {
534             jonasDescriptorName = path + baseName + "jonas-" + remainder;
535         }
536
537         log("Standard EJB descriptor name: " + descriptorName, Project.MSG_VERBOSE);
538         log("JOnAS-specific descriptor name: " + jonasDescriptorName, Project.MSG_VERBOSE);
539
540         return jonasDescriptorName;
541     }
542
543     protected String JavaDoc getJarBaseName(String JavaDoc descriptorFileName) {
544
545         String JavaDoc baseName = null;
546
547         if (getConfig().namingScheme.getValue().equals(EjbJar.NamingScheme.DESCRIPTOR)) {
548
549             // try to find JOnAS specific convention name
550
if (descriptorFileName.indexOf(getConfig().baseNameTerminator) == -1) {
551
552                 // baseNameTerminator not found: the descriptor use the
553
// JOnAS naming convention, ie [Foo.xml,jonas-Foo.xml] and
554
// not [Foo<baseNameTerminator>-ejb-jar.xml,
555
// Foo<baseNameTerminator>-jonas-ejb-jar.xml].
556

557                 String JavaDoc aCanonicalDescriptor = descriptorFileName.replace('\\', '/');
558                 int lastSeparatorIndex = aCanonicalDescriptor.lastIndexOf('/');
559                 int endOfBaseName;
560
561                 if (lastSeparatorIndex != -1) {
562                     endOfBaseName = descriptorFileName.indexOf(".xml", lastSeparatorIndex);
563                 } else {
564                     endOfBaseName = descriptorFileName.indexOf(".xml");
565                 }
566
567                 if (endOfBaseName != -1) {
568                     baseName = descriptorFileName.substring(0, endOfBaseName);
569                 }
570             }
571         }
572
573         if (baseName == null) {
574             // else get standard baseName
575
baseName = super.getJarBaseName(descriptorFileName);
576         }
577
578         log("JAR base name: " + baseName, Project.MSG_VERBOSE);
579
580         return baseName;
581     }
582
583     protected void registerKnownDTDs(DescriptorHandler handler) {
584
585         handler.registerDTD(EJB_JAR_1_1_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator
586                 + EJB_JAR_1_1_DTD);
587         handler.registerDTD(EJB_JAR_2_0_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator
588                 + EJB_JAR_2_0_DTD);
589
590         handler.registerDTD(JONAS_EJB_JAR_2_4_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator
591                 + JONAS_EJB_JAR_2_4_DTD);
592         handler.registerDTD(JONAS_EJB_JAR_2_5_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator
593                 + JONAS_EJB_JAR_2_5_DTD);
594         handler.registerDTD(JONAS_EJB_JAR_3_0_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator
595                 + JONAS_EJB_JAR_3_0_DTD);
596         handler.registerDTD(JONAS_EJB_JAR_3_2_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator
597                 + JONAS_EJB_JAR_3_2_DTD);
598         handler.registerDTD(JONAS_EJB_JAR_3_3_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator
599                 + JONAS_EJB_JAR_3_3_DTD);
600         handler.registerDTD(JONAS_EJB_JAR_3_3_2_PUBLIC_ID, jonasroot + File.separator + "xml" + File.separator
601                 + JONAS_EJB_JAR_3_3_2_DTD);
602     }
603
604     /**
605      * Add to the given hashtable all the file generated by GenIC.
606      * @param genericJarFile jar file.
607      * @param ejbFiles the hashtable.
608      */

609     private void addGenICGeneratedFiles(File JavaDoc genericJarFile, Hashtable JavaDoc ejbFiles) {
610
611         Java genicTask = null; // GenIC task
612

613         if (nogenic) {
614             return;
615         }
616
617         genicTask = (Java) getTask().getProject().createTask("java");
618         genicTask.setTaskName("genic");
619         genicTask.setFork(true);
620
621         // jonas root
622
genicTask.createJvmarg().setValue("-Dinstall.root=" + jonasroot);
623
624         // jonas base
625
genicTask.createJvmarg().setValue("-Djonas.base=" + jonasbase);
626
627         // Endorsed directory
628
File JavaDoc endorsedDir = new File JavaDoc(new File JavaDoc(jonasroot, "lib"), "endorsed");
629         genicTask.createJvmarg().setValue("-Djava.endorsed.dirs=" + endorsedDir);
630
631         // java policy file
632
String JavaDoc jonasConfigDir = jonasroot + File.separator + "conf";
633         File JavaDoc javaPolicyFile = new File JavaDoc(jonasConfigDir, "java.policy");
634         if (javaPolicyFile.exists()) {
635             genicTask.createJvmarg().setValue("-Djava.security.policy=" + javaPolicyFile.toString());
636         }
637
638         // The bootstrap class must launch the GenIC class
639
genicTask.createArg().setValue(JonasDeploymentTool.GENIC_CLASS);
640
641         // outputdir
642
try {
643             outputdir = createTempDir();
644         } catch (IOException JavaDoc aIOException) {
645             throw new BuildException("Cannot create temp dir", aIOException);
646         }
647         log("Using temporary output directory: " + outputdir, Project.MSG_VERBOSE);
648
649         genicTask.createArg().setValue("-d");
650         genicTask.createArg().setFile(outputdir);
651
652         // classpath
653
Path classpath = getCombinedClasspath();
654         if (classpath == null) {
655             classpath = new Path(getTask().getProject());
656         }
657         // add ow_jonas_bootstrap.jar
658
String JavaDoc bootJar = jonasroot + File.separator + "lib" + File.separator + "common" + File.separator
659                 + "ow_jonas_bootstrap.jar";
660         Path bootstrap = new Path(getTask().getProject(), bootJar);
661         //classpath.append(new Path(classpath.getProject(), bootJar));
662

663         classpath.append(new Path(classpath.getProject(), outputdir.toString()));
664
665         log("Using classpath: " + bootstrap.toString(), Project.MSG_VERBOSE);
666         genicTask.setClasspath(bootstrap);
667
668         if (!checkBootstrapClassName(bootstrap)) {
669             log("Cannot find bootstrap class in classpath.", Project.MSG_ERR);
670             throw new BuildException("Bootstrap class not found, please check the classpath.");
671         } else {
672             genicTask.setClassname(JonasDeploymentTool.BOOTSTRAP_CLASS);
673         }
674
675         // classpath
676
genicTask.createArg().setValue("-classpath");
677         genicTask.createArg().setPath(classpath);
678
679         // keepgenerated
680
if (keepgenerated) {
681             genicTask.createArg().setValue("-keepgenerated");
682         }
683
684         // nocompil
685
if (nocompil) {
686             genicTask.createArg().setValue("-nocompil");
687         }
688
689         // invokecmd
690
if (invokeCmd) {
691             genicTask.createArg().setValue("-invokecmd");
692         }
693
694         // novalidation
695
if (novalidation) {
696             genicTask.createArg().setValue("-novalidation");
697         }
698
699         // javac
700
if (javac != null) {
701             genicTask.createArg().setValue("-javac");
702             genicTask.createArg().setLine(javac);
703         }
704
705         // javacopts
706
if (javacopts != null && !javacopts.equals("")) {
707             genicTask.createArg().setValue("-javacopts");
708             genicTask.createArg().setValue(javacopts);
709         }
710
711         // rmicopts
712
if (rmicopts != null && !rmicopts.equals("")) {
713             genicTask.createArg().setValue("-rmicopts");
714             genicTask.createArg().setValue(rmicopts);
715         }
716
717         // verbose
718
if (verbose) {
719             genicTask.createArg().setValue("-verbose");
720         }
721
722         // additionalargs
723
if (additionalargs != null) {
724             genicTask.createArg().setLine(additionalargs);
725         }
726
727         // protocols
728
if (protocols != null) {
729             genicTask.createArg().setValue("-protocols");
730             genicTask.createArg().setValue(protocols);
731         }
732
733         // the generated classes must not be added in the generic JAR!
734
genicTask.createArg().setValue("-noaddinjar");
735
736         // jvmopts
737
if (jvmopts != null && !jvmopts.equals("")) {
738             genicTask.createJvmarg().setLine(jvmopts);
739         }
740
741         // input file to process by GenIC
742
genicTask.createArg().setValue(genericJarFile.getPath());
743
744         // calling GenIC task
745
log("Calling GenIC task for " + getConfig().descriptorDir + File.separator + descriptorName + ".",
746                 Project.MSG_VERBOSE);
747
748         if (genicTask.executeJava() != 0) {
749             // Don't delete the temp output directory in case of error:
750

751             //log("Deleting temp output directory '" + outputdir + "'.",
752
// Project.MSG_VERBOSE);
753
// the method deleteOnExit() do not work because the directory is
754
// not empty
755
//deleteAllFiles(outputdir);
756

757             if (!keepgeneric) {
758                 log("Deleting generic JAR " + genericJarFile.toString(), Project.MSG_VERBOSE);
759                 genericJarFile.delete();
760             }
761
762             throw new BuildException("GenIC reported an error.");
763         }
764
765         // add the generated files to the ejbFiles
766
addAllFiles(outputdir, "", ejbFiles);
767     }
768
769     /**
770      * Check the bootstrap class name to use in the given classpath.
771      * @param classpath classpath where the boostrap class must be searched.
772      * @return true if the bootstrap is available in the classpath
773      */

774     private boolean checkBootstrapClassName(Path classpath) {
775         log("Looking for bootstrap class in classpath: " + classpath.toString(), Project.MSG_VERBOSE);
776         AntClassLoader cl = new AntClassLoader(classpath.getProject(), classpath);
777         try {
778             cl.loadClass(JonasDeploymentTool.BOOTSTRAP_CLASS);
779             log("Found Bootstrap class '" + JonasDeploymentTool.BOOTSTRAP_CLASS + "' in classpath.",
780                     Project.MSG_VERBOSE);
781         } catch (ClassNotFoundException JavaDoc cnf1) {
782             log("Bootstrap class '" + JonasDeploymentTool.BOOTSTRAP_CLASS + "' not found in classpath.",
783                     Project.MSG_VERBOSE);
784             return false;
785         }
786         return true;
787     }
788
789     protected void checkConfiguration(String JavaDoc descriptorFileName, SAXParser JavaDoc saxParser) throws BuildException {
790         // jonasroot
791
if (jonasroot == null) {
792             throw new BuildException("The jonasroot attribute is not set.");
793         } else if (!jonasroot.isDirectory()) {
794             throw new BuildException("The jonasroot attribute '" + jonasroot + "' is not a valid directory.");
795         }
796         // jonasbase default value is jonasroot
797
if (jonasbase == null) {
798             jonasbase = jonasroot;
799         }
800
801         // additionalargs
802
if (additionalargs != null && additionalargs.equals("")) {
803             throw new BuildException("Empty additionalargs attribute.");
804         }
805
806         // javac
807
if (javac != null && javac.equals("")) {
808             throw new BuildException("Empty javac attribute.");
809         }
810     }
811
812     /* ----------------------------------------------------------------------------------- */
813     /* utilitary methods */
814     /* ----------------------------------------------------------------------------------- */
815
816     /**
817      * Create a temporary directory for GenIC output.
818      * @return the temp directory.
819      * @throws BuildException if a temp directory cannot be created.
820      */

821     private File JavaDoc createTempDir() throws IOException JavaDoc {
822         File JavaDoc tmpDir = File.createTempFile("genic", null, null);
823         tmpDir.delete();
824         if (!tmpDir.mkdir()) {
825             throw new IOException JavaDoc("Cannot create the temporary directory '" + tmpDir + "'.");
826         }
827         return tmpDir;
828     }
829
830     /**
831      * Delete a file. If the file is a directory, delete recursivly all the
832      * files inside.
833      * @param aFile file to delete.
834      */

835     private void deleteAllFiles(File JavaDoc aFile) {
836         if (aFile.isDirectory()) {
837             File JavaDoc someFiles[] = aFile.listFiles();
838             for (int i = 0; i < someFiles.length; i++) {
839                 deleteAllFiles(someFiles[i]);
840             }
841         }
842         aFile.delete();
843     }
844
845     /**
846      * Add a file to the a given hashtable. If the file is a directory, add
847      * recursivly all the files inside to the hashtable.
848      * @param file the file to add.
849      * @param rootDir the current sub-directory to scan.
850      * @param hashtable the hashtable where to add the files.
851      */

852     private void addAllFiles(File JavaDoc file, String JavaDoc rootDir, Hashtable JavaDoc hashtable) {
853
854         if (!file.exists()) {
855             throw new IllegalArgumentException JavaDoc();
856         }
857
858         String JavaDoc newRootDir;
859         if (file.isDirectory()) {
860             File JavaDoc[] files = file.listFiles();
861             for (int i = 0; i < files.length; i++) {
862                 if (rootDir.length() > 0) {
863                     newRootDir = rootDir + File.separator + files[i].getName();
864                 } else {
865                     newRootDir = files[i].getName();
866                 }
867                 addAllFiles(files[i], newRootDir, hashtable);
868             }
869         } else {
870             hashtable.put(rootDir.replace(File.separatorChar , '/'), file); // for a window platform replace \ by / is necessary
871
}
872     }
873 }
Popular Tags