1 package org.apache.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;
23 import org.apache.archiva.policies.ReleasesPolicy;
24 import org.apache.archiva.policies.SnapshotsPolicy;
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;
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 ManagedRepositoryConfiguration config = new ManagedRepositoryConfiguration();
49 config.setLocation( localCachePath );
50 config.setName( "Imported Maven-Proxy Cache" );
51 config.setId( "maven-proxy" );
52 config.setScanned( false );
53 config.setReleases( true );
54 config.setSnapshots( false );
55 configuration.addManagedRepository( config );
57 // Add the network proxies.
58 String propertyList = props.getProperty( PROXY_LIST );
59 if ( propertyList != null )
61 StringTokenizer tok = new StringTokenizer( propertyList, "," );
62 while ( tok.hasMoreTokens() )
64 String key = tok.nextToken();
65 if ( StringUtils.isNotEmpty( key ) )
67 NetworkProxyConfiguration proxy = new NetworkProxyConfiguration();
68 proxy.setHost( getMandatoryProperty( props, "proxy." + key + ".host" ) );
69 proxy.setPort( Integer.parseInt( getMandatoryProperty( props, "proxy." + key + ".port" ) ) );
71 // the username and password isn't required
72 proxy.setUsername( props.getProperty( "proxy." + key + ".username" ) );
73 proxy.setPassword( props.getProperty( "proxy." + key + ".password" ) );
75 configuration.addNetworkProxy( proxy );
80 // Add 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 // int cachePeriod = Integer.parseInt( repoProps.getProperty( "cache.period", "60" ) );
94 RemoteRepositoryConfiguration repository = new RemoteRepositoryConfiguration();
95 repository.setId( key );
96 repository.setName( "Imported Maven-Proxy Remote Proxy" );
97 repository.setUrl( url );
98 repository.setLayout( "legacy" );
100 configuration.addRemoteRepository( repository );
102 ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
103 proxyConnector.setSourceRepoId( "maven-proxy" );
104 proxyConnector.setTargetRepoId( key );
105 proxyConnector.setProxyId( proxyKey );
106 // TODO: convert cachePeriod to closest "daily" or "hourly"
107 proxyConnector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.DAILY );
108 proxyConnector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ALWAYS );
110 configuration.addProxyConnector( proxyConnector );
114 @SuppressWarnings("unchecked")
115 private Properties getSubset( Properties props, String prefix )
117 Enumeration keys = props.keys();
118 Properties result = new Properties();
119 while ( keys.hasMoreElements() )
121 String key = (String) keys.nextElement();
122 String value = props.getProperty( key );
123 if ( key.startsWith( prefix ) )
125 String newKey = key.substring( prefix.length() );
126 result.setProperty( newKey, value );
132 public void load( InputStream is, Configuration configuration )
133 throws IOException, InvalidConfigurationException
135 Properties props = new Properties();
137 load( props, configuration );
140 private String getMandatoryProperty( Properties props, String key )
141 throws InvalidConfigurationException
143 String value = props.getProperty( key );
147 throw new InvalidConfigurationException( key, "Missing required field: " + key );