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: import javax.naming.Context;
027: import javax.naming.InitialContext;
028: import javax.naming.NamingException;
029:
030: import org.apache.tools.ant.BuildException;
031:
032: import com.metaboss.enterprise.bs.BSException;
033: import com.metaboss.sdlctools.applications.anttasks.builder.ModuleDefinition;
034: import com.metaboss.sdlctools.applications.anttasks.builder.ModuleElementDefinition;
035: import com.metaboss.sdlctools.applications.anttasks.builder.ToolInvocationDefinition;
036: import com.metaboss.sdlctools.applications.anttasks.builder.tools.JavaCompilerInvocationDefinitionUtils;
037: import com.metaboss.sdlctools.applications.anttasks.builder.tools.StorageImplementationGeneratorInvocationDefinition;
038: import com.metaboss.sdlctools.applications.anttasks.builder.tools.CoreCodeGeneratorInvocationDefinition;
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.systemimplementationmodel.Domain;
042: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.systemimplementationmodel.domainimplementationmodel.DomainRelationalStorageDefinition;
043: import com.metaboss.sdlctools.services.codegeneration.BSStorageImplementationGenerator;
044: import com.metaboss.sdlctools.services.codegeneration.CodeGenerationStylesheetAccessor;
045: import com.metaboss.sdlctools.services.codegenerationstylesheet.STDomainStylesheet;
046: import com.metaboss.util.StringUtils;
047:
048: /** The definition of included storage implementation. */
049: public class IncludeStorageImplementationDefinition extends
050: ModuleElementDefinition {
051: // Initialise metadata for supplying in the constructor
052: private static ElementMetadata sElementMetadata = new ElementMetadata();
053: static {
054: sElementMetadata.ElementTypeName = "IncludeStorageImplementation";
055: sElementMetadata.SupportsModelElementRefs = true;
056: sElementMetadata.AllowedModelElementTypes = new Class[] {
057: Enterprise.class,
058: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System.class,
059: Domain.class, DomainRelationalStorageDefinition.class };
060: }
061: private BSStorageImplementationGenerator mStorageImplementationGenerator = null;
062: private Set mStoragesToDo = new HashSet();
063:
064: /** The only available constructor */
065: public IncludeStorageImplementationDefinition(
066: ModuleDefinition pOwnerModule) {
067: super (pOwnerModule, sElementMetadata);
068: }
069:
070: // Called when initialisation of parameters has been completed and generation is about to commence
071: public void completeInitialisation() throws BuildException {
072: try {
073: // First obtain the storage implementation generator
074: Context lContext = new InitialContext();
075: mStorageImplementationGenerator = (BSStorageImplementationGenerator) lContext
076: .lookup(BSStorageImplementationGenerator.COMPONENT_URL);
077:
078: // Work on included module elements
079: ModelElement[] lIncludedModelElements = getIncludedModelElements();
080: // First collect list of al domains to generate implementations for
081: for (int lElementsIndex = 0; lElementsIndex < lIncludedModelElements.length; lElementsIndex++) {
082: ModelElement lIncludedModelElement = lIncludedModelElements[lElementsIndex];
083: mStoragesToDo
084: .addAll(Arrays
085: .asList(getOwnerTask()
086: .findModelElementsByXPath(
087: lIncludedModelElement,
088: "descendant-or-self::DomainRelationalStorageDefinition",
089: new Class[] { DomainRelationalStorageDefinition.class })));
090: }
091: } catch (NamingException e) {
092: throw new BuildException(
093: "Unable to locate generator for the Storage Implementation. Caught NamingException: "
094: + e.getMessage());
095: } catch (JmiException e) {
096: throw new BuildException(
097: "Caught exception while preparing to build module. "
098: + e.getMessage());
099: }
100: }
101:
102: /** Returns array of storages included in this definition */
103: public DomainRelationalStorageDefinition[] getIncludedStorages() {
104: return (DomainRelationalStorageDefinition[]) mStoragesToDo
105: .toArray(new DomainRelationalStorageDefinition[mStoragesToDo
106: .size()]);
107: }
108:
109: // Returns plan to invoke any number of code generators necessary to build the module
110: public ToolInvocationDefinition[] getGenerationPlan() {
111: List lInvocations = new ArrayList();
112: for (Iterator lDomainStoragesIterator = mStoragesToDo
113: .iterator(); lDomainStoragesIterator.hasNext();) {
114: DomainRelationalStorageDefinition lDomainRelationalStorageDefinition = (DomainRelationalStorageDefinition) lDomainStoragesIterator
115: .next();
116: // Will need to generate core of the system this is where domain is
117: CoreCodeGeneratorInvocationDefinition lSystemCoreGeneratorInvocationDefinition = new CoreCodeGeneratorInvocationDefinition(
118: getOwnerTask());
119: lSystemCoreGeneratorInvocationDefinition
120: .setSystemRef(lDomainRelationalStorageDefinition
121: .getDomain().getSystem().getRef());
122: lInvocations.add(lSystemCoreGeneratorInvocationDefinition);
123: // Generate storage code
124: StorageImplementationGeneratorInvocationDefinition lStorageImplementationGeneratorInvocationDefinition = new StorageImplementationGeneratorInvocationDefinition(
125: getOwnerTask());
126: lStorageImplementationGeneratorInvocationDefinition
127: .setGenerator(mStorageImplementationGenerator);
128: lStorageImplementationGeneratorInvocationDefinition
129: .setDomainRelationalStorageDefinition(lDomainRelationalStorageDefinition);
130: lInvocations
131: .add(lStorageImplementationGeneratorInvocationDefinition);
132: }
133: // Return what we have
134: return (ToolInvocationDefinition[]) lInvocations
135: .toArray(new ToolInvocationDefinition[lInvocations
136: .size()]);
137: }
138:
139: // Returns tasks to do after generation. (Normally compillation would go in here
140: public ToolInvocationDefinition[] getCompilationPlan() {
141: try {
142: List lInvocations = new ArrayList();
143: // Compile parts of the core
144: for (Iterator lDomainStoragesIterator = mStoragesToDo
145: .iterator(); lDomainStoragesIterator.hasNext();) {
146: DomainRelationalStorageDefinition lDomainRelationalStorageDefinition = (DomainRelationalStorageDefinition) lDomainStoragesIterator
147: .next();
148: Domain lDomain = lDomainRelationalStorageDefinition
149: .getDomain();
150: // Will need to compile enterprise types
151: lInvocations.add(JavaCompilerInvocationDefinitionUtils
152: .createForEnterpriseTypes(this , lDomain
153: .getSystem().getEnterprise()));
154: // Will need to compile system types
155: lInvocations
156: .add(JavaCompilerInvocationDefinitionUtils
157: .createForSystemTypes(this , lDomain
158: .getSystem()));
159: // Storage interfaces
160: lInvocations.add(JavaCompilerInvocationDefinitionUtils
161: .createForStorageInterfaces(this , lDomain));
162: // Storage implementations
163: lInvocations.add(JavaCompilerInvocationDefinitionUtils
164: .createForStorageImplementation(this ,
165: lDomainRelationalStorageDefinition,
166: mStorageImplementationGenerator));
167: }
168: // Return what we have
169: return (ToolInvocationDefinition[]) lInvocations
170: .toArray(new ToolInvocationDefinition[lInvocations
171: .size()]);
172: } catch (BSException e) {
173: throw new BuildException(
174: "Caught exception while generating packaging plan. "
175: + e.getMessage());
176: }
177: }
178:
179: /** Return tool invocations */
180: public String[] getJarClassIncludes() throws BSException {
181: List lJarClassIncludes = new ArrayList();
182: // Compile parts of the core
183: for (Iterator lDomainStoragesIterator = mStoragesToDo
184: .iterator(); lDomainStoragesIterator.hasNext();) {
185: DomainRelationalStorageDefinition lDomainRelationalStorageDefinition = (DomainRelationalStorageDefinition) lDomainStoragesIterator
186: .next();
187: Domain lDomain = lDomainRelationalStorageDefinition
188: .getDomain();
189: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System lSystem = lDomain
190: .getSystem();
191: STDomainStylesheet lDomainStylesheet = CodeGenerationStylesheetAccessor
192: .getDomainStylesheet(lDomain);
193: // Storage interfaces
194: String lStorageInterfacePackage = lDomainStylesheet
195: .getStoragePackageName();
196: String lStorageInterfaceSubdirectory = StringUtils.replace(
197: lStorageInterfacePackage, ".", File.separator);
198: lJarClassIncludes.add(lStorageInterfaceSubdirectory
199: + File.separator + "*.*");
200: // Storage implementation
201: String lStorageImplementationPackage = mStorageImplementationGenerator
202: .getImplementationPackageName(lDomainRelationalStorageDefinition
203: .getRef());
204: String lStorageImplementationSubdirectory = StringUtils
205: .replace(lStorageImplementationPackage, ".",
206: File.separator);
207: lJarClassIncludes.add(lStorageImplementationSubdirectory
208: + File.separator + "**" + File.separator + "*.*");
209: }
210: // Return what we have
211: return (String[]) lJarClassIncludes
212: .toArray(new String[lJarClassIncludes.size()]);
213: }
214: }
|