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 5.7KB

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