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.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.ModuleDefinition;
035: import com.metaboss.sdlctools.applications.anttasks.builder.ModuleElementDefinition;
036: import com.metaboss.sdlctools.applications.anttasks.builder.ToolInvocationDefinition;
037: import com.metaboss.sdlctools.applications.anttasks.builder.tools.JavaCompilerInvocationDefinitionUtils;
038: import com.metaboss.sdlctools.applications.anttasks.builder.tools.ServiceImplementationGeneratorInvocationDefinition;
039: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
040: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Enterprise;
041: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Service;
042: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Servicemodule;
043: import com.metaboss.sdlctools.services.codegeneration.BSServiceImplementationGenerator;
044: import com.metaboss.util.StringUtils;
045:
046: /** The definition of how to build a generated implementation of the business service. */
047: public class BusinessServiceGeneratedImplementationDefinition extends
048: ModuleElementDefinition {
049: // Initialise metadata for supplying in the constructor
050: private static ElementMetadata sElementMetadata = new ElementMetadata();
051: static {
052: sElementMetadata.ElementTypeName = "BusinessServiceGeneratedImplementation";
053: sElementMetadata.SupportsModelElementRefs = true;
054: sElementMetadata.AllowedModelElementTypes = new Class[] {
055: Enterprise.class,
056: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System.class,
057: Servicemodule.class, Service.class, };
058: }
059: // The type of the implementation
060: private String mType = null;
061: private BSServiceImplementationGenerator mServiceImplementationGenerator = null;
062: private Set mServicesToDo = new HashSet();
063:
064: /** The only available constructor */
065: public BusinessServiceGeneratedImplementationDefinition(
066: ModuleDefinition pOwnerModule) {
067: super (pOwnerModule, sElementMetadata);
068: }
069:
070: // The setter for the "type" attribute
071: public void setType(String pType) {
072: mType = pType;
073: }
074:
075: // The getter for the "type" attribute
076: public String getType() {
077: if (mType == null)
078: throw new BuildException(
079: "Missing 'type' attribute, which is mandatory for generated implementation definition.");
080: return mType;
081: }
082:
083: // Called when initialisation of parameters has been completed and generation is about to commence
084: public void completeInitialisation() throws BuildException {
085: try {
086: // First obtain the implementation generator
087: Properties lContextProps = new Properties();
088: lContextProps
089: .setProperty(
090: "com.metaboss.naming.component.com.metaboss.sdlctools.services.codegeneration.BSServiceImplementationGenerator",
091: "com.metaboss.sdlctools.services.codegeneration.serviceimplementationgenerator."
092: + getType());
093: Context lContext = new InitialContext(lContextProps);
094: mServiceImplementationGenerator = (BSServiceImplementationGenerator) lContext
095: .lookup(BSServiceImplementationGenerator.COMPONENT_URL);
096:
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: mServicesToDo.addAll(Arrays.asList(getOwnerTask()
103: .findModelElementsByXPath(
104: lIncludedModelElement,
105: "descendant-or-self::Service",
106: new Class[] { Service.class })));
107: }
108: } catch (NamingException e) {
109: throw new BuildException(
110: "Unable to locate generator for the Service Implementation. Caught NamingException: "
111: + e.getMessage());
112: } catch (JmiException e) {
113: throw new BuildException(
114: "Caught exception while preparing to build module. "
115: + e.getMessage());
116: }
117: }
118:
119: /** Returns array of storages included in this definition */
120: public Service[] getIncludedServices() {
121: return (Service[]) mServicesToDo
122: .toArray(new Service[mServicesToDo.size()]);
123: }
124:
125: /** Return tool invocations */
126: public ToolInvocationDefinition[] getGenerationPlan()
127: throws BSException {
128: List lInvocations = new ArrayList();
129: for (Iterator lServicesIterator = mServicesToDo.iterator(); lServicesIterator
130: .hasNext();) {
131: Service lService = (Service) lServicesIterator.next();
132: ServiceImplementationGeneratorInvocationDefinition lServiceImplementationGeneratorInvocationDefinition = new ServiceImplementationGeneratorInvocationDefinition(
133: getOwnerTask());
134: lServiceImplementationGeneratorInvocationDefinition
135: .setServiceRef(lService.getRef());
136: lServiceImplementationGeneratorInvocationDefinition
137: .setType(getType());
138: lServiceImplementationGeneratorInvocationDefinition
139: .setGenerator(getGenerator());
140: lInvocations
141: .add(lServiceImplementationGeneratorInvocationDefinition);
142: }
143: // Return what we have
144: return (ToolInvocationDefinition[]) lInvocations
145: .toArray(new ToolInvocationDefinition[lInvocations
146: .size()]);
147: }
148:
149: // Returns tasks to do after generation. (Normally compillation would go in here
150: public ToolInvocationDefinition[] getCompilationPlan() {
151: try {
152: List lInvocations = new ArrayList();
153: for (Iterator lServicesIterator = mServicesToDo.iterator(); lServicesIterator
154: .hasNext();) {
155: Service lService = (Service) lServicesIterator.next();
156: // Will need to compile enterprise types
157: lInvocations.add(JavaCompilerInvocationDefinitionUtils
158: .createForEnterpriseTypes(this , lService
159: .getServicemodule().getSystem()
160: .getEnterprise()));
161: // Will need to compile system types
162: lInvocations.add(JavaCompilerInvocationDefinitionUtils
163: .createForSystemTypes(this , lService
164: .getServicemodule().getSystem()));
165: // Will need to compile servicemodule interfaces types
166: lInvocations.add(JavaCompilerInvocationDefinitionUtils
167: .createForServicemoduleInterfaces(this ,
168: lService.getServicemodule()));
169: // Now copile the actual implementation
170: lInvocations.add(JavaCompilerInvocationDefinitionUtils
171: .createForGeneratedServiceImplementation(this ,
172: getType(), lService, getGenerator()));
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 packaging plan. "
181: + e.getMessage());
182: }
183: }
184:
185: /** Return includes for the class files */
186: public String[] getJarClassIncludes() throws BSException {
187: List lJarClassIncludes = new ArrayList();
188: for (Iterator lServicesIterator = mServicesToDo.iterator(); lServicesIterator
189: .hasNext();) {
190: Service lService = (Service) lServicesIterator.next();
191: String lImplementationPackage = getGenerator()
192: .getPackageNameForService(lService.getRef());
193: String lImplementationSubdirectory = StringUtils.replace(
194: lImplementationPackage, ".", File.separator);
195: lJarClassIncludes.add(lImplementationSubdirectory
196: + File.separator + "*.*");
197: }
198: // Return what we have
199: return (String[]) lJarClassIncludes
200: .toArray(new String[lJarClassIncludes.size()]);
201: }
202:
203: /** @return generator for the adapter */
204: public BSServiceImplementationGenerator getGenerator() {
205: if (mServiceImplementationGenerator == null)
206: throw new BuildException("Element <"
207: + sElementMetadata.ElementTypeName
208: + ">has not been initialised properly.");
209: return mServiceImplementationGenerator;
210: }
211: }
|