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.cmdlinetools;
016:
017: import java.util.ArrayList;
018: import java.util.Properties;
019: import java.util.StringTokenizer;
020:
021: import javax.naming.Context;
022: import javax.naming.InitialContext;
023:
024: import com.metaboss.enterprise.bs.BSException;
025: import com.metaboss.sdlctools.models.ModelRepository;
026: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
027: import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
028: import com.metaboss.sdlctools.models.metabossmodel.ModelElementClass;
029: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Enterprise;
030: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Service;
031: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Servicemodule;
032: import com.metaboss.sdlctools.services.codegeneration.BSServiceImplementationGenerator;
033: import com.metaboss.util.PropertiesUtils;
034:
035: /** This class offers command line access to the code generation of the service implementations.
036: * It expects following arguments :
037: * <OL>
038: * <LI>Implementation mapping. Since every implementation generator must reside in subpackage of the
039: * com.metaboss.sdlctools.services.codegeneration.serviceimplementationgenerator package, implementation mapping argument is only expected to contain
040: * relative package name. <I>For example to invoke developer's simulator implementation generator implemented in "com.metaboss.sdlctools.services.codegeneration.serviceimplementationgenerator.devsimulator"
041: * this argument must only contain "devsimulator"</I>
042: * There is also the reserved word - "default" - if it is used this application will look for implementation
043: * mapping in the jndi properties (if default mapping is not configurred - naming error will occur). This feature
044: * really does not make a lot of sence for the implementation generator (since there is no such a thing as default implementation... well actually
045: * there is, but it is normally handcoded)
046: * It is provided just to satisfy ant integration. This argument can also contain a comma separated list of implementation mappings
047: * in case more than one implementation is required. The result will be just like calling this application
048: * separately for each mapping.</LI>
049: * <LI>Destination directory. The directory to generate code to.</LI>
050: * <LI>Ref. The reference to generate code for. Allowed types are Enterprise, System, Servicemodule or Service.
051: * This argument can also contain a comma separated list of references in case this appliction needs to be run for more than one element of the model.
052: * The result will be just like calling this application separately for each ref.</LI>
053: * </OL>
054: * It expects following java properties :
055: * <UL>
056: * <LI>MetaBoss.Home - this property should point to the top level installation directory of MetaBoss. <I>For example : "-DMetaBoss.Home=c:\MetaBoss-0.1"</I></LI>
057: * <LI>MetaBoss.ModelDir - this property should point to the top level directory of the model. <I>For example : "-DMetaBoss.ModelDir=c:\MetaBoss-0.1\example\EnterpriseModel"</I></LI>
058: * <LI>Any number of properties in form "MetaBoss.<implementation mapping>.<property name> to be passed to actual implementation.
059: * <i>For example to pass property to the devsimulator implementation it must be in form "MetaBoss.devsimulator.<property name></i>"</LI>
060: * </UL>
061: */
062: public class ServiceImplementationGeneratorApplication {
063: public static void main(String[] args) {
064: try {
065: // Call common initialisation
066: ApplicationUtils.initialiseApplication();
067:
068: // Scan the arguments (must be three)
069: if (args == null || args.length != 3)
070: throw new IllegalArgumentException(
071: "Invalid arguments passed to the generator. Expecting four arguments : <implementation name> <destination directory> <reference>");
072: // arg[0] is the implementation mapping - comma separated list of implementations to invoke
073: String lImplementationNames = args[0];
074: // arg[1] is the absolute path to the destination directory
075: String lDestinationDirectory = args[1];
076: // arg[2] is the ref - comma separated list of references to work on
077: String lReferences = args[2];
078:
079: // First load all requested generators
080: ArrayList lGeneratorsList = new ArrayList();
081: StringTokenizer lImplementationNamesTokenizer = new StringTokenizer(
082: lImplementationNames, ",", false);
083: while (lImplementationNamesTokenizer.hasMoreTokens()) {
084: String lImplementationSuffix = lImplementationNamesTokenizer
085: .nextToken();
086: // Prepare the property based on implementation asked for.
087: Properties lContextProps = new Properties();
088: // This is the case where we do not want to use implementations cached for us by
089: // context because this will not allow us to load different implementations.
090: // The com.metaboss.naming.component.DisableContextCaching property switches this off.
091: if (!lImplementationSuffix.equals("default"))
092: lContextProps
093: .setProperty(
094: "com.metaboss.naming.component.com.metaboss.sdlctools.services.codegeneration.BSServiceImplementationGenerator",
095: "com.metaboss.sdlctools.services.codegeneration.serviceimplementationgenerator."
096: + lImplementationSuffix);
097: // Also pass all possible MetaBoss environment properties
098: lContextProps.putAll(PropertiesUtils.filterProperties(
099: System.getProperties(), "MetaBoss.", false));
100: Context ctx = new InitialContext(lContextProps);
101: lGeneratorsList
102: .add(ctx
103: .lookup(BSServiceImplementationGenerator.COMPONENT_URL));
104: }
105: BSServiceImplementationGenerator[] lGenerators = (BSServiceImplementationGenerator[]) lGeneratorsList
106: .toArray(new BSServiceImplementationGenerator[lGeneratorsList
107: .size()]);
108: // Now iterate through references calling all generators on each reference
109: Context lContext = new InitialContext();
110: ModelRepository lModelRepository = (ModelRepository) lContext
111: .lookup(ModelRepository.COMPONENT_URL);
112: MetaBossModelPackage lMetaBossModelPackage = (MetaBossModelPackage) lModelRepository
113: .getDefaultModelExtent();
114: ModelElementClass lModelElementClass = lMetaBossModelPackage
115: .getModelElement();
116: StringTokenizer lReferencesTokenizer = new StringTokenizer(
117: lReferences, ",", false);
118: while (lReferencesTokenizer.hasMoreTokens()) {
119: String lRef = lReferencesTokenizer.nextToken();
120: ModelElement lModelElement = lModelElementClass
121: .getByRef(lRef);
122: if (lModelElement instanceof Enterprise)
123: runGenerators(
124: lGenerators,
125: ApplicationUtils
126: .getAllServiceRefs((Enterprise) lModelElement),
127: lDestinationDirectory);
128: else if (lModelElement instanceof com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System)
129: runGenerators(
130: lGenerators,
131: ApplicationUtils
132: .getAllServiceRefs((com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System) lModelElement),
133: lDestinationDirectory);
134: else if (lModelElement instanceof Servicemodule)
135: runGenerators(
136: lGenerators,
137: ApplicationUtils
138: .getAllServiceRefs((Servicemodule) lModelElement),
139: lDestinationDirectory);
140: else if (lModelElement instanceof Service)
141: runGenerators(lGenerators,
142: new String[] { lModelElement.getRef() },
143: lDestinationDirectory);
144: else
145: throw new IllegalArgumentException(
146: "Invalid reference passed to the generator. Expecting reference to the Service, Servicemodule, System or Enterprisere. Got "
147: + lRef);
148: }
149: System.exit(0);
150: } catch (Throwable t) {
151: ApplicationUtils.handleException(t);
152: System.exit(1);
153: }
154: }
155:
156: // Helper. Executes all generators for all services
157: private static void runGenerators(
158: BSServiceImplementationGenerator[] pGenerators,
159: String[] pServiceRefs, String pGenerationDirectoryPath)
160: throws BSException {
161: for (int i = 0; i < pGenerators.length; i++)
162: for (int j = 0; j < pServiceRefs.length; j++)
163: pGenerators[i].generateSourceCodeForService(
164: pGenerationDirectoryPath, pServiceRefs[j]);
165: }
166: }
|