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.examples;
016:
017: import java.io.File;
018: import java.util.Properties;
019:
020: import javax.naming.Context;
021: import javax.naming.InitialContext;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.log4j.PropertyConfigurator;
026:
027: import com.metaboss.enterprise.BaseException;
028: import com.metaboss.sdlctools.models.ModelRepository;
029: import com.metaboss.sdlctools.models.ModelValidationException;
030: import com.metaboss.sdlctools.models.metabossmodel.ModelUtils;
031: import com.metaboss.util.MetaBossSpecificUtils;
032:
033: /** This example shows how to convert UML model to the MetaBoss Model */
034: public class ConvertExistingUMLToMetaBossModelExample {
035: // Commons Logging instance.
036: private static final Log sLogger;
037: static {
038: // Read metaboss installation specific properties
039: MetaBossSpecificUtils.setupSystemProperties();
040: // Configure logger as early as possible
041: PropertyConfigurator.configure(System.getProperties());
042: sLogger = LogFactory
043: .getLog(ConvertExistingUMLToMetaBossModelExample.class);
044: }
045:
046: // Initialise enterprise model properties
047: private static File sEnterpriseModelFile = new File(
048: "C:/HatMaker.xml");
049: private static File sUMLModelFile = new File("c:/HatMakerUML.xmi");
050:
051: public static void main(String[] args) {
052: try {
053: ModelRepository lModelRepository = null;
054: try {
055: Context lContext = new InitialContext();
056: lModelRepository = (ModelRepository) lContext
057: .lookup(ModelRepository.COMPONENT_URL);
058: sLogger.info("Opening UML model from "
059: + sUMLModelFile.getAbsolutePath() + "...");
060: String lSourceModelName = sUMLModelFile
061: .getAbsolutePath();
062: lModelRepository.openModel(lSourceModelName,
063: sUMLModelFile,
064: ModelRepository.METAMODEL_NAME_UML);
065: String lTargetModelName = sEnterpriseModelFile
066: .getAbsolutePath();
067: sLogger.info("Converting model to MetaBoss...");
068: ModelUtils.convertUMLModelToMetaBossModel(
069: lSourceModelName, lTargetModelName);
070: sLogger
071: .info("Rectifying possible constraint violations...");
072: lModelRepository.rectifyModel(lTargetModelName);
073: sLogger.info("Exporting the model to "
074: + sEnterpriseModelFile + "...");
075: // Delete target file if necessary
076: if (sEnterpriseModelFile.exists())
077: sEnterpriseModelFile.delete();
078: lModelRepository.exportModel(lTargetModelName, false,
079: sEnterpriseModelFile, new Properties());
080: sLogger.info("Done");
081: } finally {
082: if (lModelRepository != null)
083: lModelRepository.close();
084: }
085: } catch (Throwable t) {
086: handleException(t);
087: }
088: }
089:
090: // This helper must be called from all tools when exception has been caught
091: // it will print the exception in the best possible manner
092: static void handleException(Throwable pThrowable) {
093: System.out.println("Caught exception "
094: + pThrowable.getClass().getName());
095: if (pThrowable instanceof BaseException) {
096: pThrowable.printStackTrace(System.out);
097: Throwable t = pThrowable.getCause();
098: while (t != null) {
099: System.out.println();
100: System.out.println();
101: System.out.println("Cause : " + t.getClass().getName());
102: t.printStackTrace(System.out);
103: // Special case for model validation exception - it has explanation
104: if (t instanceof ModelValidationException) {
105: System.out.println(((ModelValidationException) t)
106: .getFullExplanation());
107: break;
108: } else if (t instanceof BaseException) {
109: t = ((BaseException) t).getCause();
110: } else
111: break;
112: }
113: } else {
114: System.out.println(pThrowable.getMessage());
115: pThrowable.printStackTrace(System.out);
116: }
117: }
118: }
|