1 package org.apache.maven.archiva.configuration;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.lang.StringUtils;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Enumeration;
27 import java.util.Properties;
28 import java.util.StringTokenizer;
32 * @author Brett Porter
34 public class MavenProxyPropertyLoader
36 private static final String REPO_LOCAL_STORE = "repo.local.store";
38 private static final String PROXY_LIST = "proxy.list";
40 private static final String REPO_LIST = "repo.list";
42 public void load( Properties props, Configuration configuration )
43 throws InvalidConfigurationException
45 // set up the managed repository
46 String localCachePath = getMandatoryProperty( props, REPO_LOCAL_STORE );
48 RepositoryConfiguration config = new RepositoryConfiguration();
49 config.setDirectory( localCachePath );
50 config.setName( "Imported Maven-Proxy Cache" );
51 config.setId( "maven-proxy" );
52 configuration.addRepository( config );
54 //just get the first HTTP proxy and break
55 String propertyList = props.getProperty( PROXY_LIST );
56 if ( propertyList != null )
58 StringTokenizer tok = new StringTokenizer( propertyList, "," );
59 while ( tok.hasMoreTokens() )
61 String key = tok.nextToken();
62 if ( StringUtils.isNotEmpty( key ) )
64 Proxy proxy = new Proxy();
65 proxy.setHost( getMandatoryProperty( props, "proxy." + key + ".host" ) );
66 proxy.setPort( Integer.parseInt( getMandatoryProperty( props, "proxy." + key + ".port" ) ) );
68 // the username and password isn't required
69 proxy.setUsername( props.getProperty( "proxy." + key + ".username" ) );
70 proxy.setPassword( props.getProperty( "proxy." + key + ".password" ) );
72 configuration.setProxy( proxy );
74 //accept only one proxy configuration
80 //get the remote repository list
81 String repoList = getMandatoryProperty( props, REPO_LIST );
83 StringTokenizer tok = new StringTokenizer( repoList, "," );
84 while ( tok.hasMoreTokens() )
86 String key = tok.nextToken();
88 Properties repoProps = getSubset( props, "repo." + key + "." );
89 String url = getMandatoryProperty( props, "repo." + key + ".url" );
90 String proxyKey = repoProps.getProperty( "proxy" );
92 boolean cacheFailures =
93 Boolean.valueOf( repoProps.getProperty( "cache.failures", "false" ) ).booleanValue();
94 boolean hardFail = Boolean.valueOf( repoProps.getProperty( "hardfail", "true" ) ).booleanValue();
95 int cachePeriod = Integer.parseInt( repoProps.getProperty( "cache.period", "60" ) );
97 ProxiedRepositoryConfiguration repository = new ProxiedRepositoryConfiguration();
98 repository.setId( key );
99 repository.setLayout( "legacy" );
100 repository.setManagedRepository( config.getId() );
101 repository.setName( "Imported Maven-Proxy Remote Proxy" );
102 repository.setSnapshotsInterval( cachePeriod );
103 repository.setUrl( url );
104 repository.setUseNetworkProxy( StringUtils.isNotEmpty( proxyKey ) );
105 repository.setCacheFailures( cacheFailures );
106 repository.setHardFail( hardFail );
108 configuration.addProxiedRepository( repository );
112 private Properties getSubset( Properties props, String prefix )
114 Enumeration keys = props.keys();
115 Properties result = new Properties();
116 while ( keys.hasMoreElements() )
118 String key = (String) keys.nextElement();
119 String value = props.getProperty( key );
120 if ( key.startsWith( prefix ) )
122 String newKey = key.substring( prefix.length() );
123 result.setProperty( newKey, value );
129 public void load( InputStream is, Configuration configuration )
130 throws IOException, InvalidConfigurationException
132 Properties props = new Properties();
134 load( props, configuration );
137 private String getMandatoryProperty( Properties props, String key )
138 throws InvalidConfigurationException
140 String value = props.getProperty( key );
144 throw new InvalidConfigurationException( key, "Missing required field: " + key );