1 package org.apache.maven.repository.proxy.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.maven.artifact.repository.layout.DefaultRepositoryLayout;
20 import org.apache.maven.repository.proxy.repository.ProxyRepository;
21 import org.codehaus.plexus.util.StringUtils;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Properties;
31 import java.util.StringTokenizer;
36 public class MavenProxyPropertyLoader
38 private static final String REPO_LOCAL_STORE = "repo.local.store";
40 private static final String PROXY_LIST = "proxy.list";
42 private static final String REPO_LIST = "repo.list";
44 public ProxyConfiguration load( Properties props )
45 throws ValidationException
47 ProxyConfiguration config = new ProxyConfiguration();
49 config.setLayout( "default" );
51 config.setRepositoryCachePath( getMandatoryProperty( props, REPO_LOCAL_STORE ) );
53 //just get the first proxy and break
54 String propertyList = props.getProperty( PROXY_LIST );
55 if ( propertyList != null )
57 StringTokenizer tok = new StringTokenizer( propertyList, "," );
58 while ( tok.hasMoreTokens() )
60 String key = tok.nextToken();
61 if ( StringUtils.isNotEmpty( key ) )
63 String host = getMandatoryProperty( props, "proxy." + key + ".host" );
64 int port = Integer.parseInt( getMandatoryProperty( props, "proxy." + key + ".port" ) );
66 // the username and password isn't required
67 String username = props.getProperty( "proxy." + key + ".username" );
68 String password = props.getProperty( "proxy." + key + ".password" );
70 if ( StringUtils.isNotEmpty( username ) )
72 config.setHttpProxy( host, port, username, password );
76 config.setHttpProxy( host, port );
79 //accept only one proxy configuration
85 List repositories = new ArrayList();
87 //get the remote repository list
88 String repoList = getMandatoryProperty( props, REPO_LIST );
90 StringTokenizer tok = new StringTokenizer( repoList, "," );
91 while ( tok.hasMoreTokens() )
93 String key = tok.nextToken();
95 Properties repoProps = getSubset( props, "repo." + key + "." );
96 String url = getMandatoryProperty( props, "repo." + key + ".url" );
97 String proxyKey = repoProps.getProperty( "proxy" );
99 boolean cacheFailures =
100 Boolean.valueOf( repoProps.getProperty( "cache.failures", "false" ) ).booleanValue();
101 boolean hardFail = Boolean.valueOf( repoProps.getProperty( "hardfail", "true" ) ).booleanValue();
102 long cachePeriod = Long.parseLong( repoProps.getProperty( "cache.period", "0" ) );
104 ProxyRepository repository =
105 new ProxyRepository( key, url, new DefaultRepositoryLayout(), cacheFailures, cachePeriod );
107 repository.setHardfail( hardFail );
109 if ( StringUtils.isNotEmpty( proxyKey ) )
111 repository.setProxied( true );
114 repositories.add( repository );
117 config.setRepositories( repositories );
119 validateDirectories( config );
120 validateRemoteRepo( config );
126 * @todo should be shared with any other configuration loader - move method to configuration?
128 private static void validateRemoteRepo( ProxyConfiguration configuration )
129 throws ValidationException
131 //Verify remote repository set
132 //only warn if missing
133 if ( configuration.getRepositories().size() < 1 )
135 throw new ValidationException( "At least one remote repository must be configured." );
139 private Properties getSubset( Properties props, String prefix )
141 Enumeration keys = props.keys();
142 Properties result = new Properties();
143 while ( keys.hasMoreElements() )
145 String key = (String) keys.nextElement();
146 String value = props.getProperty( key );
147 if ( key.startsWith( prefix ) )
149 String newKey = key.substring( prefix.length() );
150 result.setProperty( newKey, value );
156 public ProxyConfiguration load( InputStream is )
157 throws IOException, ValidationException
159 Properties props = new Properties();
161 return load( props );
164 private String getMandatoryProperty( Properties props, String key )
165 throws ValidationException
167 String value = props.getProperty( key );
171 throw new ValidationException( "Missing property: " + key );
178 * @todo should be shared with any other configuration loader - move method to configuration?
180 private static void validateDirectories( ProxyConfiguration configuration )
181 throws ValidationException
183 File f = new File( configuration.getRepositoryCachePath() );
186 throw new ValidationException( "Specified directory does not exist: " + f.getAbsolutePath() );
189 for ( Iterator repos = configuration.getRepositories().iterator(); repos.hasNext(); )
191 ProxyRepository repo = (ProxyRepository) repos.next();
192 if ( repo.getUrl().startsWith( "file://" ) )
194 File f2 = new File( repo.getBasedir() );
197 throw new ValidationException( "Specified directory does not exist: " + f2.getAbsolutePath() );