Coverage Report - org.apache.maven.archiva.configuration.MavenProxyPropertyLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
MavenProxyPropertyLoader
0%
0/63
0%
0/14
0
 
 1  
 package org.apache.maven.archiva.configuration;
 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.commons.lang.StringUtils;
 23  
 import org.apache.maven.archiva.policies.ReleasesPolicy;
 24  
 import org.apache.maven.archiva.policies.SnapshotsPolicy;
 25  
 
 26  
 import java.io.IOException;
 27  
 import java.io.InputStream;
 28  
 import java.util.Enumeration;
 29  
 import java.util.Properties;
 30  
 import java.util.StringTokenizer;
 31  
 
 32  
 /**
 33  
  */
 34  0
 public class MavenProxyPropertyLoader
 35  
 {
 36  
     private static final String REPO_LOCAL_STORE = "repo.local.store";
 37  
 
 38  
     private static final String PROXY_LIST = "proxy.list";
 39  
 
 40  
     private static final String REPO_LIST = "repo.list";
 41  
 
 42  
     public void load( Properties props, Configuration configuration )
 43  
         throws InvalidConfigurationException
 44  
     {
 45  
         // set up the managed repository
 46  0
         String localCachePath = getMandatoryProperty( props, REPO_LOCAL_STORE );
 47  
 
 48  0
         ManagedRepositoryConfiguration config = new ManagedRepositoryConfiguration();
 49  0
         config.setLocation( localCachePath );
 50  0
         config.setName( "Imported Maven-Proxy Cache" );
 51  0
         config.setId( "maven-proxy" );
 52  0
         config.setScanned( false );
 53  0
         config.setReleases( true );
 54  0
         config.setSnapshots( false );
 55  0
         configuration.addManagedRepository( config );
 56  
 
 57  
         // Add the network proxies.
 58  0
         String propertyList = props.getProperty( PROXY_LIST );
 59  0
         if ( propertyList != null )
 60  
         {
 61  0
             StringTokenizer tok = new StringTokenizer( propertyList, "," );
 62  0
             while ( tok.hasMoreTokens() )
 63  
             {
 64  0
                 String key = tok.nextToken();
 65  0
                 if ( StringUtils.isNotEmpty( key ) )
 66  
                 {
 67  0
                     NetworkProxyConfiguration proxy = new NetworkProxyConfiguration();
 68  0
                     proxy.setHost( getMandatoryProperty( props, "proxy." + key + ".host" ) );
 69  0
                     proxy.setPort( Integer.parseInt( getMandatoryProperty( props, "proxy." + key + ".port" ) ) );
 70  
 
 71  
                     // the username and password isn't required
 72  0
                     proxy.setUsername( props.getProperty( "proxy." + key + ".username" ) );
 73  0
                     proxy.setPassword( props.getProperty( "proxy." + key + ".password" ) );
 74  
 
 75  0
                     configuration.addNetworkProxy( proxy );
 76  
                 }
 77  0
             }
 78  
         }
 79  
 
 80  
         // Add the remote repository list
 81  0
         String repoList = getMandatoryProperty( props, REPO_LIST );
 82  
 
 83  0
         StringTokenizer tok = new StringTokenizer( repoList, "," );
 84  0
         while ( tok.hasMoreTokens() )
 85  
         {
 86  0
             String key = tok.nextToken();
 87  
 
 88  0
             Properties repoProps = getSubset( props, "repo." + key + "." );
 89  0
             String url = getMandatoryProperty( props, "repo." + key + ".url" );
 90  0
             String proxyKey = repoProps.getProperty( "proxy" );
 91  
 
 92  
 //            int cachePeriod = Integer.parseInt( repoProps.getProperty( "cache.period", "60" ) );
 93  
 
 94  0
             RemoteRepositoryConfiguration repository = new RemoteRepositoryConfiguration();
 95  0
             repository.setId( key );
 96  0
             repository.setName( "Imported Maven-Proxy Remote Proxy" );
 97  0
             repository.setUrl( url );
 98  0
             repository.setLayout( "legacy" );
 99  
 
 100  0
             configuration.addRemoteRepository( repository );
 101  
 
 102  0
             ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
 103  0
             proxyConnector.setSourceRepoId( "maven-proxy" );
 104  0
             proxyConnector.setTargetRepoId( key );
 105  0
             proxyConnector.setProxyId( proxyKey );
 106  
             // TODO: convert cachePeriod to closest "daily" or "hourly"
 107  0
             proxyConnector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.DAILY );
 108  0
             proxyConnector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ALWAYS );
 109  
 
 110  0
             configuration.addProxyConnector( proxyConnector );
 111  0
         }
 112  0
     }
 113  
 
 114  
     @SuppressWarnings("unchecked")
 115  
     private Properties getSubset( Properties props, String prefix )
 116  
     {
 117  0
         Enumeration keys = props.keys();
 118  0
         Properties result = new Properties();
 119  0
         while ( keys.hasMoreElements() )
 120  
         {
 121  0
             String key = (String) keys.nextElement();
 122  0
             String value = props.getProperty( key );
 123  0
             if ( key.startsWith( prefix ) )
 124  
             {
 125  0
                 String newKey = key.substring( prefix.length() );
 126  0
                 result.setProperty( newKey, value );
 127  
             }
 128  0
         }
 129  0
         return result;
 130  
     }
 131  
 
 132  
     public void load( InputStream is, Configuration configuration )
 133  
         throws IOException, InvalidConfigurationException
 134  
     {
 135  0
         Properties props = new Properties();
 136  0
         props.load( is );
 137  0
         load( props, configuration );
 138  0
     }
 139  
 
 140  
     private String getMandatoryProperty( Properties props, String key )
 141  
         throws InvalidConfigurationException
 142  
     {
 143  0
         String value = props.getProperty( key );
 144  
 
 145  0
         if ( value == null )
 146  
         {
 147  0
             throw new InvalidConfigurationException( key, "Missing required field: " + key );
 148  
         }
 149  
 
 150  0
         return value;
 151  
     }
 152  
 }