This project has retired. For details please refer to its Attic page.
MavenProxyPropertyLoader xref
View Javadoc
1   package org.apache.archiva.configuration;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.archiva.policies.ReleasesPolicy;
23  import org.apache.archiva.policies.SnapshotsPolicy;
24  import org.apache.commons.lang.StringUtils;
25  
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;
31  
32  /**
33   */
34  public class MavenProxyPropertyLoader
35  {
36      private static final String REPO_LOCAL_STORE = "repo.local.store";
37  
38      private static final String PROXY_LIST = "proxy.list";
39  
40      private static final String REPO_LIST = "repo.list";
41  
42      public void load( Properties props, Configuration configuration )
43          throws InvalidConfigurationException
44      {
45          // set up the managed repository
46          String localCachePath = getMandatoryProperty( props, REPO_LOCAL_STORE );
47  
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 );
56  
57          // Add the network proxies.
58          String propertyList = props.getProperty( PROXY_LIST );
59          if ( propertyList != null )
60          {
61              StringTokenizer tok = new StringTokenizer( propertyList, "," );
62              while ( tok.hasMoreTokens() )
63              {
64                  String key = tok.nextToken();
65                  if ( StringUtils.isNotEmpty( key ) )
66                  {
67                      NetworkProxyConfiguration proxy = new NetworkProxyConfiguration();
68                      proxy.setHost( getMandatoryProperty( props, "proxy." + key + ".host" ) );
69                      proxy.setPort( Integer.parseInt( getMandatoryProperty( props, "proxy." + key + ".port" ) ) );
70  
71                      // the username and password isn't required
72                      proxy.setUsername( props.getProperty( "proxy." + key + ".username" ) );
73                      proxy.setPassword( props.getProperty( "proxy." + key + ".password" ) );
74  
75                      configuration.addNetworkProxy( proxy );
76                  }
77              }
78          }
79  
80          // Add the remote repository list
81          String repoList = getMandatoryProperty( props, REPO_LIST );
82  
83          StringTokenizer tok = new StringTokenizer( repoList, "," );
84          while ( tok.hasMoreTokens() )
85          {
86              String key = tok.nextToken();
87  
88              Properties repoProps = getSubset( props, "repo." + key + "." );
89              String url = getMandatoryProperty( props, "repo." + key + ".url" );
90              String proxyKey = repoProps.getProperty( "proxy" );
91  
92  //            int cachePeriod = Integer.parseInt( repoProps.getProperty( "cache.period", "60" ) );
93  
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" );
99  
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 }