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;
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.Properties;
024: import java.util.Set;
025:
026: import javax.jmi.reflect.JmiException;
027: import javax.naming.Context;
028: import javax.naming.InitialContext;
029: import javax.naming.NamingException;
030:
031: import org.apache.tools.ant.BuildException;
032:
033: import com.metaboss.enterprise.bs.BSException;
034: import com.metaboss.sdlctools.applications.anttasks.builder.MetaBossBuilderTask;
035: import com.metaboss.sdlctools.applications.anttasks.builder.ModuleDefinition;
036: import com.metaboss.sdlctools.applications.anttasks.builder.ToolInvocationDefinition;
037: import com.metaboss.sdlctools.applications.anttasks.builder.tools.JarPackagerInvocationDefinition;
038: import com.metaboss.sdlctools.applications.anttasks.builder.tools.JavaCompilerInvocationDefinitionUtils;
039: import com.metaboss.sdlctools.applications.anttasks.builder.tools.ServiceDistributionGeneratorInvocationDefinition;
040: import com.metaboss.sdlctools.applications.anttasks.builder.tools.CoreCodeGeneratorInvocationDefinition;
041: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
042: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Enterprise;
043: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Servicemodule;
044: import com.metaboss.sdlctools.services.codegeneration.BSServiceDistributionGenerator;
045: import com.metaboss.sdlctools.services.codegeneration.CodeGenerationStylesheetAccessor;
046: import com.metaboss.sdlctools.services.codegenerationstylesheet.STEnterpriseStylesheet;
047: import com.metaboss.sdlctools.services.codegenerationstylesheet.STServicemoduleStylesheet;
048: import com.metaboss.sdlctools.services.codegenerationstylesheet.STSystemStylesheet;
049: import com.metaboss.util.StringUtils;
050:
051: /** The definition of how to build system distribution module */
052: public class BusinessServicesDistributionModuleDefinition extends
053: ModuleDefinition {
054: // Initialise metadata for supplying in the constructor
055: private static ElementMetadata sElementMetadata = new ElementMetadata();
056: static {
057: sElementMetadata.ElementTypeName = "BusinessServicesDistributionModule";
058: sElementMetadata.SupportsModelElementRefs = true;
059: sElementMetadata.AllowedModelElementTypes = new Class[] {
060: Enterprise.class,
061: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System.class,
062: Servicemodule.class };
063: }
064:
065: private String mType = null;
066: private BSServiceDistributionGenerator mGenerator = null;
067: private Set mServicemodulesToDo = new HashSet();
068: private String mDestinationArchiveName = null;
069:
070: /** The only available constructor */
071: public BusinessServicesDistributionModuleDefinition(
072: MetaBossBuilderTask pOwnerTask) {
073: super (pOwnerTask, sElementMetadata);
074: }
075:
076: // The setter for the "type" attribute
077: public void setType(String pType) {
078: mType = pType;
079: }
080:
081: // The getter for the "type" attribute
082: public String getType() {
083: if (mType == null)
084: throw new BuildException(
085: "Missing 'type' attribute, which is mandatory for "
086: + getModuleName() + " definition.");
087: return mType;
088: }
089:
090: /** Called when initialisation of parameters has been completed and generation is about to commence */
091: public void completeInitialisation() throws BuildException {
092: try {
093: // Work on included module elements
094: ModelElement[] lIncludedModelElements = getIncludedModelElements();
095: // First collect list of al domains to generate implementations for
096: for (int lElementsIndex = 0; lElementsIndex < lIncludedModelElements.length; lElementsIndex++) {
097: ModelElement lIncludedModelElement = lIncludedModelElements[lElementsIndex];
098: mServicemodulesToDo.addAll(Arrays.asList(getOwnerTask()
099: .findModelElementsByXPath(
100: lIncludedModelElement,
101: "descendant-or-self::Servicemodule",
102: new Class[] { Servicemodule.class })));
103: }
104:
105: // Create suffix for the module from the type of distribution
106: StringBuffer lModuleSuffix = new StringBuffer();
107: String[] lTypeParts = getType().split("\\.", 0);
108: for (int i = 0; i < lTypeParts.length; i++)
109: lModuleSuffix.append(StringUtils.suggestName(
110: lTypeParts[i], true, false));
111:
112: // Work on the module name
113: String lModuleName = getModuleName();
114: if (lModuleName == null) {
115: ModelElement lRootModelElement = getRootModelElement();
116: if (lRootModelElement instanceof Enterprise) {
117: // Set up module name for the domain
118: STEnterpriseStylesheet lEntepriseStylesheet = CodeGenerationStylesheetAccessor
119: .getEnterpriseStylesheet(lRootModelElement
120: .getName());
121: lModuleName = lEntepriseStylesheet
122: .getBusinessServicesDistributionModuleName();
123: } else if (lRootModelElement instanceof com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System) {
124: // Set up module name for the system
125: STSystemStylesheet lSystemStylesheet = CodeGenerationStylesheetAccessor
126: .getSystemStylesheet(lRootModelElement
127: .getRef());
128: lModuleName = lSystemStylesheet
129: .getBusinessServicesDistributionModuleName();
130: } else {
131: // We are given a servicemodule
132: STServicemoduleStylesheet lServicemodulesStylesheet = CodeGenerationStylesheetAccessor
133: .getServicemoduleStylesheet(lRootModelElement
134: .getRef());
135: lModuleName = lServicemodulesStylesheet
136: .getBusinessServicesDistributionModuleName();
137: }
138: }
139: // We now have a automatically generated modules name. Se if we need to use it or the explicit one
140: StringBuffer lModuleNameBuffer = new StringBuffer(
141: lModuleName);
142: // Finish building the archive name
143: String lVersionToken = getOwnerTask().getVersionToken();
144: if (lVersionToken != null) {
145: lModuleNameBuffer.append("_");
146: lModuleNameBuffer.append(lVersionToken);
147: }
148: lModuleNameBuffer.append(".jar");
149: mDestinationArchiveName = lModuleNameBuffer.toString();
150:
151: // Initialise the generator
152: Properties lContextProps = new Properties();
153: lContextProps
154: .setProperty(
155: "com.metaboss.naming.component.com.metaboss.sdlctools.services.codegeneration.BSServiceDistributionGenerator",
156: "com.metaboss.sdlctools.services.codegeneration.servicedistributiongenerator."
157: + getType());
158: Context lContext = new InitialContext(lContextProps);
159: mGenerator = (BSServiceDistributionGenerator) lContext
160: .lookup(BSServiceDistributionGenerator.COMPONENT_URL);
161: } catch (NamingException e) {
162: throw new BuildException(
163: "Unable to locate generator for the adapter. Caught NamingException: "
164: + e.getMessage());
165: } catch (JmiException e) {
166: throw new BuildException(
167: "Caught exception while preparing to build module. "
168: + e.getMessage());
169: } catch (BSException e) {
170: throw new BuildException(
171: "Caught exception while preparing to build module. "
172: + e.getMessage());
173: }
174: }
175:
176: // Returns plan to invoke any number of code generators necessary to build the module
177: public ToolInvocationDefinition[] getGenerationPlan() {
178: List lInvocations = new ArrayList();
179: for (Iterator lServicemodulesIterator = mServicemodulesToDo
180: .iterator(); lServicemodulesIterator.hasNext();) {
181: Servicemodule lServicemodule = (Servicemodule) lServicemodulesIterator
182: .next();
183: // Will need to generate system core in order to get types, services etc.
184: CoreCodeGeneratorInvocationDefinition lSystemCoreGeneratorInvocationDefinition = new CoreCodeGeneratorInvocationDefinition(
185: getOwnerTask());
186: lSystemCoreGeneratorInvocationDefinition
187: .setSystemRef(lServicemodule.getSystem().getRef());
188: lInvocations.add(lSystemCoreGeneratorInvocationDefinition);
189: // Now add distribution generator
190: ServiceDistributionGeneratorInvocationDefinition lServicemoduleDistributionGeneratorInvocationDefinition = new ServiceDistributionGeneratorInvocationDefinition(
191: getOwnerTask());
192: lServicemoduleDistributionGeneratorInvocationDefinition
193: .setType(getType());
194: lServicemoduleDistributionGeneratorInvocationDefinition
195: .setServicemoduleRef(lServicemodule.getRef());
196: lServicemoduleDistributionGeneratorInvocationDefinition
197: .setGenerator(mGenerator);
198: lInvocations
199: .add(lServicemoduleDistributionGeneratorInvocationDefinition);
200: }
201: // Return what we have
202: return (ToolInvocationDefinition[]) lInvocations
203: .toArray(new ToolInvocationDefinition[lInvocations
204: .size()]);
205: }
206:
207: // Returns tasks to invoke any number of compilers necessary to build the module
208: public ToolInvocationDefinition[] getCompilationPlan() {
209: try {
210: List lInvocations = new ArrayList();
211: for (Iterator lServicemodulesIterator = mServicemodulesToDo
212: .iterator(); lServicemodulesIterator.hasNext();) {
213: Servicemodule lServicemodule = (Servicemodule) lServicemodulesIterator
214: .next();
215: // Will need to compile enterprise types
216: lInvocations.add(JavaCompilerInvocationDefinitionUtils
217: .createForEnterpriseTypes(this , lServicemodule
218: .getSystem().getEnterprise()));
219: // Will need to compile system types
220: lInvocations.add(JavaCompilerInvocationDefinitionUtils
221: .createForSystemTypes(this , lServicemodule
222: .getSystem()));
223: // Will need to compile servicemodule interface
224: lInvocations.add(JavaCompilerInvocationDefinitionUtils
225: .createForServicemoduleInterfaces(this ,
226: lServicemodule));
227: // Will need to compile servicemodule distribution
228: lInvocations.add(JavaCompilerInvocationDefinitionUtils
229: .createForServicemoduleDistribution(this ,
230: lServicemodule, mGenerator));
231: }
232: // Return what we have
233: return (ToolInvocationDefinition[]) lInvocations
234: .toArray(new ToolInvocationDefinition[lInvocations
235: .size()]);
236: } catch (BSException e) {
237: throw new BuildException(
238: "Caught exception while generating packaging plan. "
239: + e.getMessage());
240: }
241: }
242:
243: // Returns tasks to invoke any number of packagers necessary to build the module
244: public ToolInvocationDefinition[] getPackagingPlan()
245: throws BuildException {
246: try {
247: JarPackagerInvocationDefinition lJarPackager = new JarPackagerInvocationDefinition(
248: getOwnerTask());
249: lJarPackager.setDestFileName(mDestinationArchiveName);
250: // There may be some additional files to include
251: lJarPackager.addFileSetIncludes(getAdditionalFiles());
252:
253: // Return what we have
254: for (Iterator lServicemodulesIterator = mServicemodulesToDo
255: .iterator(); lServicemodulesIterator.hasNext();) {
256: Servicemodule lServicemodule = (Servicemodule) lServicemodulesIterator
257: .next();
258: String lServicesClientClassesSubdirectory = StringUtils
259: .replace(
260: mGenerator
261: .getServicesClientImplementationPackageName(lServicemodule
262: .getRef()), ".",
263: File.separator);
264: String lServicesServerClassesSubdirectory = StringUtils
265: .replace(
266: mGenerator
267: .getServicesServerImplementationPackageName(lServicemodule
268: .getRef()), ".",
269: File.separator);
270: String lEventPublishersClientClassesSubdirectory = StringUtils
271: .replace(
272: mGenerator
273: .getEventPublishersClientImplementationPackageName(lServicemodule
274: .getRef()), ".",
275: File.separator);
276: String lEventPublishersServerClassesSubdirectory = StringUtils
277: .replace(
278: mGenerator
279: .getEventPublishersServerImplementationPackageName(lServicemodule
280: .getRef()), ".",
281: File.separator);
282: lJarPackager
283: .addClassInclude(lServicesClientClassesSubdirectory
284: + File.separator + "*.*");
285: lJarPackager
286: .addClassInclude(lServicesServerClassesSubdirectory
287: + File.separator + "*.*");
288: lJarPackager
289: .addClassInclude(lEventPublishersClientClassesSubdirectory
290: + File.separator + "*.*");
291: lJarPackager
292: .addClassInclude(lEventPublishersServerClassesSubdirectory
293: + File.separator + "*.*");
294: }
295: // Return what we have
296: return new ToolInvocationDefinition[] { lJarPackager };
297: } catch (BSException e) {
298: throw new BuildException(
299: "Caught exception while generating packaging plan. "
300: + e.getMessage());
301: }
302: }
303: }
|