001: package org.andromda.core.configuration;
002:
003: import org.andromda.core.common.ResourceUtils;
004: import org.andromda.core.common.XmlObjectFactory;
005: import org.andromda.core.mapping.Mappings;
006: import org.apache.commons.lang.StringUtils;
007:
008: import java.io.InputStream;
009: import java.io.InputStreamReader;
010: import java.io.Serializable;
011: import java.net.URL;
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.Collection;
015: import java.util.Iterator;
016:
017: /**
018: * This object is configured from the AndroMDA configuration
019: * XML file. Its used to configure AndroMDA before modeling
020: * processing occurs.
021: *
022: * @author Chad Brandon
023: */
024: public class Configuration implements Serializable {
025: /**
026: * Gets a Configuration instance from the given <code>uri</code>.
027: *
028: * @param uri the URI to the configuration file.
029: * @return the configured instance.
030: */
031: public static Configuration getInstance(final URL uri) {
032: final Configuration configuration = (Configuration) XmlObjectFactory
033: .getInstance(Configuration.class).getObject(uri);
034: configuration.setContents(ResourceUtils.getContents(uri));
035: return configuration;
036: }
037:
038: /**
039: * Gets a Configuration instance from the given <code>stream</code>.
040: *
041: * @param stream the InputStream containing the configuration file.
042: * @return the configured instance.
043: */
044: public static Configuration getInstance(final InputStream stream) {
045: final Configuration configuration = (Configuration) XmlObjectFactory
046: .getInstance(Configuration.class).getObject(
047: new InputStreamReader(stream));
048: configuration.setContents(ResourceUtils
049: .getContents(new InputStreamReader(stream)));
050: return configuration;
051: }
052:
053: /**
054: * Gets a Configuration instance from the given <code>string</code>.
055: *
056: * @param string the String containing the configuration.
057: * @return the configured instance.
058: */
059: public static Configuration getInstance(final String string) {
060: final Configuration configuration = (Configuration) XmlObjectFactory
061: .getInstance(Configuration.class).getObject(string);
062: configuration.setContents(string);
063: return configuration;
064: }
065:
066: /**
067: * Initializes this configuration instance.
068: */
069: public void initialize() {
070: this .initializeNamespaces();
071: this .initializeMappings();
072: }
073:
074: /**
075: * Stores the repositories for this Configuration instance.
076: */
077: private final Collection repositories = new ArrayList();
078:
079: /**
080: * Adds the repository to this configuration.
081: *
082: * @param repository the repository instance.
083: */
084: public void addRepository(final Repository repository) {
085: this .repositories.add(repository);
086: }
087:
088: /**
089: * Gets the repository instances belonging to this configuration.
090: *
091: * @return the collection of repository instances.
092: */
093: public Repository[] getRepositories() {
094: return (Repository[]) this .repositories
095: .toArray(new Repository[0]);
096: }
097:
098: /**
099: * Stores the configuration namespaces.
100: */
101: private final Collection namespaces = new ArrayList();
102:
103: /**
104: * Adds a namespace to this configuration.
105: *
106: * @param namespace the configured namespace to add.
107: */
108: public void addNamespace(final Namespace namespace) {
109: this .namespaces.add(namespace);
110: }
111:
112: /**
113: * Gets the configuration namespaces.
114: *
115: * @return the array of {@link Namespace} instances.
116: */
117: public Namespace[] getNamespaces() {
118: return (Namespace[]) this .namespaces.toArray(new Namespace[0]);
119: }
120:
121: /**
122: * Stores the properties for this configuration (these
123: * gobally configure AndroMDA).
124: */
125: private final Collection properties = new ArrayList();
126:
127: /**
128: * Adds a property to this configuration instance.
129: *
130: * @param property the property to add.
131: */
132: public void addProperty(final Property property) {
133: this .properties.add(property);
134: }
135:
136: /**
137: * Gets the properties belonging to this configuration.
138: *
139: * @return the collection of {@link Property} instances.
140: */
141: public Property[] getProperties() {
142: return (Property[]) this .properties.toArray(new Property[0]);
143: }
144:
145: /**
146: * Stores the AndroMDA server configuration information.
147: */
148: private Server server;
149:
150: /**
151: * Sets the server instance for this configuration.
152: *
153: * @param server the information which configures the AndroMDA server.
154: */
155: public void setServer(final Server server) {
156: this .server = server;
157: }
158:
159: /**
160: * Gets the server instance for this configuration.
161: * The {@link Server} holds the information to configure
162: * the AndroMDA server.
163: *
164: * @return the andromda server.
165: */
166: public Server getServer() {
167: return this .server;
168: }
169:
170: /**
171: * The locations in which to search for mappings.
172: */
173: private final Collection mappingsSearchLocations = new ArrayList();
174:
175: /**
176: * Adds a mappings search location (these are the locations
177: * in which a search for mappings is performed).
178: *
179: * @param location a location path.
180: * @see #addMappingsSearchLocation(String)
181: */
182: public void addMappingsSearchLocation(final Location location) {
183: if (location != null) {
184: this .mappingsSearchLocations.add(location);
185: }
186: }
187:
188: /**
189: * Adds a mappings search location path (a location
190: * without a pattern defined).
191: *
192: * @param path a location path.
193: * @see #addMappingsSearchLocation(Location)
194: */
195: public void addMappingsSearchLocation(final String path) {
196: if (path != null) {
197: final Location location = new Location();
198: location.setPath(path);
199: this .mappingsSearchLocations.add(location);
200: }
201: }
202:
203: /**
204: * Gets the mappings search locations for this configuration instance.
205: *
206: * @return the mappings search locations.
207: */
208: public Location[] getMappingsSearchLocations() {
209: return (Location[]) this .mappingsSearchLocations
210: .toArray(new Location[0]);
211: }
212:
213: /**
214: * Stores the contents of the configuration as a string.
215: */
216: private String contents = null;
217:
218: /**
219: * Gets the URI from which this instance was
220: * configured or null (it it was not set).
221: * @return the URI as a String instance
222: */
223: public String getContents() {
224: return this .contents;
225: }
226:
227: /**
228: * Clears out any caches used by this configuration.
229: */
230: public static void clearCaches() {
231: Model.clearLastModifiedTimes();
232: }
233:
234: /**
235: * Sets the contents of this configuration as a
236: * string.
237: * @param contents the contents of this configuration as a string.
238: */
239: private void setContents(final String contents) {
240: this .contents = StringUtils.trimToEmpty(contents);
241: }
242:
243: /**
244: * Initializes the namespaces with the namespaces from
245: * this configuration.
246: */
247: private void initializeNamespaces() {
248: final Namespaces namespaces = Namespaces.instance();
249: namespaces.clear();
250: namespaces.addNamespaces(this .getNamespaces());
251: }
252:
253: /**
254: * Loads all mappings from the specified mapping search locations.
255: * If the location points to a directory the directory
256: * contents will be loaded, otherwise just the mapping itself will be loaded.
257: */
258: private void initializeMappings() {
259: if (this .mappingsSearchLocations != null) {
260: final Collection mappingsLocations = new ArrayList();
261: final Location[] locations = this
262: .getMappingsSearchLocations();
263: for (int ctr = 0; ctr < locations.length; ctr++) {
264: mappingsLocations.addAll(Arrays.asList(locations[ctr]
265: .getResources()));
266: }
267:
268: // clear out any old cached mappings
269: Mappings.clearLogicalMappings();
270:
271: for (final Iterator iterator = mappingsLocations.iterator(); iterator
272: .hasNext();) {
273: try {
274: Mappings.addLogicalMappings((URL) iterator.next());
275: } catch (final Throwable throwable) {
276: // - ignore the exception (probably means its a file
277: // other than a mapping and in that case we don't care)
278: }
279: }
280: // - now initialize the logical mappings since we've found them all
281: Mappings.initializeLogicalMappings();
282: }
283: }
284: }
|