001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package org.apache.tools.ant.taskdefs.optional.metaboss;
016:
017: import java.io.File;
018:
019: import org.apache.tools.ant.BuildException;
020: import org.apache.tools.ant.Project;
021: import org.apache.tools.ant.taskdefs.Java;
022: import org.apache.tools.ant.types.Commandline;
023: import org.apache.tools.ant.types.Environment;
024: import org.apache.tools.ant.types.Path;
025:
026: import com.metaboss.util.AntUtils;
027:
028: /** The abstract base class for all metaboss generator invocation tasks. */
029: public abstract class MetaBossGeneratorToolTask extends
030: MetaBossModelToolTask {
031: private File mDestinationDir = null;
032: private Path mClasspath = null;
033: private Environment mImplementationEnvironment = null;
034: private String mRef = null;
035: private String mGeneratorClassName = null;
036: private String mImplementationname = "default"; // Default is reserved for mapping coming from to the jndi properties
037:
038: /** Constructor. */
039: protected MetaBossGeneratorToolTask() {
040: }
041:
042: /** The setter for the "generatorclassname" attribute */
043: public void setGeneratorclassname(String pGeneratorclassname) {
044: mGeneratorClassName = pGeneratorclassname;
045: }
046:
047: // The getter for the "generatorclassname" attribute
048: protected String getGeneratorclassname() {
049: if (mDestinationDir == null)
050: throw new BuildException(
051: "Missing 'generatorclassname' attribute, which is mandatory for <"
052: + getTaskName() + "> task.");
053: return mGeneratorClassName;
054: }
055:
056: /** The setter for the "destinationdir" attribute */
057: public void setDestinationdir(File pDestinationDir) {
058: mDestinationDir = pDestinationDir;
059: }
060:
061: // The getter for the "destinationdir" attribute
062: protected File getDestinationdir() {
063: if (mDestinationDir == null)
064: throw new BuildException(
065: "Missing 'destinationdir' attribute, which is mandatory for <"
066: + getTaskName() + "> task.");
067: return mDestinationDir;
068: }
069:
070: /** The setter for the "ref" attribute */
071: public void setRef(String pRef) {
072: mRef = pRef;
073: }
074:
075: // The getter for the "Ref" attribute. Passed to the destination application as an argument
076: protected String getRef() {
077: if (mRef == null)
078: throw new BuildException(
079: "Missing 'ref' attribute, which is mandatory for <"
080: + getTaskName() + "> task.");
081: return mRef;
082: }
083:
084: /** The setter for the "implementationname" attribute */
085: public void setImplementationname(String pImplementationname) {
086: mImplementationname = pImplementationname;
087: }
088:
089: // The getter for the "implementationname" attribute
090: protected String getImplementationname() {
091: if (mImplementationname == null)
092: throw new BuildException(
093: "Missing 'implementationname' attribute, which is mandatory for <"
094: + getTaskName() + "> task.");
095: return mImplementationname;
096: }
097:
098: /** Adds the variable to the implementation environment - to be passed to the implementation */
099: public void addImplementationEnvironment(
100: Environment.Variable pVariable) {
101: if (mImplementationEnvironment == null)
102: mImplementationEnvironment = new Environment();
103: mImplementationEnvironment.addVariable(pVariable);
104: }
105:
106: /** The setter for the "classpath" attribute */
107: public void setClasspath(Path pClasspath) {
108: mClasspath = pClasspath;
109: }
110:
111: // Builds classpath of all things required by generator
112: protected Path getClasspath() throws BuildException {
113: String lJavaHome = AntUtils
114: .getMandatoryEnvironmentProperty("JAVA_HOME");
115: String lMetaBossHome = getMetaBossHome().getAbsolutePath();
116:
117: Path lClasspath = new Path(getProject());
118: lClasspath.createPathElement().setPath(
119: lJavaHome + File.separator + "lib" + File.separator
120: + "tools.jar");
121: lClasspath.createPathElement().setPath(
122: lMetaBossHome + File.separator + "thirdpartylib"
123: + File.separator + "log4j-1.2.8.jar");
124: lClasspath.createPathElement().setPath(
125: lMetaBossHome + File.separator + "thirdpartylib"
126: + File.separator + "jaxb-rt-1.0-ea.jar");
127: lClasspath.createPathElement().setPath(
128: lMetaBossHome + File.separator + "thirdpartylib"
129: + File.separator + "velocity-1.3.1.jar");
130: lClasspath.createPathElement().setPath(
131: lMetaBossHome + File.separator + "thirdpartylib"
132: + File.separator
133: + "commons-collections-2.1.jar");
134: lClasspath.createPathElement().setPath(
135: lMetaBossHome + File.separator + "lib" + File.separator
136: + "MetaBossCore.jar");
137: lClasspath.createPathElement().setPath(
138: lMetaBossHome + File.separator + "lib" + File.separator
139: + "MetaBossComponentNamingProvider.jar");
140: lClasspath.createPathElement().setPath(
141: lMetaBossHome + File.separator + "lib" + File.separator
142: + "MetaBossEnterprise.jar");
143: lClasspath.createPathElement().setPath(
144: lMetaBossHome + File.separator + "lib" + File.separator
145: + "MetaBossModel.jar");
146: lClasspath.createPathElement().setPath(
147: lMetaBossHome + File.separator + "lib" + File.separator
148: + "MetaBossJDKTools.jar");
149: lClasspath.createPathElement().setPath(
150: lMetaBossHome + File.separator + "lib" + File.separator
151: + "MetaBossGenerators.jar");
152: if (mClasspath != null)
153: lClasspath.addExisting(mClasspath);
154: return lClasspath;
155: }
156:
157: // The method executing the task
158: public void execute() throws BuildException {
159: Project lThisProject = getProject();
160: // Now create and execute the java task
161: Java lJavaTask = (Java) lThisProject.createTask("java");
162: lJavaTask.setTaskName(getTaskName());
163: lJavaTask.setClassname(getGeneratorclassname());
164: {
165: Environment.Variable lVariable = new Environment.Variable();
166: lVariable.setKey("MetaBoss.Home");
167: lVariable.setFile(getMetaBossHome());
168: lJavaTask.addSysproperty(lVariable);
169: }
170: {
171: Environment.Variable lVariable = new Environment.Variable();
172: lVariable.setKey("MetaBoss.ModelDir");
173: lVariable.setFile(getModelDir());
174: lJavaTask.addSysproperty(lVariable);
175: }
176: Commandline.Argument lArg0 = lJavaTask.createArg();
177: lArg0.setValue(getImplementationname());
178: Commandline.Argument lArg1 = lJavaTask.createArg();
179: lArg1.setValue(getDestinationdir().getAbsolutePath());
180: Commandline.Argument lArg2 = lJavaTask.createArg();
181: lArg2.setValue(getRef());
182:
183: Path lClasspath = lJavaTask.createClasspath();
184: lClasspath.addExisting(getClasspath());
185:
186: // Need fork because of class loader hierarchy problem (may need to come fix JarClassLoader)
187: // when runs inprocess (and this means that jars from the classpath are most probably are loaded by
188: // Ant's classloader) any class loaded by JarClassLoader (i.e.typetemplate) is not able to load
189: // any of the classes from jars from the classpath. Forking seem to fix it
190: lJavaTask.setFork(true);
191: lJavaTask.setFailonerror(true);
192: lJavaTask.execute();
193: }
194: }
|