KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > settings > SettingsUtils


1 package org.apache.maven.settings;
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.ActivationFile;
20 import org.codehaus.plexus.util.StringUtils;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 public final class SettingsUtils
29 {
30     private SettingsUtils()
31     {
32         // don't allow construction.
33
}
34
35     public static void merge( Settings dominant, Settings recessive, String JavaDoc recessiveSourceLevel )
36     {
37         if ( dominant == null || recessive == null )
38         {
39             return;
40         }
41
42         recessive.setSourceLevel( recessiveSourceLevel );
43
44         List JavaDoc dominantActiveProfiles = dominant.getActiveProfiles();
45         List JavaDoc recessiveActiveProfiles = recessive.getActiveProfiles();
46
47         if ( recessiveActiveProfiles != null )
48         {
49             if ( dominantActiveProfiles == null )
50             {
51                 dominantActiveProfiles = new ArrayList JavaDoc();
52                 dominant.setActiveProfiles( dominantActiveProfiles );
53             }
54
55             for ( Iterator JavaDoc it = recessiveActiveProfiles.iterator(); it.hasNext(); )
56             {
57                 String JavaDoc profileId = (String JavaDoc) it.next();
58
59                 if ( !dominantActiveProfiles.contains( profileId ) )
60                 {
61                     dominantActiveProfiles.add( profileId );
62
63                     dominant.getRuntimeInfo().setActiveProfileSourceLevel( profileId, recessiveSourceLevel );
64                 }
65             }
66         }
67
68         List JavaDoc dominantPluginGroupIds = dominant.getPluginGroups();
69         List JavaDoc recessivePluginGroupIds = recessive.getPluginGroups();
70
71         if ( recessivePluginGroupIds != null )
72         {
73             if ( dominantPluginGroupIds == null )
74             {
75                 dominantPluginGroupIds = new ArrayList JavaDoc();
76                 dominant.setPluginGroups( dominantPluginGroupIds );
77             }
78
79             for ( Iterator JavaDoc it = recessivePluginGroupIds.iterator(); it.hasNext(); )
80             {
81                 String JavaDoc pluginGroupId = (String JavaDoc) it.next();
82
83                 if ( !dominantPluginGroupIds.contains( pluginGroupId ) )
84                 {
85                     dominantPluginGroupIds.add( pluginGroupId );
86
87                     dominant.getRuntimeInfo().setPluginGroupIdSourceLevel( pluginGroupId, recessiveSourceLevel );
88                 }
89             }
90         }
91
92         if ( StringUtils.isEmpty( dominant.getLocalRepository() ) )
93         {
94             dominant.setLocalRepository( recessive.getLocalRepository() );
95
96             dominant.getRuntimeInfo().setLocalRepositorySourceLevel( recessiveSourceLevel );
97         }
98
99         shallowMergeById( dominant.getMirrors(), recessive.getMirrors(), recessiveSourceLevel );
100         shallowMergeById( dominant.getServers(), recessive.getServers(), recessiveSourceLevel );
101         shallowMergeById( dominant.getProxies(), recessive.getProxies(), recessiveSourceLevel );
102         shallowMergeById( dominant.getProfiles(), recessive.getProfiles(), recessiveSourceLevel );
103
104     }
105
106     private static void shallowMergeById( List JavaDoc dominant, List JavaDoc recessive, String JavaDoc recessiveSourceLevel )
107     {
108         Map JavaDoc dominantById = mapById( dominant );
109
110         for ( Iterator JavaDoc it = recessive.iterator(); it.hasNext(); )
111         {
112             IdentifiableBase identifiable = (IdentifiableBase) it.next();
113
114             if ( !dominantById.containsKey( identifiable.getId() ) )
115             {
116                 identifiable.setSourceLevel( recessiveSourceLevel );
117
118                 dominant.add( identifiable );
119             }
120         }
121     }
122
123     private static Map JavaDoc mapById( List JavaDoc identifiables )
124     {
125         Map JavaDoc byId = new HashMap JavaDoc();
126
127         for ( Iterator JavaDoc it = identifiables.iterator(); it.hasNext(); )
128         {
129             IdentifiableBase identifiable = (IdentifiableBase) it.next();
130
131             byId.put( identifiable.getId(), identifiable );
132         }
133
134         return byId;
135     }
136
137     public static org.apache.maven.model.Profile convertFromSettingsProfile( Profile settingsProfile )
138     {
139         org.apache.maven.model.Profile profile = new org.apache.maven.model.Profile();
140
141         profile.setId( settingsProfile.getId() );
142
143         profile.setSource( "settings.xml" );
144
145         Activation settingsActivation = settingsProfile.getActivation();
146
147         if ( settingsActivation != null )
148         {
149             org.apache.maven.model.Activation activation = new org.apache.maven.model.Activation();
150
151             activation.setActiveByDefault( settingsActivation.isActiveByDefault() );
152
153             activation.setJdk( settingsActivation.getJdk() );
154
155             ActivationProperty settingsProp = settingsActivation.getProperty();
156
157             if ( settingsProp != null )
158             {
159                 org.apache.maven.model.ActivationProperty prop = new org.apache.maven.model.ActivationProperty();
160
161                 prop.setName( settingsProp.getName() );
162                 prop.setValue( settingsProp.getValue() );
163
164                 activation.setProperty( prop );
165             }
166
167             ActivationOS settingsOs = settingsActivation.getOs();
168
169             if ( settingsOs != null )
170             {
171                 org.apache.maven.model.ActivationOS os = new org.apache.maven.model.ActivationOS();
172
173                 os.setArch( settingsOs.getArch() );
174                 os.setFamily( settingsOs.getFamily() );
175                 os.setName( settingsOs.getName() );
176                 os.setVersion( settingsOs.getVersion() );
177
178                 activation.setOs( os );
179             }
180
181             org.apache.maven.settings.ActivationFile settingsFile = settingsActivation.getFile();
182
183             if ( settingsFile != null )
184             {
185                 ActivationFile file = new ActivationFile();
186
187                 file.setExists( settingsFile.getExists() );
188                 file.setMissing( settingsFile.getMissing() );
189
190                 activation.setFile( file );
191             }
192
193             profile.setActivation( activation );
194         }
195
196         profile.setProperties( settingsProfile.getProperties() );
197
198         List JavaDoc repos = settingsProfile.getRepositories();
199         if ( repos != null )
200         {
201             for ( Iterator JavaDoc it = repos.iterator(); it.hasNext(); )
202             {
203                 profile.addRepository( convertFromSettingsRepository( (Repository) it.next() ) );
204             }
205         }
206
207         List JavaDoc pluginRepos = settingsProfile.getPluginRepositories();
208         if ( pluginRepos != null )
209         {
210             for ( Iterator JavaDoc it = pluginRepos.iterator(); it.hasNext(); )
211             {
212                 profile.addPluginRepository( convertFromSettingsRepository( (Repository) it.next() ) );
213             }
214         }
215
216         return profile;
217     }
218
219     private static org.apache.maven.model.Repository convertFromSettingsRepository( Repository settingsRepo )
220     {
221         org.apache.maven.model.Repository repo = new org.apache.maven.model.Repository();
222
223         repo.setId( settingsRepo.getId() );
224         repo.setLayout( settingsRepo.getLayout() );
225         repo.setName( settingsRepo.getName() );
226         repo.setUrl( settingsRepo.getUrl() );
227
228         if ( settingsRepo.getSnapshots() != null )
229         {
230             repo.setSnapshots( convertRepositoryPolicy( settingsRepo.getSnapshots() ) );
231         }
232         if ( settingsRepo.getReleases() != null )
233         {
234             repo.setReleases( convertRepositoryPolicy( settingsRepo.getReleases() ) );
235         }
236
237         return repo;
238     }
239
240     private static org.apache.maven.model.RepositoryPolicy convertRepositoryPolicy( RepositoryPolicy settingsPolicy )
241     {
242         org.apache.maven.model.RepositoryPolicy policy = new org.apache.maven.model.RepositoryPolicy();
243         policy.setEnabled( settingsPolicy.isEnabled() );
244         policy.setUpdatePolicy( settingsPolicy.getUpdatePolicy() );
245         policy.setChecksumPolicy( settingsPolicy.getChecksumPolicy() );
246         return policy;
247     }
248
249 }
250
Popular Tags