1 package org.apache.maven.plugin.registry;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 public final class PluginRegistryUtils
29 {
30
31 private PluginRegistryUtils()
32 {
33
34 }
35
36 public static void merge( PluginRegistry dominant, PluginRegistry recessive, String recessiveSourceLevel )
37 {
38
39 if ( dominant == null || recessive == null )
40 {
41 return;
42 }
43
44 RuntimeInfo dominantRtInfo = dominant.getRuntimeInfo();
45
46 String dominantUpdateInterval = dominant.getUpdateInterval();
47
48 if ( dominantUpdateInterval == null )
49 {
50 String recessiveUpdateInterval = recessive.getUpdateInterval();
51
52 if ( recessiveUpdateInterval != null )
53 {
54 dominant.setUpdateInterval( recessiveUpdateInterval );
55 dominantRtInfo.setUpdateIntervalSourceLevel( recessiveSourceLevel );
56 }
57 }
58
59 String dominantAutoUpdate = dominant.getAutoUpdate();
60
61 if ( dominantAutoUpdate == null )
62 {
63 String recessiveAutoUpdate = recessive.getAutoUpdate();
64
65 if ( recessiveAutoUpdate != null )
66 {
67 dominant.setAutoUpdate( recessiveAutoUpdate );
68 dominantRtInfo.setAutoUpdateSourceLevel( recessiveSourceLevel );
69 }
70 }
71
72 List recessivePlugins = null;
73
74 if ( recessive != null )
75 {
76 recessivePlugins = recessive.getPlugins();
77 }
78 else
79 {
80 recessivePlugins = Collections.EMPTY_LIST;
81 }
82
83 shallowMergePlugins( dominant, recessivePlugins, recessiveSourceLevel );
84 }
85
86 public static void recursivelySetSourceLevel( PluginRegistry pluginRegistry, String sourceLevel )
87 {
88 if ( pluginRegistry == null )
89 {
90 return;
91 }
92
93 pluginRegistry.setSourceLevel( sourceLevel );
94
95 for ( Iterator it = pluginRegistry.getPlugins().iterator(); it.hasNext(); )
96 {
97 Plugin plugin = (Plugin) it.next();
98
99 plugin.setSourceLevel( sourceLevel );
100 }
101 }
102
103 private static void shallowMergePlugins( PluginRegistry dominant, List recessive, String recessiveSourceLevel )
104 {
105 Map dominantByKey = dominant.getPluginsByKey();
106
107 List dominantPlugins = dominant.getPlugins();
108
109 for ( Iterator it = recessive.iterator(); it.hasNext(); )
110 {
111 Plugin recessivePlugin = (Plugin) it.next();
112
113 if ( !dominantByKey.containsKey( recessivePlugin.getKey() ) )
114 {
115 recessivePlugin.setSourceLevel( recessiveSourceLevel );
116
117 dominantPlugins.add( recessivePlugin );
118 }
119 }
120
121 dominant.flushPluginsByKey();
122 }
123
124 public static PluginRegistry extractUserPluginRegistry( PluginRegistry pluginRegistry )
125 {
126 PluginRegistry userRegistry = null;
127
128
129 if ( pluginRegistry != null && !PluginRegistry.GLOBAL_LEVEL.equals( pluginRegistry.getSourceLevel() ) )
130 {
131 userRegistry = new PluginRegistry();
132
133 RuntimeInfo rtInfo = new RuntimeInfo( userRegistry );
134
135 userRegistry.setRuntimeInfo( rtInfo );
136
137 RuntimeInfo oldRtInfo = pluginRegistry.getRuntimeInfo();
138
139 if ( TrackableBase.USER_LEVEL.equals( oldRtInfo.getAutoUpdateSourceLevel() ) )
140 {
141 userRegistry.setAutoUpdate( pluginRegistry.getAutoUpdate() );
142 }
143
144 if ( TrackableBase.USER_LEVEL.equals( oldRtInfo.getUpdateIntervalSourceLevel() ) )
145 {
146 userRegistry.setUpdateInterval( pluginRegistry.getUpdateInterval() );
147 }
148
149 List plugins = new ArrayList();
150
151 for ( Iterator it = pluginRegistry.getPlugins().iterator(); it.hasNext(); )
152 {
153 Plugin plugin = (Plugin) it.next();
154
155 if ( TrackableBase.USER_LEVEL.equals( plugin.getSourceLevel() ) )
156 {
157 plugins.add( plugin );
158 }
159 }
160
161 userRegistry.setPlugins( plugins );
162
163 rtInfo.setFile( pluginRegistry.getRuntimeInfo().getFile() );
164 }
165
166 return userRegistry;
167 }
168
169 }