KickJava   Java API By Example, From Geeks To Geeks.

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


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.settings.io.xpp3.SettingsXpp3Reader;
20 import org.codehaus.plexus.logging.AbstractLogEnabled;
21 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
22 import org.codehaus.plexus.util.IOUtil;
23 import org.codehaus.plexus.util.StringUtils;
24 import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource;
25 import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
26 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
27
28 import java.io.File JavaDoc;
29 import java.io.FileReader JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.StringReader JavaDoc;
32 import java.io.StringWriter JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35
36 /**
37  * @author jdcasey
38  * @version $Id: DefaultMavenSettingsBuilder.java 189510 2005-06-08 03:27:43Z jdcasey $
39  */

40 public class DefaultMavenSettingsBuilder
41     extends AbstractLogEnabled
42     implements MavenSettingsBuilder, Initializable
43 {
44     public static final String JavaDoc userHome = System.getProperty( "user.home" );
45
46     /**
47      * @configuration
48      */

49     private String JavaDoc userSettingsPath;
50
51     /**
52      * @configuration
53      */

54     private String JavaDoc globalSettingsPath;
55
56     private File JavaDoc userSettingsFile;
57
58     private File JavaDoc globalSettingsFile;
59
60     private Settings loadedSettings;
61
62     // ----------------------------------------------------------------------
63
// Component Lifecycle
64
// ----------------------------------------------------------------------
65

66     public void initialize()
67     {
68         userSettingsFile =
69             getFile( userSettingsPath, "user.home", MavenSettingsBuilder.ALT_USER_SETTINGS_XML_LOCATION );
70
71         globalSettingsFile =
72             getFile( globalSettingsPath, "maven.home", MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION );
73
74         getLogger().debug(
75             "Building Maven global-level settings from: '" + globalSettingsFile.getAbsolutePath() + "'" );
76         getLogger().debug( "Building Maven user-level settings from: '" + userSettingsFile.getAbsolutePath() + "'" );
77     }
78
79     // ----------------------------------------------------------------------
80
// MavenProfilesBuilder Implementation
81
// ----------------------------------------------------------------------
82

83     private Settings readSettings( File JavaDoc settingsFile )
84         throws IOException JavaDoc, XmlPullParserException
85     {
86         Settings settings = null;
87
88         if ( settingsFile.exists() && settingsFile.isFile() )
89         {
90             FileReader JavaDoc reader = null;
91             try
92             {
93                 reader = new FileReader JavaDoc( settingsFile );
94                 StringWriter JavaDoc sWriter = new StringWriter JavaDoc();
95
96                 IOUtil.copy( reader, sWriter );
97
98                 String JavaDoc rawInput = sWriter.toString();
99
100                 try
101                 {
102                     RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
103                     interpolator.addValueSource( new EnvarBasedValueSource() );
104
105                     rawInput = interpolator.interpolate( rawInput, "settings" );
106                 }
107                 catch ( Exception JavaDoc e )
108                 {
109                     getLogger().warn(
110                         "Failed to initialize environment variable resolver. Skipping environment substitution in settings." );
111                     getLogger().debug( "Failed to initialize envar resolver. Skipping resolution.", e );
112                 }
113
114                 StringReader JavaDoc sReader = new StringReader JavaDoc( rawInput );
115
116                 SettingsXpp3Reader modelReader = new SettingsXpp3Reader();
117
118                 settings = modelReader.read( sReader );
119
120                 RuntimeInfo rtInfo = new RuntimeInfo( settings );
121
122                 rtInfo.setFile( settingsFile );
123
124                 settings.setRuntimeInfo( rtInfo );
125             }
126             finally
127             {
128                 IOUtil.close( reader );
129             }
130         }
131
132         return settings;
133     }
134
135     public Settings buildSettings()
136         throws IOException JavaDoc, XmlPullParserException
137     {
138         return buildSettings( userSettingsFile );
139     }
140
141     public Settings buildSettings( File JavaDoc userSettingsFile )
142         throws IOException JavaDoc, XmlPullParserException
143     {
144         if ( loadedSettings == null )
145         {
146             Settings globalSettings = readSettings( globalSettingsFile );
147             Settings userSettings = readSettings( userSettingsFile );
148
149             if ( globalSettings == null )
150             {
151                 globalSettings = new Settings();
152             }
153
154             if ( userSettings == null )
155             {
156                 userSettings = new Settings();
157                 userSettings.setRuntimeInfo( new RuntimeInfo( userSettings ) );
158             }
159
160             SettingsUtils.merge( userSettings, globalSettings, TrackableBase.GLOBAL_LEVEL );
161
162             activateDefaultProfiles( userSettings );
163
164             setLocalRepository( userSettings );
165
166             loadedSettings = userSettings;
167         }
168
169         return loadedSettings;
170     }
171
172     private void activateDefaultProfiles( Settings settings )
173     {
174         List JavaDoc activeProfiles = settings.getActiveProfiles();
175
176         for ( Iterator JavaDoc profiles = settings.getProfiles().iterator(); profiles.hasNext(); )
177         {
178             Profile profile = (Profile) profiles.next();
179             if ( profile.getActivation() != null && profile.getActivation().isActiveByDefault() )
180             {
181                 if ( !activeProfiles.contains( profile.getId() ) )
182                 {
183                     settings.addActiveProfile( profile.getId() );
184                 }
185             }
186         }
187     }
188
189     private void setLocalRepository( Settings userSettings )
190     {
191         // try using the local repository specified on the command line...
192
String JavaDoc localRepository = System.getProperty( MavenSettingsBuilder.ALT_LOCAL_REPOSITORY_LOCATION );
193
194         // otherwise, use the one in settings.xml
195
if ( localRepository == null || localRepository.length() < 1 )
196         {
197             localRepository = userSettings.getLocalRepository();
198         }
199
200         // if all of the above are missing, default to ~/.m2/repository.
201
if ( localRepository == null || localRepository.length() < 1 )
202         {
203             File JavaDoc mavenUserConfigurationDirectory = new File JavaDoc( userHome, ".m2" );
204             if ( !mavenUserConfigurationDirectory.exists() )
205             {
206                 if ( !mavenUserConfigurationDirectory.mkdirs() )
207                 {
208                     //throw a configuration exception
209
}
210             }
211
212             localRepository = new File JavaDoc( mavenUserConfigurationDirectory, "repository" ).getAbsolutePath();
213         }
214
215         userSettings.setLocalRepository( localRepository );
216     }
217
218     private File JavaDoc getFile( String JavaDoc pathPattern, String JavaDoc basedirSysProp, String JavaDoc altLocationSysProp )
219     {
220         // -------------------------------------------------------------------------------------
221
// Alright, here's the justification for all the regexp wizardry below...
222
//
223
// Continuum and other server-like apps may need to locate the user-level and
224
// global-level settings somewhere other than ${user.home} and ${maven.home},
225
// respectively. Using a simple replacement of these patterns will allow them
226
// to specify the absolute path to these files in a customized components.xml
227
// file. Ideally, we'd do full pattern-evaluation against the sysprops, but this
228
// is a first step. There are several replacements below, in order to normalize
229
// the path character before we operate on the string as a regex input, and
230
// in order to avoid surprises with the File construction...
231
// -------------------------------------------------------------------------------------
232

233         String JavaDoc path = System.getProperty( altLocationSysProp );
234
235         if ( StringUtils.isEmpty( path ) )
236         {
237             // TODO: This replacing shouldn't be necessary as user.home should be in the
238
// context of the container and thus the value would be interpolated by Plexus
239
String JavaDoc basedir = System.getProperty( basedirSysProp );
240             if ( basedir == null )
241             {
242                 basedir = System.getProperty( "user.dir" );
243             }
244
245             basedir = basedir.replaceAll( "\\\\", "/" );
246             basedir = basedir.replaceAll( "\\$", "\\\\\\$" );
247
248             path = pathPattern.replaceAll( "\\$\\{" + basedirSysProp + "\\}", basedir );
249             path = path.replaceAll( "\\\\", "/" );
250             path = path.replaceAll( "//", "/" );
251
252             return new File JavaDoc( path ).getAbsoluteFile();
253         }
254         else
255         {
256             return new File JavaDoc( path ).getAbsoluteFile();
257         }
258     }
259
260 }
261
Popular Tags