001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.optional.ejb;
019:
020: import java.io.File;
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.DirectoryScanner;
023: import org.apache.tools.ant.taskdefs.Java;
024: import org.apache.tools.ant.taskdefs.MatchingTask;
025: import org.apache.tools.ant.types.Commandline;
026: import org.apache.tools.ant.types.Path;
027: import org.apache.tools.ant.util.FileUtils;
028:
029: /**
030: * Builds EJB support classes using WebLogic's ejbc tool from a directory containing
031: * a set of deployment descriptors.
032: *
033: *
034: */
035: public class Ejbc extends MatchingTask {
036: /**
037: * The root directory of the tree containing the serialised deployment desciptors. The actual
038: * deployment descriptor files are selected using include and exclude constructs
039: * on the ejbc task provided by the MatchingTask superclass.
040: */
041: private File descriptorDirectory;
042:
043: /**
044: * The directory where generated files are placed.
045: */
046: private File generatedFilesDirectory;
047:
048: /**
049: * The name of the manifest file generated for the EJB jar.
050: */
051: private File generatedManifestFile;
052:
053: /**
054: * The classpath to be used in the weblogic ejbc calls. It must contain the weblogic
055: * classes <b>and</b> the implementation classes of the home and remote interfaces.
056: */
057: private String classpath;
058:
059: /**
060: * The source directory for the home and remote interfaces. This is used to determine if
061: * the generated deployment classes are out of date.
062: */
063: private File sourceDirectory;
064:
065: // CheckStyle:VisibilityModifier OFF - bc
066: /** Whether to keep the generated files */
067: public boolean keepgenerated;
068:
069: // CheckStyle:VisibilityModifier ON
070:
071: /**
072: * Do the work.
073: *
074: * The work is actually done by creating a separate JVM to run a helper task.
075: * This approach allows the classpath of the helper task to be set. Since the
076: * weblogic tools require the class files of the project's home and remote
077: * interfaces to be available in the classpath, this also avoids having to
078: * start ant with the class path of the project it is building.
079: *
080: * @exception BuildException if someting goes wrong with the build
081: */
082: public void execute() throws BuildException {
083: if (descriptorDirectory == null
084: || !descriptorDirectory.isDirectory()) {
085: throw new BuildException("descriptors directory "
086: + descriptorDirectory.getPath() + " is not valid");
087: }
088: if (generatedFilesDirectory == null
089: || !generatedFilesDirectory.isDirectory()) {
090: throw new BuildException("dest directory "
091: + generatedFilesDirectory.getPath()
092: + " is not valid");
093: }
094:
095: if (sourceDirectory == null || !sourceDirectory.isDirectory()) {
096: throw new BuildException("src directory "
097: + sourceDirectory.getPath() + " is not valid");
098: }
099:
100: String systemClassPath = System.getProperty("java.class.path");
101: String execClassPath = FileUtils.translatePath(systemClassPath
102: + ":" + classpath + ":" + generatedFilesDirectory);
103: // get all the files in the descriptor directory
104: DirectoryScanner ds = super
105: .getDirectoryScanner(descriptorDirectory);
106:
107: String[] files = ds.getIncludedFiles();
108:
109: Java helperTask = new Java(this );
110: helperTask.setFork(true);
111: helperTask
112: .setClassname("org.apache.tools.ant.taskdefs.optional.ejb.EjbcHelper");
113: String args = "";
114: args += " " + descriptorDirectory;
115: args += " " + generatedFilesDirectory;
116: args += " " + sourceDirectory;
117: args += " " + generatedManifestFile;
118: args += " " + keepgenerated;
119:
120: for (int i = 0; i < files.length; ++i) {
121: args += " " + files[i];
122: }
123:
124: Commandline.Argument arguments = helperTask.createArg();
125: arguments.setLine(args);
126: helperTask.setClasspath(new Path(getProject(), execClassPath));
127: if (helperTask.executeJava() != 0) {
128: throw new BuildException("Execution of ejbc helper failed");
129: }
130: }
131:
132: /**
133: * get the keep generated attribute.
134: * @return the attribute.
135: */
136: public boolean getKeepgenerated() {
137: return keepgenerated;
138: }
139:
140: /**
141: * Set the directory from where the serialized deployment descriptors are
142: * to be read.
143: *
144: * @param dirName the name of the directory containing the serialised deployment descriptors.
145: */
146: public void setDescriptors(String dirName) {
147: descriptorDirectory = new File(dirName);
148: }
149:
150: /**
151: * Set the directory into which the support classes, RMI stubs, etc are to be written.
152: *
153: * @param dirName the name of the directory into which code is generated
154: */
155: public void setDest(String dirName) {
156: generatedFilesDirectory = new File(dirName);
157: }
158:
159: /**
160: * If true, ejbc will keep the
161: * intermediate Java files used to build the class files.
162: * This can be useful when debugging.
163: * @param newKeepgenerated a boolean as a string.
164: */
165: public void setKeepgenerated(String newKeepgenerated) {
166: keepgenerated = Boolean.valueOf(newKeepgenerated.trim())
167: .booleanValue();
168:
169: }
170:
171: /**
172: * Set the name of the generated manifest file.
173: *
174: * For each EJB that is processed an entry is created in this file. This can then be used
175: * to create a jar file for dploying the beans.
176: *
177: * @param manifestFilename the name of the manifest file to be generated.
178: */
179: public void setManifest(String manifestFilename) {
180: generatedManifestFile = new File(manifestFilename);
181: }
182:
183: /**
184: * Set the classpath to be used for this compilation.
185: * @param s the classpath (as a string) to use.
186: */
187: public void setClasspath(String s) {
188: this .classpath = FileUtils.translatePath(s);
189: }
190:
191: /**
192: * Set the directory containing the source code for the home interface, remote interface
193: * and public key class definitions.
194: *
195: * @param dirName the directory containg the source tree for the EJB's interface classes.
196: */
197: public void setSrc(String dirName) {
198: sourceDirectory = new File(dirName);
199: }
200: }
|