Coverage Report - org.apache.maven.plugin.registry.DefaultPluginRegistryBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultPluginRegistryBuilder
0 %
0/42
0 %
0/16
2
 
 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  0
 public class DefaultPluginRegistryBuilder
 35  
     extends AbstractLogEnabled
 36  
     implements MavenPluginRegistryBuilder, Initializable
 37  
 {
 38  
 
 39  0
     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  0
         userRegistryFile = getFile( userRegistryPath, "user.home", MavenPluginRegistryBuilder.ALT_USER_PLUGIN_REG_LOCATION );
 62  
 
 63  0
         getLogger().debug( "Building Maven user-level plugin registry from: '" + userRegistryFile.getAbsolutePath() + "'" );
 64  
 
 65  0
         if ( System.getProperty( "maven.home" ) != null ||
 66  
              System.getProperty( MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION ) != null )
 67  
         {
 68  0
             globalRegistryFile = getFile( globalRegistryPath, "maven.home", MavenPluginRegistryBuilder.ALT_GLOBAL_PLUGIN_REG_LOCATION );
 69  
 
 70  0
             getLogger().debug( "Building Maven global-level plugin registry from: '" + globalRegistryFile.getAbsolutePath() + "'" );
 71  
         }
 72  0
     }
 73  
 
 74  
     public PluginRegistry buildPluginRegistry()
 75  
         throws IOException, XmlPullParserException
 76  
     {
 77  0
         PluginRegistry global = readPluginRegistry( globalRegistryFile );
 78  
 
 79  0
         PluginRegistry user = readPluginRegistry( userRegistryFile );
 80  
 
 81  0
         if ( user == null && global != null )
 82  
         {
 83  
             // we'll use the globals, but first we have to recursively mark them as global...
 84  0
             PluginRegistryUtils.recursivelySetSourceLevel( global, PluginRegistry.GLOBAL_LEVEL );
 85  
 
 86  0
             user = global;
 87  
         }
 88  
         else
 89  
         {
 90  
             // merge non-colliding plugins into the user registry.
 91  0
             PluginRegistryUtils.merge( user, global, TrackableBase.GLOBAL_LEVEL );
 92  
         }
 93  
 
 94  0
         return user;
 95  
     }
 96  
 
 97  
     private PluginRegistry readPluginRegistry( File registryFile )
 98  
         throws IOException, XmlPullParserException
 99  
     {
 100  0
         PluginRegistry registry = null;
 101  
 
 102  0
         if ( registryFile != null && registryFile.exists() && registryFile.isFile() )
 103  
         {
 104  0
             Reader reader = null;
 105  
             try
 106  
             {
 107  0
                 reader = ReaderFactory.newXmlReader( registryFile );
 108  
 
 109  0
                 PluginRegistryXpp3Reader modelReader = new PluginRegistryXpp3Reader();
 110  
 
 111  0
                 registry = modelReader.read( reader );
 112  
 
 113  0
                 RuntimeInfo rtInfo = new RuntimeInfo( registry );
 114  
 
 115  0
                 registry.setRuntimeInfo( rtInfo );
 116  
 
 117  0
                 rtInfo.setFile( registryFile );
 118  
             }
 119  
             finally
 120  
             {
 121  0
                 IOUtil.close( reader );
 122  0
             }
 123  
         }
 124  
 
 125  0
         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  0
         String path = System.getProperty( altLocationSysProp );
 144  
 
 145  0
         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  0
             String basedir = System.getProperty( basedirSysProp );
 150  
 
 151  0
             basedir = basedir.replaceAll( "\\\\", "/" );
 152  0
             basedir = basedir.replaceAll("\\$", "\\\\\\$");
 153  
 
 154  0
             path = pathPattern.replaceAll( "\\$\\{" + basedirSysProp + "\\}", basedir );
 155  0
             path = path.replaceAll( "\\\\", "/" );
 156  0
             path = path.replaceAll( "//", "/" );
 157  
 
 158  0
             return new File( path ).getAbsoluteFile();
 159  
         }
 160  
         else
 161  
         {
 162  0
             return new File( path ).getAbsoluteFile();
 163  
         }
 164  
     }
 165  
 
 166  
     public PluginRegistry createUserPluginRegistry()
 167  
     {
 168  0
         PluginRegistry registry = new PluginRegistry();
 169  
 
 170  0
         RuntimeInfo rtInfo = new RuntimeInfo( registry );
 171  
 
 172  0
         registry.setRuntimeInfo( rtInfo );
 173  
 
 174  0
         rtInfo.setFile( userRegistryFile );
 175  
 
 176  0
         return registry;
 177  
     }
 178  
 
 179  
 }