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: import java.util.Iterator;
019: import java.util.Map;
020: import java.util.Properties;
021:
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.Project;
024: import org.apache.tools.ant.taskdefs.Java;
025: import org.apache.tools.ant.types.Commandline;
026: import org.apache.tools.ant.types.Environment;
027: import org.apache.tools.ant.types.Path;
028:
029: import com.metaboss.util.AntUtils;
030:
031: /** The abstract base class for all metaboss generator invocation tasks. */
032: public abstract class MetaBossImplementationGeneratorTask extends
033: MetaBossModelToolTask {
034: private File mDestinationDir = null;
035: private Path mClasspath = null;
036: private Environment mImplementationEnvironment = null;
037: private String mRef = null;
038: private String mGeneratorClassName = null;
039: private String mImplementationname = "default"; // Default is reserved for mapping coming from to the jndi properties
040:
041: /** Constructor. Leaves ref attribute name unassigned. Useful when there is more than one choice */
042: protected MetaBossImplementationGeneratorTask() {
043: }
044:
045: /** The setter for the "generatorclassname" attribute */
046: public void setGeneratorclassname(String pGeneratorclassname) {
047: mGeneratorClassName = pGeneratorclassname;
048: }
049:
050: // The getter for the "generatorclassname" attribute
051: protected String getGeneratorclassname() {
052: if (mGeneratorClassName == null)
053: throw new BuildException(
054: "Missing 'generatorclassname' attribute, which is mandatory for <"
055: + getTaskName() + "> task.");
056: return mGeneratorClassName;
057: }
058:
059: /** The setter for the "destinationdir" attribute */
060: public void setDestinationdir(File pDestinationDir) {
061: mDestinationDir = pDestinationDir;
062: }
063:
064: // The getter for the "destinationdir" attribute
065: protected File getDestinationdir() {
066: if (mDestinationDir == null)
067: throw new BuildException(
068: "Missing 'destinationdir' attribute, which is mandatory for <"
069: + getTaskName() + "> task.");
070: return mDestinationDir;
071: }
072:
073: /** The setter for the "ref" attribute */
074: public void setRef(String pRef) {
075: mRef = pRef;
076: }
077:
078: // The getter for the "Ref" attribute. Passed to the destination application as an argument
079: protected String getRef() {
080: if (mRef == null)
081: throw new BuildException(
082: "Missing 'ref' attribute, which is mandatory for <"
083: + getTaskName() + "> task.");
084: return mRef;
085: }
086:
087: /** The setter for the "implementationname" attribute */
088: public void setImplementationname(String pImplementationname) {
089: mImplementationname = pImplementationname;
090: }
091:
092: // The getter for the "implementationname" attribute
093: protected String getImplementationname() {
094: if (mImplementationname == null)
095: throw new BuildException(
096: "Missing 'implementationname' attribute, which is mandatory for <"
097: + getTaskName() + "> task.");
098: return mImplementationname;
099: }
100:
101: /** Adds the variable to the implementation environment - to be passed to the implementation */
102: public void addImplementationEnvironment(
103: Environment.Variable pVariable) {
104: if (mImplementationEnvironment == null)
105: mImplementationEnvironment = new Environment();
106: mImplementationEnvironment.addVariable(pVariable);
107: }
108:
109: /** The setter for the "classpath" attribute */
110: public void setClasspath(Path pClasspath) {
111: mClasspath = pClasspath;
112: }
113:
114: // Builds classpath of all things required by generator
115: protected Path getClasspath() throws BuildException {
116: String lJavaHome = AntUtils
117: .getMandatoryEnvironmentProperty("JAVA_HOME");
118: String lMetaBossHome = getMetaBossHome().getAbsolutePath();
119:
120: Path lClasspath = new Path(getProject());
121: lClasspath.createPathElement().setPath(
122: lJavaHome + File.separator + "lib" + File.separator
123: + "tools.jar");
124: lClasspath.addExisting(getPath("MetaBossModelUserClasspath"));
125: if (mClasspath != null)
126: lClasspath.addExisting(mClasspath);
127: return lClasspath;
128: }
129:
130: // The method executing the task
131: public void execute() throws BuildException {
132: Project lThisProject = getProject();
133: // Now create and execute the java task
134: Java lJavaTask = (Java) lThisProject.createTask("java");
135: lJavaTask.setTaskName(getTaskName());
136: lJavaTask.setClassname(getGeneratorclassname());
137: {
138: Environment.Variable lVariable = new Environment.Variable();
139: lVariable.setKey("MetaBoss.Home");
140: lVariable.setFile(getMetaBossHome());
141: lJavaTask.addSysproperty(lVariable);
142: }
143: {
144: Environment.Variable lVariable = new Environment.Variable();
145: lVariable.setKey("MetaBoss.ModelDir");
146: lVariable.setFile(getModelDir());
147: lJavaTask.addSysproperty(lVariable);
148: }
149:
150: Properties lInvocationParams = getInvocationParams();
151: Iterator lIter = lInvocationParams.entrySet().iterator();
152: while (lIter.hasNext()) {
153: Map.Entry lEntry = (Map.Entry) lIter.next();
154: Environment.Variable lVariable = new Environment.Variable();
155: lVariable.setKey("MetaBoss." + (String) lEntry.getKey());
156: lVariable.setValue((String) lEntry.getValue());
157: lJavaTask.addSysproperty(lVariable);
158: }
159:
160: // Application arguments
161: Commandline.Argument lArg0 = lJavaTask.createArg();
162: lArg0.setValue(getImplementationname());
163: Commandline.Argument lArg1 = lJavaTask.createArg();
164: lArg1.setValue(getDestinationdir().getAbsolutePath());
165: Commandline.Argument lArg2 = lJavaTask.createArg();
166: lArg2.setValue(getRef());
167: // Deal with extra arguments
168: String[] lExtraArguments = getExtraArguments();
169: for (int i = 0; i < lExtraArguments.length; i++) {
170: Commandline.Argument lArgN = lJavaTask.createArg();
171: lArgN.setValue(lExtraArguments[i]);
172: }
173: Path lClasspath = lJavaTask.createClasspath();
174: lClasspath.addExisting(getClasspath());
175:
176: // Need fork because of class loader hierarchy problem (may need to come fix JarClassLoader)
177: // when runs inprocess (and this means that jars from the classpath are most probably are loaded by
178: // Ant's classloader) any class loaded by JarClassLoader (i.e.typetemplate) is not able to load
179: // any of the classes from jars from the classpath. Forking seem to fix it
180: lJavaTask.setFork(true);
181: lJavaTask.setFailonerror(true);
182: lJavaTask.execute();
183: }
184: }
|