1 package org.andromda.maven.plugin.configuration;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.StringReader;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.net.URLClassLoader;
9 import java.util.List;
10 import java.util.Properties;
11 import org.andromda.core.common.ResourceUtils;
12 import org.andromda.core.configuration.Configuration;
13 import org.apache.commons.lang.ObjectUtils;
14 import org.apache.maven.artifact.Artifact;
15 import org.apache.maven.artifact.factory.ArtifactFactory;
16 import org.apache.maven.artifact.repository.ArtifactRepository;
17 import org.apache.maven.model.Dependency;
18 import org.apache.maven.model.Plugin;
19 import org.apache.maven.plugin.AbstractMojo;
20 import org.apache.maven.project.MavenProject;
21 import org.apache.maven.settings.Settings;
22 import org.apache.maven.shared.filtering.PropertyUtils;
23 import org.codehaus.plexus.util.InterpolationFilterReader;
24
25
26
27
28
29
30
31
32
33 public abstract class AbstractConfigurationMojo
34 extends AbstractMojo
35 {
36
37
38
39 private static final String MAPPINGS_PATH = "META-INF/andromda/mappings";
40
41
42
43
44
45
46
47
48 protected Configuration getConfiguration(final URL configurationUri)
49 throws IOException
50 {
51 final String contents = this.replaceProperties(ResourceUtils.getContents(configurationUri));
52 final Configuration configuration = Configuration.getInstance(contents);
53 final URL mappingsUrl = ResourceUtils.getResource(MAPPINGS_PATH);
54 if (mappingsUrl != null)
55 {
56 configuration.addMappingsSearchLocation(mappingsUrl.toString());
57 }
58 return configuration;
59 }
60
61
62
63
64
65
66
67 protected Properties getProperties()
68 throws IOException
69 {
70
71 final Properties properties = new Properties();
72
73 properties.put(
74 "settings",
75 this.getSettings());
76
77
78 properties.putAll(this.getProject().getProperties());
79 for (final String propertiesFile : (Iterable<String>) this.getPropertyFiles())
80 {
81 final Properties projectProperties = PropertyUtils.loadPropertyFile(
82 new File(propertiesFile),
83 true,
84 true);
85
86 properties.putAll(projectProperties);
87 }
88
89 for (Object objProperty : properties.keySet())
90 {
91 final String property = (String) objProperty;
92 final String value = this.replaceProperties(
93 properties,
94 ObjectUtils.toString(properties.get(property)));
95 properties.put(
96 property,
97 value);
98 }
99
100 properties.putAll(System.getProperties());
101
102 return properties;
103 }
104
105
106
107
108
109
110
111
112
113
114 protected String replaceProperties(final String string)
115 throws IOException
116 {
117 return this.replaceProperties(
118 this.getProperties(),
119 string);
120 }
121
122
123
124
125 private static final String BEGIN_TOKEN = "${";
126
127
128
129
130 private static final String END_TOKEN = "}";
131
132
133
134
135
136
137
138
139
140 private String replaceProperties(
141 final Properties properties,
142 final String string)
143 throws IOException
144 {
145 final StringReader stringReader = new StringReader(string);
146 InterpolationFilterReader reader = new InterpolationFilterReader(stringReader, properties, "${", "}");
147 reader.reset();
148 reader = new InterpolationFilterReader(
149 reader,
150 new BeanProperties(this.getProject()),
151 BEGIN_TOKEN,
152 END_TOKEN);
153 reader = new InterpolationFilterReader(
154 reader,
155 new BeanProperties(this.getSettings()),
156 BEGIN_TOKEN,
157 END_TOKEN);
158 return ResourceUtils.getContents(reader);
159 }
160
161
162
163
164
165
166
167 protected void initializeClasspathFromClassPathElements(final List<String> classpathFiles)
168 throws MalformedURLException
169 {
170 if (classpathFiles != null && !classpathFiles.isEmpty())
171 {
172 final URL[] classpathUrls = new URL[classpathFiles.size()];
173
174 for (int ctr = 0; ctr < classpathFiles.size(); ++ctr)
175 {
176 final File file = new File(classpathFiles.get(ctr));
177 if (this.getLog().isDebugEnabled())
178 {
179 getLog().debug("adding to classpath '" + file + '\'');
180 }
181 classpathUrls[ctr] = file.toURI().toURL();
182 }
183
184 final URLClassLoader loader =
185 new ConfigurationClassLoader(classpathUrls,
186 Thread.currentThread().getContextClassLoader());
187 Thread.currentThread().setContextClassLoader(loader);
188 }
189 }
190
191
192
193
194
195
196
197
198 protected void addPluginDependencies(
199 final String pluginArtifactId,
200 final String scope)
201 {
202 if (pluginArtifactId != null)
203 {
204 final List<Plugin> plugins = this.getPlugins();
205 if (plugins != null)
206 {
207 for (final Plugin plugin : plugins)
208 {
209 if (pluginArtifactId.equals(plugin.getArtifactId()))
210 {
211 final List<Dependency> dependencies = plugin.getDependencies();
212 if (dependencies != null)
213 {
214 for (Dependency dependency : plugin.getDependencies())
215 {
216 this.addDependency(
217 dependency,
218 scope);
219 }
220 }
221 }
222 }
223 }
224 }
225 }
226
227
228 @SuppressWarnings("unchecked")
229
230
231
232
233
234 private void addDependency(
235 final Dependency dependency,
236 final String scope)
237 {
238 final ArtifactRepository localRepository = this.getLocalRepository();
239 final MavenProject project = this.getProject();
240 if (project != null && localRepository != null)
241 {
242 if (dependency != null)
243 {
244 final Artifact artifact =
245 this.getFactory().createArtifact(
246 dependency.getGroupId(),
247 dependency.getArtifactId(),
248 dependency.getVersion(),
249 scope,
250 dependency.getType());
251 final File file = new File(
252 localRepository.getBasedir(),
253 localRepository.pathOf(artifact));
254 artifact.setFile(file);
255 project.getDependencies().add(dependency);
256 project.getArtifacts().add(artifact);
257 }
258 }
259 }
260
261
262
263
264
265
266 protected abstract MavenProject getProject();
267
268
269
270
271
272
273
274 protected abstract List<String> getPropertyFiles();
275
276
277
278
279
280
281 protected abstract Settings getSettings();
282
283
284
285
286
287 protected abstract ArtifactFactory getFactory();
288
289
290
291
292
293
294
295
296
297 protected abstract List<Plugin> getPlugins();
298
299
300
301
302
303
304 protected abstract ArtifactRepository getLocalRepository();
305
306
307
308
309
310
311 protected boolean skip;
312
313
314
315
316
317
318 protected boolean skipTests;
319
320
321
322
323
324
325 protected boolean testFailureIgnore;
326 }