KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.DirectoryScanner;
23 import org.apache.tools.ant.taskdefs.Java;
24 import org.apache.tools.ant.taskdefs.MatchingTask;
25 import org.apache.tools.ant.types.Commandline;
26 import org.apache.tools.ant.types.Path;
27 import org.apache.tools.ant.util.FileUtils;
28
29 /**
30  * Builds EJB support classes using WebLogic's ejbc tool from a directory containing
31  * a set of deployment descriptors.
32  *
33  *
34  */

35 public class Ejbc extends MatchingTask {
36     /**
37      * The root directory of the tree containing the serialised deployment desciptors. The actual
38      * deployment descriptor files are selected using include and exclude constructs
39      * on the ejbc task provided by the MatchingTask superclass.
40      */

41     private File JavaDoc descriptorDirectory;
42
43     /**
44      * The directory where generated files are placed.
45      */

46     private File JavaDoc generatedFilesDirectory;
47
48     /**
49      * The name of the manifest file generated for the EJB jar.
50      */

51     private File JavaDoc generatedManifestFile;
52
53     /**
54      * The classpath to be used in the weblogic ejbc calls. It must contain the weblogic
55      * classes <b>and</b> the implementation classes of the home and remote interfaces.
56      */

57     private String JavaDoc classpath;
58
59     /**
60      * The source directory for the home and remote interfaces. This is used to determine if
61      * the generated deployment classes are out of date.
62      */

63     private File JavaDoc sourceDirectory;
64
65     // CheckStyle:VisibilityModifier OFF - bc
66
/** Whether to keep the generated files */
67     public boolean keepgenerated;
68     // CheckStyle:VisibilityModifier ON
69

70     /**
71      * Do the work.
72      *
73      * The work is actually done by creating a separate JVM to run a helper task.
74      * This approach allows the classpath of the helper task to be set. Since the
75      * weblogic tools require the class files of the project's home and remote
76      * interfaces to be available in the classpath, this also avoids having to
77      * start ant with the class path of the project it is building.
78      *
79      * @exception BuildException if someting goes wrong with the build
80      */

81     public void execute() throws BuildException {
82         if (descriptorDirectory == null
83             || !descriptorDirectory.isDirectory()) {
84             throw new BuildException("descriptors directory "
85                 + descriptorDirectory.getPath() + " is not valid");
86         }
87         if (generatedFilesDirectory == null
88             || !generatedFilesDirectory.isDirectory()) {
89             throw new BuildException("dest directory "
90                 + generatedFilesDirectory.getPath() + " is not valid");
91         }
92
93         if (sourceDirectory == null
94             || !sourceDirectory.isDirectory()) {
95             throw new BuildException("src directory "
96                 + sourceDirectory.getPath() + " is not valid");
97         }
98
99         String JavaDoc systemClassPath = System.getProperty("java.class.path");
100         String JavaDoc execClassPath
101             = FileUtils.translatePath(systemClassPath + ":" + classpath
102                                          + ":" + generatedFilesDirectory);
103         // get all the files in the descriptor directory
104
DirectoryScanner ds = super.getDirectoryScanner(descriptorDirectory);
105
106         String JavaDoc[] files = ds.getIncludedFiles();
107
108         Java helperTask = new Java(this);
109         helperTask.setFork(true);
110         helperTask.setClassname("org.apache.tools.ant.taskdefs.optional.ejb.EjbcHelper");
111         String JavaDoc args = "";
112         args += " " + descriptorDirectory;
113         args += " " + generatedFilesDirectory;
114         args += " " + sourceDirectory;
115         args += " " + generatedManifestFile;
116         args += " " + keepgenerated;
117
118         for (int i = 0; i < files.length; ++i) {
119             args += " " + files[i];
120         }
121
122         Commandline.Argument arguments = helperTask.createArg();
123         arguments.setLine(args);
124         helperTask.setClasspath(new Path(getProject(), execClassPath));
125         if (helperTask.executeJava() != 0) {
126             throw new BuildException("Execution of ejbc helper failed");
127         }
128     }
129
130     /**
131      * get the keep generated attribute.
132      * @return the attribute.
133      */

134     public boolean getKeepgenerated() {
135         return keepgenerated;
136     }
137
138     /**
139      * Set the directory from where the serialized deployment descriptors are
140      * to be read.
141      *
142      * @param dirName the name of the directory containing the serialised deployment descriptors.
143      */

144     public void setDescriptors(String JavaDoc dirName) {
145         descriptorDirectory = new File JavaDoc(dirName);
146     }
147
148     /**
149      * Set the directory into which the support classes, RMI stubs, etc are to be written.
150      *
151      * @param dirName the name of the directory into which code is generated
152      */

153     public void setDest(String JavaDoc dirName) {
154         generatedFilesDirectory = new File JavaDoc(dirName);
155     }
156
157     /**
158      * If true, ejbc will keep the
159      * intermediate Java files used to build the class files.
160      * This can be useful when debugging.
161      * @param newKeepgenerated a boolean as a string.
162      */

163     public void setKeepgenerated(String JavaDoc newKeepgenerated) {
164         keepgenerated = Boolean.valueOf(newKeepgenerated.trim()).booleanValue();
165
166     }
167
168     /**
169      * Set the name of the generated manifest file.
170      *
171      * For each EJB that is processed an entry is created in this file. This can then be used
172      * to create a jar file for dploying the beans.
173      *
174      * @param manifestFilename the name of the manifest file to be generated.
175      */

176     public void setManifest(String JavaDoc manifestFilename) {
177         generatedManifestFile = new File JavaDoc(manifestFilename);
178     }
179
180     /**
181      * Set the classpath to be used for this compilation.
182      * @param s the classpath (as a string) to use.
183      */

184     public void setClasspath(String JavaDoc s) {
185         this.classpath = FileUtils.translatePath(s);
186     }
187
188     /**
189      * Set the directory containing the source code for the home interface, remote interface
190      * and public key class definitions.
191      *
192      * @param dirName the directory containg the source tree for the EJB's interface classes.
193      */

194     public void setSrc(String JavaDoc dirName) {
195         sourceDirectory = new File JavaDoc(dirName);
196     }
197 }
198
Popular Tags