1 package org.apache.maven.archiva.configuration;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.commons.lang.StringUtils;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Enumeration;
24 import java.util.Properties;
25 import java.util.StringTokenizer;
29 * @author Brett Porter
31 public class MavenProxyPropertyLoader
33 private static final String REPO_LOCAL_STORE = "repo.local.store";
35 private static final String PROXY_LIST = "proxy.list";
37 private static final String REPO_LIST = "repo.list";
39 public void load( Properties props, Configuration configuration )
40 throws InvalidConfigurationException
42 // set up the managed repository
43 String localCachePath = getMandatoryProperty( props, REPO_LOCAL_STORE );
45 RepositoryConfiguration config = new RepositoryConfiguration();
46 config.setDirectory( localCachePath );
47 config.setName( "Imported Maven-Proxy Cache" );
48 config.setId( "maven-proxy" );
49 configuration.addRepository( config );
51 //just get the first HTTP proxy and break
52 String propertyList = props.getProperty( PROXY_LIST );
53 if ( propertyList != null )
55 StringTokenizer tok = new StringTokenizer( propertyList, "," );
56 while ( tok.hasMoreTokens() )
58 String key = tok.nextToken();
59 if ( StringUtils.isNotEmpty( key ) )
61 Proxy proxy = new Proxy();
62 proxy.setHost( getMandatoryProperty( props, "proxy." + key + ".host" ) );
63 proxy.setPort( Integer.parseInt( getMandatoryProperty( props, "proxy." + key + ".port" ) ) );
65 // the username and password isn't required
66 proxy.setUsername( props.getProperty( "proxy." + key + ".username" ) );
67 proxy.setPassword( props.getProperty( "proxy." + key + ".password" ) );
69 configuration.setProxy( proxy );
71 //accept only one proxy configuration
77 //get the remote repository list
78 String repoList = getMandatoryProperty( props, REPO_LIST );
80 StringTokenizer tok = new StringTokenizer( repoList, "," );
81 while ( tok.hasMoreTokens() )
83 String key = tok.nextToken();
85 Properties repoProps = getSubset( props, "repo." + key + "." );
86 String url = getMandatoryProperty( props, "repo." + key + ".url" );
87 String proxyKey = repoProps.getProperty( "proxy" );
89 boolean cacheFailures =
90 Boolean.valueOf( repoProps.getProperty( "cache.failures", "false" ) ).booleanValue();
91 boolean hardFail = Boolean.valueOf( repoProps.getProperty( "hardfail", "true" ) ).booleanValue();
92 int cachePeriod = Integer.parseInt( repoProps.getProperty( "cache.period", "60" ) );
94 ProxiedRepositoryConfiguration repository = new ProxiedRepositoryConfiguration();
95 repository.setId( key );
96 repository.setLayout( "legacy" );
97 repository.setManagedRepository( config.getId() );
98 repository.setName( "Imported Maven-Proxy Remote Proxy" );
99 repository.setSnapshotsInterval( cachePeriod );
100 repository.setUrl( url );
101 repository.setUseNetworkProxy( StringUtils.isNotEmpty( proxyKey ) );
102 repository.setCacheFailures( cacheFailures );
103 repository.setHardFail( hardFail );
105 configuration.addProxiedRepository( repository );
109 private Properties getSubset( Properties props, String prefix )
111 Enumeration keys = props.keys();
112 Properties result = new Properties();
113 while ( keys.hasMoreElements() )
115 String key = (String) keys.nextElement();
116 String value = props.getProperty( key );
117 if ( key.startsWith( prefix ) )
119 String newKey = key.substring( prefix.length() );
120 result.setProperty( newKey, value );
126 public void load( InputStream is, Configuration configuration )
127 throws IOException, InvalidConfigurationException
129 Properties props = new Properties();
131 load( props, configuration );
134 private String getMandatoryProperty( Properties props, String key )
135 throws InvalidConfigurationException
137 String value = props.getProperty( key );
141 throw new InvalidConfigurationException( key, "Missing required field: " + key );