1 package org.andromda.core;
2
3 import java.io.EOFException;
4 import java.io.InputStream;
5 import java.net.ConnectException;
6 import java.net.URL;
7 import org.andromda.core.common.AndroMDALogger;
8 import org.andromda.core.common.ComponentContainer;
9 import org.andromda.core.common.ExceptionUtils;
10 import org.andromda.core.configuration.Configuration;
11 import org.andromda.core.engine.Engine;
12 import org.andromda.core.metafacade.ModelValidationMessage;
13 import org.andromda.core.server.Client;
14 import org.apache.log4j.Logger;
15
16
17
18
19
20
21
22
23
24
25 public final class AndroMDA
26 {
27
28
29
30 private static final Logger LOGGER = Logger.getLogger(AndroMDA.class);
31
32
33
34
35 private Engine engine;
36
37
38
39
40
41
42 public static AndroMDA newInstance()
43 {
44 return new AndroMDA();
45 }
46
47
48
49
50 private AndroMDA()
51 {
52 AndroMDALogger.initialize();
53 this.engine = Engine.newInstance();
54 }
55
56
57
58
59
60
61
62 public void run(final URL configurationUri)
63 {
64 ExceptionUtils.checkNull(
65 "configurationUri",
66 configurationUri);
67 this.run(Configuration.getInstance(configurationUri), false, null);
68 }
69
70
71
72
73
74
75
76 public void run(final InputStream configurationStream)
77 {
78 ExceptionUtils.checkNull(
79 "configurationStream",
80 configurationStream);
81 this.run(Configuration.getInstance(configurationStream), false, null);
82 }
83
84
85
86
87
88
89
90 public void run(final String configuration)
91 {
92 ExceptionUtils.checkEmpty(
93 "configuration",
94 configuration);
95 this.run(Configuration.getInstance(configuration), false, null);
96 }
97
98
99
100
101
102
103 public void initialize(final Configuration configuration)
104 {
105 ExceptionUtils.checkNull(
106 "configuration",
107 configuration);
108 this.engine.initialize(configuration);
109 }
110
111
112
113
114
115
116
117 public void initialize(final InputStream configurationStream)
118 {
119 ExceptionUtils.checkNull(
120 "configurationStream",
121 configurationStream);
122 this.engine.initialize(Configuration.getInstance(configurationStream));
123 }
124
125
126
127
128
129
130
131 public void initialize(final String configuration)
132 {
133 ExceptionUtils.checkEmpty(
134 "configuration",
135 configuration);
136 this.engine.initialize(Configuration.getInstance(configuration));
137 }
138
139
140
141
142
143
144
145 public void initialize(final URL configurationUri)
146 {
147 ExceptionUtils.checkNull(
148 "configurationUri",
149 configurationUri);
150 this.run(Configuration.getInstance(configurationUri), false, null);
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public ModelValidationMessage[] run(final Configuration configuration,
166 boolean lastModifiedCheck, final String historyDir)
167 {
168 ModelValidationMessage[] messages = null;
169 if (configuration != null)
170 {
171 final Client serverClient = (Client)ComponentContainer.instance().findRequiredComponent(Client.class);
172 boolean client = true;
173
174
175
176 if (configuration.getServer() != null)
177 {
178 try
179 {
180 serverClient.start(configuration);
181 }
182 catch (final Throwable throwable)
183 {
184 final Throwable cause = ExceptionUtils.getRootCause(throwable);
185 if (cause instanceof ConnectException ||
186 cause instanceof EOFException)
187 {
188
189
190 client = false;
191 }
192 else
193 {
194 throw new RuntimeException(throwable);
195 }
196 }
197 }
198 else
199 {
200 client = false;
201 }
202
203
204 if (!client)
205 {
206 this.engine.initialize(configuration);
207 messages = this.engine.run(configuration, lastModifiedCheck, historyDir);
208 }
209 }
210 else
211 {
212 LOGGER.warn("AndroMDA could not run because no configuration was defined");
213 }
214 return messages == null ? new ModelValidationMessage[0] : messages;
215 }
216
217
218
219
220 public void shutdown()
221 {
222 this.engine.shutdown();
223 }
224 }