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