1 package org.apache.maven.archiva.configuration;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.collections.MapUtils;
24 import org.apache.commons.configuration.BaseConfiguration;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.archiva.configuration.functors.ProxyConnectorConfigurationOrderComparator;
28 import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryReader;
29 import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryWriter;
30 import org.apache.maven.archiva.policies.AbstractUpdatePolicy;
31 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
32 import org.apache.maven.archiva.policies.ChecksumPolicy;
33 import org.apache.maven.archiva.policies.Policy;
34 import org.apache.maven.archiva.policies.PostDownloadPolicy;
35 import org.apache.maven.archiva.policies.PreDownloadPolicy;
36 import org.codehaus.plexus.evaluator.DefaultExpressionEvaluator;
37 import org.codehaus.plexus.evaluator.EvaluatorException;
38 import org.codehaus.plexus.evaluator.ExpressionEvaluator;
39 import org.codehaus.plexus.evaluator.sources.SystemPropertyExpressionSource;
40 import org.codehaus.plexus.registry.Registry;
41 import org.codehaus.plexus.registry.RegistryException;
42 import org.codehaus.plexus.registry.RegistryListener;
43 import org.codehaus.redback.components.registry.commons.CommonsConfigurationRegistry;
44 import org.codehaus.redback.components.springutils.ComponentContainer;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Service;
49 import javax.annotation.PostConstruct;
50 import javax.inject.Inject;
51 import javax.inject.Named;
53 import java.io.IOException;
54 import java.util.ArrayList;
55 import java.util.Collection;
56 import java.util.Collections;
57 import java.util.HashMap;
58 import java.util.HashSet;
59 import java.util.Iterator;
60 import java.util.List;
62 import java.util.Map.Entry;
67 * Implementation of configuration holder that retrieves it from the registry.
70 * The registry layers and merges the 2 configuration files: user, and application server.
73 * Instead of relying on the model defaults, if the registry is empty a default configuration file is loaded and
74 * applied from a resource. The defaults are not loaded into the registry as the lists (eg repositories) could no longer
75 * be removed if that was the case.
78 * When saving the configuration, it is saved to the location it was read from. If it was read from the defaults, it
79 * will be saved to the user location.
80 * However, if the configuration contains information from both sources, an exception is raised as this is currently
81 * unsupported. The reason for this is that it is not possible to identify where to re-save elements, and can result
82 * in list configurations (eg repositories) becoming inconsistent.
85 * If the configuration is outdated, it will be upgraded when it is loaded. This is done by checking the version flag
86 * before reading it from the registry.
89 @Service( "archivaConfiguration#default" )
90 public class DefaultArchivaConfiguration
91 implements ArchivaConfiguration, RegistryListener
93 private Logger log = LoggerFactory.getLogger( DefaultArchivaConfiguration.class );
96 * Plexus registry to read the configuration from.
98 * plexus.requirement role-hint="commons-configuration"
101 @Named( value = "commons-configuration" )
102 private Registry registry;
105 private ComponentContainer componentContainer;
108 * The configuration that has been converted.
110 private Configuration configuration;
114 * plexus.requirement role="org.apache.maven.archiva.policies.PreDownloadPolicy"
116 * @todo these don't strictly belong in here
118 private Map<String, PreDownloadPolicy> prePolicies;
122 * plexus.requirement role="org.apache.maven.archiva.policies.PostDownloadPolicy"
124 * @todo these don't strictly belong in here
126 private Map<String, PostDownloadPolicy> postPolicies;
130 * plexus.configuration default-value="${user.home}/.m2/archiva.xml"
132 private String userConfigFilename = "${user.home}/.m2/archiva.xml";
136 * plexus.configuration default-value="${appserver.base}/conf/archiva.xml"
138 private String altConfigFilename = "${appserver.base}/conf/archiva.xml";
141 * Configuration Listeners we've registered.
143 private Set<ConfigurationListener> listeners = new HashSet<ConfigurationListener>();
146 * Registry Listeners we've registered.
148 private Set<RegistryListener> registryListeners = new HashSet<RegistryListener>();
151 * Boolean to help determine if the configuration exists as a result of pulling in
152 * the default-archiva.xml
154 private boolean isConfigurationDefaulted = false;
156 private static final String KEY = "org.apache.maven.archiva";
158 public Configuration getConfiguration()
160 return loadConfiguration();
163 private synchronized Configuration loadConfiguration()
165 if ( configuration == null )
167 configuration = load();
168 configuration = unescapeExpressions( configuration );
169 if ( isConfigurationDefaulted )
171 configuration = checkRepositoryLocations( configuration );
175 return configuration;
178 @SuppressWarnings( "unchecked" )
179 private Configuration load()
181 // TODO: should this be the same as section? make sure unnamed sections still work (eg, sys properties)
182 Registry subset = registry.getSubset( KEY );
183 if ( subset.getString( "version" ) == null )
185 // a little autodetection of v1, even if version is omitted (this was previously allowed)
186 if ( subset.getSubset( "repositoryScanning" ).isEmpty() )
188 // only for empty, or v < 1
189 subset = readDefaultConfiguration();
193 Configuration config = new ConfigurationRegistryReader().read( subset );
195 config.getRepositoryGroups();
196 config.getRepositoryGroupsAsMap();
197 if ( !config.getRepositories().isEmpty() )
199 for ( Iterator<V1RepositoryConfiguration> i = config.getRepositories().iterator(); i.hasNext(); )
201 V1RepositoryConfiguration r = i.next();
202 r.setScanned( r.isIndexed() );
204 if ( r.getUrl().startsWith( "file://" ) )
206 r.setLocation( r.getUrl().substring( 7 ) );
207 config.addManagedRepository( r );
209 else if ( r.getUrl().startsWith( "file:" ) )
211 r.setLocation( r.getUrl().substring( 5 ) );
212 config.addManagedRepository( r );
216 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
217 repo.setId( r.getId() );
218 repo.setLayout( r.getLayout() );
219 repo.setName( r.getName() );
220 repo.setUrl( r.getUrl() );
221 config.addRemoteRepository( repo );
225 // Prevent duplicate repositories from showing up.
226 config.getRepositories().clear();
228 registry.removeSubset( KEY + ".repositories" );
231 if ( !CollectionUtils.isEmpty( config.getRemoteRepositories() ) )
233 List<RemoteRepositoryConfiguration> remoteRepos = config.getRemoteRepositories();
234 for ( RemoteRepositoryConfiguration repo : remoteRepos )
236 // [MRM-582] Remote Repositories with empty <username> and <password> fields shouldn't be created in configuration.
237 if ( StringUtils.isBlank( repo.getUsername() ) )
239 repo.setUsername( null );
242 if ( StringUtils.isBlank( repo.getPassword() ) )
244 repo.setPassword( null );
249 if ( !config.getProxyConnectors().isEmpty() )
251 // Fix Proxy Connector Settings.
253 List<ProxyConnectorConfiguration> proxyConnectorList = new ArrayList<ProxyConnectorConfiguration>();
254 // Create a copy of the list to read from (to prevent concurrent modification exceptions)
255 proxyConnectorList.addAll( config.getProxyConnectors() );
256 // Remove the old connector list.
257 config.getProxyConnectors().clear();
259 for ( ProxyConnectorConfiguration connector : proxyConnectorList )
262 boolean connectorValid = true;
264 Map<String, String> policies = new HashMap<String, String>();
265 // Make copy of policies
266 policies.putAll( connector.getPolicies() );
267 // Clear out policies
268 connector.getPolicies().clear();
270 // Work thru policies. cleaning them up.
271 for ( Entry<String, String> entry : policies.entrySet() )
273 String policyId = entry.getKey();
274 String setting = entry.getValue();
276 // Upgrade old policy settings.
277 if ( "releases".equals( policyId ) || "snapshots".equals( policyId ) )
279 if ( "ignored".equals( setting ) )
281 setting = AbstractUpdatePolicy.ALWAYS;
283 else if ( "disabled".equals( setting ) )
285 setting = AbstractUpdatePolicy.NEVER;
288 else if ( "cache-failures".equals( policyId ) )
290 if ( "ignored".equals( setting ) )
292 setting = CachedFailuresPolicy.NO;
294 else if ( "cached".equals( setting ) )
296 setting = CachedFailuresPolicy.YES;
299 else if ( "checksum".equals( policyId ) )
301 if ( "ignored".equals( setting ) )
303 setting = ChecksumPolicy.IGNORE;
307 // Validate existance of policy key.
308 if ( policyExists( policyId ) )
310 Policy policy = findPolicy( policyId );
311 // Does option exist?
312 if ( !policy.getOptions().contains( setting ) )
314 setting = policy.getDefaultOption();
316 connector.addPolicy( policyId, setting );
320 // Policy key doesn't exist. Don't add it to golden version.
321 log.warn( "Policy [" + policyId + "] does not exist." );
325 if ( connectorValid )
327 config.addProxyConnector( connector );
331 // Normalize the order fields in the proxy connectors.
332 Map<String, java.util.List<ProxyConnectorConfiguration>> proxyConnectorMap =
333 config.getProxyConnectorAsMap();
335 for ( List<ProxyConnectorConfiguration> connectors : proxyConnectorMap.values() )
337 // Sort connectors by order field.
338 Collections.sort( connectors, ProxyConnectorConfigurationOrderComparator.getInstance() );
340 // Normalize the order field values.
342 for ( ProxyConnectorConfiguration connector : connectors )
344 connector.setOrder( order++ );
352 private Policy findPolicy( String policyId )
354 if ( MapUtils.isEmpty( prePolicies ) )
356 log.error( "No PreDownloadPolicies found!" );
360 if ( MapUtils.isEmpty( postPolicies ) )
362 log.error( "No PostDownloadPolicies found!" );
368 policy = prePolicies.get( policyId );
369 if ( policy != null )
374 policy = postPolicies.get( policyId );
375 if ( policy != null )
383 private boolean policyExists( String policyId )
385 if ( MapUtils.isEmpty( prePolicies ) )
387 log.error( "No PreDownloadPolicies found!" );
391 if ( MapUtils.isEmpty( postPolicies ) )
393 log.error( "No PostDownloadPolicies found!" );
397 return ( prePolicies.containsKey( policyId ) || postPolicies.containsKey( policyId ) );
400 private Registry readDefaultConfiguration()
402 // if it contains some old configuration, remove it (Archiva 0.9)
403 registry.removeSubset( KEY );
407 registry.addConfigurationFromResource( "org/apache/maven/archiva/configuration/default-archiva.xml", KEY );
408 this.isConfigurationDefaulted = true;
410 catch ( RegistryException e )
412 throw new ConfigurationRuntimeException(
413 "Fatal error: Unable to find the built-in default configuration and load it into the registry", e );
415 return registry.getSubset( KEY );
418 @SuppressWarnings( "unchecked" )
419 public synchronized void save( Configuration configuration )
420 throws IndeterminateConfigurationException, RegistryException
422 Registry section = registry.getSection( KEY + ".user" );
423 Registry baseSection = registry.getSection( KEY + ".base" );
424 if ( section == null )
426 section = baseSection;
427 if ( section == null )
429 section = createDefaultConfigurationFile();
432 else if ( baseSection != null )
434 Collection<String> keys = baseSection.getKeys();
435 boolean foundList = false;
436 for ( Iterator<String> i = keys.iterator(); i.hasNext() && !foundList; )
438 String key = i.next();
440 // a little aggressive with the repositoryScanning and databaseScanning - should be no need to split
441 // that configuration
442 if ( key.startsWith( "repositories" ) || key.startsWith( "proxyConnectors" )
443 || key.startsWith( "networkProxies" ) || key.startsWith( "repositoryScanning" )
444 || key.startsWith( "databaseScanning" ) || key.startsWith( "remoteRepositories" )
445 || key.startsWith( "managedRepositories" ) || key.startsWith( "repositoryGroups" ) )
453 this.configuration = null;
455 throw new IndeterminateConfigurationException(
456 "Configuration can not be saved when it is loaded from two sources" );
460 // escape all cron expressions to handle ','
461 escapeCronExpressions( configuration );
463 // [MRM-661] Due to a bug in the modello registry writer, we need to take these out by hand. They'll be put back by the writer.
464 if ( configuration.getManagedRepositories().isEmpty() && section != null )
466 section.removeSubset( "managedRepositories" );
468 if ( configuration.getRemoteRepositories().isEmpty() && section != null )
470 section.removeSubset( "remoteRepositories" );
473 if ( configuration.getProxyConnectors().isEmpty() && section != null )
475 section.removeSubset( "proxyConnectors" );
477 if ( configuration.getNetworkProxies().isEmpty() && section != null )
479 section.removeSubset( "networkProxies" );
481 if ( configuration.getLegacyArtifactPaths().isEmpty() && section != null )
483 section.removeSubset( "legacyArtifactPaths" );
485 if ( configuration.getRepositoryGroups().isEmpty() && section != null )
487 section.removeSubset( "repositoryGroups" );
489 if ( configuration.getRepositoryScanning() != null )
491 if ( configuration.getRepositoryScanning().getKnownContentConsumers().isEmpty() && section != null )
493 section.removeSubset( "repositoryScanning.knownContentConsumers" );
495 if ( configuration.getRepositoryScanning().getInvalidContentConsumers().isEmpty() && section != null )
497 section.removeSubset( "repositoryScanning.invalidContentConsumers" );
500 if ( configuration.getDatabaseScanning() != null )
502 if ( configuration.getDatabaseScanning().getCleanupConsumers().isEmpty() && section != null )
504 section.removeSubset( "databaseScanning.cleanupConsumers" );
507 if ( configuration.getDatabaseScanning().getUnprocessedConsumers().isEmpty() && section != null )
509 section.removeSubset( "databaseScanning.unprocessedConsumers" );
513 new ConfigurationRegistryWriter().write( configuration, section );
516 this.configuration = unescapeExpressions( configuration );
518 triggerEvent( ConfigurationEvent.SAVED );
521 private void escapeCronExpressions( Configuration configuration )
523 for ( ManagedRepositoryConfiguration c : (List<ManagedRepositoryConfiguration>) configuration.getManagedRepositories() )
525 c.setRefreshCronExpression( escapeCronExpression( c.getRefreshCronExpression() ) );
528 DatabaseScanningConfiguration scanning = configuration.getDatabaseScanning();
529 if ( scanning != null )
531 scanning.setCronExpression( escapeCronExpression( scanning.getCronExpression() ) );
535 private Registry createDefaultConfigurationFile()
536 throws RegistryException
538 // TODO: may not be needed under commons-configuration 1.4 - check
539 // UPDATE: Upgrading to commons-configuration 1.4 breaks half the unit tests. 2007-10-11 (joakime)
541 String contents = "<configuration />";
543 String fileLocation = userConfigFilename;
545 if ( !writeFile( "user configuration", userConfigFilename, contents ) )
547 fileLocation = altConfigFilename;
548 if ( !writeFile( "alternative configuration", altConfigFilename, contents ) )
550 throw new RegistryException(
551 "Unable to create configuration file in either user [" + userConfigFilename + "] or alternative ["
553 + "] locations on disk, usually happens when not allowed to write to those locations." );
557 // olamy hackish I know :-)
558 contents = "<configuration><xml fileName=\"" + fileLocation +"\" config-forceCreate=\"true\" config-name=\"org.apache.maven.archiva.user\"/>"
559 + "</configuration>";
562 ( (CommonsConfigurationRegistry) registry ).setProperties( contents );
564 ( (CommonsConfigurationRegistry) registry ).initialize();
566 for ( RegistryListener regListener : registryListeners )
568 addRegistryChangeListener( regListener );
571 triggerEvent( ConfigurationEvent.SAVED );
573 Registry section = registry.getSection( KEY + ".user" );
574 return section == null ? new CommonsConfigurationRegistry( new BaseConfiguration() ) : section;
578 * Attempts to write the contents to a file, if an IOException occurs, return false.
580 * The file will be created if the directory to the file exists, otherwise this will return false.
582 * @param filetype the filetype (freeform text) to use in logging messages when failure to write.
583 * @param path the path to write to.
584 * @param contents the contents to write.
585 * @return true if write successful.
587 private boolean writeFile( String filetype, String path, String contents )
589 File file = new File( path );
593 // Check parent directory (if it is declared)
594 if ( file.getParentFile() != null )
596 // Check that directory exists
597 if ( ( file.getParentFile().exists() == false ) || ( file.getParentFile().isDirectory() == false ) )
599 // Directory to file must exist for file to be created
604 FileUtils.writeStringToFile( file, contents, "UTF-8" );
607 catch ( IOException e )
609 log.error( "Unable to create " + filetype + " file: " + e.getMessage(), e );
614 private void triggerEvent( int type )
616 ConfigurationEvent evt = new ConfigurationEvent( type );
617 for ( ConfigurationListener listener : listeners )
619 listener.configurationEvent( evt );
623 public void addListener( ConfigurationListener listener )
625 if ( listener == null )
630 listeners.add( listener );
633 public void removeListener( ConfigurationListener listener )
635 if ( listener == null )
640 listeners.remove( listener );
643 public void addChangeListener( RegistryListener listener )
645 addRegistryChangeListener( listener );
647 // keep track for later
648 registryListeners.add( listener );
651 private void addRegistryChangeListener( RegistryListener listener )
653 Registry section = registry.getSection( KEY + ".user" );
654 if ( section != null )
656 section.addChangeListener( listener );
658 section = registry.getSection( KEY + ".base" );
659 if ( section != null )
661 section.addChangeListener( listener );
666 public void initialize()
669 this.postPolicies = componentContainer.buildMapWithRole( PostDownloadPolicy.class );
670 this.prePolicies = componentContainer.buildMapWithRole( PreDownloadPolicy.class );
671 // Resolve expressions in the userConfigFilename and altConfigFilename
674 ExpressionEvaluator expressionEvaluator = new DefaultExpressionEvaluator();
675 expressionEvaluator.addExpressionSource( new SystemPropertyExpressionSource() );
676 userConfigFilename = expressionEvaluator.expand( userConfigFilename );
677 altConfigFilename = expressionEvaluator.expand( altConfigFilename );
680 catch ( EvaluatorException e )
682 throw new RuntimeException(
683 "Unable to evaluate expressions found in " + "userConfigFilename or altConfigFilename." );
685 registry.addChangeListener( this );
690 this.configuration = null;
693 this.registry.initialize();
695 catch ( RegistryException e )
697 throw new ConfigurationRuntimeException( e.getMessage(), e );
702 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
704 // nothing to do here
707 public synchronized void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
709 configuration = null;
712 private String removeExpressions( String directory )
714 String value = StringUtils.replace( directory, "${appserver.base}",
715 registry.getString( "appserver.base", "${appserver.base}" ) );
716 value = StringUtils.replace( value, "${appserver.home}",
717 registry.getString( "appserver.home", "${appserver.home}" ) );
721 private String unescapeCronExpression( String cronExpression )
723 return StringUtils.replace( cronExpression, "\\,", "," );
726 private String escapeCronExpression( String cronExpression )
728 return StringUtils.replace( cronExpression, ",", "\\," );
731 private Configuration unescapeExpressions( Configuration config )
733 // TODO: for commons-configuration 1.3 only
734 for ( Iterator<ManagedRepositoryConfiguration> i = config.getManagedRepositories().iterator(); i.hasNext(); )
736 ManagedRepositoryConfiguration c = i.next();
737 c.setLocation( removeExpressions( c.getLocation() ) );
738 c.setRefreshCronExpression( unescapeCronExpression( c.getRefreshCronExpression() ) );
741 DatabaseScanningConfiguration databaseScanning = config.getDatabaseScanning();
742 if ( databaseScanning != null )
744 String cron = databaseScanning.getCronExpression();
745 databaseScanning.setCronExpression( unescapeCronExpression( cron ) );
751 private Configuration checkRepositoryLocations( Configuration config )
753 // additional check for [MRM-789], ensure that the location of the default repositories
754 // are not installed in the server installation
755 for ( ManagedRepositoryConfiguration repo : (List<ManagedRepositoryConfiguration>) config.getManagedRepositories() )
757 String repoPath = repo.getLocation();
758 File repoLocation = new File( repoPath );
760 if ( repoLocation.exists() && repoLocation.isDirectory() && !repoPath.endsWith(
761 "data/repositories/" + repo.getId() ) )
763 repo.setLocation( repoPath + "/data/repositories/" + repo.getId() );
770 public String getUserConfigFilename()
772 return userConfigFilename;
775 public String getAltConfigFilename()
777 return altConfigFilename;
780 public boolean isDefaulted()
782 return this.isConfigurationDefaulted;
785 public Registry getRegistry()
790 public void setRegistry( Registry registry )
792 this.registry = registry;
796 public void setUserConfigFilename( String userConfigFilename )
798 this.userConfigFilename = userConfigFilename;
801 public void setAltConfigFilename( String altConfigFilename )
803 this.altConfigFilename = altConfigFilename;