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.ArrayList;
019: import java.util.Arrays;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Set;
024:
025: import javax.naming.Context;
026: import javax.naming.InitialContext;
027:
028: import org.apache.tools.ant.BuildException;
029:
030: import com.metaboss.sdlctools.applications.anttasks.MetaBossModelToolTask;
031: import com.metaboss.sdlctools.applications.anttasks.ModelElementRefType;
032: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
033: import com.metaboss.sdlctools.services.codegeneration.BSGenericGenerator;
034:
035: /** The Ant task used to invoke MetaBoss Generic pluggable generators.
036: * <p>In addition to atttributes supported by {@link com.metaboss.sdlctools.applications.anttasks.MetaBossModelToolTask MetaBossModelToolTask},
037: * this task supports following attributes:
038: * <table border="1" cellpadding="2" cellspacing="0">
039: * <tr>
040: * <th>Attribute Name</th>
041: * <th>Attribute Description</th>
042: * <th>Required</th>
043: * </tr>
044: * <tr>
045: * <td valign="top">planname</td>
046: * <td valign="top">Mandatory string attribute used to specify the name of the plan to execute.
047: * </td>
048: * <td valign="top">Yes</td>
049: * </tr>
050: * <tr>
051: * <td valign="top">ref</td>
052: * <td valign="top">Optional string attribute used to specify the reference of the
053: * topmost model element to execute the plan with. If it is not given - plan will be
054: * executed on the whole enteprise model.</td>
055: * <td valign="top">No</td>
056: * </tr>
057: * <tr>
058: * <td valign="top">destinationdir</td>
059: * <td valign="top">The mandatory directory location where generated files shall be stored.
060: * Directory will be created if it does not exist</td>
061: * <td valign="top">Yes</td>
062: * </tr>
063: * </table>
064: */
065: public class MetaBossGeneratorTask extends MetaBossModelToolTask {
066: private File mDestinationDir = null;
067: private String mPlanName = null;
068: private String mRef = null;
069: // List of the custom references to the model elements. If this
070: // list is empty it means that whole root element should be used
071: private List mModelElementIncludes = null;
072:
073: /** Default constructor */
074: public MetaBossGeneratorTask() {
075: // This task uses existing model
076: super (true);
077: }
078:
079: /** The setter for the "planname" attribute */
080: public void setPlanName(String pPlanName) {
081: mPlanName = pPlanName;
082: }
083:
084: /** The getter for the "planname" attribute. */
085: public String getPlanName() {
086: if (mPlanName == null)
087: throw new BuildException(
088: "Missing 'PlanName' attribute, which is mandatory for <"
089: + getTaskName() + "> task.");
090: return mPlanName;
091: }
092:
093: /** The setter for the "ref" attribute */
094: public void setRef(String pRef) {
095: mRef = pRef;
096: }
097:
098: /** The getter for the "ref" attribute. */
099: public String getRef() {
100: return mRef;
101: }
102:
103: /** The special creator asking to include only particular ModelElements in to the generation process.
104: * This allows to filter down elements owned by root element. For example if 'ref' attribute of the
105: * module dealing with services is referring to the System, the IncludeModelElement subelements could be used
106: * to only specify certain subset of services */
107: public ModelElementRefType createIncludeModelElement()
108: throws BuildException {
109: if (mModelElementIncludes == null)
110: mModelElementIncludes = new ArrayList();
111: ModelElementRefType lModelElementRefType = new ModelElementRefType(
112: this , null);
113: mModelElementIncludes.add(lModelElementRefType);
114: return lModelElementRefType;
115: }
116:
117: /** The setter for the "destinationdir" attribute */
118: public void setDestinationdir(File pDestinationDir) {
119: mDestinationDir = pDestinationDir;
120: }
121:
122: // The getter for the "destinationdir" attribute
123: protected File getDestinationdir() {
124: if (mDestinationDir == null)
125: throw new BuildException(
126: "Missing 'destinationdir' attribute, which is mandatory for <"
127: + getTaskName() + "> task.");
128: return mDestinationDir;
129: }
130:
131: // The method executing the tool
132: public void runTool() throws Exception {
133: // Get the generator
134: Context ctx = new InitialContext();
135: BSGenericGenerator lGenerator = (BSGenericGenerator) ctx
136: .lookup(BSGenericGenerator.COMPONENT_URL);
137: // Cal various flavours of the generator depending what has been set
138: if (mRef == null && mModelElementIncludes == null) {
139: // Generator will run against the whole model
140: lGenerator.generateDirectoryAsPerPlan(getDestinationdir()
141: .getAbsolutePath(), getPlanName(),
142: getInvocationParameters());
143: } else if (mRef != null && mModelElementIncludes == null) {
144: // Generator will run against particular model element
145: lGenerator
146: .generateDirectoryAsPerPlanForMetaBossModelElement(
147: getDestinationdir().getAbsolutePath(),
148: getPlanName(), mRef,
149: getInvocationParameters());
150: } else {
151: // Generator will run against the set of elements
152: ModelElement lRootModelElement = getRef() != null ? this
153: .getModelElement(getRef(), null) : null;
154:
155: // Collect all modules from all definitions
156: Set lAllElements = new HashSet();
157: for (Iterator lIter = mModelElementIncludes.iterator(); lIter
158: .hasNext();) {
159: ModelElementRefType lModelElementRefType = (ModelElementRefType) lIter
160: .next();
161: lAllElements
162: .addAll(Arrays
163: .asList(lModelElementRefType
164: .getReferencedModelElements(lRootModelElement)));
165: }
166: lGenerator.generateDirectoryAsPerPlan(getDestinationdir()
167: .getAbsolutePath(), getPlanName(), lAllElements,
168: getInvocationParameters());
169: }
170: }
171: }
|