View Javadoc

1   package org.apache.maven.plugin.registry;
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.plugin.registry.io.xpp3.PluginRegistryXpp3Reader;
23  import org.codehaus.plexus.logging.AbstractLogEnabled;
24  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
25  import org.codehaus.plexus.util.IOUtil;
26  import org.codehaus.plexus.util.ReaderFactory;
27  import org.codehaus.plexus.util.StringUtils;
28  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.io.Reader;
33  
34  public class DefaultPluginRegistryBuilder
35      extends AbstractLogEnabled
36      implements MavenPluginRegistryBuilder, Initializable
37  {
38  
39      public static final String userHome = System.getProperty( "user.home" );
40  
41      /**
42       * @configuration
43       */
44      private String userRegistryPath;
45  
46      /**
47       * @configuration
48       */
49      private String globalRegistryPath;
50  
51      private File userRegistryFile;
52  
53      private File globalRegistryFile;
54  
55      // ----------------------------------------------------------------------
56      // Component Lifecycle
57      // ----------------------------------------------------------------------
58  
59      public void initialize()
60      {
61          userRegistryFile = getFile( userRegistryPath, "user.home", MavenPluginRegistryBuilder.ALT_USER_PLUGIN_REG_LOCATION );
62  
63          getLogger().debug( "Building Maven user-level plugin registry from: '" + userRegistryFile.getAbsolutePath() + "'" );
64  
65          if ( System.getProperty( "maven.home" ) != null ||
66               System.getProperty( MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION ) != null )
67          {
68              globalRegistryFile = getFile( globalRegistryPath, "maven.home", MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION );
69  
70              getLogger().debug( "Building Maven global-level plugin registry from: '" + globalRegistryFile.getAbsolutePath() + "'" );
71          }
72      }
73  
74      public PluginRegistry buildPluginRegistry()
75          throws IOException, XmlPullParserException
76      {
77          PluginRegistry global = readPluginRegistry( globalRegistryFile );
78  
79          PluginRegistry user = readPluginRegistry( userRegistryFile );
80  
81          if ( user == null && global != null )
82          {
83              // we'll use the globals, but first we have to recursively mark them as global...
84              PluginRegistryUtils.recursivelySetSourceLevel( global, PluginRegistry.GLOBAL_LEVEL );
85  
86              user = global;
87          }
88          else
89          {
90              // merge non-colliding plugins into the user registry.
91              PluginRegistryUtils.merge( user, global, TrackableBase.GLOBAL_LEVEL );
92          }
93  
94          return user;
95      }
96  
97      private PluginRegistry readPluginRegistry( File registryFile )
98          throws IOException, XmlPullParserException
99      {
100         PluginRegistry registry = null;
101 
102         if ( registryFile != null && registryFile.exists() && registryFile.isFile() )
103         {
104             Reader reader = null;
105             try
106             {
107                 reader = ReaderFactory.newXmlReader( registryFile );
108 
109                 PluginRegistryXpp3Reader modelReader = new PluginRegistryXpp3Reader();
110 
111                 registry = modelReader.read( reader );
112 
113                 RuntimeInfo rtInfo = new RuntimeInfo( registry );
114 
115                 registry.setRuntimeInfo( rtInfo );
116 
117                 rtInfo.setFile( registryFile );
118             }
119             finally
120             {
121                 IOUtil.close( reader );
122             }
123         }
124 
125         return registry;
126     }
127 
128     private File getFile( String pathPattern, String basedirSysProp, String altLocationSysProp )
129     {
130         // -------------------------------------------------------------------------------------
131         // Alright, here's the justification for all the regexp wizardry below...
132         //
133         // Continuum and other server-like apps may need to locate the user-level and 
134         // global-level settings somewhere other than ${user.home} and ${maven.home},
135         // respectively. Using a simple replacement of these patterns will allow them
136         // to specify the absolute path to these files in a customized components.xml
137         // file. Ideally, we'd do full pattern-evaluation against the sysprops, but this
138         // is a first step. There are several replacements below, in order to normalize
139         // the path character before we operate on the string as a regex input, and 
140         // in order to avoid surprises with the File construction...
141         // -------------------------------------------------------------------------------------
142 
143         String path = System.getProperty( altLocationSysProp );
144 
145         if ( StringUtils.isEmpty( path ) )
146         {
147             // TODO: This replacing shouldn't be necessary as user.home should be in the
148             // context of the container and thus the value would be interpolated by Plexus
149             String basedir = System.getProperty( basedirSysProp );
150 
151             basedir = basedir.replaceAll( "\\\\", "/" );
152             basedir = basedir.replaceAll("\\$", "\\\\\\$");
153 
154             path = pathPattern.replaceAll( "\\$\\{" + basedirSysProp + "\\}", basedir );
155             path = path.replaceAll( "\\\\", "/" );
156             path = path.replaceAll( "//", "/" );
157 
158             return new File( path ).getAbsoluteFile();
159         }
160         else
161         {
162             return new File( path ).getAbsoluteFile();
163         }
164     }
165 
166     public PluginRegistry createUserPluginRegistry()
167     {
168         PluginRegistry registry = new PluginRegistry();
169 
170         RuntimeInfo rtInfo = new RuntimeInfo( registry );
171 
172         registry.setRuntimeInfo( rtInfo );
173 
174         rtInfo.setFile( userRegistryFile );
175 
176         return registry;
177     }
178 
179 }