1 package org.andromda.core.configuration;
2
3 import org.andromda.core.common.ResourceUtils;
4 import org.andromda.core.common.XmlObjectFactory;
5 import org.andromda.core.mapping.Mappings;
6 import org.apache.commons.lang.StringUtils;
7
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.io.Serializable;
11 import java.net.URL;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.Iterator;
16
17
18 /***
19 * This object is configured from the AndroMDA configuration
20 * XML file. Its used to configure AndroMDA before modeling
21 * processing occurs.
22 *
23 * @author Chad Brandon
24 */
25 public class Configuration
26 implements Serializable
27 {
28 /***
29 * Gets a Configuration instance from the given <code>uri</code>.
30 *
31 * @param uri the URI to the configuration file.
32 * @return the configured instance.
33 */
34 public static Configuration getInstance(final URL uri)
35 {
36 final Configuration configuration =
37 (Configuration)XmlObjectFactory.getInstance(Configuration.class).getObject(uri);
38 configuration.setContents(ResourceUtils.getContents(uri));
39 return configuration;
40 }
41
42 /***
43 * Gets a Configuration instance from the given <code>stream</code>.
44 *
45 * @param stream the InputStream containing the configuration file.
46 * @return the configured instance.
47 */
48 public static Configuration getInstance(final InputStream stream)
49 {
50 final Configuration configuration =
51 (Configuration)XmlObjectFactory.getInstance(Configuration.class).getObject(new InputStreamReader(stream));
52 configuration.setContents(ResourceUtils.getContents(new InputStreamReader(stream)));
53 return configuration;
54 }
55
56 /***
57 * Gets a Configuration instance from the given <code>string</code>.
58 *
59 * @param string the String containing the configuration.
60 * @return the configured instance.
61 */
62 public static Configuration getInstance(final String string)
63 {
64 final Configuration configuration =
65 (Configuration)XmlObjectFactory.getInstance(Configuration.class).getObject(string);
66 configuration.setContents(string);
67 return configuration;
68 }
69
70 /***
71 * Initializes this configuration instance.
72 */
73 public void initialize()
74 {
75 this.initializeNamespaces();
76 this.initializeMappings();
77 }
78
79 /***
80 * Stores the repositories for this Configuration instance.
81 */
82 private final Collection repositories = new ArrayList();
83
84 /***
85 * Adds the repository to this configuration.
86 *
87 * @param repository the repository instance.
88 */
89 public void addRepository(final Repository repository)
90 {
91 this.repositories.add(repository);
92 }
93
94 /***
95 * Gets the repository instances belonging to this configuration.
96 *
97 * @return the collection of repository instances.
98 */
99 public Repository[] getRepositories()
100 {
101 return (Repository[])this.repositories.toArray(new Repository[0]);
102 }
103
104 /***
105 * Stores the configuration namespaces.
106 */
107 private final Collection namespaces = new ArrayList();
108
109 /***
110 * Adds a namespace to this configuration.
111 *
112 * @param namespace the configured namespace to add.
113 */
114 public void addNamespace(final Namespace namespace)
115 {
116 this.namespaces.add(namespace);
117 }
118
119 /***
120 * Gets the configuration namespaces.
121 *
122 * @return the array of {@link Namespace} instances.
123 */
124 public Namespace[] getNamespaces()
125 {
126 return (Namespace[])this.namespaces.toArray(new Namespace[0]);
127 }
128
129 /***
130 * Stores the properties for this configuration (these
131 * gobally configure AndroMDA).
132 */
133 private final Collection properties = new ArrayList();
134
135 /***
136 * Adds a property to this configuration instance.
137 *
138 * @param property the property to add.
139 */
140 public void addProperty(final Property property)
141 {
142 this.properties.add(property);
143 }
144
145 /***
146 * Gets the properties belonging to this configuration.
147 *
148 * @return the collection of {@link Property} instances.
149 */
150 public Property[] getProperties()
151 {
152 return (Property[])this.properties.toArray(new Property[0]);
153 }
154
155 /***
156 * Stores the AndroMDA server configuration information.
157 */
158 private Server server;
159
160 /***
161 * Sets the server instance for this configuration.
162 *
163 * @param server the information which configures the AndroMDA server.
164 */
165 public void setServer(final Server server)
166 {
167 this.server = server;
168 }
169
170 /***
171 * Gets the server instance for this configuration.
172 * The {@link Server} holds the information to configure
173 * the AndroMDA server.
174 *
175 * @return the andromda server.
176 */
177 public Server getServer()
178 {
179 return this.server;
180 }
181
182 /***
183 * The locations in which to search for mappings.
184 */
185 private final Collection mappingsSearchLocations = new ArrayList();
186
187 /***
188 * Adds a mappings search location (these are the locations
189 * in which a search for mappings is performed).
190 *
191 * @param location a location path.
192 * @see #addMappingsSearchLocation(String)
193 */
194 public void addMappingsSearchLocation(final Location location)
195 {
196 if (location != null)
197 {
198 this.mappingsSearchLocations.add(location);
199 }
200 }
201
202 /***
203 * Adds a mappings search location path (a location
204 * without a pattern defined).
205 *
206 * @param path a location path.
207 * @see #addMappingsSearchLocation(Location)
208 */
209 public void addMappingsSearchLocation(final String path)
210 {
211 if (path != null)
212 {
213 final Location location = new Location();
214 location.setPath(path);
215 this.mappingsSearchLocations.add(location);
216 }
217 }
218
219 /***
220 * Gets the mappings search locations for this configuration instance.
221 *
222 * @return the mappings search locations.
223 */
224 public Location[] getMappingsSearchLocations()
225 {
226 return (Location[])this.mappingsSearchLocations.toArray(new Location[0]);
227 }
228
229 /***
230 * Stores the contents of the configuration as a string.
231 */
232 private String contents = null;
233
234 /***
235 * Gets the URI from which this instance was
236 * configured or null (it it was not set).
237 * @return the URI as a String instance
238 */
239 public String getContents()
240 {
241 return this.contents;
242 }
243
244 /***
245 * Clears out any caches used by this configuration.
246 */
247 public static void clearCaches()
248 {
249 Model.clearLastModifiedTimes();
250 }
251
252 /***
253 * Sets the contents of this configuration as a
254 * string.
255 * @param contents the contents of this configuration as a string.
256 */
257 private void setContents(final String contents)
258 {
259 this.contents = StringUtils.trimToEmpty(contents);
260 }
261
262 /***
263 * Initializes the namespaces with the namespaces from
264 * this configuration.
265 */
266 private void initializeNamespaces()
267 {
268 final Namespaces namespaces = Namespaces.instance();
269 namespaces.clear();
270 namespaces.addNamespaces(this.getNamespaces());
271 }
272
273 /***
274 * Loads all mappings from the specified mapping search locations.
275 * If the location points to a directory the directory
276 * contents will be loaded, otherwise just the mapping itself will be loaded.
277 */
278 private void initializeMappings()
279 {
280 if (this.mappingsSearchLocations != null)
281 {
282 final Collection mappingsLocations = new ArrayList();
283 final Location[] locations = this.getMappingsSearchLocations();
284 for (int ctr = 0; ctr < locations.length; ctr++)
285 {
286 mappingsLocations.addAll(Arrays.asList(locations[ctr].getResources()));
287 }
288
289
290 Mappings.clearLogicalMappings();
291
292 for (final Iterator iterator = mappingsLocations.iterator(); iterator.hasNext();)
293 {
294 try
295 {
296 Mappings.addLogicalMappings((URL)iterator.next());
297 }
298 catch (final Throwable throwable)
299 {
300
301
302 }
303 }
304
305 Mappings.initializeLogicalMappings();
306 }
307 }
308 }