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 );
125 private void validateRemoteRepo( ProxyConfiguration configuration )
126 throws ValidationException
128 //Verify remote repository set
129 //only warn if missing
130 if ( configuration.getRepositories().size() < 1 )
132 throw new ValidationException( "At least one remote repository must be configured." );
136 private Properties getSubset( Properties props, String prefix )
138 Enumeration keys = props.keys();
139 Properties result = new Properties();
140 while ( keys.hasMoreElements() )
142 String key = (String) keys.nextElement();
143 String value = props.getProperty( key );
144 if ( key.startsWith( prefix ) )
146 String newKey = key.substring( prefix.length() );
147 result.setProperty( newKey, value );
153 public ProxyConfiguration load( InputStream is )
154 throws IOException, ValidationException
156 Properties props = new Properties();
158 return load( props );
161 private String getMandatoryProperty( Properties props, String key )
162 throws ValidationException
164 String value = props.getProperty( key );
168 throw new ValidationException( "Missing property: " + key );
174 private void validateDirectories( ProxyConfiguration configuration )
175 throws ValidationException
177 File f = new File( configuration.getRepositoryCachePath() );
180 throw new ValidationException( "Specified directory does not exist: " + f.getAbsolutePath() );
183 for ( Iterator repos = configuration.getRepositories().iterator(); repos.hasNext(); )
185 ProxyRepository repo = (ProxyRepository) repos.next();
186 if ( repo.getUrl().startsWith( "file://" ) )
188 File f2 = new File( repo.getBasedir() );
191 throw new ValidationException( "Specified directory does not exist: " + f2.getAbsolutePath() );