1 package org.nuiton.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import org.apache.maven.doxia.siterenderer.Renderer;
26 import org.apache.maven.plugins.annotations.Component;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.apache.maven.project.MavenProject;
29 import org.apache.maven.reporting.AbstractMavenReport;
30 import org.apache.maven.reporting.MavenReportException;
31 import org.codehaus.plexus.i18n.I18N;
32 import org.codehaus.plexus.util.StringUtils;
33 import org.nuiton.i18n.I18n;
34 import org.nuiton.i18n.init.ClassPathI18nInitializer;
35 import org.nuiton.i18n.init.DefaultI18nInitializer;
36 import org.nuiton.i18n.init.I18nInitializer;
37
38 import java.io.File;
39 import java.net.MalformedURLException;
40 import java.net.URL;
41 import java.net.URLClassLoader;
42 import java.util.Arrays;
43 import java.util.HashSet;
44 import java.util.Locale;
45 import java.util.Set;
46
47
48
49
50
51
52
53 public abstract class AbstractApplicationConfigReport extends AbstractMavenReport {
54
55
56
57
58
59
60
61
62 @Parameter(property = "config.outputDirectory",
63 defaultValue = "${project.reporting.outputDirectory}",
64 required = true)
65 protected File outputDirectory;
66
67
68
69
70
71
72
73
74 @Parameter(property = "config.outputEncoding",
75 defaultValue = "${project.reporting.outputEncoding}",
76 required = true)
77 protected String outputEncoding;
78
79
80
81
82
83
84
85
86
87
88
89 @Parameter(property = "config.i18nBundleName")
90 protected String i18nBundleName;
91
92
93
94
95
96
97
98
99
100 @Parameter(property = "config.include")
101 protected String include;
102
103
104
105
106
107
108
109
110 @Parameter(property = "config.exclude")
111 protected String exclude;
112
113
114
115
116
117
118
119
120
121 @Parameter(property = "config.verbose", defaultValue = "${maven.verbose}")
122 protected boolean verbose;
123
124
125
126
127
128
129 @Parameter(property = "config.showOptionDetail", defaultValue = "true")
130 protected boolean showOptionDetail;
131
132
133
134
135
136
137 @Parameter(property = "config.skip")
138 protected boolean skip;
139
140
141
142
143
144
145 @Parameter(defaultValue = "${project}", required = true)
146 protected MavenProject project;
147
148
149
150
151
152
153
154 @Component
155 protected Renderer siteRenderer;
156
157
158
159
160
161
162 @Component
163 protected I18N i18n;
164
165
166
167
168
169
170
171 protected ClassLoader newClassLoader;
172
173
174
175
176
177
178 protected Set<ApplicationConfigProvider> configProviders;
179
180 @Override
181 public String getOutputName() {
182 return "config-report";
183 }
184
185 public String getDescription(Locale locale) {
186 return i18n.getString(getOutputName(), locale, "report.description");
187 }
188
189 public String getName(Locale locale) {
190 return i18n.getString(getOutputName(), locale, "report.title");
191 }
192
193 @Override
194 public String getCategoryName() {
195 return CATEGORY_PROJECT_REPORTS;
196 }
197
198 @Override
199 public boolean canGenerateReport() {
200 return !skip;
201 }
202
203 @Override
204 protected Renderer getSiteRenderer() {
205 return siteRenderer;
206 }
207
208 @Override
209 protected String getOutputDirectory() {
210 return outputDirectory.getAbsolutePath();
211 }
212
213 @Override
214 protected MavenProject getProject() {
215 return project;
216 }
217
218 protected abstract ClassLoader createClassLoader() throws MavenReportException;
219
220 @Override
221 protected void executeReport(Locale locale) throws MavenReportException {
222
223 if (newClassLoader == null) {
224
225
226 init(locale);
227 } else {
228
229
230 I18n.setDefaultLocale(locale);
231 }
232 ApplicationConfigReportRenderer renderer =
233 new ApplicationConfigReportRenderer(getSink(),
234 i18n,
235 locale,
236 getOutputName(),
237 getOutputName(),
238 configProviders,
239 showOptionDetail);
240 renderer.render();
241 }
242
243 protected void init(Locale locale) throws MavenReportException {
244 if (getLog().isDebugEnabled()) {
245
246
247 verbose = true;
248 }
249
250 if (newClassLoader == null) {
251
252 newClassLoader = createClassLoader();
253 }
254
255
256 I18nInitializer initializer;
257 if (StringUtils.isNotEmpty(i18nBundleName)) {
258 initializer = new DefaultI18nInitializer(i18nBundleName,
259 newClassLoader);
260 } else {
261 initializer = new ClassPathI18nInitializer(newClassLoader);
262 }
263
264 I18n.init(initializer, locale);
265
266 if (configProviders == null) {
267
268 Set<String> includes = null;
269 if (StringUtils.isNotEmpty(include)) {
270 includes = new HashSet<String>(Arrays.asList(include.split("\\s*,\\s*")));
271 if (verbose) {
272 getLog().info("config - includes : " + includes);
273 }
274 }
275
276 Set<String> excludes = null;
277 if (StringUtils.isNotEmpty(exclude)) {
278 excludes = new HashSet<String>(Arrays.asList(exclude.split("\\s*,\\s*")));
279 if (verbose) {
280 getLog().info("config - excludes : " + excludes);
281 }
282 }
283
284 configProviders = ApplicationConfigHelper.getProviders(
285 newClassLoader,
286 includes,
287 excludes,
288 verbose
289 );
290 }
291 }
292
293 protected ClassLoader createClassLoader(Set<String> paths) {
294
295 Set<URL> urls = new HashSet<URL>();
296
297 for (String fileName : paths) {
298 File pathElem = new File(fileName);
299 try {
300 URL url = pathElem.toURI().toURL();
301 urls.add(url);
302 getLog().debug("Added classpathElement URL " + url);
303 } catch (MalformedURLException e) {
304 throw new RuntimeException(
305 "Error in adding the classpath " + pathElem, e);
306 }
307 }
308
309 return new URLClassLoader(
310 urls.toArray(new URL[urls.size()]),
311 Thread.currentThread().getContextClassLoader());
312 }
313 }