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 com.metaboss.sdlctools.applications.anttasks.builder.modules.subelements;
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.jmi.reflect.JmiException;
026:
027: import org.apache.tools.ant.BuildException;
028:
029: import com.metaboss.enterprise.bs.BSException;
030: import com.metaboss.sdlctools.applications.anttasks.builder.ModuleDefinition;
031: import com.metaboss.sdlctools.applications.anttasks.builder.ModuleElementDefinition;
032: import com.metaboss.sdlctools.applications.anttasks.builder.ToolInvocationDefinition;
033: import com.metaboss.sdlctools.applications.anttasks.builder.tools.JavaCompilerInvocationDefinitionUtils;
034: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
035: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Enterprise;
036: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Servicemodule;
037: import com.metaboss.sdlctools.services.codegeneration.CodeGenerationStylesheetAccessor;
038: import com.metaboss.sdlctools.services.codegenerationstylesheet.STServicemoduleStylesheet;
039: import com.metaboss.util.StringUtils;
040:
041: /** The definition of how to build a handcoded implementation of the business service. */
042: public class IncludeBusinessServiceHandcodedImplementationDefinition
043: extends ModuleElementDefinition {
044: // Initialise metadata for supplying in the constructor
045: private static ElementMetadata sElementMetadata = new ElementMetadata();
046: static {
047: sElementMetadata.ElementTypeName = "IncludeBusinessServiceHandcodedImplementation";
048: sElementMetadata.SupportsModelElementRefs = true;
049: sElementMetadata.AllowedModelElementTypes = new Class[] {
050: Enterprise.class,
051: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System.class,
052: Servicemodule.class, };
053: }
054: // The type of the implementation
055: private String mType = null;
056: private Set mServicemodulesToDo = new HashSet();
057: // Additional attribute pinting to the absolute directory where handcoded source resides
058: private File mImplsSrcDir = null;
059:
060: /** The only available constructor */
061: public IncludeBusinessServiceHandcodedImplementationDefinition(
062: ModuleDefinition pOwnerModule) {
063: super (pOwnerModule, sElementMetadata);
064: }
065:
066: // The setter for the "type" attribute
067: public void setType(String pType) {
068: mType = pType;
069: }
070:
071: // The getter for the "type" attribute
072: public String getType() {
073: if (mType == null)
074: throw new BuildException(
075: "Missing 'type' attribute, which is mandatory for generated implementation definition.");
076: return mType;
077: }
078:
079: // The setter for optional "implssrcdir" attribute
080: public void setImplsSrcDir(File pSrcDir) {
081: mImplsSrcDir = pSrcDir;
082: }
083:
084: // The getter for optional "srcdir" attribute
085: public File getImplsSrcDir() throws BuildException {
086: if (mImplsSrcDir != null)
087: return mImplsSrcDir;
088: if (getOwnerTask().getImplsSrcDir() != null)
089: return getOwnerTask().getImplsSrcDir();
090: throw new BuildException(
091: "Missing 'implssrcdir' attribute here and in the task. This is expected to be defined.");
092: }
093:
094: // Called when initialisation of parameters has been completed and generation is about to commence
095: public void completeInitialisation() throws BuildException {
096: try {
097: // Work on included module elements
098: ModelElement[] lIncludedModelElements = getIncludedModelElements();
099: // First collect list of al domains to generate implementations for
100: for (int lElementsIndex = 0; lElementsIndex < lIncludedModelElements.length; lElementsIndex++) {
101: ModelElement lIncludedModelElement = lIncludedModelElements[lElementsIndex];
102: mServicemodulesToDo.addAll(Arrays.asList(getOwnerTask()
103: .findModelElementsByXPath(
104: lIncludedModelElement,
105: "descendant-or-self::Servicemodule",
106: new Class[] { Servicemodule.class })));
107: }
108: } catch (JmiException e) {
109: throw new BuildException(
110: "Caught exception while preparing to build module. "
111: + e.getMessage());
112: }
113: }
114:
115: /** Returns array of storages included in this definition */
116: public Servicemodule[] getIncludedServicemodules() {
117: return (Servicemodule[]) mServicemodulesToDo
118: .toArray(new Servicemodule[mServicemodulesToDo.size()]);
119: }
120:
121: /** @return proposed package name for the given servicemodule */
122: public String getPackageName(Servicemodule pServicemodule)
123: throws BSException {
124: STServicemoduleStylesheet lServicemoduleStylesheet = CodeGenerationStylesheetAccessor
125: .getServicemoduleStylesheet(pServicemodule);
126: return lServicemoduleStylesheet.getPackageName() + "."
127: + getType().toLowerCase();
128: }
129:
130: /** @return proposed source location for the given servicemodule */
131: public String getSourceLocation(Servicemodule pServicemodule)
132: throws BSException {
133: String lImplementationPackageName = getPackageName(pServicemodule);
134: File lSrcDir = getImplsSrcDir();
135: STServicemoduleStylesheet lServicemoduleStylesheet = CodeGenerationStylesheetAccessor
136: .getServicemoduleStylesheet(pServicemodule);
137: return lSrcDir.getAbsolutePath()
138: + File.separator
139: + StringUtils.replace(lImplementationPackageName, ".",
140: File.separator);
141: }
142:
143: // Returns plan to invoke any number of code generators necessary to build the module
144: public ToolInvocationDefinition[] getGenerationPlan() {
145: List lInvocations = new ArrayList();
146: // Nothing to generate
147: // Return what we have
148: return (ToolInvocationDefinition[]) lInvocations
149: .toArray(new ToolInvocationDefinition[lInvocations
150: .size()]);
151: }
152:
153: // Returns tasks to do after generation. (Normally compillation would go in here
154: public ToolInvocationDefinition[] getCompilationPlan() {
155: try {
156: List lInvocations = new ArrayList();
157: // Compile parts of the core
158: for (Iterator lServicemodulesIterator = mServicemodulesToDo
159: .iterator(); lServicemodulesIterator.hasNext();) {
160: Servicemodule lServicemodule = (Servicemodule) lServicemodulesIterator
161: .next();
162:
163: File lImplementationDirectory = new File(
164: getSourceLocation(lServicemodule))
165: .getAbsoluteFile();
166: if (lImplementationDirectory != null
167: && lImplementationDirectory.exists())
168: lInvocations
169: .add(JavaCompilerInvocationDefinitionUtils
170: .createForAbsoluteSourceDirectory(
171: this ,
172: lImplementationDirectory));
173: }
174: // Return what we have
175: return (ToolInvocationDefinition[]) lInvocations
176: .toArray(new ToolInvocationDefinition[lInvocations
177: .size()]);
178: } catch (BSException e) {
179: throw new BuildException(
180: "Caught exception while generating compillation plan. "
181: + e.getMessage());
182: }
183: }
184:
185: /** Return tool invocations */
186: public String[] getJarClassIncludes() throws BSException {
187: List lJarClassIncludes = new ArrayList();
188: // Compile parts of the core
189: for (Iterator lServicemodulesIterator = mServicemodulesToDo
190: .iterator(); lServicemodulesIterator.hasNext();) {
191: Servicemodule lServicemodule = (Servicemodule) lServicemodulesIterator
192: .next();
193: String lImplementationPackage = getPackageName(lServicemodule);
194: String lImplementationSubdirectory = StringUtils.replace(
195: lImplementationPackage, ".", File.separator);
196: lJarClassIncludes.add(lImplementationSubdirectory
197: + File.separator + "**" + File.separator + "*.*");
198: }
199: // Return what we have
200: return (String[]) lJarClassIncludes
201: .toArray(new String[lJarClassIncludes.size()]);
202: }
203: }
|