You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MavenProxyPropertyLoader.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package org.apache.archiva.configuration.provider;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.configuration.model.Configuration;
  20. import org.apache.archiva.configuration.model.ManagedRepositoryConfiguration;
  21. import org.apache.archiva.configuration.model.NetworkProxyConfiguration;
  22. import org.apache.archiva.configuration.model.ProxyConnectorConfiguration;
  23. import org.apache.archiva.configuration.model.RemoteRepositoryConfiguration;
  24. import org.apache.archiva.policies.ReleasesPolicy;
  25. import org.apache.archiva.policies.SnapshotsPolicy;
  26. import org.apache.commons.lang3.StringUtils;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.util.Enumeration;
  30. import java.util.Properties;
  31. import java.util.StringTokenizer;
  32. /**
  33. */
  34. public class MavenProxyPropertyLoader
  35. {
  36. private static final String REPO_LOCAL_STORE = "repo.local.store";
  37. private static final String PROXY_LIST = "proxy.list";
  38. private static final String REPO_LIST = "repo.list";
  39. public void load( Properties props, Configuration configuration )
  40. throws InvalidConfigurationException
  41. {
  42. // set up the managed repository
  43. String localCachePath = getMandatoryProperty( props, REPO_LOCAL_STORE );
  44. ManagedRepositoryConfiguration config = new ManagedRepositoryConfiguration();
  45. config.setLocation( localCachePath );
  46. config.setName( "Imported Maven-Proxy Cache" );
  47. config.setId( "maven-proxy" );
  48. config.setScanned( false );
  49. config.setReleases( true );
  50. config.setSnapshots( false );
  51. configuration.addManagedRepository( config );
  52. // Add the network proxies.
  53. String propertyList = props.getProperty( PROXY_LIST );
  54. if ( propertyList != null )
  55. {
  56. StringTokenizer tok = new StringTokenizer( propertyList, "," );
  57. while ( tok.hasMoreTokens() )
  58. {
  59. String key = tok.nextToken();
  60. if ( StringUtils.isNotEmpty( key ) )
  61. {
  62. NetworkProxyConfiguration proxy = new NetworkProxyConfiguration();
  63. proxy.setHost( getMandatoryProperty( props, "proxy." + key + ".host" ) );
  64. proxy.setPort( Integer.parseInt( getMandatoryProperty( props, "proxy." + key + ".port" ) ) );
  65. // the username and password isn't required
  66. proxy.setUsername( props.getProperty( "proxy." + key + ".username" ) );
  67. proxy.setPassword( props.getProperty( "proxy." + key + ".password" ) );
  68. configuration.addNetworkProxy( proxy );
  69. }
  70. }
  71. }
  72. // Add the remote repository list
  73. String repoList = getMandatoryProperty( props, REPO_LIST );
  74. StringTokenizer tok = new StringTokenizer( repoList, "," );
  75. while ( tok.hasMoreTokens() )
  76. {
  77. String key = tok.nextToken();
  78. Properties repoProps = getSubset( props, "repo." + key + "." );
  79. String url = getMandatoryProperty( props, "repo." + key + ".url" );
  80. String proxyKey = repoProps.getProperty( "proxy" );
  81. // int cachePeriod = Integer.parseInt( repoProps.getProperty( "cache.period", "60" ) );
  82. RemoteRepositoryConfiguration repository = new RemoteRepositoryConfiguration();
  83. repository.setId( key );
  84. repository.setName( "Imported Maven-Proxy Remote Proxy" );
  85. repository.setUrl( url );
  86. repository.setLayout( "legacy" );
  87. configuration.addRemoteRepository( repository );
  88. ProxyConnectorConfiguration proxyConnector = new ProxyConnectorConfiguration();
  89. proxyConnector.setSourceRepoId( "maven-proxy" );
  90. proxyConnector.setTargetRepoId( key );
  91. proxyConnector.setProxyId( proxyKey );
  92. // TODO: convert cachePeriod to closest "daily" or "hourly"
  93. proxyConnector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, SnapshotsPolicy.DAILY.getId() );
  94. proxyConnector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, ReleasesPolicy.ALWAYS.getId() );
  95. configuration.addProxyConnector( proxyConnector );
  96. }
  97. }
  98. @SuppressWarnings( "unchecked" )
  99. private Properties getSubset( Properties props, String prefix )
  100. {
  101. Enumeration keys = props.keys();
  102. Properties result = new Properties();
  103. while ( keys.hasMoreElements() )
  104. {
  105. String key = (String) keys.nextElement();
  106. String value = props.getProperty( key );
  107. if ( key.startsWith( prefix ) )
  108. {
  109. String newKey = key.substring( prefix.length() );
  110. result.setProperty( newKey, value );
  111. }
  112. }
  113. return result;
  114. }
  115. public void load( InputStream is, Configuration configuration )
  116. throws IOException, InvalidConfigurationException
  117. {
  118. Properties props = new Properties();
  119. props.load( is );
  120. load( props, configuration );
  121. }
  122. private String getMandatoryProperty( Properties props, String key )
  123. throws InvalidConfigurationException
  124. {
  125. String value = props.getProperty( key );
  126. if ( value == null )
  127. {
  128. throw new InvalidConfigurationException( key, "Missing required field: " + key );
  129. }
  130. return value;
  131. }
  132. }