This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.configuration;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.policies.ReleasesPolicy;
023import org.apache.archiva.policies.SnapshotsPolicy;
024import org.apache.commons.lang.StringUtils;
025
026import java.io.IOException;
027import java.io.InputStream;
028import java.util.Enumeration;
029import java.util.Properties;
030import java.util.StringTokenizer;
031
032/**
033 */
034public class MavenProxyPropertyLoader
035{
036    private static final String REPO_LOCAL_STORE = "repo.local.store";
037
038    private static final String PROXY_LIST = "proxy.list";
039
040    private static final String REPO_LIST = "repo.list";
041
042    public void load( Properties props, Configuration configuration )
043        throws InvalidConfigurationException
044    {
045        // set up the managed repository
046        String localCachePath = getMandatoryProperty( props, REPO_LOCAL_STORE );
047
048        ManagedRepositoryConfiguration config = new ManagedRepositoryConfiguration();
049        config.setLocation( localCachePath );
050        config.setName( "Imported Maven-Proxy Cache" );
051        config.setId( "maven-proxy" );
052        config.setScanned( false );
053        config.setReleases( true );
054        config.setSnapshots( false );
055        configuration.addManagedRepository( config );
056
057        // Add the network proxies.
058        String propertyList = props.getProperty( PROXY_LIST );
059        if ( propertyList != null )
060        {
061            StringTokenizer tok = new StringTokenizer( propertyList, "," );
062            while ( tok.hasMoreTokens() )
063            {
064                String key = tok.nextToken();
065                if ( StringUtils.isNotEmpty( key ) )
066                {
067                    NetworkProxyConfiguration proxy = new NetworkProxyConfiguration();
068                    proxy.setHost( getMandatoryProperty( props, "proxy." + key + ".host" ) );
069                    proxy.setPort( Integer.parseInt( getMandatoryProperty( props, "proxy." + key + ".port" ) ) );
070
071                    // the username and password isn't required
072                    proxy.setUsername( props.getProperty( "proxy." + key + ".username" ) );
073                    proxy.setPassword( props.getProperty( "proxy." + key + ".password" ) );
074
075                    configuration.addNetworkProxy( proxy );
076                }
077            }
078        }
079
080        // Add the remote repository list
081        String repoList = getMandatoryProperty( props, REPO_LIST );
082
083        StringTokenizer tok = new StringTokenizer( repoList, "," );
084        while ( tok.hasMoreTokens() )
085        {
086            String key = tok.nextToken();
087
088            Properties repoProps = getSubset( props, "repo." + key + "." );
089            String url = getMandatoryProperty( props, "repo." + key + ".url" );
090            String proxyKey = repoProps.getProperty( "proxy" );
091
092//            int cachePeriod = Integer.parseInt( repoProps.getProperty( "cache.period", "60" ) );
093
094            RemoteRepositoryConfiguration repository = new RemoteRepositoryConfiguration();
095            repository.setId( key );
096            repository.setName( "Imported Maven-Proxy Remote Proxy" );
097            repository.setUrl( url );
098            repository.setLayout( "legacy" );
099
100            configuration.addRemoteRepository( repository );
101
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 );
109
110            configuration.addProxyConnector( proxyConnector );
111        }
112    }
113
114    @SuppressWarnings( "unchecked" )
115    private Properties getSubset( Properties props, String prefix )
116    {
117        Enumeration keys = props.keys();
118        Properties result = new Properties();
119        while ( keys.hasMoreElements() )
120        {
121            String key = (String) keys.nextElement();
122            String value = props.getProperty( key );
123            if ( key.startsWith( prefix ) )
124            {
125                String newKey = key.substring( prefix.length() );
126                result.setProperty( newKey, value );
127            }
128        }
129        return result;
130    }
131
132    public void load( InputStream is, Configuration configuration )
133        throws IOException, InvalidConfigurationException
134    {
135        Properties props = new Properties();
136        props.load( is );
137        load( props, configuration );
138    }
139
140    private String getMandatoryProperty( Properties props, String key )
141        throws InvalidConfigurationException
142    {
143        String value = props.getProperty( key );
144
145        if ( value == null )
146        {
147            throw new InvalidConfigurationException( key, "Missing required field: " + key );
148        }
149
150        return value;
151    }
152}