1 package org.andromda.maven.plugin.andromdapp;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.StringReader;
6 import java.net.URL;
7 import java.util.List;
8 import java.util.Properties;
9 import org.andromda.core.common.ResourceUtils;
10 import org.apache.commons.lang.ObjectUtils;
11 import org.apache.commons.lang.StringUtils;
12 import org.apache.maven.plugin.AbstractMojo;
13 import org.apache.maven.plugin.MojoExecutionException;
14 import org.apache.maven.project.MavenProject;
15 import org.apache.maven.settings.Settings;
16 import org.apache.maven.shared.filtering.PropertyUtils;
17 import org.codehaus.plexus.util.InterpolationFilterReader;
18
19
20
21
22
23
24
25
26 public abstract class AbstractAndroMDAppMojo
27 extends AbstractMojo
28 {
29
30
31
32
33
34 private String configurationUri;
35
36
37
38
39
40
41 private MavenProject project;
42
43
44
45
46
47
48
49
50
51 protected Settings settings;
52
53
54
55
56 protected List<String> propertyFiles;
57
58
59
60
61
62
63 protected boolean skip;
64
65
66
67
68
69
70 protected boolean skipTests;
71
72
73
74
75
76
77 protected boolean testFailureIgnore;
78
79
80
81
82
83
84
85 protected boolean skipProcessing = false;
86
87
88
89
90
91
92
93 private Properties getProperties()
94 throws IOException
95 {
96
97 final Properties properties = new Properties();
98
99 properties.put(
100 "settings",
101 this.settings);
102
103
104 properties.putAll(this.project.getProperties());
105 if (this.propertyFiles != null)
106 {
107 for (final String propertiesFile : this.propertyFiles)
108 {
109 final Properties projectProperties = PropertyUtils.loadPropertyFile(
110 new File(propertiesFile),
111 true,
112 true);
113 properties.putAll(projectProperties);
114 }
115 }
116
117 for (final Object propertyObject : properties.keySet())
118 {
119 final String property = (String)propertyObject;
120 final String value = this.replaceProperties(properties, ObjectUtils.toString(properties.get(property)));
121 properties.put(property, value);
122 }
123
124 properties.putAll(System.getProperties());
125
126 return properties;
127 }
128
129
130
131
132
133
134
135
136
137
138 protected String replaceProperties(final String string)
139 throws IOException
140 {
141 return this.replaceProperties(this.getProperties(), string);
142 }
143
144
145
146
147
148
149
150
151 protected String getConfigurationContents() throws MojoExecutionException, IOException
152 {
153 String contents = null;
154 if (StringUtils.isNotBlank(this.configurationUri))
155 {
156 final URL configuration = ResourceUtils.toURL(this.configurationUri);
157 if (configuration == null)
158 {
159 throw new MojoExecutionException("No configuration could be loaded from --> '" + this.configurationUri + '\'');
160 }
161 contents = this.replaceProperties(ResourceUtils.getContents(configuration));
162 }
163 return contents;
164 }
165
166
167
168
169
170 private static final String BEGIN_TOKEN = "$${";
171
172
173
174
175 private static final String END_TOKEN = "}";
176
177
178
179
180
181
182
183
184
185 @SuppressWarnings("resource")
186 private String replaceProperties(final Properties properties, final String string) throws IOException
187 {
188 String replacement = null;
189 final StringReader stringReader = new StringReader(string);
190 InterpolationFilterReader reader = new InterpolationFilterReader(stringReader, properties, "${", "}");
191 reader.reset();
192 try
193 {
194 reader = new InterpolationFilterReader(
195 reader,
196 new BeanProperties(this.project),
197 BEGIN_TOKEN,
198 END_TOKEN);
199 reader = new InterpolationFilterReader(
200 reader,
201 new BeanProperties(this.project),
202 BEGIN_TOKEN,
203 END_TOKEN);
204 replacement = ResourceUtils.getContents(reader);
205 }
206 finally
207 {
208 reader.close();
209 }
210 return replacement;
211 }
212
213 }