KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > project > injection > DefaultProfileInjector


1 package org.apache.maven.project.injection;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.model.Build;
20 import org.apache.maven.model.BuildBase;
21 import org.apache.maven.model.ConfigurationContainer;
22 import org.apache.maven.model.Dependency;
23 import org.apache.maven.model.DependencyManagement;
24 import org.apache.maven.model.DistributionManagement;
25 import org.apache.maven.model.Model;
26 import org.apache.maven.model.Plugin;
27 import org.apache.maven.model.PluginContainer;
28 import org.apache.maven.model.PluginExecution;
29 import org.apache.maven.model.PluginManagement;
30 import org.apache.maven.model.Profile;
31 import org.apache.maven.model.ReportPlugin;
32 import org.apache.maven.model.ReportSet;
33 import org.apache.maven.model.Reporting;
34 import org.apache.maven.project.ModelUtils;
35 import org.codehaus.plexus.util.StringUtils;
36 import org.codehaus.plexus.util.xml.Xpp3Dom;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.Properties JavaDoc;
44 import java.util.TreeMap JavaDoc;
45
46 /**
47  * Inject profile data into a Model, using the profile as the dominant data source, and
48  * persisting results of the injection in the Model.
49  *
50  * This will look similar to the ModelUtils/DefaultModelInheritanceAssembler code, but
51  * they are distinct. In model inheritance, the child provides data dominance AND persists
52  * the results of the merge...sort of a 'merge-out' system.
53  *
54  * In this system, the profile is dominant, but the model receives the merge result...sort
55  * of a 'merge-in' system. The two pieces of code look like they could be combined with a
56  * set of flags to determine which direction to merge 'to', but there are enough differences
57  * in the code to justify the extra code involved with separating them, in order to simplify
58  * the logic.
59  */

60 public class DefaultProfileInjector
61     implements ProfileInjector
62 {
63
64     public void inject( Profile profile, Model model )
65     {
66         injectDependencies( profile, model );
67
68         injectModules( profile, model );
69
70         model.setRepositories( ModelUtils.mergeRepositoryLists( profile.getRepositories(), model.getRepositories() ) );
71         model.setPluginRepositories( ModelUtils.mergeRepositoryLists( profile.getPluginRepositories(), model
72             .getPluginRepositories() ) );
73
74         injectReporting( profile, model );
75
76         injectDependencyManagement( profile, model );
77
78         injectDistributionManagement( profile, model );
79
80         injectBuild( profile, model );
81
82         Properties JavaDoc props = new Properties JavaDoc();
83         props.putAll( model.getProperties() );
84         props.putAll( profile.getProperties() );
85
86         model.setProperties( props );
87     }
88
89     private void injectBuild( Profile profile, Model model )
90     {
91         BuildBase profileBuild = profile.getBuild();
92         Build modelBuild = model.getBuild();
93
94         // if the parent build is null, obviously we cannot inherit from it...
95
if ( profileBuild != null )
96         {
97             if ( modelBuild == null )
98             {
99                 modelBuild = new Build();
100                 model.setBuild( modelBuild );
101             }
102
103             if ( profileBuild.getDirectory() != null )
104             {
105                 modelBuild.setDirectory( profileBuild.getDirectory() );
106             }
107
108             if ( profileBuild.getDefaultGoal() != null )
109             {
110                 modelBuild.setDefaultGoal( profileBuild.getDefaultGoal() );
111             }
112
113             if ( profileBuild.getFinalName() != null )
114             {
115                 modelBuild.setFinalName( profileBuild.getFinalName() );
116             }
117
118             ModelUtils.mergeFilterLists( modelBuild.getFilters(), profileBuild.getFilters() );
119             ModelUtils.mergeResourceLists( modelBuild.getResources(), profileBuild.getResources() );
120             ModelUtils.mergeResourceLists( modelBuild.getTestResources(), profileBuild.getTestResources() );
121
122             injectPlugins( profileBuild, modelBuild );
123
124             // Plugin management :: aggregate
125
PluginManagement profilePM = profileBuild.getPluginManagement();
126             PluginManagement modelPM = modelBuild.getPluginManagement();
127
128             if ( modelPM == null )
129             {
130                 modelBuild.setPluginManagement( profilePM );
131             }
132             else
133             {
134                 injectPlugins( profilePM, modelPM );
135             }
136         }
137     }
138
139     private void injectPlugins( PluginContainer profileContainer, PluginContainer modelContainer )
140     {
141         if ( profileContainer == null || modelContainer == null )
142         {
143             // nothing to do...
144
return;
145         }
146         
147         List JavaDoc modelPlugins = modelContainer.getPlugins();
148
149         if ( modelPlugins == null )
150         {
151             modelContainer.setPlugins( profileContainer.getPlugins() );
152         }
153         else if ( profileContainer.getPlugins() != null )
154         {
155             Map JavaDoc mergedPlugins = new TreeMap JavaDoc();
156
157             Map JavaDoc profilePlugins = profileContainer.getPluginsAsMap();
158
159             for ( Iterator JavaDoc it = modelPlugins.iterator(); it.hasNext(); )
160             {
161                 Plugin modelPlugin = (Plugin) it.next();
162
163                 Plugin mergedPlugin = modelPlugin;
164
165                 Plugin profilePlugin = (Plugin) profilePlugins.get( modelPlugin.getKey() );
166
167                 if ( profilePlugin != null )
168                 {
169                     mergedPlugin = modelPlugin;
170
171                     injectPluginDefinition( profilePlugin, modelPlugin );
172                 }
173
174                 mergedPlugins.put( mergedPlugin.getKey(), mergedPlugin );
175             }
176
177             for ( Iterator JavaDoc it = profilePlugins.values().iterator(); it.hasNext(); )
178             {
179                 Plugin profilePlugin = (Plugin) it.next();
180
181                 if ( !mergedPlugins.containsKey( profilePlugin.getKey() ) )
182                 {
183                     mergedPlugins.put( profilePlugin.getKey(), profilePlugin );
184                 }
185             }
186
187             modelContainer.setPlugins( new ArrayList JavaDoc( mergedPlugins.values() ) );
188
189             modelContainer.flushPluginMap();
190         }
191     }
192
193     private void injectPluginDefinition( Plugin profilePlugin, Plugin modelPlugin )
194     {
195         if ( profilePlugin == null || modelPlugin == null )
196         {
197             // nothing to do.
198
return;
199         }
200
201         if ( profilePlugin.isExtensions() )
202         {
203             modelPlugin.setExtensions( true );
204         }
205
206         if ( profilePlugin.getVersion() != null )
207         {
208             modelPlugin.setVersion( profilePlugin.getVersion() );
209         }
210
211         // merge the lists of goals that are not attached to an <execution/>
212
injectConfigurationContainer( profilePlugin, modelPlugin );
213
214         // from here to the end of the method is dealing with merging of the <executions/> section.
215
List JavaDoc modelExecutions = modelPlugin.getExecutions();
216
217         if ( modelExecutions == null || modelExecutions.isEmpty() )
218         {
219             modelPlugin.setExecutions( profilePlugin.getExecutions() );
220         }
221         else
222         {
223             Map JavaDoc executions = new TreeMap JavaDoc();
224
225             Map JavaDoc profileExecutions = profilePlugin.getExecutionsAsMap();
226
227             for ( Iterator JavaDoc it = modelExecutions.iterator(); it.hasNext(); )
228             {
229                 PluginExecution modelExecution = (PluginExecution) it.next();
230
231                 PluginExecution profileExecution = (PluginExecution) profileExecutions.get( modelExecution.getId() );
232
233                 if ( profileExecution != null )
234                 {
235                     injectConfigurationContainer( profileExecution, modelExecution );
236
237                     if ( profileExecution.getPhase() != null )
238                     {
239                         modelExecution.setPhase( profileExecution.getPhase() );
240                     }
241
242                     List JavaDoc profileGoals = profileExecution.getGoals();
243                     List JavaDoc modelGoals = modelExecution.getGoals();
244
245                     List JavaDoc goals = new ArrayList JavaDoc();
246
247                     if ( modelGoals != null && !modelGoals.isEmpty() )
248                     {
249                         goals.addAll( modelGoals );
250                     }
251
252                     if ( profileGoals != null )
253                     {
254                         for ( Iterator JavaDoc goalIterator = profileGoals.iterator(); goalIterator.hasNext(); )
255                         {
256                             String JavaDoc goal = (String JavaDoc) goalIterator.next();
257
258                             if ( !goals.contains( goal ) )
259                             {
260                                 goals.add( goal );
261                             }
262                         }
263                     }
264
265                     modelExecution.setGoals( goals );
266                 }
267
268                 executions.put( modelExecution.getId(), modelExecution );
269             }
270
271             for ( Iterator JavaDoc it = profileExecutions.entrySet().iterator(); it.hasNext(); )
272             {
273                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
274
275                 String JavaDoc id = (String JavaDoc) entry.getKey();
276
277                 if ( !executions.containsKey( id ) )
278                 {
279                     executions.put( id, entry.getValue() );
280                 }
281             }
282
283             modelPlugin.setExecutions( new ArrayList JavaDoc( executions.values() ) );
284
285             modelPlugin.flushExecutionMap();
286         }
287
288     }
289
290     private void injectConfigurationContainer( ConfigurationContainer profileContainer,
291                                                ConfigurationContainer modelContainer )
292     {
293         Xpp3Dom configuration = (Xpp3Dom) profileContainer.getConfiguration();
294         Xpp3Dom parentConfiguration = (Xpp3Dom) modelContainer.getConfiguration();
295
296         configuration = Xpp3Dom.mergeXpp3Dom( configuration, parentConfiguration );
297
298         modelContainer.setConfiguration( configuration );
299     }
300
301     /**
302      * Append modules specified in the profile to the end of the list supplied by the model, if
303      * they don't already exist.
304      */

305     private void injectModules( Profile profile, Model model )
306     {
307         List JavaDoc modules = new ArrayList JavaDoc();
308
309         List JavaDoc modelModules = model.getModules();
310
311         if ( modelModules != null && !modelModules.isEmpty() )
312         {
313             modules.addAll( modelModules );
314         }
315
316         List JavaDoc profileModules = profile.getModules();
317
318         if ( profileModules != null )
319         {
320             for ( Iterator JavaDoc it = profileModules.iterator(); it.hasNext(); )
321             {
322                 String JavaDoc module = (String JavaDoc) it.next();
323
324                 if ( !modules.contains( module ) )
325                 {
326                     modules.add( module );
327                 }
328             }
329         }
330
331         model.setModules( modules );
332     }
333
334     private void injectDistributionManagement( Profile profile, Model model )
335     {
336         DistributionManagement pDistMgmt = profile.getDistributionManagement();
337         DistributionManagement mDistMgmt = model.getDistributionManagement();
338
339         if ( mDistMgmt == null )
340         {
341             model.setDistributionManagement( pDistMgmt );
342         }
343         else if ( pDistMgmt != null )
344         {
345             if ( pDistMgmt.getRepository() != null )
346             {
347                 mDistMgmt.setRepository( pDistMgmt.getRepository() );
348             }
349
350             if ( pDistMgmt.getSnapshotRepository() != null )
351             {
352                 mDistMgmt.setSnapshotRepository( pDistMgmt.getSnapshotRepository() );
353             }
354
355             if ( StringUtils.isNotEmpty( pDistMgmt.getDownloadUrl() ) )
356             {
357                 mDistMgmt.setDownloadUrl( pDistMgmt.getDownloadUrl() );
358             }
359
360             if ( pDistMgmt.getRelocation() != null )
361             {
362                 mDistMgmt.setRelocation( pDistMgmt.getRelocation() );
363             }
364
365             if ( pDistMgmt.getSite() != null )
366             {
367                 mDistMgmt.setSite( pDistMgmt.getSite() );
368             }
369
370             // NOTE: We SHOULD NOT be inheriting status, since this is an assessment of the POM quality.
371
}
372     }
373
374     private void injectDependencyManagement( Profile profile, Model model )
375     {
376         DependencyManagement modelDepMgmt = model.getDependencyManagement();
377
378         DependencyManagement profileDepMgmt = profile.getDependencyManagement();
379
380         if ( profileDepMgmt != null )
381         {
382             if ( modelDepMgmt == null )
383             {
384                 model.setDependencyManagement( profileDepMgmt );
385             }
386             else
387             {
388                 Map JavaDoc depsMap = new HashMap JavaDoc();
389
390                 List JavaDoc deps = modelDepMgmt.getDependencies();
391
392                 if ( deps != null )
393                 {
394                     for ( Iterator JavaDoc it = deps.iterator(); it.hasNext(); )
395                     {
396                         Dependency dependency = (Dependency) it.next();
397                         depsMap.put( dependency.getManagementKey(), dependency );
398                     }
399                 }
400
401                 deps = profileDepMgmt.getDependencies();
402
403                 if ( deps != null )
404                 {
405                     for ( Iterator JavaDoc it = deps.iterator(); it.hasNext(); )
406                     {
407                         Dependency dependency = (Dependency) it.next();
408                         depsMap.put( dependency.getManagementKey(), dependency );
409                     }
410                 }
411
412                 modelDepMgmt.setDependencies( new ArrayList JavaDoc( depsMap.values() ) );
413             }
414         }
415     }
416
417     private void injectReporting( Profile profile, Model model )
418     {
419         // Reports :: aggregate
420
Reporting profileReporting = profile.getReporting();
421         Reporting modelReporting = model.getReporting();
422
423         if ( profileReporting != null )
424         {
425             if ( modelReporting == null )
426             {
427                 model.setReporting( profileReporting );
428             }
429             else
430             {
431                 if ( StringUtils.isEmpty( modelReporting.getOutputDirectory() ) )
432                 {
433                     modelReporting.setOutputDirectory( profileReporting.getOutputDirectory() );
434                 }
435
436                 Map JavaDoc mergedReportPlugins = new HashMap JavaDoc();
437
438                 Map JavaDoc profileReportersByKey = profileReporting.getReportPluginsAsMap();
439
440                 List JavaDoc modelReportPlugins = modelReporting.getPlugins();
441
442                 if ( modelReportPlugins != null )
443                 {
444                     for ( Iterator JavaDoc it = modelReportPlugins.iterator(); it.hasNext(); )
445                     {
446                         ReportPlugin modelReportPlugin = (ReportPlugin) it.next();
447
448                         String JavaDoc inherited = modelReportPlugin.getInherited();
449
450                         if ( StringUtils.isEmpty( inherited ) || Boolean.valueOf( inherited ).booleanValue() )
451                         {
452                             ReportPlugin profileReportPlugin = (ReportPlugin) profileReportersByKey
453                                 .get( modelReportPlugin.getKey() );
454
455                             ReportPlugin mergedReportPlugin = modelReportPlugin;
456
457                             if ( profileReportPlugin != null )
458                             {
459                                 mergedReportPlugin = profileReportPlugin;
460
461                                 mergeReportPlugins( profileReportPlugin, modelReportPlugin );
462                             }
463                             else if ( StringUtils.isEmpty( inherited ) )
464                             {
465                                 mergedReportPlugin.unsetInheritanceApplied();
466                             }
467
468                             mergedReportPlugins.put( mergedReportPlugin.getKey(), mergedReportPlugin );
469                         }
470                     }
471                 }
472
473                 for ( Iterator JavaDoc it = profileReportersByKey.entrySet().iterator(); it.hasNext(); )
474                 {
475                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
476
477                     String JavaDoc key = (String JavaDoc) entry.getKey();
478
479                     if ( !mergedReportPlugins.containsKey( key ) )
480                     {
481                         mergedReportPlugins.put( key, entry.getValue() );
482                     }
483                 }
484
485                 modelReporting.setPlugins( new ArrayList JavaDoc( mergedReportPlugins.values() ) );
486
487                 modelReporting.flushReportPluginMap();
488             }
489         }
490     }
491
492     private void mergeReportPlugins( ReportPlugin dominant, ReportPlugin recessive )
493     {
494         if ( StringUtils.isEmpty( recessive.getVersion() ) )
495         {
496             recessive.setVersion( dominant.getVersion() );
497         }
498
499         Xpp3Dom dominantConfig = (Xpp3Dom) dominant.getConfiguration();
500         Xpp3Dom recessiveConfig = (Xpp3Dom) recessive.getConfiguration();
501
502         recessive.setConfiguration( Xpp3Dom.mergeXpp3Dom( dominantConfig, recessiveConfig ) );
503
504         Map JavaDoc mergedReportSets = new HashMap JavaDoc();
505
506         Map JavaDoc dominantReportSetsById = dominant.getReportSetsAsMap();
507
508         for ( Iterator JavaDoc it = recessive.getReportSets().iterator(); it.hasNext(); )
509         {
510             ReportSet recessiveReportSet = (ReportSet) it.next();
511
512             ReportSet dominantReportSet = (ReportSet) dominantReportSetsById.get( recessiveReportSet.getId() );
513
514             ReportSet merged = recessiveReportSet;
515
516             if ( dominantReportSet != null )
517             {
518                 merged = recessiveReportSet;
519
520                 Xpp3Dom dominantRSConfig = (Xpp3Dom) dominantReportSet.getConfiguration();
521                 Xpp3Dom mergedRSConfig = (Xpp3Dom) merged.getConfiguration();
522
523                 merged.setConfiguration( Xpp3Dom.mergeXpp3Dom( dominantRSConfig, mergedRSConfig ) );
524
525                 List JavaDoc mergedReports = merged.getReports();
526
527                 if ( mergedReports == null )
528                 {
529                     mergedReports = new ArrayList JavaDoc();
530
531                     merged.setReports( mergedReports );
532                 }
533
534                 List JavaDoc dominantRSReports = dominantReportSet.getReports();
535
536                 if ( dominantRSReports != null )
537                 {
538                     for ( Iterator JavaDoc reportIterator = dominantRSReports.iterator(); reportIterator.hasNext(); )
539                     {
540                         String JavaDoc report = (String JavaDoc) reportIterator.next();
541
542                         if ( !mergedReports.contains( report ) )
543                         {
544                             mergedReports.add( report );
545                         }
546                     }
547                 }
548
549                 mergedReportSets.put( merged.getId(), merged );
550             }
551         }
552
553         for ( Iterator JavaDoc rsIterator = dominantReportSetsById.entrySet().iterator(); rsIterator.hasNext(); )
554         {
555             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) rsIterator.next();
556
557             String JavaDoc key = (String JavaDoc) entry.getKey();
558
559             if ( !mergedReportSets.containsKey( key ) )
560             {
561                 mergedReportSets.put( key, entry.getValue() );
562             }
563         }
564
565         recessive.setReportSets( new ArrayList JavaDoc( mergedReportSets.values() ) );
566
567         recessive.flushReportSetMap();
568     }
569
570     private void injectDependencies( Profile profile, Model model )
571     {
572         Map JavaDoc depsMap = new HashMap JavaDoc();
573
574         List JavaDoc deps = model.getDependencies();
575
576         if ( deps != null )
577         {
578             for ( Iterator JavaDoc it = deps.iterator(); it.hasNext(); )
579             {
580                 Dependency dependency = (Dependency) it.next();
581                 depsMap.put( dependency.getManagementKey(), dependency );
582             }
583         }
584
585         deps = profile.getDependencies();
586
587         if ( deps != null )
588         {
589             for ( Iterator JavaDoc it = deps.iterator(); it.hasNext(); )
590             {
591                 Dependency dependency = (Dependency) it.next();
592                 depsMap.put( dependency.getManagementKey(), dependency );
593             }
594         }
595
596         model.setDependencies( new ArrayList JavaDoc( depsMap.values() ) );
597     }
598
599 }
600
Popular Tags