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