KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > plugin > registry > PluginRegistryUtils


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

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