1 package org.apache.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.archiva.configuration.functors.ProxyConnectorConfigurationOrderComparator;
23 import org.apache.archiva.configuration.io.registry.ConfigurationRegistryReader;
24 import org.apache.archiva.configuration.io.registry.ConfigurationRegistryWriter;
25 import org.apache.archiva.policies.AbstractUpdatePolicy;
26 import org.apache.archiva.policies.CachedFailuresPolicy;
27 import org.apache.archiva.policies.ChecksumPolicy;
28 import org.apache.archiva.policies.DownloadErrorPolicy;
29 import org.apache.archiva.policies.Policy;
30 import org.apache.archiva.policies.PostDownloadPolicy;
31 import org.apache.archiva.policies.PreDownloadPolicy;
32 import org.apache.archiva.redback.components.evaluator.DefaultExpressionEvaluator;
33 import org.apache.archiva.redback.components.evaluator.EvaluatorException;
34 import org.apache.archiva.redback.components.evaluator.ExpressionEvaluator;
35 import org.apache.archiva.redback.components.evaluator.sources.SystemPropertyExpressionSource;
36 import org.apache.archiva.redback.components.registry.Registry;
37 import org.apache.archiva.redback.components.registry.RegistryException;
38 import org.apache.archiva.redback.components.registry.RegistryListener;
39 import org.apache.archiva.redback.components.registry.commons.CommonsConfigurationRegistry;
40 import org.apache.archiva.redback.components.springutils.ComponentContainer;
41 import org.apache.commons.collections.CollectionUtils;
42 import org.apache.commons.collections.ListUtils;
43 import org.apache.commons.collections.MapUtils;
44 import org.apache.commons.configuration.BaseConfiguration;
45 import org.apache.commons.io.FileUtils;
46 import org.apache.commons.lang.StringUtils;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Service;
51 import javax.annotation.PostConstruct;
52 import javax.inject.Inject;
53 import javax.inject.Named;
55 import java.io.IOException;
56 import java.util.ArrayList;
57 import java.util.Arrays;
58 import java.util.Collection;
59 import java.util.Collections;
60 import java.util.HashMap;
61 import java.util.HashSet;
62 import java.util.Iterator;
63 import java.util.List;
65 import java.util.Map.Entry;
70 * Implementation of configuration holder that retrieves it from the registry.
73 * The registry layers and merges the 2 configuration files: user, and application server.
76 * Instead of relying on the model defaults, if the registry is empty a default configuration file is loaded and
77 * applied from a resource. The defaults are not loaded into the registry as the lists (eg repositories) could no longer
78 * be removed if that was the case.
81 * When saving the configuration, it is saved to the location it was read from. If it was read from the defaults, it
82 * will be saved to the user location.
83 * However, if the configuration contains information from both sources, an exception is raised as this is currently
84 * unsupported. The reason for this is that it is not possible to identify where to re-save elements, and can result
85 * in list configurations (eg repositories) becoming inconsistent.
88 * If the configuration is outdated, it will be upgraded when it is loaded. This is done by checking the version flag
89 * before reading it from the registry.
92 @Service ( "archivaConfiguration#default" )
93 public class DefaultArchivaConfiguration
94 implements ArchivaConfiguration, RegistryListener
96 private Logger log = LoggerFactory.getLogger( DefaultArchivaConfiguration.class );
99 * Plexus registry to read the configuration from.
102 @Named ( value = "commons-configuration" )
103 private Registry registry;
106 private ComponentContainer componentContainer;
109 * The configuration that has been converted.
111 private Configuration configuration;
116 * @todo these don't strictly belong in here
118 private Map<String, PreDownloadPolicy> prePolicies;
123 * @todo these don't strictly belong in here
125 private Map<String, PostDownloadPolicy> postPolicies;
130 * @todo these don't strictly belong in here
132 private Map<String, DownloadErrorPolicy> downloadErrorPolicies;
137 * default-value="${user.home}/.m2/archiva.xml"
139 private String userConfigFilename = "${user.home}/.m2/archiva.xml";
143 * default-value="${appserver.base}/conf/archiva.xml"
145 private String altConfigFilename = "${appserver.base}/conf/archiva.xml";
148 * Configuration Listeners we've registered.
150 private Set<ConfigurationListener> listeners = new HashSet<>();
153 * Registry Listeners we've registered.
155 private Set<RegistryListener> registryListeners = new HashSet<>();
158 * Boolean to help determine if the configuration exists as a result of pulling in
159 * the default-archiva.xml
161 private boolean isConfigurationDefaulted = false;
163 private static final String KEY = "org.apache.archiva";
166 public Configuration getConfiguration()
168 return loadConfiguration();
171 private synchronized Configuration loadConfiguration()
173 if ( configuration == null )
175 configuration = load();
176 configuration = unescapeExpressions( configuration );
177 if ( isConfigurationDefaulted )
179 configuration = checkRepositoryLocations( configuration );
183 return configuration;
186 @SuppressWarnings ( "unchecked" )
187 private Configuration load()
189 // TODO: should this be the same as section? make sure unnamed sections still work (eg, sys properties)
190 Registry subset = registry.getSubset( KEY );
191 if ( subset.getString( "version" ) == null )
193 // a little autodetection of v1, even if version is omitted (this was previously allowed)
194 if ( subset.getSubset( "repositoryScanning" ).isEmpty() )
196 // only for empty, or v < 1
197 subset = readDefaultConfiguration();
201 Configuration config = new ConfigurationRegistryReader().read( subset );
203 config.getRepositoryGroups();
204 config.getRepositoryGroupsAsMap();
205 if ( !config.getRepositories().isEmpty() )
207 for ( V1RepositoryConfiguration r : config.getRepositories() )
209 r.setScanned( r.isIndexed() );
211 if ( StringUtils.startsWith( r.getUrl(), "file://" ) )
213 r.setLocation( r.getUrl().substring( 7 ) );
214 config.addManagedRepository( r );
216 else if ( StringUtils.startsWith( r.getUrl(), "file:" ) )
218 r.setLocation( r.getUrl().substring( 5 ) );
219 config.addManagedRepository( r );
221 else if ( StringUtils.isEmpty( r.getUrl() ) )
223 // in case of empty url we can consider it as a managed one
224 // check if location is null
225 //file://${appserver.base}/repositories/${id}
226 if ( StringUtils.isEmpty( r.getLocation() ) )
228 r.setLocation( "file://${appserver.base}/repositories/" + r.getId() );
230 config.addManagedRepository( r );
234 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
235 repo.setId( r.getId() );
236 repo.setLayout( r.getLayout() );
237 repo.setName( r.getName() );
238 repo.setUrl( r.getUrl() );
239 config.addRemoteRepository( repo );
243 // Prevent duplicate repositories from showing up.
244 config.getRepositories().clear();
246 registry.removeSubset( KEY + ".repositories" );
249 if ( !CollectionUtils.isEmpty( config.getRemoteRepositories() ) )
251 List<RemoteRepositoryConfiguration> remoteRepos = config.getRemoteRepositories();
252 for ( RemoteRepositoryConfiguration repo : remoteRepos )
254 // [MRM-582] Remote Repositories with empty <username> and <password> fields shouldn't be created in configuration.
255 if ( StringUtils.isBlank( repo.getUsername() ) )
257 repo.setUsername( null );
260 if ( StringUtils.isBlank( repo.getPassword() ) )
262 repo.setPassword( null );
267 if ( !config.getProxyConnectors().isEmpty() )
269 // Fix Proxy Connector Settings.
271 // Create a copy of the list to read from (to prevent concurrent modification exceptions)
272 List<ProxyConnectorConfiguration> proxyConnectorList =
273 new ArrayList<>( config.getProxyConnectors() );
274 // Remove the old connector list.
275 config.getProxyConnectors().clear();
277 for ( ProxyConnectorConfiguration connector : proxyConnectorList )
280 boolean connectorValid = true;
282 Map<String, String> policies = new HashMap<>();
283 // Make copy of policies
284 policies.putAll( connector.getPolicies() );
285 // Clear out policies
286 connector.getPolicies().clear();
288 // Work thru policies. cleaning them up.
289 for ( Entry<String, String> entry : policies.entrySet() )
291 String policyId = entry.getKey();
292 String setting = entry.getValue();
294 // Upgrade old policy settings.
295 if ( "releases".equals( policyId ) || "snapshots".equals( policyId ) )
297 if ( "ignored".equals( setting ) )
299 setting = AbstractUpdatePolicy.ALWAYS;
301 else if ( "disabled".equals( setting ) )
303 setting = AbstractUpdatePolicy.NEVER;
306 else if ( "cache-failures".equals( policyId ) )
308 if ( "ignored".equals( setting ) )
310 setting = CachedFailuresPolicy.NO;
312 else if ( "cached".equals( setting ) )
314 setting = CachedFailuresPolicy.YES;
317 else if ( "checksum".equals( policyId ) )
319 if ( "ignored".equals( setting ) )
321 setting = ChecksumPolicy.IGNORE;
325 // Validate existance of policy key.
326 if ( policyExists( policyId ) )
328 Policy policy = findPolicy( policyId );
329 // Does option exist?
330 if ( !policy.getOptions().contains( setting ) )
332 setting = policy.getDefaultOption();
334 connector.addPolicy( policyId, setting );
338 // Policy key doesn't exist. Don't add it to golden version.
339 log.warn( "Policy [{}] does not exist.", policyId );
343 if ( connectorValid )
345 config.addProxyConnector( connector );
349 // Normalize the order fields in the proxy connectors.
350 Map<String, java.util.List<ProxyConnectorConfiguration>> proxyConnectorMap =
351 config.getProxyConnectorAsMap();
353 for ( List<ProxyConnectorConfiguration> connectors : proxyConnectorMap.values() )
355 // Sort connectors by order field.
356 Collections.sort( connectors, ProxyConnectorConfigurationOrderComparator.getInstance() );
358 // Normalize the order field values.
360 for ( ProxyConnectorConfiguration connector : connectors )
362 connector.setOrder( order++ );
370 private Policy findPolicy( String policyId )
372 if ( MapUtils.isEmpty( prePolicies ) )
374 log.error( "No PreDownloadPolicies found!" );
378 if ( MapUtils.isEmpty( postPolicies ) )
380 log.error( "No PostDownloadPolicies found!" );
386 policy = prePolicies.get( policyId );
387 if ( policy != null )
392 policy = postPolicies.get( policyId );
393 if ( policy != null )
398 policy = downloadErrorPolicies.get( policyId );
399 if ( policy != null )
407 private boolean policyExists( String policyId )
409 if ( MapUtils.isEmpty( prePolicies ) )
411 log.error( "No PreDownloadPolicies found!" );
415 if ( MapUtils.isEmpty( postPolicies ) )
417 log.error( "No PostDownloadPolicies found!" );
421 return ( prePolicies.containsKey( policyId ) || postPolicies.containsKey( policyId )
422 || downloadErrorPolicies.containsKey( policyId ) );
425 private Registry readDefaultConfiguration()
427 // if it contains some old configuration, remove it (Archiva 0.9)
428 registry.removeSubset( KEY );
432 registry.addConfigurationFromResource( "org/apache/archiva/configuration/default-archiva.xml", KEY );
433 this.isConfigurationDefaulted = true;
435 catch ( RegistryException e )
437 throw new ConfigurationRuntimeException(
438 "Fatal error: Unable to find the built-in default configuration and load it into the registry", e );
440 return registry.getSubset( KEY );
443 @SuppressWarnings ( "unchecked" )
445 public synchronized void save( Configuration configuration )
446 throws IndeterminateConfigurationException, RegistryException
448 Registry section = registry.getSection( KEY + ".user" );
449 Registry baseSection = registry.getSection( KEY + ".base" );
450 if ( section == null )
452 section = baseSection;
453 if ( section == null )
455 section = createDefaultConfigurationFile();
458 else if ( baseSection != null )
460 Collection<String> keys = baseSection.getKeys();
461 boolean foundList = false;
462 for ( Iterator<String> i = keys.iterator(); i.hasNext() && !foundList; )
464 String key = i.next();
466 // a little aggressive with the repositoryScanning and databaseScanning - should be no need to split
467 // that configuration
468 if ( key.startsWith( "repositories" ) || key.startsWith( "proxyConnectors" ) || key.startsWith(
469 "networkProxies" ) || key.startsWith( "repositoryScanning" ) || key.startsWith(
470 "remoteRepositories" ) || key.startsWith( "managedRepositories" ) || key.startsWith(
471 "repositoryGroups" ) )
479 this.configuration = null;
481 throw new IndeterminateConfigurationException(
482 "Configuration can not be saved when it is loaded from two sources" );
486 // escape all cron expressions to handle ','
487 escapeCronExpressions( configuration );
489 // [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.
490 if ( configuration.getManagedRepositories().isEmpty() && section != null )
492 section.removeSubset( "managedRepositories" );
494 if ( configuration.getRemoteRepositories().isEmpty() && section != null )
496 section.removeSubset( "remoteRepositories" );
499 if ( configuration.getProxyConnectors().isEmpty() && section != null )
501 section.removeSubset( "proxyConnectors" );
503 if ( configuration.getNetworkProxies().isEmpty() && section != null )
505 section.removeSubset( "networkProxies" );
507 if ( configuration.getLegacyArtifactPaths().isEmpty() && section != null )
509 section.removeSubset( "legacyArtifactPaths" );
511 if ( configuration.getRepositoryGroups().isEmpty() && section != null )
513 section.removeSubset( "repositoryGroups" );
515 if ( configuration.getRepositoryScanning() != null )
517 if ( configuration.getRepositoryScanning().getKnownContentConsumers().isEmpty() && section != null )
519 section.removeSubset( "repositoryScanning.knownContentConsumers" );
521 if ( configuration.getRepositoryScanning().getInvalidContentConsumers().isEmpty() && section != null )
523 section.removeSubset( "repositoryScanning.invalidContentConsumers" );
527 new ConfigurationRegistryWriter().write( configuration, section );
530 this.configuration = unescapeExpressions( configuration );
532 triggerEvent( ConfigurationEvent.SAVED );
535 private void escapeCronExpressions( Configuration configuration )
537 for ( ManagedRepositoryConfiguration c : configuration.getManagedRepositories() )
539 c.setRefreshCronExpression( escapeCronExpression( c.getRefreshCronExpression() ) );
545 private Registry createDefaultConfigurationFile()
546 throws RegistryException
548 // TODO: may not be needed under commons-configuration 1.4 - check
550 String contents = "<configuration />";
552 String fileLocation = userConfigFilename;
554 if ( !writeFile( "user configuration", userConfigFilename, contents ) )
556 fileLocation = altConfigFilename;
557 if ( !writeFile( "alternative configuration", altConfigFilename, contents ) )
559 throw new RegistryException(
560 "Unable to create configuration file in either user [" + userConfigFilename + "] or alternative ["
562 + "] locations on disk, usually happens when not allowed to write to those locations." );
566 // olamy hackish I know :-)
567 contents = "<configuration><xml fileName=\"" + fileLocation
568 + "\" config-forceCreate=\"true\" config-name=\"org.apache.archiva.user\"/>" + "</configuration>";
570 ( (CommonsConfigurationRegistry) registry ).setProperties( contents );
572 registry.initialize();
574 for ( RegistryListener regListener : registryListeners )
576 addRegistryChangeListener( regListener );
579 triggerEvent( ConfigurationEvent.SAVED );
581 Registry section = registry.getSection( KEY + ".user" );
582 return section == null ? new CommonsConfigurationRegistry( new BaseConfiguration() ) : section;
586 * Attempts to write the contents to a file, if an IOException occurs, return false.
588 * The file will be created if the directory to the file exists, otherwise this will return false.
590 * @param filetype the filetype (freeform text) to use in logging messages when failure to write.
591 * @param path the path to write to.
592 * @param contents the contents to write.
593 * @return true if write successful.
595 private boolean writeFile( String filetype, String path, String contents )
597 File file = new File( path );
601 // Check parent directory (if it is declared)
602 if ( file.getParentFile() != null )
604 // Check that directory exists
605 if ( ! file.getParentFile().isDirectory() )
607 // Directory to file must exist for file to be created
612 FileUtils.writeStringToFile( file, contents, "UTF-8" );
615 catch ( IOException e )
617 log.error( "Unable to create " + filetype + " file: " + e.getMessage(), e );
622 private void triggerEvent( int type )
624 ConfigurationEvent evt = new ConfigurationEvent( type );
625 for ( ConfigurationListener listener : listeners )
627 listener.configurationEvent( evt );
632 public void addListener( ConfigurationListener listener )
634 if ( listener == null )
639 listeners.add( listener );
643 public void removeListener( ConfigurationListener listener )
645 if ( listener == null )
650 listeners.remove( listener );
654 public void addChangeListener( RegistryListener listener )
656 addRegistryChangeListener( listener );
658 // keep track for later
659 registryListeners.add( listener );
662 private void addRegistryChangeListener( RegistryListener listener )
664 Registry section = registry.getSection( KEY + ".user" );
665 if ( section != null )
667 section.addChangeListener( listener );
669 section = registry.getSection( KEY + ".base" );
670 if ( section != null )
672 section.addChangeListener( listener );
677 public void initialize()
680 this.postPolicies = componentContainer.buildMapWithRole( PostDownloadPolicy.class );
681 this.prePolicies = componentContainer.buildMapWithRole( PreDownloadPolicy.class );
682 this.downloadErrorPolicies = componentContainer.buildMapWithRole( DownloadErrorPolicy.class );
683 // Resolve expressions in the userConfigFilename and altConfigFilename
686 ExpressionEvaluator expressionEvaluator = new DefaultExpressionEvaluator();
687 expressionEvaluator.addExpressionSource( new SystemPropertyExpressionSource() );
688 String userConfigFileNameSysProps = System.getProperty( "archiva.user.configFileName" );
689 if ( StringUtils.isNotBlank( userConfigFileNameSysProps ) )
691 userConfigFilename = userConfigFileNameSysProps;
695 userConfigFilename = expressionEvaluator.expand( userConfigFilename );
697 altConfigFilename = expressionEvaluator.expand( altConfigFilename );
699 handleUpgradeConfiguration();
701 catch ( IndeterminateConfigurationException | RegistryException e )
703 throw new RuntimeException( "failed during upgrade from previous version" + e.getMessage(), e );
705 catch ( EvaluatorException e )
707 throw new RuntimeException(
708 "Unable to evaluate expressions found in " + "userConfigFilename or altConfigFilename.", e);
710 registry.addChangeListener( this );
716 private void handleUpgradeConfiguration()
717 throws RegistryException, IndeterminateConfigurationException
720 List<String> dbConsumers = Arrays.asList( "update-db-artifact", "update-db-repository-metadata" );
722 // remove database consumers if here
723 List<String> intersec =
724 ListUtils.intersection( dbConsumers, configuration.getRepositoryScanning().getKnownContentConsumers() );
726 if ( !intersec.isEmpty() )
729 List<String> knowContentConsumers =
730 new ArrayList<>( configuration.getRepositoryScanning().getKnownContentConsumers().size() );
731 for ( String knowContentConsumer : configuration.getRepositoryScanning().getKnownContentConsumers() )
733 if ( !dbConsumers.contains( knowContentConsumer ) )
735 knowContentConsumers.add( knowContentConsumer );
739 configuration.getRepositoryScanning().setKnownContentConsumers( knowContentConsumers );
742 // ensure create-archiva-metadata is here
743 if ( !configuration.getRepositoryScanning().getKnownContentConsumers().contains( "create-archiva-metadata" ) )
745 List<String> knowContentConsumers =
746 new ArrayList<>( configuration.getRepositoryScanning().getKnownContentConsumers() );
747 knowContentConsumers.add( "create-archiva-metadata" );
748 configuration.getRepositoryScanning().setKnownContentConsumers( knowContentConsumers );
751 // ensure duplicate-artifacts is here
752 if ( !configuration.getRepositoryScanning().getKnownContentConsumers().contains( "duplicate-artifacts" ) )
754 List<String> knowContentConsumers =
755 new ArrayList<>( configuration.getRepositoryScanning().getKnownContentConsumers() );
756 knowContentConsumers.add( "duplicate-artifacts" );
757 configuration.getRepositoryScanning().setKnownContentConsumers( knowContentConsumers );
760 //save( configuration );
766 this.configuration = null;
769 this.registry.initialize();
771 catch ( RegistryException e )
773 throw new ConfigurationRuntimeException( e.getMessage(), e );
779 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
781 // nothing to do here
785 public synchronized void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
787 configuration = null;
790 private String removeExpressions( String directory )
792 String value = StringUtils.replace( directory, "${appserver.base}",
793 registry.getString( "appserver.base", "${appserver.base}" ) );
794 value = StringUtils.replace( value, "${appserver.home}",
795 registry.getString( "appserver.home", "${appserver.home}" ) );
799 private String unescapeCronExpression( String cronExpression )
801 return StringUtils.replace( cronExpression, "\\,", "," );
804 private String escapeCronExpression( String cronExpression )
806 return StringUtils.replace( cronExpression, ",", "\\," );
809 private Configuration unescapeExpressions( Configuration config )
811 // TODO: for commons-configuration 1.3 only
812 for ( ManagedRepositoryConfiguration c : config.getManagedRepositories() ) {
813 c.setLocation( removeExpressions( c.getLocation() ) );
814 c.setRefreshCronExpression( unescapeCronExpression( c.getRefreshCronExpression() ) );
820 private Configuration checkRepositoryLocations( Configuration config )
822 // additional check for [MRM-789], ensure that the location of the default repositories
823 // are not installed in the server installation
824 for ( ManagedRepositoryConfiguration repo : (List<ManagedRepositoryConfiguration>) config.getManagedRepositories() )
826 String repoPath = repo.getLocation();
827 File repoLocation = new File( repoPath );
829 if ( repoLocation.exists() && repoLocation.isDirectory() && !repoPath.endsWith(
830 "data/repositories/" + repo.getId() ) )
832 repo.setLocation( repoPath + "/data/repositories/" + repo.getId() );
839 public String getUserConfigFilename()
841 return userConfigFilename;
844 public String getAltConfigFilename()
846 return altConfigFilename;
850 public boolean isDefaulted()
852 return this.isConfigurationDefaulted;
855 public Registry getRegistry()
860 public void setRegistry( Registry registry )
862 this.registry = registry;
866 public void setUserConfigFilename( String userConfigFilename )
868 this.userConfigFilename = userConfigFilename;
871 public void setAltConfigFilename( String altConfigFilename )
873 this.altConfigFilename = altConfigFilename;