1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.jomc.mojo;
32
33 import java.io.BufferedReader;
34 import java.io.File;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.StringReader;
38 import java.io.StringWriter;
39 import java.net.MalformedURLException;
40 import java.net.SocketTimeoutException;
41 import java.net.URI;
42 import java.net.URISyntaxException;
43 import java.net.URL;
44 import java.net.URLClassLoader;
45 import java.net.URLConnection;
46 import java.util.Collection;
47 import java.util.Date;
48 import java.util.HashSet;
49 import java.util.Iterator;
50 import java.util.List;
51 import java.util.Locale;
52 import java.util.Map;
53 import java.util.Properties;
54 import java.util.Set;
55 import java.util.logging.Level;
56 import javax.xml.bind.JAXBException;
57 import javax.xml.bind.Marshaller;
58 import javax.xml.transform.ErrorListener;
59 import javax.xml.transform.Transformer;
60 import javax.xml.transform.TransformerConfigurationException;
61 import javax.xml.transform.TransformerException;
62 import javax.xml.transform.TransformerFactory;
63 import javax.xml.transform.stream.StreamSource;
64 import org.apache.commons.lang.StringEscapeUtils;
65 import org.apache.commons.lang.StringUtils;
66 import org.apache.maven.artifact.Artifact;
67 import org.apache.maven.artifact.ArtifactUtils;
68 import org.apache.maven.execution.MavenSession;
69 import org.apache.maven.plugin.AbstractMojo;
70 import org.apache.maven.plugin.MojoExecutionException;
71 import org.apache.maven.plugin.MojoFailureException;
72 import org.apache.maven.plugin.descriptor.MojoDescriptor;
73 import org.apache.maven.project.MavenProject;
74 import org.jomc.model.Module;
75 import org.jomc.model.Modules;
76 import org.jomc.model.modlet.DefaultModelProcessor;
77 import org.jomc.model.modlet.DefaultModelProvider;
78 import org.jomc.model.modlet.DefaultModelValidator;
79 import org.jomc.model.modlet.ModelHelper;
80 import org.jomc.modlet.DefaultModelContext;
81 import org.jomc.modlet.DefaultModletProvider;
82 import org.jomc.modlet.Model;
83 import org.jomc.modlet.ModelContext;
84 import org.jomc.modlet.ModelContextFactory;
85 import org.jomc.modlet.ModelException;
86 import org.jomc.modlet.ModelValidationReport;
87 import org.jomc.modlet.Modlet;
88 import org.jomc.modlet.Modlets;
89 import org.jomc.tools.ClassFileProcessor;
90 import org.jomc.tools.JomcTool;
91 import org.jomc.tools.ResourceFileProcessor;
92 import org.jomc.tools.SourceFileProcessor;
93 import org.jomc.tools.modlet.ToolsModelProcessor;
94 import org.jomc.tools.modlet.ToolsModelProvider;
95
96
97
98
99
100
101
102 public abstract class AbstractJomcMojo extends AbstractMojo
103 {
104
105
106
107
108
109
110 private String sourceEncoding;
111
112
113
114
115
116
117
118
119 @Deprecated
120 private String templateEncoding;
121
122
123
124
125
126
127
128
129 private String defaultTemplateEncoding;
130
131
132
133
134
135
136
137
138
139
140
141 private String templateLocation;
142
143
144
145
146
147
148 private String templateProfile;
149
150
151
152
153
154
155 private String defaultTemplateProfile;
156
157
158
159
160
161
162 private String providerLocation;
163
164
165
166
167
168
169 private String platformProviderLocation;
170
171
172
173
174
175
176 private String model;
177
178
179
180
181
182
183
184 private String modelContextFactoryClassName;
185
186
187
188
189
190
191 private String modletLocation;
192
193
194
195
196
197
198
199 private String modletSchemaSystemId;
200
201
202
203
204
205
206 private String moduleLocation;
207
208
209
210
211
212
213 private String transformerLocation;
214
215
216
217
218
219
220 private String indentation;
221
222
223
224
225
226
227 private String lineSeparator;
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 private LocaleType locale;
244
245
246
247
248
249
250 private boolean verbose;
251
252
253
254
255
256
257 private boolean sourceProcessingEnabled;
258
259
260
261
262
263
264 private boolean resourceProcessingEnabled;
265
266
267
268
269
270
271 private boolean classProcessingEnabled;
272
273
274
275
276
277
278 private boolean modelProcessingEnabled;
279
280
281
282
283
284
285 private boolean modelObjectClasspathResolutionEnabled;
286
287
288
289
290
291
292 private String moduleName;
293
294
295
296
297
298
299 private String testModuleName;
300
301
302
303
304
305
306
307
308 @Deprecated
309 private String classesDirectory;
310
311
312
313
314
315
316
317
318 @Deprecated
319 private String testClassesDirectory;
320
321
322
323
324
325
326
327 private String outputDirectory;
328
329
330
331
332
333
334
335 private String testOutputDirectory;
336
337
338
339
340
341
342
343 private String sourceDirectory;
344
345
346
347
348
349
350
351 private String testSourceDirectory;
352
353
354
355
356
357
358
359 private String sessionDirectory;
360
361
362
363
364
365
366
367 private String reportOutputDirectory;
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384 private List<VelocityProperty> velocityProperties;
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418 private List<VelocityPropertyResource> velocityPropertyResources;
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435 private List<TemplateParameter> templateParameters;
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469 private List<TemplateParameterResource> templateParameterResources;
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486 private List<TransformationParameter> transformationParameters;
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503 private List<TransformationOutputProperty> transformationOutputProperties;
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537 private List<TransformationParameterResource> transformationParameterResources;
538
539
540
541
542
543
544
545 private String classFileProcessorClassName;
546
547
548
549
550
551
552
553
554 private String resourceFileProcessorClassName;
555
556
557
558
559
560
561
562 private String sourceFileProcessorClassName;
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579 private List<ModelContextAttribute> modelContextAttributes;
580
581
582
583
584
585
586
587
588 private boolean modelResourceValidationEnabled;
589
590
591
592
593
594
595
596
597 private boolean modletResourceValidationEnabled;
598
599
600
601
602
603
604
605
606 private boolean javaValidationEnabled;
607
608
609
610
611
612
613
614
615 private List<String> modletExcludes;
616
617
618
619
620
621
622
623
624 private List<String> modletIncludes;
625
626
627
628
629
630
631
632
633 private MavenProject mavenProject;
634
635
636
637
638
639
640
641
642 private List<Artifact> pluginArtifacts;
643
644
645
646
647
648
649
650
651
652 private MavenSession mavenSession;
653
654
655 public AbstractJomcMojo()
656 {
657 super();
658 }
659
660
661
662
663
664
665
666 public void execute() throws MojoExecutionException, MojoFailureException
667 {
668 this.assertValidParameters();
669
670 try
671 {
672 this.logSeparator();
673
674 if ( this.isLoggable( Level.INFO ) )
675 {
676 this.log( Level.INFO, Messages.getMessage( "title" ), null );
677 }
678
679 if ( this.isExecutionPermitted() )
680 {
681 this.executeTool();
682 }
683 else if ( this.isLoggable( Level.INFO ) )
684 {
685 this.log( Level.INFO, Messages.getMessage( "executionSuppressed", this.getExecutionStrategy() ), null );
686 }
687 }
688 catch ( final Exception e )
689 {
690 throw new MojoExecutionException( Messages.getMessage( e ), e );
691 }
692 finally
693 {
694 JomcTool.setDefaultTemplateProfile( null );
695 this.logSeparator();
696 }
697 }
698
699
700
701
702
703
704
705
706
707 protected void assertValidParameters() throws MojoFailureException
708 {
709 this.assertValidResources( this.templateParameterResources );
710 this.assertValidResources( this.transformationParameterResources );
711 this.assertValidResources( this.velocityPropertyResources );
712 }
713
714
715
716
717
718
719
720
721
722
723
724
725
726 protected final void assertValidResources( final Collection<? extends ResourceType> resources )
727 throws MojoFailureException
728 {
729 if ( resources != null )
730 {
731 for ( final ResourceType r : resources )
732 {
733 if ( r.getLocation() == null )
734 {
735 throw new MojoFailureException( Messages.getMessage( "mandatoryParameter", "location" ) );
736 }
737
738 if ( r instanceof PropertiesResourceType )
739 {
740 final PropertiesResourceType p = (PropertiesResourceType) r;
741
742 if ( !PropertiesResourceType.isFormatSupported( p.getFormat() ) )
743 {
744 throw new MojoFailureException( Messages.getMessage(
745 "illegalPropertiesFormat", p.getFormat(),
746 StringUtils.join( PropertiesResourceType.getSupportedFormats(), ',' ) ) );
747
748 }
749 }
750 }
751 }
752 }
753
754
755
756
757
758
759 protected abstract void executeTool() throws Exception;
760
761
762
763
764
765
766
767
768
769 protected abstract String getGoal() throws MojoExecutionException;
770
771
772
773
774
775
776
777
778
779 protected abstract String getExecutionStrategy() throws MojoExecutionException;
780
781
782
783
784
785
786
787
788
789
790
791
792
793 protected boolean isExecutionPermitted() throws MojoExecutionException
794 {
795 try
796 {
797 boolean permitted = true;
798
799 if ( MojoDescriptor.SINGLE_PASS_EXEC_STRATEGY.equals( this.getExecutionStrategy() ) )
800 {
801 final File flagFile =
802 new File( this.getSessionDirectory(),
803 ArtifactUtils.versionlessKey( this.getMavenProject().getArtifact() ).hashCode()
804 + "-" + this.getGoal()
805 + "-" + this.getMavenSession().getStartTime().getTime() + ".flg" );
806
807 if ( !this.getSessionDirectory().exists() && !this.getSessionDirectory().mkdirs() )
808 {
809 throw new MojoExecutionException( Messages.getMessage(
810 "failedCreatingDirectory", this.getSessionDirectory().getAbsolutePath() ) );
811
812 }
813
814 permitted = flagFile.createNewFile();
815 }
816
817 return permitted;
818 }
819 catch ( final IOException e )
820 {
821 throw new MojoExecutionException( Messages.getMessage( e ), e );
822 }
823 }
824
825
826
827
828
829
830
831
832 protected MavenProject getMavenProject() throws MojoExecutionException
833 {
834 return this.mavenProject;
835 }
836
837
838
839
840
841
842
843
844
845
846 protected MavenSession getMavenSession() throws MojoExecutionException
847 {
848 return this.mavenSession;
849 }
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865 protected File getAbsoluteFile( final String name ) throws MojoExecutionException
866 {
867 if ( name == null )
868 {
869 throw new NullPointerException( "name" );
870 }
871
872 File file = new File( name );
873 if ( !file.isAbsolute() )
874 {
875 file = new File( this.getMavenProject().getBasedir(), name );
876 }
877
878 return file;
879 }
880
881
882
883
884
885
886
887
888
889
890 protected File getOutputDirectory() throws MojoExecutionException
891 {
892 if ( this.classesDirectory != null )
893 {
894 if ( this.isLoggable( Level.WARNING ) )
895 {
896 this.log( Level.WARNING, Messages.getMessage(
897 "deprecationWarning", "classesDirectory", "outputDirectory" ), null );
898
899 }
900
901 if ( !this.classesDirectory.equals( this.outputDirectory ) )
902 {
903 if ( this.isLoggable( Level.WARNING ) )
904 {
905 this.log( Level.WARNING, Messages.getMessage( "ignoringParameter", "outputDirectory" ), null );
906 }
907
908 this.outputDirectory = this.classesDirectory;
909 }
910
911 this.classesDirectory = null;
912 }
913
914 final File dir = this.getAbsoluteFile( this.outputDirectory );
915 if ( !dir.exists() && !dir.mkdirs() )
916 {
917 throw new MojoExecutionException( Messages.getMessage( "failedCreatingDirectory", dir.getAbsolutePath() ) );
918 }
919
920 return dir;
921 }
922
923
924
925
926
927
928
929
930
931
932 protected File getTestOutputDirectory() throws MojoExecutionException
933 {
934 if ( this.testClassesDirectory != null )
935 {
936 if ( this.isLoggable( Level.WARNING ) )
937 {
938 this.log( Level.WARNING, Messages.getMessage(
939 "deprecationWarning", "testClassesDirectory", "testOutputDirectory" ), null );
940
941 }
942
943 if ( !this.testClassesDirectory.equals( this.testOutputDirectory ) )
944 {
945 if ( this.isLoggable( Level.WARNING ) )
946 {
947 this.log( Level.WARNING, Messages.getMessage( "ignoringParameter", "testOutputDirectory" ), null );
948 }
949
950 this.testOutputDirectory = this.testClassesDirectory;
951 }
952
953 this.testClassesDirectory = null;
954 }
955
956 final File dir = this.getAbsoluteFile( this.testOutputDirectory );
957 if ( !dir.exists() && !dir.mkdirs() )
958 {
959 throw new MojoExecutionException( Messages.getMessage( "failedCreatingDirectory", dir.getAbsolutePath() ) );
960 }
961
962 return dir;
963 }
964
965
966
967
968
969
970
971
972
973
974 protected File getSourceDirectory() throws MojoExecutionException
975 {
976 return this.getAbsoluteFile( this.sourceDirectory );
977 }
978
979
980
981
982
983
984
985
986
987
988 protected File getTestSourceDirectory() throws MojoExecutionException
989 {
990 return this.getAbsoluteFile( this.testSourceDirectory );
991 }
992
993
994
995
996
997
998
999
1000
1001
1002 protected File getSessionDirectory() throws MojoExecutionException
1003 {
1004 return this.getAbsoluteFile( this.sessionDirectory );
1005 }
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016 protected File getReportOutputDirectory() throws MojoExecutionException
1017 {
1018 return this.getAbsoluteFile( this.reportOutputDirectory );
1019 }
1020
1021
1022
1023
1024
1025
1026
1027
1028 protected ClassLoader getMainClassLoader() throws MojoExecutionException
1029 {
1030 try
1031 {
1032 final Set<String> mainClasspathElements = this.getMainClasspathElements();
1033 final Set<URI> uris = new HashSet<URI>( mainClasspathElements.size() );
1034
1035 for ( final String element : mainClasspathElements )
1036 {
1037 final URI uri = new File( element ).toURI();
1038 if ( !uris.contains( uri ) )
1039 {
1040 uris.add( uri );
1041 }
1042 }
1043
1044 if ( this.isLoggable( Level.FINEST ) )
1045 {
1046 this.log( Level.FINEST, Messages.getMessage( "mainClasspathInfo" ), null );
1047 }
1048
1049 int i = 0;
1050 final URL[] urls = new URL[ uris.size() ];
1051 for ( final URI uri : uris )
1052 {
1053 urls[i++] = uri.toURL();
1054
1055 if ( this.isLoggable( Level.FINEST ) )
1056 {
1057 this.log( Level.FINEST, "\t" + urls[i - 1].toExternalForm(), null );
1058 }
1059 }
1060
1061 return new URLClassLoader( urls, Thread.currentThread().getContextClassLoader() );
1062 }
1063 catch ( final IOException e )
1064 {
1065 throw new MojoExecutionException( Messages.getMessage( e ), e );
1066 }
1067 }
1068
1069
1070
1071
1072
1073
1074
1075
1076 protected ClassLoader getTestClassLoader() throws MojoExecutionException
1077 {
1078 try
1079 {
1080 final Set<String> testClasspathElements = this.getTestClasspathElements();
1081 final Set<URI> uris = new HashSet<URI>( testClasspathElements.size() );
1082
1083 for ( final String element : testClasspathElements )
1084 {
1085 final URI uri = new File( element ).toURI();
1086 if ( !uris.contains( uri ) )
1087 {
1088 uris.add( uri );
1089 }
1090 }
1091
1092 if ( this.isLoggable( Level.FINEST ) )
1093 {
1094 this.log( Level.FINEST, Messages.getMessage( "testClasspathInfo" ), null );
1095 }
1096
1097 int i = 0;
1098 final URL[] urls = new URL[ uris.size() ];
1099 for ( final URI uri : uris )
1100 {
1101 urls[i++] = uri.toURL();
1102
1103 if ( this.isLoggable( Level.FINEST ) )
1104 {
1105 this.log( Level.FINEST, "\t" + urls[i - 1].toExternalForm(), null );
1106 }
1107 }
1108
1109 return new URLClassLoader( urls, Thread.currentThread().getContextClassLoader() );
1110 }
1111 catch ( final IOException e )
1112 {
1113 throw new MojoExecutionException( Messages.getMessage( e ), e );
1114 }
1115 }
1116
1117
1118
1119
1120
1121
1122
1123
1124 protected Set<String> getMainClasspathElements() throws MojoExecutionException
1125 {
1126 final List<?> runtimeArtifacts = this.getMavenProject().getRuntimeArtifacts();
1127 final List<?> compileArtifacts = this.getMavenProject().getCompileArtifacts();
1128 final Set<String> elements = new HashSet<String>( runtimeArtifacts.size() + compileArtifacts.size() + 1 );
1129 elements.add( this.getOutputDirectory().getAbsolutePath() );
1130
1131 for ( final Iterator<?> it = runtimeArtifacts.iterator(); it.hasNext(); )
1132 {
1133 final Artifact a = (Artifact) it.next();
1134 final Artifact pluginArtifact = this.getPluginArtifact( a );
1135
1136 if ( a.getFile() == null )
1137 {
1138 if ( this.isLoggable( Level.WARNING ) )
1139 {
1140 this.log( Level.WARNING, Messages.getMessage( "ignoringArtifact", a.toString() ), null );
1141 }
1142
1143 continue;
1144 }
1145
1146 if ( pluginArtifact != null )
1147 {
1148 if ( this.isLoggable( Level.FINER ) )
1149 {
1150 this.log( Level.FINER, Messages.getMessage(
1151 "ignoringPluginArtifact", a.toString(), pluginArtifact.toString() ), null );
1152
1153 }
1154
1155 continue;
1156 }
1157
1158 final String element = a.getFile().getAbsolutePath();
1159 elements.add( element );
1160 }
1161
1162 for ( final Iterator<?> it = compileArtifacts.iterator(); it.hasNext(); )
1163 {
1164 final Artifact a = (Artifact) it.next();
1165 final Artifact pluginArtifact = this.getPluginArtifact( a );
1166
1167 if ( a.getFile() == null )
1168 {
1169 if ( this.isLoggable( Level.WARNING ) )
1170 {
1171 this.log( Level.WARNING, Messages.getMessage( "ignoringArtifact", a.toString() ), null );
1172 }
1173
1174 continue;
1175 }
1176
1177 if ( pluginArtifact != null )
1178 {
1179 if ( this.isLoggable( Level.FINER ) )
1180 {
1181 this.log( Level.FINER, Messages.getMessage(
1182 "ignoringPluginArtifact", a.toString(), pluginArtifact.toString() ), null );
1183
1184 }
1185
1186 continue;
1187 }
1188
1189 final String element = a.getFile().getAbsolutePath();
1190 elements.add( element );
1191 }
1192
1193 return elements;
1194 }
1195
1196
1197
1198
1199
1200
1201
1202
1203 protected Set<String> getTestClasspathElements() throws MojoExecutionException
1204 {
1205 final List<?> testArtifacts = this.getMavenProject().getTestArtifacts();
1206 final Set<String> elements = new HashSet<String>( testArtifacts.size() + 2 );
1207 elements.add( this.getOutputDirectory().getAbsolutePath() );
1208 elements.add( this.getTestOutputDirectory().getAbsolutePath() );
1209
1210 for ( final Iterator<?> it = testArtifacts.iterator(); it.hasNext(); )
1211 {
1212 final Artifact a = (Artifact) it.next();
1213 final Artifact pluginArtifact = this.getPluginArtifact( a );
1214
1215 if ( a.getFile() == null )
1216 {
1217 if ( this.isLoggable( Level.WARNING ) )
1218 {
1219 this.log( Level.WARNING, Messages.getMessage( "ignoringArtifact", a.toString() ), null );
1220 }
1221
1222 continue;
1223 }
1224
1225 if ( pluginArtifact != null )
1226 {
1227 if ( this.isLoggable( Level.FINER ) )
1228 {
1229 this.log( Level.FINER, Messages.getMessage(
1230 "ignoringPluginArtifact", a.toString(), pluginArtifact.toString() ), null );
1231
1232 }
1233
1234 continue;
1235 }
1236
1237 final String element = a.getFile().getAbsolutePath();
1238 elements.add( element );
1239 }
1240
1241 return elements;
1242 }
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253 protected final boolean isVerbose() throws MojoExecutionException
1254 {
1255 return this.verbose;
1256 }
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267 protected final void setVerbose( final boolean value ) throws MojoExecutionException
1268 {
1269 this.verbose = value;
1270 }
1271
1272
1273
1274
1275
1276
1277
1278
1279 protected final boolean isSourceProcessingEnabled() throws MojoExecutionException
1280 {
1281 return this.sourceProcessingEnabled;
1282 }
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293 protected final void setSourceProcessingEnabled( final boolean value ) throws MojoExecutionException
1294 {
1295 this.sourceProcessingEnabled = value;
1296 }
1297
1298
1299
1300
1301
1302
1303
1304
1305 protected final boolean isResourceProcessingEnabled() throws MojoExecutionException
1306 {
1307 return this.resourceProcessingEnabled;
1308 }
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319 protected final void setResourceProcessingEnabled( final boolean value ) throws MojoExecutionException
1320 {
1321 this.resourceProcessingEnabled = value;
1322 }
1323
1324
1325
1326
1327
1328
1329
1330
1331 protected final boolean isClassProcessingEnabled() throws MojoExecutionException
1332 {
1333 return this.classProcessingEnabled;
1334 }
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345 protected final void setClassProcessingEnabled( final boolean value ) throws MojoExecutionException
1346 {
1347 this.classProcessingEnabled = value;
1348 }
1349
1350
1351
1352
1353
1354
1355
1356
1357 protected final boolean isModelProcessingEnabled() throws MojoExecutionException
1358 {
1359 return this.modelProcessingEnabled;
1360 }
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371 protected final void setModelProcessingEnabled( final boolean value ) throws MojoExecutionException
1372 {
1373 this.modelProcessingEnabled = value;
1374 }
1375
1376
1377
1378
1379
1380
1381
1382
1383 protected final boolean isModelObjectClasspathResolutionEnabled() throws MojoExecutionException
1384 {
1385 return this.modelObjectClasspathResolutionEnabled;
1386 }
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398 protected final void setModelObjectClasspathResolutionEnabled( final boolean value ) throws MojoExecutionException
1399 {
1400 this.modelObjectClasspathResolutionEnabled = value;
1401 }
1402
1403
1404
1405
1406
1407
1408
1409
1410 protected String getModel() throws MojoExecutionException
1411 {
1412 return this.model;
1413 }
1414
1415
1416
1417
1418
1419
1420
1421
1422 protected String getModuleName() throws MojoExecutionException
1423 {
1424 return this.moduleName;
1425 }
1426
1427
1428
1429
1430
1431
1432
1433
1434 protected String getTestModuleName() throws MojoExecutionException
1435 {
1436 return this.testModuleName;
1437 }
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449 protected Model getModel( final ModelContext context ) throws MojoExecutionException
1450 {
1451 if ( context == null )
1452 {
1453 throw new NullPointerException( "context" );
1454 }
1455
1456 try
1457 {
1458 Model m = context.findModel( this.getModel() );
1459 final Modules modules = ModelHelper.getModules( m );
1460
1461 if ( modules != null && this.isModelObjectClasspathResolutionEnabled() )
1462 {
1463 final Module classpathModule =
1464 modules.getClasspathModule( Modules.getDefaultClasspathModuleName(), context.getClassLoader() );
1465
1466 if ( classpathModule != null )
1467 {
1468 modules.getModule().add( classpathModule );
1469 }
1470 }
1471
1472 if ( this.isModelProcessingEnabled() )
1473 {
1474 m = context.processModel( m );
1475 }
1476
1477 return m;
1478 }
1479 catch ( final ModelException e )
1480 {
1481 throw new MojoExecutionException( Messages.getMessage( e ), e );
1482 }
1483 }
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496 protected ModelContext createModelContext( final ClassLoader classLoader ) throws MojoExecutionException
1497 {
1498 final ModelContextFactory modelContextFactory;
1499 if ( this.modelContextFactoryClassName != null )
1500 {
1501 modelContextFactory = ModelContextFactory.newInstance( this.modelContextFactoryClassName );
1502 }
1503 else
1504 {
1505 modelContextFactory = ModelContextFactory.newInstance();
1506 }
1507
1508 final ModelContext context = modelContextFactory.newModelContext( classLoader );
1509 this.setupModelContext( context );
1510
1511 return context;
1512 }
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526 protected SourceFileProcessor createSourceFileProcessor( final ModelContext context ) throws MojoExecutionException
1527 {
1528 if ( context == null )
1529 {
1530 throw new NullPointerException( "context" );
1531 }
1532
1533 return this.createJomcTool( context, this.sourceFileProcessorClassName, SourceFileProcessor.class );
1534 }
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548 protected ResourceFileProcessor createResourceFileProcessor( final ModelContext context )
1549 throws MojoExecutionException
1550 {
1551 if ( context == null )
1552 {
1553 throw new NullPointerException( "context" );
1554 }
1555
1556 return this.createJomcTool( context, this.resourceFileProcessorClassName, ResourceFileProcessor.class );
1557 }
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571 protected ClassFileProcessor createClassFileProcessor( final ModelContext context ) throws MojoExecutionException
1572 {
1573 if ( context == null )
1574 {
1575 throw new NullPointerException( "context" );
1576 }
1577
1578 return this.createJomcTool( context, this.classFileProcessorClassName, ClassFileProcessor.class );
1579 }
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599 protected <T extends JomcTool> T createJomcTool( final ModelContext context, final String className,
1600 final Class<T> type ) throws MojoExecutionException
1601 {
1602 if ( context == null )
1603 {
1604 throw new NullPointerException( "context" );
1605 }
1606 if ( className == null )
1607 {
1608 throw new NullPointerException( "className" );
1609 }
1610 if ( type == null )
1611 {
1612 throw new NullPointerException( "type" );
1613 }
1614
1615 final T tool = this.createObject( className, type );
1616 this.setupJomcTool( context, tool );
1617 return tool;
1618 }
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634 protected <T> T createObject( final String className, final Class<T> type ) throws MojoExecutionException
1635 {
1636 if ( className == null )
1637 {
1638 throw new NullPointerException( "className" );
1639 }
1640 if ( type == null )
1641 {
1642 throw new NullPointerException( "type" );
1643 }
1644
1645 try
1646 {
1647 return Class.forName( className ).asSubclass( type ).newInstance();
1648 }
1649 catch ( final InstantiationException e )
1650 {
1651 throw new MojoExecutionException( Messages.getMessage( "failedCreatingObject", className ), e );
1652 }
1653 catch ( final IllegalAccessException e )
1654 {
1655 throw new MojoExecutionException( Messages.getMessage( "failedCreatingObject", className ), e );
1656 }
1657 catch ( final ClassNotFoundException e )
1658 {
1659 throw new MojoExecutionException( Messages.getMessage( "failedCreatingObject", className ), e );
1660 }
1661 catch ( final ClassCastException e )
1662 {
1663 throw new MojoExecutionException( Messages.getMessage( "failedCreatingObject", className ), e );
1664 }
1665 }
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685 protected URL getResource( final String location ) throws MojoExecutionException
1686 {
1687 if ( location == null )
1688 {
1689 throw new NullPointerException( "location" );
1690 }
1691
1692 try
1693 {
1694 String absolute = location;
1695 if ( !absolute.startsWith( "/" ) )
1696 {
1697 absolute = "/" + location;
1698 }
1699
1700 URL resource = this.getClass().getResource( absolute );
1701 if ( resource == null )
1702 {
1703 try
1704 {
1705 resource = new URL( location );
1706 }
1707 catch ( final MalformedURLException e )
1708 {
1709 if ( this.isLoggable( Level.FINEST ) )
1710 {
1711 this.log( Level.FINEST, Messages.getMessage( e ), e );
1712 }
1713
1714 resource = null;
1715 }
1716 }
1717
1718 if ( resource == null )
1719 {
1720 final File f = this.getAbsoluteFile( location );
1721
1722 if ( f.isFile() )
1723 {
1724 resource = f.toURI().toURL();
1725 }
1726 }
1727
1728 return resource;
1729 }
1730 catch ( final MalformedURLException e )
1731 {
1732 String m = Messages.getMessage( e );
1733 m = m == null ? "" : " " + m;
1734
1735 throw new MojoExecutionException( Messages.getMessage( "malformedLocation", location, m ), e );
1736 }
1737 }
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755 protected URL getDirectory( final String location ) throws MojoExecutionException
1756 {
1757 if ( location == null )
1758 {
1759 throw new NullPointerException( "location" );
1760 }
1761
1762 try
1763 {
1764 URL resource = null;
1765
1766 try
1767 {
1768 resource = new URL( location );
1769 }
1770 catch ( final MalformedURLException e )
1771 {
1772 if ( this.isLoggable( Level.FINEST ) )
1773 {
1774 this.log( Level.FINEST, Messages.getMessage( e ), e );
1775 }
1776
1777 resource = null;
1778 }
1779
1780 if ( resource == null )
1781 {
1782 final File f = this.getAbsoluteFile( location );
1783
1784 if ( f.isDirectory() )
1785 {
1786 resource = f.toURI().toURL();
1787 }
1788 }
1789
1790 return resource;
1791 }
1792 catch ( final MalformedURLException e )
1793 {
1794 String m = Messages.getMessage( e );
1795 m = m == null ? "" : " " + m;
1796
1797 throw new MojoExecutionException( Messages.getMessage( "malformedLocation", location, m ), e );
1798 }
1799 }
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815 protected Transformer getTransformer( final TransformerResourceType resource ) throws MojoExecutionException
1816 {
1817 if ( resource == null )
1818 {
1819 throw new NullPointerException( "resource" );
1820 }
1821
1822 InputStream in = null;
1823 boolean suppressExceptionOnClose = true;
1824 final URL url = this.getResource( resource.getLocation() );
1825 final ErrorListener errorListener = new ErrorListener()
1826 {
1827
1828 public void warning( final TransformerException exception ) throws TransformerException
1829 {
1830 try
1831 {
1832 log( Level.WARNING, Messages.getMessage( exception ), exception );
1833 }
1834 catch ( final MojoExecutionException e )
1835 {
1836 getLog().warn( exception );
1837 getLog().error( e );
1838 }
1839 }
1840
1841 public void error( final TransformerException exception ) throws TransformerException
1842 {
1843 try
1844 {
1845 log( Level.SEVERE, Messages.getMessage( exception ), exception );
1846 }
1847 catch ( final MojoExecutionException e )
1848 {
1849 getLog().error( exception );
1850 getLog().error( e );
1851 }
1852
1853 throw exception;
1854 }
1855
1856 public void fatalError( final TransformerException exception ) throws TransformerException
1857 {
1858 try
1859 {
1860 log( Level.SEVERE, Messages.getMessage( exception ), exception );
1861 }
1862 catch ( final MojoExecutionException e )
1863 {
1864 getLog().error( exception );
1865 getLog().error( e );
1866 }
1867
1868 throw exception;
1869 }
1870
1871 };
1872
1873 try
1874 {
1875 if ( url != null )
1876 {
1877 if ( this.isLoggable( Level.FINER ) )
1878 {
1879 this.log( Level.FINER, Messages.getMessage( "loadingTransformer", url.toExternalForm() ), null );
1880 }
1881
1882 final URLConnection con = url.openConnection();
1883 con.setConnectTimeout( resource.getConnectTimeout() );
1884 con.setReadTimeout( resource.getReadTimeout() );
1885 con.connect();
1886 in = con.getInputStream();
1887
1888 final TransformerFactory transformerFactory = TransformerFactory.newInstance();
1889 transformerFactory.setErrorListener( errorListener );
1890 final Transformer transformer =
1891 transformerFactory.newTransformer( new StreamSource( in, url.toURI().toASCIIString() ) );
1892
1893 transformer.setErrorListener( errorListener );
1894
1895 for ( final Map.Entry<Object, Object> e : System.getProperties().entrySet() )
1896 {
1897 transformer.setParameter( e.getKey().toString(), e.getValue() );
1898 }
1899
1900 if ( this.getMavenProject().getProperties() != null )
1901 {
1902 for ( final Map.Entry<Object, Object> e : this.getMavenProject().getProperties().entrySet() )
1903 {
1904 transformer.setParameter( e.getKey().toString(), e.getValue() );
1905 }
1906 }
1907
1908 if ( this.transformationParameterResources != null )
1909 {
1910 for ( int i = 0, s0 = this.transformationParameterResources.size(); i < s0; i++ )
1911 {
1912 for ( final Map.Entry<Object, Object> e : this.getProperties(
1913 this.transformationParameterResources.get( i ) ).entrySet() )
1914 {
1915 transformer.setParameter( e.getKey().toString(), e.getValue() );
1916 }
1917 }
1918 }
1919
1920 if ( this.transformationParameters != null )
1921 {
1922 for ( final TransformationParameter e : this.transformationParameters )
1923 {
1924 transformer.setParameter( e.getKey(), e.getObject() );
1925 }
1926 }
1927
1928 if ( this.transformationOutputProperties != null )
1929 {
1930 for ( final TransformationOutputProperty e : this.transformationOutputProperties )
1931 {
1932 transformer.setOutputProperty( e.getKey(), e.getValue() );
1933 }
1934 }
1935
1936 for ( int i = 0, s0 = resource.getTransformationParameterResources().size(); i < s0; i++ )
1937 {
1938 for ( final Map.Entry<Object, Object> e : this.getProperties(
1939 resource.getTransformationParameterResources().get( i ) ).entrySet() )
1940 {
1941 transformer.setParameter( e.getKey().toString(), e.getValue() );
1942 }
1943 }
1944
1945 for ( final TransformationParameter e : resource.getTransformationParameters() )
1946 {
1947 transformer.setParameter( e.getKey(), e.getObject() );
1948 }
1949
1950 for ( final TransformationOutputProperty e : resource.getTransformationOutputProperties() )
1951 {
1952 transformer.setOutputProperty( e.getKey(), e.getValue() );
1953 }
1954
1955 suppressExceptionOnClose = false;
1956 return transformer;
1957 }
1958 else if ( resource.isOptional() )
1959 {
1960 if ( this.isLoggable( Level.WARNING ) )
1961 {
1962 this.log( Level.WARNING, Messages.getMessage(
1963 "transformerNotFound", resource.getLocation() ), null );
1964
1965 }
1966 }
1967 else
1968 {
1969 throw new MojoExecutionException( Messages.getMessage(
1970 "transformerNotFound", resource.getLocation() ) );
1971
1972 }
1973 }
1974 catch ( final InstantiationException e )
1975 {
1976 throw new MojoExecutionException( Messages.getMessage( e ), e );
1977 }
1978 catch ( final URISyntaxException e )
1979 {
1980 throw new MojoExecutionException( Messages.getMessage( e ), e );
1981 }
1982 catch ( final TransformerConfigurationException e )
1983 {
1984 String m = Messages.getMessage( e );
1985 if ( m == null )
1986 {
1987 m = Messages.getMessage( e.getException() );
1988 }
1989
1990 m = m == null ? "" : " " + m;
1991
1992 throw new MojoExecutionException( Messages.getMessage(
1993 "failedCreatingTransformer", resource.getLocation(), m ), e );
1994
1995 }
1996 catch ( final SocketTimeoutException e )
1997 {
1998 String m = Messages.getMessage( e );
1999 m = m == null ? "" : " " + m;
2000
2001 if ( resource.isOptional() )
2002 {
2003 if ( this.isLoggable( Level.WARNING ) )
2004 {
2005 this.log( Level.WARNING, Messages.getMessage(
2006 "failedLoadingTransformer", url.toExternalForm(), m ), e );
2007
2008 }
2009 }
2010 else
2011 {
2012 throw new MojoExecutionException( Messages.getMessage(
2013 "failedLoadingTransformer", url.toExternalForm(), m ), e );
2014
2015 }
2016 }
2017 catch ( final IOException e )
2018 {
2019 String m = Messages.getMessage( e );
2020 m = m == null ? "" : " " + m;
2021
2022 if ( resource.isOptional() )
2023 {
2024 if ( this.isLoggable( Level.WARNING ) )
2025 {
2026 this.log( Level.WARNING, Messages.getMessage(
2027 "failedLoadingTransformer", url.toExternalForm(), m ), e );
2028
2029 }
2030 }
2031 else
2032 {
2033 throw new MojoExecutionException( Messages.getMessage(
2034 "failedLoadingTransformer", url.toExternalForm(), m ), e );
2035
2036 }
2037 }
2038 finally
2039 {
2040 try
2041 {
2042 if ( in != null )
2043 {
2044 in.close();
2045 }
2046 }
2047 catch ( final IOException e )
2048 {
2049 if ( suppressExceptionOnClose )
2050 {
2051 this.getLog().error( e );
2052 }
2053 else
2054 {
2055 throw new MojoExecutionException( Messages.getMessage( e ), e );
2056 }
2057 }
2058 }
2059
2060 return null;
2061 }
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076 protected Properties getProperties( final PropertiesResourceType propertiesResourceType )
2077 throws MojoExecutionException
2078 {
2079 if ( propertiesResourceType == null )
2080 {
2081 throw new NullPointerException( "propertiesResourceType" );
2082 }
2083
2084 InputStream in = null;
2085 boolean suppressExceptionOnClose = true;
2086 final URL url = this.getResource( propertiesResourceType.getLocation() );
2087 final Properties properties = new Properties();
2088
2089 try
2090 {
2091 if ( url != null )
2092 {
2093 if ( this.isLoggable( Level.FINER ) )
2094 {
2095 this.log( Level.FINER, Messages.getMessage( "loadingProperties", url.toExternalForm() ), null );
2096 }
2097
2098 final URLConnection con = url.openConnection();
2099 con.setConnectTimeout( propertiesResourceType.getConnectTimeout() );
2100 con.setReadTimeout( propertiesResourceType.getReadTimeout() );
2101 con.connect();
2102
2103 in = con.getInputStream();
2104
2105 if ( PropertiesResourceType.PLAIN_FORMAT.equalsIgnoreCase( propertiesResourceType.getFormat() ) )
2106 {
2107 properties.load( in );
2108 }
2109 else if ( PropertiesResourceType.XML_FORMAT.equalsIgnoreCase( propertiesResourceType.getFormat() ) )
2110 {
2111 properties.loadFromXML( in );
2112 }
2113 }
2114 else if ( propertiesResourceType.isOptional() )
2115 {
2116 if ( this.isLoggable( Level.WARNING ) )
2117 {
2118 this.log( Level.WARNING, Messages.getMessage(
2119 "propertiesNotFound", propertiesResourceType.getLocation() ), null );
2120
2121 }
2122 }
2123 else
2124 {
2125 throw new MojoExecutionException( Messages.getMessage(
2126 "propertiesNotFound", propertiesResourceType.getLocation() ) );
2127
2128 }
2129
2130 suppressExceptionOnClose = false;
2131 }
2132 catch ( final SocketTimeoutException e )
2133 {
2134 String m = Messages.getMessage( e );
2135 m = m == null ? "" : " " + m;
2136
2137 if ( propertiesResourceType.isOptional() )
2138 {
2139 if ( this.isLoggable( Level.WARNING ) )
2140 {
2141 this.log( Level.WARNING, Messages.getMessage(
2142 "failedLoadingProperties", url.toExternalForm(), m ), e );
2143
2144 }
2145 }
2146 else
2147 {
2148 throw new MojoExecutionException( Messages.getMessage(
2149 "failedLoadingProperties", url.toExternalForm(), m ), e );
2150
2151 }
2152 }
2153 catch ( final IOException e )
2154 {
2155 String m = Messages.getMessage( e );
2156 m = m == null ? "" : " " + m;
2157
2158 if ( propertiesResourceType.isOptional() )
2159 {
2160 if ( this.isLoggable( Level.WARNING ) )
2161 {
2162 this.log( Level.WARNING, Messages.getMessage(
2163 "failedLoadingProperties", url.toExternalForm(), m ), e );
2164
2165 }
2166 }
2167 else
2168 {
2169 throw new MojoExecutionException( Messages.getMessage(
2170 "failedLoadingProperties", url.toExternalForm(), m ), e );
2171
2172 }
2173 }
2174 finally
2175 {
2176 try
2177 {
2178 if ( in != null )
2179 {
2180 in.close();
2181 }
2182 }
2183 catch ( final IOException e )
2184 {
2185 if ( suppressExceptionOnClose )
2186 {
2187 this.getLog().error( e );
2188 }
2189 else
2190 {
2191 throw new MojoExecutionException( Messages.getMessage( e ), e );
2192 }
2193 }
2194 }
2195
2196 return properties;
2197 }
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213 protected boolean isLoggable( final Level level ) throws MojoExecutionException
2214 {
2215 if ( level == null )
2216 {
2217 throw new NullPointerException( "level" );
2218 }
2219
2220 boolean loggable = false;
2221
2222 if ( level.intValue() <= Level.CONFIG.intValue() )
2223 {
2224 loggable = this.getLog().isDebugEnabled();
2225 }
2226 else if ( level.intValue() <= Level.INFO.intValue() )
2227 {
2228 loggable = this.getLog().isInfoEnabled() && this.isVerbose();
2229 }
2230 else if ( level.intValue() <= Level.WARNING.intValue() )
2231 {
2232 loggable = this.getLog().isWarnEnabled();
2233 }
2234 else if ( level.intValue() <= Level.SEVERE.intValue() )
2235 {
2236 loggable = this.getLog().isErrorEnabled();
2237 }
2238
2239 return loggable;
2240 }
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252 @Deprecated
2253 protected void logSeparator( final Level level ) throws MojoExecutionException
2254 {
2255 this.logSeparator();
2256 }
2257
2258
2259
2260
2261
2262
2263
2264
2265 protected void logSeparator() throws MojoExecutionException
2266 {
2267 if ( this.isLoggable( Level.INFO ) )
2268 {
2269 this.log( Level.INFO, Messages.getMessage( "separator" ), null );
2270 }
2271 }
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281 protected void logProcessingModule( final String toolName, final String module ) throws MojoExecutionException
2282 {
2283 if ( this.isLoggable( Level.INFO ) )
2284 {
2285 this.log( Level.INFO, Messages.getMessage( "processingModule", toolName, module ), null );
2286 }
2287 }
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299 protected void logProcessingModel( final String toolName, final String model ) throws MojoExecutionException
2300 {
2301 if ( this.isLoggable( Level.INFO ) )
2302 {
2303 this.log( Level.INFO, Messages.getMessage( "processingModel", toolName, model ), null );
2304 }
2305 }
2306
2307
2308
2309
2310
2311
2312
2313
2314 protected void logMissingModule( final String module ) throws MojoExecutionException
2315 {
2316 if ( this.isLoggable( Level.WARNING ) )
2317 {
2318 this.log( Level.WARNING, Messages.getMessage( "missingModule", module ), null );
2319 }
2320 }
2321
2322
2323
2324
2325
2326
2327
2328
2329 protected void logToolSuccess( final String toolName ) throws MojoExecutionException
2330 {
2331 if ( this.isLoggable( Level.INFO ) )
2332 {
2333 this.log( Level.INFO, Messages.getMessage( "toolSuccess", toolName ), null );
2334 }
2335 }
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346 protected void log( final ModelContext context, final Level level, final ModelValidationReport report )
2347 throws MojoExecutionException
2348 {
2349 try
2350 {
2351 if ( !report.getDetails().isEmpty() )
2352 {
2353 this.logSeparator();
2354 Marshaller marshaller = null;
2355
2356 for ( final ModelValidationReport.Detail detail : report.getDetails() )
2357 {
2358 this.log( detail.getLevel(), "o " + detail.getMessage(), null );
2359
2360 if ( detail.getElement() != null && this.isLoggable( Level.FINEST ) )
2361 {
2362 if ( marshaller == null )
2363 {
2364 marshaller = context.createMarshaller( this.getModel() );
2365 marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
2366 }
2367
2368 final StringWriter stringWriter = new StringWriter();
2369 marshaller.marshal( detail.getElement(), stringWriter );
2370 this.log( Level.FINEST, stringWriter.toString(), null );
2371 }
2372 }
2373 }
2374 }
2375 catch ( final ModelException e )
2376 {
2377 throw new MojoExecutionException( Messages.getMessage( e ), e );
2378 }
2379 catch ( final JAXBException e )
2380 {
2381 String message = Messages.getMessage( e );
2382 if ( message == null && e.getLinkedException() != null )
2383 {
2384 message = Messages.getMessage( e.getLinkedException() );
2385 }
2386
2387 throw new MojoExecutionException( message, e );
2388 }
2389 }
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400 protected void log( final Level level, final String message, final Throwable throwable )
2401 throws MojoExecutionException
2402 {
2403 BufferedReader reader = null;
2404 boolean suppressExceptionOnClose = true;
2405
2406 try
2407 {
2408 if ( this.isLoggable( level ) )
2409 {
2410 String line;
2411 reader = new BufferedReader( new StringReader( message == null ? "" : message ) );
2412 boolean throwableLogged = false;
2413
2414 while ( ( line = reader.readLine() ) != null )
2415 {
2416 final String mojoMessage =
2417 Messages.getMessage( this.getLog().isDebugEnabled() ? "debugMessage" : "logMessage", line,
2418 Thread.currentThread().getName(), new Date( System.currentTimeMillis() ) );
2419
2420 if ( level.intValue() <= Level.CONFIG.intValue() )
2421 {
2422 this.getLog().debug( mojoMessage, throwableLogged ? null : throwable );
2423 }
2424 else if ( level.intValue() <= Level.INFO.intValue() )
2425 {
2426 this.getLog().info( mojoMessage, throwableLogged ? null : throwable );
2427 }
2428 else if ( level.intValue() <= Level.WARNING.intValue() )
2429 {
2430 this.getLog().warn( mojoMessage, throwableLogged ? null : throwable );
2431 }
2432 else if ( level.intValue() <= Level.SEVERE.intValue() )
2433 {
2434 this.getLog().error( mojoMessage, throwableLogged ? null : throwable );
2435 }
2436
2437 throwableLogged = true;
2438 }
2439 }
2440
2441 suppressExceptionOnClose = false;
2442 }
2443 catch ( final IOException e )
2444 {
2445 this.getLog().error( e );
2446 throw new AssertionError( e );
2447 }
2448 finally
2449 {
2450 try
2451 {
2452 if ( reader != null )
2453 {
2454 reader.close();
2455 }
2456 }
2457 catch ( final IOException e )
2458 {
2459 if ( !suppressExceptionOnClose )
2460 {
2461 throw new AssertionError( e );
2462 }
2463 }
2464 }
2465 }
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475 protected void setupModelContext( final ModelContext context ) throws MojoExecutionException
2476 {
2477 if ( context == null )
2478 {
2479 throw new NullPointerException( "context" );
2480 }
2481
2482 if ( this.isVerbose() || this.getLog().isDebugEnabled() )
2483 {
2484 context.setLogLevel( this.getLog().isDebugEnabled() ? Level.ALL : Level.INFO );
2485 }
2486
2487 try
2488 {
2489 context.setModletSchemaSystemId( this.modletSchemaSystemId );
2490 context.getListeners().add( new ModelContext.Listener()
2491 {
2492
2493 @Override
2494 public void onLog( final Level level, final String message, final Throwable t )
2495 {
2496 super.onLog( level, message, t );
2497
2498 try
2499 {
2500 log( level, message, t );
2501 }
2502 catch ( final MojoExecutionException e )
2503 {
2504 getLog().error( e );
2505 }
2506 }
2507
2508 } );
2509
2510 if ( this.providerLocation != null )
2511 {
2512 context.setAttribute( DefaultModelContext.PROVIDER_LOCATION_ATTRIBUTE_NAME, this.providerLocation );
2513 }
2514
2515 if ( this.platformProviderLocation != null )
2516 {
2517 context.setAttribute( DefaultModelContext.PLATFORM_PROVIDER_LOCATION_ATTRIBUTE_NAME,
2518 this.platformProviderLocation );
2519
2520 }
2521
2522 if ( this.modletLocation != null )
2523 {
2524 context.setAttribute( DefaultModletProvider.MODLET_LOCATION_ATTRIBUTE_NAME, this.modletLocation );
2525 }
2526
2527 if ( this.transformerLocation != null )
2528 {
2529 context.setAttribute( DefaultModelProcessor.TRANSFORMER_LOCATION_ATTRIBUTE_NAME,
2530 this.transformerLocation );
2531 }
2532
2533 if ( this.moduleLocation != null )
2534 {
2535 context.setAttribute( DefaultModelProvider.MODULE_LOCATION_ATTRIBUTE_NAME, this.moduleLocation );
2536 }
2537
2538 context.setAttribute( ToolsModelProvider.MODEL_OBJECT_CLASSPATH_RESOLUTION_ENABLED_ATTRIBUTE_NAME,
2539 this.modelObjectClasspathResolutionEnabled );
2540
2541 context.setAttribute( ToolsModelProcessor.MODEL_OBJECT_CLASSPATH_RESOLUTION_ENABLED_ATTRIBUTE_NAME,
2542 this.modelObjectClasspathResolutionEnabled );
2543
2544 context.setAttribute( DefaultModletProvider.VALIDATING_ATTRIBUTE_NAME,
2545 this.modletResourceValidationEnabled );
2546
2547 context.setAttribute( DefaultModelProvider.VALIDATING_ATTRIBUTE_NAME, this.modelResourceValidationEnabled );
2548 context.setAttribute( DefaultModelValidator.VALIDATE_JAVA_ATTRIBUTE_NAME, this.javaValidationEnabled );
2549
2550 if ( this.modelContextAttributes != null )
2551 {
2552 for ( final ModelContextAttribute e : this.modelContextAttributes )
2553 {
2554 final Object object = e.getObject();
2555
2556 if ( object != null )
2557 {
2558 context.setAttribute( e.getKey(), object );
2559 }
2560 else
2561 {
2562 context.clearAttribute( e.getKey() );
2563 }
2564 }
2565 }
2566
2567 if ( ( this.modletIncludes != null && !this.modletIncludes.isEmpty() )
2568 || ( this.modletExcludes != null && !this.modletExcludes.isEmpty() ) )
2569 {
2570 final Modlets modlets = context.getModlets().clone();
2571
2572 for ( final Iterator<Modlet> it = modlets.getModlet().iterator(); it.hasNext(); )
2573 {
2574 final Modlet modlet = it.next();
2575
2576 if ( this.modletIncludes != null
2577 && !this.modletIncludes.isEmpty()
2578 && !this.modletIncludes.contains( modlet.getName() ) )
2579 {
2580 it.remove();
2581 this.log( Level.INFO, Messages.getMessage( "excludingModlet", modlet.getName() ), null );
2582 continue;
2583 }
2584
2585 if ( this.modletExcludes != null
2586 && !this.modletExcludes.isEmpty()
2587 && this.modletExcludes.contains( modlet.getName() ) )
2588 {
2589 it.remove();
2590 this.log( Level.INFO, Messages.getMessage( "excludingModlet", modlet.getName() ), null );
2591 continue;
2592 }
2593
2594 this.log( Level.INFO, Messages.getMessage( "includingModlet", modlet.getName() ), null );
2595 }
2596
2597 context.setModlets( modlets );
2598 }
2599 }
2600 catch ( final InstantiationException e )
2601 {
2602 throw new MojoExecutionException( Messages.getMessage( e ), e );
2603 }
2604 catch ( final ModelException e )
2605 {
2606 throw new MojoExecutionException( Messages.getMessage( e ), e );
2607 }
2608 }
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619 protected void setupJomcTool( final ModelContext context, final JomcTool tool ) throws MojoExecutionException
2620 {
2621 if ( context == null )
2622 {
2623 throw new NullPointerException( "context" );
2624 }
2625 if ( tool == null )
2626 {
2627 throw new NullPointerException( "tool" );
2628 }
2629
2630 try
2631 {
2632 if ( this.isVerbose() || this.getLog().isDebugEnabled() )
2633 {
2634 tool.setLogLevel( this.getLog().isDebugEnabled() ? Level.ALL : Level.INFO );
2635 }
2636
2637 tool.getListeners().add( new JomcTool.Listener()
2638 {
2639
2640 @Override
2641 public void onLog( final Level level, final String message, final Throwable t )
2642 {
2643 super.onLog( level, message, t );
2644
2645 try
2646 {
2647 log( level, message, t );
2648 }
2649 catch ( final MojoExecutionException e )
2650 {
2651 getLog().error( e );
2652 }
2653 }
2654
2655 } );
2656
2657 if ( this.templateEncoding != null )
2658 {
2659 if ( this.isLoggable( Level.WARNING ) )
2660 {
2661 this.log( Level.WARNING, Messages.getMessage(
2662 "deprecationWarning", "templateEncoding", "defaultTemplateEncoding" ), null );
2663
2664 }
2665
2666 tool.setDefaultTemplateEncoding( this.templateEncoding );
2667 }
2668 else
2669 {
2670 tool.setDefaultTemplateEncoding( this.defaultTemplateEncoding );
2671 }
2672
2673 tool.setInputEncoding( this.sourceEncoding );
2674 tool.setOutputEncoding( this.sourceEncoding );
2675 tool.setDefaultTemplateProfile( this.defaultTemplateProfile );
2676 tool.setTemplateProfile( this.templateProfile );
2677 tool.setModel( this.getModel( context ) );
2678
2679 if ( this.indentation != null )
2680 {
2681 tool.setIndentation( StringEscapeUtils.unescapeJava( this.indentation ) );
2682 }
2683
2684 if ( this.lineSeparator != null )
2685 {
2686 tool.setLineSeparator( StringEscapeUtils.unescapeJava( this.lineSeparator ) );
2687 }
2688
2689 if ( this.locale != null )
2690 {
2691 tool.setLocale( new Locale( StringUtils.defaultString( this.locale.getLanguage() ),
2692 StringUtils.defaultString( this.locale.getCountry() ),
2693 StringUtils.defaultString( this.locale.getVariant() ) ) );
2694
2695 }
2696
2697 if ( this.velocityPropertyResources != null )
2698 {
2699 for ( int i = 0, s0 = this.velocityPropertyResources.size(); i < s0; i++ )
2700 {
2701 for ( final Map.Entry<Object, Object> e : this.getProperties(
2702 this.velocityPropertyResources.get( i ) ).entrySet() )
2703 {
2704 if ( e.getValue() != null )
2705 {
2706 tool.getVelocityEngine().setProperty( e.getKey().toString(), e );
2707 }
2708 else
2709 {
2710 tool.getVelocityEngine().clearProperty( e.getKey().toString() );
2711 }
2712 }
2713 }
2714 }
2715
2716 if ( this.velocityProperties != null )
2717 {
2718 for ( final VelocityProperty e : this.velocityProperties )
2719 {
2720 final Object object = e.getObject();
2721
2722 if ( object != null )
2723 {
2724 tool.getVelocityEngine().setProperty( e.getKey(), object );
2725 }
2726 else
2727 {
2728 tool.getVelocityEngine().clearProperty( e.getKey() );
2729 }
2730 }
2731 }
2732
2733 for ( final Map.Entry<Object, Object> e : System.getProperties().entrySet() )
2734 {
2735 tool.getTemplateParameters().put( e.getKey().toString(), e.getValue() );
2736 }
2737
2738 if ( this.getMavenProject().getProperties() != null )
2739 {
2740 for ( final Map.Entry<Object, Object> e : System.getProperties().entrySet() )
2741 {
2742 tool.getTemplateParameters().put( e.getKey().toString(), e.getValue() );
2743 }
2744 }
2745
2746 if ( this.templateParameterResources != null )
2747 {
2748 for ( int i = 0, s0 = this.templateParameterResources.size(); i < s0; i++ )
2749 {
2750 for ( final Map.Entry<Object, Object> e : this.getProperties(
2751 this.templateParameterResources.get( i ) ).entrySet() )
2752 {
2753 if ( e.getValue() != null )
2754 {
2755 tool.getTemplateParameters().put( e.getKey().toString(), e.getValue() );
2756 }
2757 else
2758 {
2759 tool.getTemplateParameters().remove( e.getKey().toString() );
2760 }
2761 }
2762 }
2763 }
2764
2765 if ( this.templateParameters != null )
2766 {
2767 for ( final TemplateParameter e : this.templateParameters )
2768 {
2769 final Object object = e.getObject();
2770
2771 if ( object != null )
2772 {
2773 tool.getTemplateParameters().put( e.getKey(), object );
2774 }
2775 else
2776 {
2777 tool.getTemplateParameters().remove( e.getKey() );
2778 }
2779 }
2780 }
2781
2782 if ( this.templateLocation != null )
2783 {
2784 final URL url = this.getDirectory( this.templateLocation );
2785 tool.setTemplateLocation( url );
2786
2787 if ( url == null && this.isLoggable( Level.WARNING ) )
2788 {
2789 this.log( Level.WARNING, Messages.getMessage( "locationNotFound", this.templateLocation ), null );
2790 }
2791 }
2792 }
2793 catch ( final InstantiationException e )
2794 {
2795 throw new MojoExecutionException( Messages.getMessage( e ), e );
2796 }
2797 catch ( final IOException e )
2798 {
2799 throw new MojoExecutionException( Messages.getMessage( e ), e );
2800 }
2801 }
2802
2803 private Artifact getPluginArtifact( final Artifact a )
2804 {
2805 for ( int i = 0, s0 = this.pluginArtifacts.size(); i < s0; i++ )
2806 {
2807 final Artifact pluginArtifact = this.pluginArtifacts.get( i );
2808
2809 if ( pluginArtifact.getGroupId().equals( a.getGroupId() )
2810 && pluginArtifact.getArtifactId().equals( a.getArtifactId() )
2811 && ( pluginArtifact.hasClassifier()
2812 ? pluginArtifact.getClassifier().equals( a.getClassifier() )
2813 : !a.hasClassifier() ) )
2814 {
2815 return pluginArtifact;
2816 }
2817 }
2818
2819 return null;
2820 }
2821
2822 }