Coverage Report - org.apache.maven.plugin.coreit.LoadResourceMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadResourceMojo
0 %
0/40
0 %
0/12
14
 
 1  
 package org.apache.maven.plugin.coreit;
 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.artifact.manager.WagonManager;
 23  
 import org.apache.maven.plugin.AbstractMojo;
 24  
 import org.apache.maven.plugin.MojoExecutionException;
 25  
 import org.apache.maven.plugin.MojoFailureException;
 26  
 import org.apache.maven.wagon.repository.Repository;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.FileOutputStream;
 30  
 import java.io.IOException;
 31  
 import java.io.OutputStream;
 32  
 import java.net.URL;
 33  
 import java.util.Collections;
 34  
 import java.util.List;
 35  
 import java.util.Properties;
 36  
 
 37  
 /**
 38  
  * Loads resources from a class loader used to load a wagon provider. The wagon is merely used to access the extension
 39  
  * class loader it came from which is otherwise not accessible to a plugin.
 40  
  * 
 41  
  * @goal load-resource
 42  
  * @phase validate
 43  
  * 
 44  
  * @author Benjamin Bentmann
 45  
  * @version $Id: LoadResourceMojo.java 798744 2009-07-29 01:40:29Z brett $
 46  
  */
 47  0
 public class LoadResourceMojo
 48  
     extends AbstractMojo
 49  
 {
 50  
 
 51  
     /**
 52  
      * The Wagon manager used to retrieve wagon providers.
 53  
      * 
 54  
      * @component
 55  
      */
 56  
     private WagonManager wagonManager;
 57  
 
 58  
     /**
 59  
      * The path to the properties file used to track the results of the resource loading via the wagon's class loader.
 60  
      * 
 61  
      * @parameter expression="${wagon.wagonClassLoaderOutput}"
 62  
      */
 63  
     private File wagonClassLoaderOutput;
 64  
 
 65  
     /**
 66  
      * The role hint for the wagon provider to load. The class loader of this provider will be used to load the
 67  
      * resources.
 68  
      * 
 69  
      * @parameter expression="${wagon.wagonProtocol}"
 70  
      */
 71  
     private String wagonProtocol;
 72  
 
 73  
     /**
 74  
      * The repository to load the wagon for, if applicable.
 75  
      * 
 76  
      * @parameter expression="${wagon.repositoryId}"
 77  
      */
 78  
     private String repositoryId;
 79  
 
 80  
     /**
 81  
      * The set of resources to load. For each specified absolute resource path <code>ARP</code> that was successfully
 82  
      * loaded, the generated properties files will contain a key named <code>ARP</code> whose value gives the URL to the
 83  
      * resource. In addition, the keys <code>ARP.count</code>, <code>ARP.0</code>, <code>ARP.1</code> etc. will
 84  
      * enumerate all URLs matching the resource name.
 85  
      * 
 86  
      * @parameter
 87  
      */
 88  
     private String[] resourcePaths;
 89  
 
 90  
     /**
 91  
      * Runs this mojo.
 92  
      * 
 93  
      * @throws MojoFailureException If the attached file has not been set.
 94  
      */
 95  
     public void execute()
 96  
         throws MojoExecutionException, MojoFailureException
 97  
     {
 98  0
         getLog().info( "[MAVEN-CORE-IT-LOG] Looking up wagon for protocol " + wagonProtocol );
 99  
 
 100  
         Object wagon;
 101  
         try
 102  
         {
 103  0
             if ( repositoryId != null )
 104  
             {
 105  0
                 wagon = wagonManager.getWagon( new Repository( repositoryId, wagonProtocol + "://host/path" ) );
 106  
             }
 107  
             else
 108  
             {
 109  0
                 wagon = wagonManager.getWagon( wagonProtocol );
 110  
             }
 111  
         }
 112  0
         catch ( Exception e )
 113  
         {
 114  0
             throw new MojoExecutionException( "Failed to load wagon for protocol " + wagonProtocol, e );
 115  0
         }
 116  
 
 117  0
         ClassLoader classLoader = wagon.getClass().getClassLoader();
 118  
 
 119  0
         getLog().info( "[MAVEN-CORE-IT-LOG] Using class loader " + classLoader );
 120  
 
 121  0
         Properties loaderProperties = new Properties();
 122  0
         loaderProperties.setProperty( "wagon.class", wagon.getClass().getName() );
 123  
 
 124  0
         if ( resourcePaths != null )
 125  
         {
 126  0
             for ( int i = 0; i < resourcePaths.length; i++ )
 127  
             {
 128  0
                 String path = resourcePaths[i];
 129  0
                 getLog().info( "[MAVEN-CORE-IT-LOG] Loading resource " + path );
 130  
     
 131  0
                 URL url = classLoader.getResource( path );
 132  0
                 getLog().info( "[MAVEN-CORE-IT-LOG]   Loaded resource from " + url );
 133  0
                 if ( url != null )
 134  
                 {
 135  0
                     loaderProperties.setProperty( path, url.toString() );
 136  
                 }
 137  
     
 138  
                 try
 139  
                 {
 140  0
                     List urls = Collections.list( classLoader.getResources( path ) );
 141  0
                     loaderProperties.setProperty( path + ".count", "" + urls.size() );
 142  0
                     for ( int j = 0; j < urls.size(); j++ )
 143  
                     {
 144  0
                         loaderProperties.setProperty( path + "." + j, urls.get( j ).toString() );
 145  
                     }
 146  
                 }
 147  0
                 catch ( IOException e )
 148  
                 {
 149  0
                     throw new MojoExecutionException( "Resources could not be enumerated: " + path, e );
 150  0
                 }
 151  
             }
 152  
         }
 153  
 
 154  0
         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + wagonClassLoaderOutput );
 155  
 
 156  0
         OutputStream out = null;
 157  
         try
 158  
         {
 159  0
             wagonClassLoaderOutput.getParentFile().mkdirs();
 160  0
             out = new FileOutputStream( wagonClassLoaderOutput );
 161  0
             loaderProperties.store( out, "MAVEN-CORE-IT-LOG" );
 162  
         }
 163  0
         catch ( IOException e )
 164  
         {
 165  0
             throw new MojoExecutionException( "Output file could not be created: " + wagonClassLoaderOutput, e );
 166  
         }
 167  
         finally
 168  
         {
 169  0
             if ( out != null )
 170  
             {
 171  
                 try
 172  
                 {
 173  0
                     out.close();
 174  
                 }
 175  0
                 catch ( IOException e )
 176  
                 {
 177  
                     // just ignore
 178  0
                 }
 179  
             }
 180  
         }
 181  
 
 182  0
         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + wagonClassLoaderOutput );
 183  0
     }
 184  
 
 185  
 }