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.io.File;
018: import java.util.ArrayList;
019: import java.util.Iterator;
020:
021: import javax.naming.Context;
022: import javax.naming.InitialContext;
023: import javax.naming.NamingException;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.log4j.PropertyConfigurator;
028:
029: import com.metaboss.enterprise.BaseException;
030: import com.metaboss.enterprise.bs.BSDomainObjectInvocationException;
031: import com.metaboss.enterprise.bs.BSException;
032: import com.metaboss.enterprise.bs.BSIllegalArgumentException;
033: import com.metaboss.enterprise.bs.BSNamingAndDirectoryServiceInvocationException;
034: import com.metaboss.sdlctools.models.ModelRepository;
035: import com.metaboss.sdlctools.models.ModelRepositoryException;
036: import com.metaboss.sdlctools.models.ModelValidationException;
037: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Enterprise;
038: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Service;
039: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Servicemodule;
040: import com.metaboss.util.MetaBossSpecificUtils;
041:
042: class ApplicationUtils {
043: // Commons Logging instance.
044: private static Log sLogger = null;
045:
046: static void initialiseApplication() throws BSException {
047: try {
048: // Read metaboss installation specific properties
049: MetaBossSpecificUtils.setupSystemProperties();
050: // Configure logger as early as possible, but after we have read property files
051: PropertyConfigurator.configure(System.getProperties());
052: // Now we can initialise logger
053: sLogger = LogFactory.getLog(ApplicationUtils.class);
054: // Load the model from "MetaBoss.ModelDir"
055: String lEnterpriseModelDir = System
056: .getProperty("MetaBoss.ModelDir");
057: if (lEnterpriseModelDir == null
058: || lEnterpriseModelDir.length() == 0)
059: throw new BSIllegalArgumentException(
060: "MetaBoss.ModelDir java system property must be defined. It must point to the top level directory of the enterprise model.");
061: File lModelDir = new File(lEnterpriseModelDir)
062: .getAbsoluteFile();
063: File lModelFile = new File(lEnterpriseModelDir
064: + File.separator + "Model.xml").getAbsoluteFile();
065: String lModelName = lModelFile.getAbsolutePath();
066: sLogger.info("Loading MetaBossEnterpriseModel from "
067: + lModelDir.getAbsolutePath() + " file.");
068: Context lContext = new InitialContext();
069: ModelRepository lModelRepository = (ModelRepository) lContext
070: .lookup(ModelRepository.COMPONENT_URL);
071: lModelRepository.openModel(lModelName, lModelFile,
072: "MetaBossMetaModel");
073: lModelRepository.setDefaultModel(lModelName);
074: sLogger
075: .info("Successfully loaded MetaBossEnterpriseModel from "
076: + lModelDir.getAbsolutePath() + " file.");
077: } catch (NamingException e) {
078: throw new BSNamingAndDirectoryServiceInvocationException(
079: "Unable to locate model repository implementation",
080: e);
081: } catch (ModelRepositoryException e) {
082: throw new BSDomainObjectInvocationException(
083: "Unable to open enterprise model", e);
084: }
085: }
086:
087: // Helper. Gets array of all refs of all services for the enterprise
088: static String[] getAllServiceRefs(Enterprise pEnterprise)
089: throws Exception {
090: ArrayList lServiceRefs = new ArrayList();
091: Iterator lSystemsIter = pEnterprise.getSystems().iterator();
092: while (lSystemsIter.hasNext()) {
093: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System lSystem = (com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System) lSystemsIter
094: .next();
095: Iterator lServicemodulesIter = lSystem.getServicemodules()
096: .iterator();
097: while (lServicemodulesIter.hasNext()) {
098: Servicemodule lServicemodule = (Servicemodule) lServicemodulesIter
099: .next();
100: Iterator lServicesIter = lServicemodule.getServices()
101: .iterator();
102: while (lServicesIter.hasNext()) {
103: Service lService = (Service) lServicesIter.next();
104: lServiceRefs.add(lService.getRef());
105: }
106: }
107: }
108: return (String[]) lServiceRefs.toArray(new String[lServiceRefs
109: .size()]);
110: }
111:
112: // Helper. Gets array of all refs of all services for the system
113: static String[] getAllServiceRefs(
114: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System pSystem)
115: throws Exception {
116: ArrayList lServiceRefs = new ArrayList();
117: Iterator lServicemodulesIter = pSystem.getServicemodules()
118: .iterator();
119: while (lServicemodulesIter.hasNext()) {
120: Servicemodule lServicemodule = (Servicemodule) lServicemodulesIter
121: .next();
122: Iterator lServicesIter = lServicemodule.getServices()
123: .iterator();
124: while (lServicesIter.hasNext()) {
125: Service lService = (Service) lServicesIter.next();
126: lServiceRefs.add(lService.getRef());
127: }
128: }
129: return (String[]) lServiceRefs.toArray(new String[lServiceRefs
130: .size()]);
131: }
132:
133: // Helper. Gets array of all refs of all services for the servicemodule
134: static String[] getAllServiceRefs(Servicemodule pServicemodule)
135: throws Exception {
136: ArrayList lServiceRefs = new ArrayList();
137: Iterator lServicesIter = pServicemodule.getServices()
138: .iterator();
139: while (lServicesIter.hasNext()) {
140: Service lService = (Service) lServicesIter.next();
141: lServiceRefs.add(lService.getRef());
142: }
143: return (String[]) lServiceRefs.toArray(new String[lServiceRefs
144: .size()]);
145: }
146:
147: // This helper must be called from all tools when exception has been caught
148: // it will print the exception in the best possible manner
149: static void handleException(Throwable pThrowable) {
150: System.out.println("Caught exception "
151: + pThrowable.getClass().getName());
152: if (pThrowable instanceof BaseException) {
153: pThrowable.printStackTrace(System.out);
154: Throwable t = pThrowable.getCause();
155: while (t != null) {
156: System.out.println();
157: System.out.println();
158: System.out.println("Cause : " + t.getClass().getName());
159: t.printStackTrace(System.out);
160: // Special case for model validation exception - it has explanation
161: if (t instanceof ModelValidationException) {
162: System.out.println(((ModelValidationException) t)
163: .getFullExplanation());
164: break;
165: } else if (t instanceof BaseException) {
166: t = ((BaseException) t).getCause();
167: } else
168: break;
169: }
170: } else {
171: System.out.println(pThrowable.getMessage());
172: pThrowable.printStackTrace(System.out);
173: }
174: }
175: }
|