KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.maven.plugin.registry;
2
3 import org.apache.maven.plugin.registry.io.xpp3.PluginRegistryXpp3Reader;
4 import org.codehaus.plexus.logging.AbstractLogEnabled;
5 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
6 import org.codehaus.plexus.util.IOUtil;
7 import org.codehaus.plexus.util.StringUtils;
8 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
9
10 import java.io.File JavaDoc;
11 import java.io.FileReader JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 /*
15  * Copyright 2001-2005 The Apache Software Foundation.
16  *
17  * Licensed under the Apache License, Version 2.0 (the "License");
18  * you may not use this file except in compliance with the License.
19  * You may obtain a copy of the License at
20  *
21  * http://www.apache.org/licenses/LICENSE-2.0
22  *
23  * Unless required by applicable law or agreed to in writing, software
24  * distributed under the License is distributed on an "AS IS" BASIS,
25  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  * See the License for the specific language governing permissions and
27  * limitations under the License.
28  */

29
30 public class DefaultPluginRegistryBuilder
31     extends AbstractLogEnabled
32     implements MavenPluginRegistryBuilder, Initializable
33 {
34
35     public static final String JavaDoc userHome = System.getProperty( "user.home" );
36
37     /**
38      * @configuration
39      */

40     private String JavaDoc userRegistryPath;
41
42     /**
43      * @configuration
44      */

45     private String JavaDoc globalRegistryPath;
46
47     private File JavaDoc userRegistryFile;
48
49     private File JavaDoc globalRegistryFile;
50
51     // ----------------------------------------------------------------------
52
// Component Lifecycle
53
// ----------------------------------------------------------------------
54

55     public void initialize()
56     {
57         userRegistryFile = getFile( userRegistryPath, "user.home", MavenPluginRegistryBuilder.ALT_USER_PLUGIN_REG_LOCATION );
58
59         getLogger().debug( "Building Maven user-level plugin registry from: '" + userRegistryFile.getAbsolutePath() + "'" );
60
61         if ( System.getProperty( "maven.home" ) != null ||
62              System.getProperty( MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION ) != null )
63         {
64             globalRegistryFile = getFile( globalRegistryPath, "maven.home", MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION );
65
66             getLogger().debug( "Building Maven global-level plugin registry from: '" + globalRegistryFile.getAbsolutePath() + "'" );
67         }
68     }
69     
70     public PluginRegistry buildPluginRegistry()
71         throws IOException JavaDoc, XmlPullParserException
72     {
73         PluginRegistry global = readPluginRegistry( globalRegistryFile );
74         
75         PluginRegistry user = readPluginRegistry( userRegistryFile );
76
77         if ( user == null && global != null )
78         {
79             // we'll use the globals, but first we have to recursively mark them as global...
80
PluginRegistryUtils.recursivelySetSourceLevel( global, PluginRegistry.GLOBAL_LEVEL );
81             
82             user = global;
83         }
84         else
85         {
86             // merge non-colliding plugins into the user registry.
87
PluginRegistryUtils.merge( user, global, TrackableBase.GLOBAL_LEVEL );
88         }
89
90         return user;
91     }
92
93     private PluginRegistry readPluginRegistry( File JavaDoc registryFile )
94         throws IOException JavaDoc, XmlPullParserException
95     {
96         PluginRegistry registry = null;
97
98         if ( registryFile != null && registryFile.exists() && registryFile.isFile() )
99         {
100             FileReader JavaDoc reader = null;
101             try
102             {
103                 reader = new FileReader JavaDoc( registryFile );
104
105                 PluginRegistryXpp3Reader modelReader = new PluginRegistryXpp3Reader();
106
107                 registry = modelReader.read( reader );
108                 
109                 RuntimeInfo rtInfo = new RuntimeInfo( registry );
110                 
111                 registry.setRuntimeInfo( rtInfo );
112                 
113                 rtInfo.setFile( registryFile );
114             }
115             finally
116             {
117                 IOUtil.close( reader );
118             }
119         }
120
121         return registry;
122     }
123
124     private File JavaDoc getFile( String JavaDoc pathPattern, String JavaDoc basedirSysProp, String JavaDoc altLocationSysProp )
125     {
126         // -------------------------------------------------------------------------------------
127
// Alright, here's the justification for all the regexp wizardry below...
128
//
129
// Continuum and other server-like apps may need to locate the user-level and
130
// global-level settings somewhere other than ${user.home} and ${maven.home},
131
// respectively. Using a simple replacement of these patterns will allow them
132
// to specify the absolute path to these files in a customized components.xml
133
// file. Ideally, we'd do full pattern-evaluation against the sysprops, but this
134
// is a first step. There are several replacements below, in order to normalize
135
// the path character before we operate on the string as a regex input, and
136
// in order to avoid surprises with the File construction...
137
// -------------------------------------------------------------------------------------
138

139         String JavaDoc path = System.getProperty( altLocationSysProp );
140
141         if ( StringUtils.isEmpty( path ) )
142         {
143             // TODO: This replacing shouldn't be necessary as user.home should be in the
144
// context of the container and thus the value would be interpolated by Plexus
145
String JavaDoc basedir = System.getProperty( basedirSysProp );
146
147             basedir = basedir.replaceAll( "\\\\", "/" );
148             basedir = basedir.replaceAll("\\$", "\\\\\\$");
149             
150             path = pathPattern.replaceAll( "\\$\\{" + basedirSysProp + "\\}", basedir );
151             path = path.replaceAll( "\\\\", "/" );
152             path = path.replaceAll( "//", "/" );
153
154             return new File JavaDoc( path ).getAbsoluteFile();
155         }
156         else
157         {
158             return new File JavaDoc( path ).getAbsoluteFile();
159         }
160     }
161
162     public PluginRegistry createUserPluginRegistry()
163     {
164         PluginRegistry registry = new PluginRegistry();
165         
166         RuntimeInfo rtInfo = new RuntimeInfo( registry );
167         
168         registry.setRuntimeInfo( rtInfo );
169         
170         rtInfo.setFile( userRegistryFile );
171         
172         return registry;
173     }
174     
175 }
176
Popular Tags