001: package org.andromda.core;
002:
003: import java.io.InputStream;
004:
005: import java.net.ConnectException;
006: import java.net.URL;
007:
008: import org.andromda.core.common.AndroMDALogger;
009: import org.andromda.core.common.ComponentContainer;
010: import org.andromda.core.common.ExceptionUtils;
011: import org.andromda.core.configuration.Configuration;
012: import org.andromda.core.engine.Engine;
013: import org.andromda.core.metafacade.ModelValidationMessage;
014: import org.andromda.core.server.Client;
015: import org.apache.log4j.Logger;
016:
017: /**
018: * The main entry point to the framework. Handles the processing of models.
019: * Facilitates Model Driven Architecture by enabling the generation of source
020: * code, configuration files, and other such artifacts from a single or multiple
021: * models.
022: *
023: * @see Engine
024: * @author Chad Brandon
025: */
026: public class AndroMDA {
027: /**
028: * The logger instance.
029: */
030: private static final Logger logger = Logger
031: .getLogger(AndroMDA.class);
032:
033: /**
034: * The AndroMDA engine instance.
035: */
036: private Engine engine;
037:
038: /**
039: * Gets a new instance of AndroMDA.
040: *
041: * @return the new instance of AndroMDA.
042: */
043: public static AndroMDA newInstance() {
044: return new AndroMDA();
045: }
046:
047: /**
048: * Hidden constructor.
049: */
050: private AndroMDA() {
051: AndroMDALogger.initialize();
052: this .engine = Engine.newInstance();
053: }
054:
055: /**
056: * Runs AndroMDA with the given configuration.
057: *
058: * @param configurationUri the URI to the configuration file that configures
059: * AndroMDA.
060: */
061: public void run(final URL configurationUri) {
062: ExceptionUtils.checkNull("configurationUri", configurationUri);
063: this .run(Configuration.getInstance(configurationUri));
064: }
065:
066: /**
067: * Runs AndroMDA with the given configuration.
068: *
069: * @param configurationStream the InputStream that contains the
070: * configuration contents for configuring AndroMDA.
071: */
072: public void run(final InputStream configurationStream) {
073: ExceptionUtils.checkNull("configurationStream",
074: configurationStream);
075: this .run(Configuration.getInstance(configurationStream));
076: }
077:
078: /**
079: * Runs AndroMDA with the given configuration.
080: *
081: * @param configuration the String that contains the configuration contents
082: * for configuring AndroMDA.
083: */
084: public void run(final String configuration) {
085: ExceptionUtils.checkEmpty("configuration", configuration);
086: this .run(Configuration.getInstance(configuration));
087: }
088:
089: /**
090: * Initializes AndroMDA without running the engine.
091: *
092: * @param configuration the configuration from which to initialize AndroMDA
093: */
094: public void initialize(final Configuration configuration) {
095: ExceptionUtils.checkNull("configuration", configuration);
096: this .engine.initialize(configuration);
097: }
098:
099: /**
100: * Initializes AndroMDA without running the engine.
101: *
102: * @param configurationStream the InputStream that contains the
103: * configuration contents for configuring AndroMDA.
104: */
105: public void initialize(final InputStream configurationStream) {
106: ExceptionUtils.checkNull("configurationStream",
107: configurationStream);
108: this .engine.initialize(Configuration
109: .getInstance(configurationStream));
110: }
111:
112: /**
113: * Initializes AndroMDA without running the engine.
114: *
115: * @param configuration the String that contains the configuration contents
116: * for configuring AndroMDA.
117: */
118: public void initialize(final String configuration) {
119: ExceptionUtils.checkEmpty("configuration", configuration);
120: this .engine
121: .initialize(Configuration.getInstance(configuration));
122: }
123:
124: /**
125: * Initializes AndroMDA without running the engine.
126: *
127: * @param configurationUri the URI to the configuration file that configures
128: * AndroMDA.
129: */
130: public void initialize(final URL configurationUri) {
131: ExceptionUtils.checkNull("configurationUri", configurationUri);
132: this .run(Configuration.getInstance(configurationUri));
133: }
134:
135: /**
136: * Runs AndroMDA with the given configuration. Determines whether or not
137: * AndroMDA should be run in client/server mode (if the client can contact
138: * the AndroMDA server), or just stand-alone mode if the server can NOT be
139: * contacted.
140: *
141: * @param configuration the configuration instance that configures AndroMDA.
142: * @return an array of model validation messages (if there have been any collected
143: * during AndroMDA execution).
144: */
145: public ModelValidationMessage[] run(
146: final Configuration configuration) {
147: ModelValidationMessage[] messages = null;
148: if (configuration != null) {
149: final Client serverClient = (Client) ComponentContainer
150: .instance().findRequiredComponent(Client.class);
151: boolean client = true;
152:
153: // only attempt to run with the client, if they
154: // have a server defined in their configuration
155: if (configuration.getServer() != null) {
156: try {
157: serverClient.start(configuration);
158: } catch (final ConnectException exception) {
159: // - if we can't connect to the server, it means
160: // we aren't running in client mode
161: client = false;
162: }
163: } else {
164: client = false;
165: }
166:
167: // - since we aren't running in 'client' mode, run the engine as usual
168: if (!client) {
169: this .engine.initialize(configuration);
170: messages = this .engine.run(configuration);
171: }
172: } else {
173: logger
174: .warn("AndroMDA could not run because no configuration was defined");
175: }
176: return messages == null ? new ModelValidationMessage[0]
177: : messages;
178: }
179:
180: /**
181: * Shuts down AndroMDA.
182: */
183: public void shutdown() {
184: this.engine.shutdown();
185: }
186: }
|