This project has retired. For details please refer to its
Attic page.
MavenProxyPropertyLoader xref
1 package org.apache.archiva.configuration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
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
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
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
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 }