View Javadoc

1   package org.apache.maven.settings;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.model.ActivationFile;
23  import org.codehaus.plexus.util.StringUtils;
24  
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * Several convenience methods to handle settings
33   *
34   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
35   * @version $Id: SettingsUtils.java 682323 2008-08-04 11:14:28Z vsiveton $
36   */
37  public final class SettingsUtils
38  {
39      private SettingsUtils()
40      {
41          // don't allow construction.
42      }
43  
44      /**
45       * @param dominant
46       * @param recessive
47       * @param recessiveSourceLevel
48       */
49      public static void merge( Settings dominant, Settings recessive, String recessiveSourceLevel )
50      {
51          if ( dominant == null || recessive == null )
52          {
53              return;
54          }
55  
56          recessive.setSourceLevel( recessiveSourceLevel );
57  
58          List dominantActiveProfiles = dominant.getActiveProfiles();
59          List recessiveActiveProfiles = recessive.getActiveProfiles();
60  
61          if ( recessiveActiveProfiles != null )
62          {
63              if ( dominantActiveProfiles == null )
64              {
65                  dominantActiveProfiles = new ArrayList();
66                  dominant.setActiveProfiles( dominantActiveProfiles );
67              }
68  
69              for ( Iterator it = recessiveActiveProfiles.iterator(); it.hasNext(); )
70              {
71                  String profileId = (String) it.next();
72  
73                  if ( !dominantActiveProfiles.contains( profileId ) )
74                  {
75                      dominantActiveProfiles.add( profileId );
76  
77                      dominant.getRuntimeInfo().setActiveProfileSourceLevel( profileId, recessiveSourceLevel );
78                  }
79              }
80          }
81  
82          List dominantPluginGroupIds = dominant.getPluginGroups();
83          List recessivePluginGroupIds = recessive.getPluginGroups();
84  
85          if ( recessivePluginGroupIds != null )
86          {
87              if ( dominantPluginGroupIds == null )
88              {
89                  dominantPluginGroupIds = new ArrayList();
90                  dominant.setPluginGroups( dominantPluginGroupIds );
91              }
92  
93              for ( Iterator it = recessivePluginGroupIds.iterator(); it.hasNext(); )
94              {
95                  String pluginGroupId = (String) it.next();
96  
97                  if ( !dominantPluginGroupIds.contains( pluginGroupId ) )
98                  {
99                      dominantPluginGroupIds.add( pluginGroupId );
100 
101                     dominant.getRuntimeInfo().setPluginGroupIdSourceLevel( pluginGroupId, recessiveSourceLevel );
102                 }
103             }
104         }
105 
106         if ( StringUtils.isEmpty( dominant.getLocalRepository() ) )
107         {
108             dominant.setLocalRepository( recessive.getLocalRepository() );
109 
110             dominant.getRuntimeInfo().setLocalRepositorySourceLevel( recessiveSourceLevel );
111         }
112 
113         shallowMergeById( dominant.getMirrors(), recessive.getMirrors(), recessiveSourceLevel );
114         shallowMergeById( dominant.getServers(), recessive.getServers(), recessiveSourceLevel );
115         shallowMergeById( dominant.getProxies(), recessive.getProxies(), recessiveSourceLevel );
116         shallowMergeById( dominant.getProfiles(), recessive.getProfiles(), recessiveSourceLevel );
117     }
118 
119     /**
120      * @param dominant
121      * @param recessive
122      * @param recessiveSourceLevel
123      */
124     private static void shallowMergeById( List dominant, List recessive, String recessiveSourceLevel )
125     {
126         Map dominantById = mapById( dominant );
127 
128         for ( Iterator it = recessive.iterator(); it.hasNext(); )
129         {
130             IdentifiableBase identifiable = (IdentifiableBase) it.next();
131 
132             if ( !dominantById.containsKey( identifiable.getId() ) )
133             {
134                 identifiable.setSourceLevel( recessiveSourceLevel );
135 
136                 dominant.add( identifiable );
137             }
138         }
139     }
140 
141     /**
142      * @param identifiables
143      * @return a map
144      */
145     private static Map mapById( List identifiables )
146     {
147         Map byId = new HashMap();
148 
149         for ( Iterator it = identifiables.iterator(); it.hasNext(); )
150         {
151             IdentifiableBase identifiable = (IdentifiableBase) it.next();
152 
153             byId.put( identifiable.getId(), identifiable );
154         }
155 
156         return byId;
157     }
158 
159     /**
160      * @param settingsProfile
161      * @return a profile
162      */
163     public static org.apache.maven.model.Profile convertFromSettingsProfile( Profile settingsProfile )
164     {
165         org.apache.maven.model.Profile profile = new org.apache.maven.model.Profile();
166 
167         profile.setId( settingsProfile.getId() );
168 
169         profile.setSource( "settings.xml" );
170 
171         Activation settingsActivation = settingsProfile.getActivation();
172 
173         if ( settingsActivation != null )
174         {
175             org.apache.maven.model.Activation activation = new org.apache.maven.model.Activation();
176 
177             activation.setActiveByDefault( settingsActivation.isActiveByDefault() );
178 
179             activation.setJdk( settingsActivation.getJdk() );
180 
181             ActivationProperty settingsProp = settingsActivation.getProperty();
182 
183             if ( settingsProp != null )
184             {
185                 org.apache.maven.model.ActivationProperty prop = new org.apache.maven.model.ActivationProperty();
186 
187                 prop.setName( settingsProp.getName() );
188                 prop.setValue( settingsProp.getValue() );
189 
190                 activation.setProperty( prop );
191             }
192 
193             ActivationOS settingsOs = settingsActivation.getOs();
194 
195             if ( settingsOs != null )
196             {
197                 org.apache.maven.model.ActivationOS os = new org.apache.maven.model.ActivationOS();
198 
199                 os.setArch( settingsOs.getArch() );
200                 os.setFamily( settingsOs.getFamily() );
201                 os.setName( settingsOs.getName() );
202                 os.setVersion( settingsOs.getVersion() );
203 
204                 activation.setOs( os );
205             }
206 
207             org.apache.maven.settings.ActivationFile settingsFile = settingsActivation.getFile();
208 
209             if ( settingsFile != null )
210             {
211                 ActivationFile file = new ActivationFile();
212 
213                 file.setExists( settingsFile.getExists() );
214                 file.setMissing( settingsFile.getMissing() );
215 
216                 activation.setFile( file );
217             }
218 
219             profile.setActivation( activation );
220         }
221 
222         profile.setProperties( settingsProfile.getProperties() );
223 
224         List repos = settingsProfile.getRepositories();
225         if ( repos != null )
226         {
227             for ( Iterator it = repos.iterator(); it.hasNext(); )
228             {
229                 profile.addRepository( convertFromSettingsRepository( (Repository) it.next() ) );
230             }
231         }
232 
233         List pluginRepos = settingsProfile.getPluginRepositories();
234         if ( pluginRepos != null )
235         {
236             for ( Iterator it = pluginRepos.iterator(); it.hasNext(); )
237             {
238                 profile.addPluginRepository( convertFromSettingsRepository( (Repository) it.next() ) );
239             }
240         }
241 
242         return profile;
243     }
244 
245     /**
246      * @param settingsRepo
247      * @return a repository
248      */
249     private static org.apache.maven.model.Repository convertFromSettingsRepository( Repository settingsRepo )
250     {
251         org.apache.maven.model.Repository repo = new org.apache.maven.model.Repository();
252 
253         repo.setId( settingsRepo.getId() );
254         repo.setLayout( settingsRepo.getLayout() );
255         repo.setName( settingsRepo.getName() );
256         repo.setUrl( settingsRepo.getUrl() );
257 
258         if ( settingsRepo.getSnapshots() != null )
259         {
260             repo.setSnapshots( convertRepositoryPolicy( settingsRepo.getSnapshots() ) );
261         }
262         if ( settingsRepo.getReleases() != null )
263         {
264             repo.setReleases( convertRepositoryPolicy( settingsRepo.getReleases() ) );
265         }
266 
267         return repo;
268     }
269 
270     /**
271      * @param settingsPolicy
272      * @return a RepositoryPolicy
273      */
274     private static org.apache.maven.model.RepositoryPolicy convertRepositoryPolicy( RepositoryPolicy settingsPolicy )
275     {
276         org.apache.maven.model.RepositoryPolicy policy = new org.apache.maven.model.RepositoryPolicy();
277         policy.setEnabled( settingsPolicy.isEnabled() );
278         policy.setUpdatePolicy( settingsPolicy.getUpdatePolicy() );
279         policy.setChecksumPolicy( settingsPolicy.getChecksumPolicy() );
280         return policy;
281     }
282 
283     /**
284      * @param settings could be null
285      * @return a new instance of settings or null if settings was null.
286      */
287     public static Settings copySettings( Settings settings )
288     {
289         if ( settings == null )
290         {
291             return null;
292         }
293 
294         Settings clone = new Settings();
295         clone.setActiveProfiles( settings.getActiveProfiles() );
296         clone.setInteractiveMode( settings.isInteractiveMode() );
297         clone.setLocalRepository( settings.getLocalRepository() );
298         clone.setMirrors( settings.getMirrors() );
299         clone.setModelEncoding( settings.getModelEncoding() );
300         clone.setOffline( settings.isOffline() );
301         clone.setPluginGroups( settings.getPluginGroups() );
302         clone.setProfiles( settings.getProfiles() );
303         clone.setProxies( settings.getProxies() );
304         clone.setRuntimeInfo( settings.getRuntimeInfo() );
305         clone.setServers( settings.getServers() );
306         clone.setSourceLevel( settings.getSourceLevel() );
307         clone.setUsePluginRegistry( settings.isUsePluginRegistry() );
308 
309         return clone;
310     }
311 }