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";
165 // Section used for default only configuration
166 private static final String KEY_DEFAULT_ONLY = "org.apache.archiva_default";
169 public Configuration getConfiguration()
171 return loadConfiguration();
174 private synchronized Configuration loadConfiguration()
176 if ( configuration == null )
178 configuration = load();
179 configuration = unescapeExpressions( configuration );
180 if ( isConfigurationDefaulted )
182 configuration = checkRepositoryLocations( configuration );
186 return configuration;
189 private boolean hasConfigVersionChanged(Configuration current, Registry defaultOnlyConfiguration) {
190 return current==null || current.getVersion()==null ||
191 !current.getVersion().trim().equals(defaultOnlyConfiguration.getString("version","").trim());
194 @SuppressWarnings("unchecked")
195 private Configuration load()
197 // TODO: should this be the same as section? make sure unnamed sections still work (eg, sys properties)
198 Registry subset = registry.getSubset( KEY );
199 if ( subset.getString( "version" ) == null )
201 // a little autodetection of v1, even if version is omitted (this was previously allowed)
202 if ( subset.getSubset( "repositoryScanning" ).isEmpty() )
204 // only for empty, or v < 1
205 subset = readDefaultConfiguration();
209 Configuration config = new ConfigurationRegistryReader().read( subset );
212 config.getRepositoryGroups();
213 config.getRepositoryGroupsAsMap();
214 if ( !config.getRepositories().isEmpty() )
216 for ( V1RepositoryConfiguration r : config.getRepositories() )
218 r.setScanned( r.isIndexed() );
220 if ( StringUtils.startsWith( r.getUrl(), "file://" ) )
222 r.setLocation( r.getUrl().substring( 7 ) );
223 config.addManagedRepository( r );
225 else if ( StringUtils.startsWith( r.getUrl(), "file:" ) )
227 r.setLocation( r.getUrl().substring( 5 ) );
228 config.addManagedRepository( r );
230 else if ( StringUtils.isEmpty( r.getUrl() ) )
232 // in case of empty url we can consider it as a managed one
233 // check if location is null
234 //file://${appserver.base}/repositories/${id}
235 if ( StringUtils.isEmpty( r.getLocation() ) )
237 r.setLocation( "file://${appserver.base}/repositories/" + r.getId() );
239 config.addManagedRepository( r );
243 RemoteRepositoryConfiguration repo = new RemoteRepositoryConfiguration();
244 repo.setId( r.getId() );
245 repo.setLayout( r.getLayout() );
246 repo.setName( r.getName() );
247 repo.setUrl( r.getUrl() );
248 config.addRemoteRepository( repo );
252 // Prevent duplicate repositories from showing up.
253 config.getRepositories().clear();
255 registry.removeSubset( KEY + ".repositories" );
258 if ( !CollectionUtils.isEmpty( config.getRemoteRepositories() ) )
260 List<RemoteRepositoryConfiguration> remoteRepos = config.getRemoteRepositories();
261 for ( RemoteRepositoryConfiguration repo : remoteRepos )
263 // [MRM-582] Remote Repositories with empty <username> and <password> fields shouldn't be created in configuration.
264 if ( StringUtils.isBlank( repo.getUsername() ) )
266 repo.setUsername( null );
269 if ( StringUtils.isBlank( repo.getPassword() ) )
271 repo.setPassword( null );
276 if ( !config.getProxyConnectors().isEmpty() )
278 // Fix Proxy Connector Settings.
280 // Create a copy of the list to read from (to prevent concurrent modification exceptions)
281 List<ProxyConnectorConfiguration> proxyConnectorList = new ArrayList<>( config.getProxyConnectors() );
282 // Remove the old connector list.
283 config.getProxyConnectors().clear();
285 for ( ProxyConnectorConfiguration connector : proxyConnectorList )
288 boolean connectorValid = true;
290 Map<String, String> policies = new HashMap<>();
291 // Make copy of policies
292 policies.putAll( connector.getPolicies() );
293 // Clear out policies
294 connector.getPolicies().clear();
296 // Work thru policies. cleaning them up.
297 for ( Entry<String, String> entry : policies.entrySet() )
299 String policyId = entry.getKey();
300 String setting = entry.getValue();
302 // Upgrade old policy settings.
303 if ( "releases".equals( policyId ) || "snapshots".equals( policyId ) )
305 if ( "ignored".equals( setting ) )
307 setting = AbstractUpdatePolicy.ALWAYS;
309 else if ( "disabled".equals( setting ) )
311 setting = AbstractUpdatePolicy.NEVER;
314 else if ( "cache-failures".equals( policyId ) )
316 if ( "ignored".equals( setting ) )
318 setting = CachedFailuresPolicy.NO;
320 else if ( "cached".equals( setting ) )
322 setting = CachedFailuresPolicy.YES;
325 else if ( "checksum".equals( policyId ) )
327 if ( "ignored".equals( setting ) )
329 setting = ChecksumPolicy.IGNORE;
333 // Validate existance of policy key.
334 if ( policyExists( policyId ) )
336 Policy policy = findPolicy( policyId );
337 // Does option exist?
338 if ( !policy.getOptions().contains( setting ) )
340 setting = policy.getDefaultOption();
342 connector.addPolicy( policyId, setting );
346 // Policy key doesn't exist. Don't add it to golden version.
347 log.warn( "Policy [{}] does not exist.", policyId );
351 if ( connectorValid )
353 config.addProxyConnector( connector );
357 // Normalize the order fields in the proxy connectors.
358 Map<String, java.util.List<ProxyConnectorConfiguration>> proxyConnectorMap =
359 config.getProxyConnectorAsMap();
361 for ( List<ProxyConnectorConfiguration> connectors : proxyConnectorMap.values() )
363 // Sort connectors by order field.
364 Collections.sort( connectors, ProxyConnectorConfigurationOrderComparator.getInstance() );
366 // Normalize the order field values.
368 for ( ProxyConnectorConfiguration connector : connectors )
370 connector.setOrder( order++ );
381 * Updates the checkpath list for repositories.
383 * We are replacing existing ones and adding new ones. This allows to update the list with new releases.
385 * We are also updating existing remote repositories, if they exist already.
387 * This update method should only be called, if the config version changes to avoid overwriting
388 * user repository settings all the time.
390 private void updateCheckPathDefaults(Configuration config, Registry defaultConfiguration) {
391 List<RepositoryCheckPath> existingCheckPathList = config.getArchivaDefaultConfiguration().getDefaultCheckPaths();
392 HashMap<String, RepositoryCheckPath> existingCheckPaths = new HashMap<>();
393 HashMap<String, RepositoryCheckPath> newCheckPaths = new HashMap<>();
394 for (RepositoryCheckPath path : config.getArchivaDefaultConfiguration().getDefaultCheckPaths()) {
395 existingCheckPaths.put(path.getUrl(), path);
397 List defaultCheckPathsSubsets = defaultConfiguration.getSubsetList("archivaDefaultConfiguration.defaultCheckPaths.defaultCheckPath" );
398 for ( Iterator i = defaultCheckPathsSubsets.iterator(); i.hasNext(); )
400 RepositoryCheckPath v = readRepositoryCheckPath( (Registry) i.next() );
401 if (existingCheckPaths.containsKey(v.getUrl())) {
402 existingCheckPathList.remove(existingCheckPaths.get(v.getUrl()));
404 existingCheckPathList.add(v);
405 newCheckPaths.put(v.getUrl(), v);
407 // Remote repositories update
408 for (RemoteRepositoryConfiguration remoteRepositoryConfiguration : config.getRemoteRepositories()) {
409 String url = remoteRepositoryConfiguration.getUrl().toLowerCase();
410 if (newCheckPaths.containsKey(url)) {
411 String currentPath = remoteRepositoryConfiguration.getCheckPath();
412 String newPath = newCheckPaths.get(url).getPath();
413 log.info("Updating connection check path for repository {}, from '{}' to '{}'.", remoteRepositoryConfiguration.getId(),
414 currentPath, newPath);
415 remoteRepositoryConfiguration.setCheckPath(newPath);
420 private RepositoryCheckPath readRepositoryCheckPath( Registry registry )
422 RepositoryCheckPath value = new RepositoryCheckPath();
424 String url = registry.getString( "url", value.getUrl() );
427 String path = registry.getString( "path", value.getPath() );
428 value.setPath( path );
432 private Policy findPolicy( String policyId )
434 if ( MapUtils.isEmpty( prePolicies ) )
436 log.error( "No PreDownloadPolicies found!" );
440 if ( MapUtils.isEmpty( postPolicies ) )
442 log.error( "No PostDownloadPolicies found!" );
448 policy = prePolicies.get( policyId );
449 if ( policy != null )
454 policy = postPolicies.get( policyId );
455 if ( policy != null )
460 policy = downloadErrorPolicies.get( policyId );
461 if ( policy != null )
469 private boolean policyExists( String policyId )
471 if ( MapUtils.isEmpty( prePolicies ) )
473 log.error( "No PreDownloadPolicies found!" );
477 if ( MapUtils.isEmpty( postPolicies ) )
479 log.error( "No PostDownloadPolicies found!" );
483 return ( prePolicies.containsKey( policyId ) || postPolicies.containsKey( policyId )
484 || downloadErrorPolicies.containsKey( policyId ) );
487 private Registry readDefaultConfiguration()
489 // if it contains some old configuration, remove it (Archiva 0.9)
490 registry.removeSubset( KEY );
494 registry.addConfigurationFromResource( "org/apache/archiva/configuration/default-archiva.xml", KEY );
495 this.isConfigurationDefaulted = true;
497 catch ( RegistryException e )
499 throw new ConfigurationRuntimeException(
500 "Fatal error: Unable to find the built-in default configuration and load it into the registry", e );
502 return registry.getSubset( KEY );
506 * Reads the default only configuration into a special prefix. This allows to check for changes
507 * of the default configuration.
509 private Registry readDefaultOnlyConfiguration()
511 registry.removeSubset(KEY_DEFAULT_ONLY);
514 registry.addConfigurationFromResource( "org/apache/archiva/configuration/default-archiva.xml", KEY_DEFAULT_ONLY);
516 catch ( RegistryException e )
518 throw new ConfigurationRuntimeException(
519 "Fatal error: Unable to find the built-in default configuration and load it into the registry", e );
521 return registry.getSubset(KEY_DEFAULT_ONLY);
524 @SuppressWarnings("unchecked")
526 public synchronized void save( Configuration configuration )
527 throws IndeterminateConfigurationException, RegistryException
529 Registry section = registry.getSection( KEY + ".user" );
530 Registry baseSection = registry.getSection( KEY + ".base" );
531 if ( section == null )
533 section = baseSection;
534 if ( section == null )
536 section = createDefaultConfigurationFile();
539 else if ( baseSection != null )
541 Collection<String> keys = baseSection.getKeys();
542 boolean foundList = false;
543 for ( Iterator<String> i = keys.iterator(); i.hasNext() && !foundList; )
545 String key = i.next();
547 // a little aggressive with the repositoryScanning and databaseScanning - should be no need to split
548 // that configuration
549 if ( key.startsWith( "repositories" ) //
550 || key.startsWith( "proxyConnectors" ) //
551 || key.startsWith( "networkProxies" ) //
552 || key.startsWith( "repositoryScanning" ) //
553 || key.startsWith( "remoteRepositories" ) //
554 || key.startsWith( "managedRepositories" ) //
555 || key.startsWith( "repositoryGroups" ) ) //
563 this.configuration = null;
565 throw new IndeterminateConfigurationException(
566 "Configuration can not be saved when it is loaded from two sources" );
570 // escape all cron expressions to handle ','
571 escapeCronExpressions( configuration );
573 // [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.
574 if ( section != null )
576 if ( configuration.getManagedRepositories().isEmpty() )
578 section.removeSubset( "managedRepositories" );
580 if ( configuration.getRemoteRepositories().isEmpty() )
582 section.removeSubset( "remoteRepositories" );
585 if ( configuration.getProxyConnectors().isEmpty() )
587 section.removeSubset( "proxyConnectors" );
589 if ( configuration.getNetworkProxies().isEmpty() )
591 section.removeSubset( "networkProxies" );
593 if ( configuration.getLegacyArtifactPaths().isEmpty() )
595 section.removeSubset( "legacyArtifactPaths" );
597 if ( configuration.getRepositoryGroups().isEmpty() )
599 section.removeSubset( "repositoryGroups" );
601 if ( configuration.getRepositoryScanning() != null )
603 if ( configuration.getRepositoryScanning().getKnownContentConsumers().isEmpty() )
605 section.removeSubset( "repositoryScanning.knownContentConsumers" );
607 if ( configuration.getRepositoryScanning().getInvalidContentConsumers().isEmpty() )
609 section.removeSubset( "repositoryScanning.invalidContentConsumers" );
612 if (configuration.getArchivaRuntimeConfiguration()!=null) {
613 section.removeSubset("archivaRuntimeConfiguration.defaultCheckPaths");
616 new ConfigurationRegistryWriter().write( configuration, section );
622 this.configuration = unescapeExpressions( configuration );
624 triggerEvent( ConfigurationEvent.SAVED );
627 private void escapeCronExpressions( Configuration configuration )
629 for ( ManagedRepositoryConfiguration c : configuration.getManagedRepositories() )
631 c.setRefreshCronExpression( escapeCronExpression( c.getRefreshCronExpression() ) );
635 private Registry createDefaultConfigurationFile()
636 throws RegistryException
638 // TODO: may not be needed under commons-configuration 1.4 - check
640 String contents = "<configuration />";
642 String fileLocation = userConfigFilename;
644 if ( !writeFile( "user configuration", userConfigFilename, contents ) )
646 fileLocation = altConfigFilename;
647 if ( !writeFile( "alternative configuration", altConfigFilename, contents ) )
649 throw new RegistryException(
650 "Unable to create configuration file in either user [" + userConfigFilename + "] or alternative ["
652 + "] locations on disk, usually happens when not allowed to write to those locations." );
656 // olamy hackish I know :-)
657 contents = "<configuration><xml fileName=\"" + fileLocation
658 + "\" config-forceCreate=\"true\" config-name=\"org.apache.archiva.user\"/>" + "</configuration>";
660 ( (CommonsConfigurationRegistry) registry ).setProperties( contents );
662 registry.initialize();
664 for ( RegistryListener regListener : registryListeners )
666 addRegistryChangeListener( regListener );
669 triggerEvent( ConfigurationEvent.SAVED );
671 Registry section = registry.getSection( KEY + ".user" );
672 return section == null ? new CommonsConfigurationRegistry( new BaseConfiguration() ) : section;
676 * Attempts to write the contents to a file, if an IOException occurs, return false.
678 * The file will be created if the directory to the file exists, otherwise this will return false.
680 * @param filetype the filetype (freeform text) to use in logging messages when failure to write.
681 * @param path the path to write to.
682 * @param contents the contents to write.
683 * @return true if write successful.
685 private boolean writeFile( String filetype, String path, String contents )
687 File file = new File( path );
691 // Check parent directory (if it is declared)
692 if ( file.getParentFile() != null )
694 // Check that directory exists
695 if ( !file.getParentFile().isDirectory() )
697 // Directory to file must exist for file to be created
701 FileUtils.writeStringToFile( file, contents, "UTF-8" );
704 catch ( IOException e )
706 log.error( "Unable to create " + filetype + " file: " + e.getMessage(), e );
711 private void triggerEvent( int type )
713 ConfigurationEvent evt = new ConfigurationEvent( type );
714 for ( ConfigurationListener listener : listeners )
716 listener.configurationEvent( evt );
721 public void addListener( ConfigurationListener listener )
723 if ( listener == null )
728 listeners.add( listener );
732 public void removeListener( ConfigurationListener listener )
734 if ( listener == null )
739 listeners.remove( listener );
744 public void addChangeListener( RegistryListener listener )
746 addRegistryChangeListener( listener );
748 // keep track for later
749 registryListeners.add( listener );
752 private void addRegistryChangeListener( RegistryListener listener )
754 Registry section = registry.getSection( KEY + ".user" );
755 if ( section != null )
757 section.addChangeListener( listener );
759 section = registry.getSection( KEY + ".base" );
760 if ( section != null )
762 section.addChangeListener( listener );
767 public void removeChangeListener( RegistryListener listener )
769 boolean removed = registryListeners.remove( listener );
770 log.debug( "RegistryListener: '{}' removed {}", listener, removed );
772 Registry section = registry.getSection( KEY + ".user" );
773 if ( section != null )
775 section.removeChangeListener( listener );
777 section = registry.getSection( KEY + ".base" );
778 if ( section != null )
780 section.removeChangeListener( listener );
786 public void initialize()
789 this.postPolicies = componentContainer.buildMapWithRole( PostDownloadPolicy.class );
790 this.prePolicies = componentContainer.buildMapWithRole( PreDownloadPolicy.class );
791 this.downloadErrorPolicies = componentContainer.buildMapWithRole( DownloadErrorPolicy.class );
792 // Resolve expressions in the userConfigFilename and altConfigFilename
795 ExpressionEvaluator expressionEvaluator = new DefaultExpressionEvaluator();
796 expressionEvaluator.addExpressionSource( new SystemPropertyExpressionSource() );
797 String userConfigFileNameSysProps = System.getProperty( "archiva.user.configFileName" );
798 if ( StringUtils.isNotBlank( userConfigFileNameSysProps ) )
800 userConfigFilename = userConfigFileNameSysProps;
804 userConfigFilename = expressionEvaluator.expand( userConfigFilename );
806 altConfigFilename = expressionEvaluator.expand( altConfigFilename );
808 handleUpgradeConfiguration();
810 catch ( IndeterminateConfigurationException | RegistryException e )
812 throw new RuntimeException( "failed during upgrade from previous version" + e.getMessage(), e );
814 catch ( EvaluatorException e )
816 throw new RuntimeException(
817 "Unable to evaluate expressions found in " + "userConfigFilename or altConfigFilename.", e );
819 registry.addChangeListener( this );
823 * Handle upgrade to newer version
825 private void handleUpgradeConfiguration()
826 throws RegistryException, IndeterminateConfigurationException
829 List<String> dbConsumers = Arrays.asList( "update-db-artifact", "update-db-repository-metadata" );
831 // remove database consumers if here
832 List<String> intersec =
833 ListUtils.intersection( dbConsumers, configuration.getRepositoryScanning().getKnownContentConsumers() );
835 if ( !intersec.isEmpty() )
838 List<String> knowContentConsumers =
839 new ArrayList<>( configuration.getRepositoryScanning().getKnownContentConsumers().size() );
840 for ( String knowContentConsumer : configuration.getRepositoryScanning().getKnownContentConsumers() )
842 if ( !dbConsumers.contains( knowContentConsumer ) )
844 knowContentConsumers.add( knowContentConsumer );
848 configuration.getRepositoryScanning().setKnownContentConsumers( knowContentConsumers );
851 // ensure create-archiva-metadata is here
852 if ( !configuration.getRepositoryScanning().getKnownContentConsumers().contains( "create-archiva-metadata" ) )
854 List<String> knowContentConsumers =
855 new ArrayList<>( configuration.getRepositoryScanning().getKnownContentConsumers() );
856 knowContentConsumers.add( "create-archiva-metadata" );
857 configuration.getRepositoryScanning().setKnownContentConsumers( knowContentConsumers );
860 // ensure duplicate-artifacts is here
861 if ( !configuration.getRepositoryScanning().getKnownContentConsumers().contains( "duplicate-artifacts" ) )
863 List<String> knowContentConsumers =
864 new ArrayList<>( configuration.getRepositoryScanning().getKnownContentConsumers() );
865 knowContentConsumers.add( "duplicate-artifacts" );
866 configuration.getRepositoryScanning().setKnownContentConsumers( knowContentConsumers );
869 Registry defaultOnlyConfiguration = readDefaultOnlyConfiguration();
870 // Currently we check only for configuration version change, not certain version numbers.
871 if (hasConfigVersionChanged(configuration, defaultOnlyConfiguration)) {
872 updateCheckPathDefaults(configuration, defaultOnlyConfiguration);
873 String newVersion = defaultOnlyConfiguration.getString("version");
874 if (newVersion==null) {
875 throw new IndeterminateConfigurationException("The default configuration has no version information!");
877 configuration.setVersion(newVersion);
880 } catch (IndeterminateConfigurationException e) {
881 log.error("Error occured during configuration update to new version: {}", e.getMessage());
882 } catch (RegistryException e) {
883 log.error("Error occured during configuration update to new version: {}", e.getMessage());
891 this.configuration = null;
894 this.registry.initialize();
896 catch ( RegistryException e )
898 throw new ConfigurationRuntimeException( e.getMessage(), e );
904 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
906 // nothing to do here
910 public synchronized void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
912 configuration = null;
915 private String removeExpressions( String directory )
917 String value = StringUtils.replace( directory, "${appserver.base}",
918 registry.getString( "appserver.base", "${appserver.base}" ) );
919 value = StringUtils.replace( value, "${appserver.home}",
920 registry.getString( "appserver.home", "${appserver.home}" ) );
924 private String unescapeCronExpression( String cronExpression )
926 return StringUtils.replace( cronExpression, "\\,", "," );
929 private String escapeCronExpression( String cronExpression )
931 return StringUtils.replace( cronExpression, ",", "\\," );
934 private Configuration unescapeExpressions( Configuration config )
936 // TODO: for commons-configuration 1.3 only
937 for ( ManagedRepositoryConfiguration c : config.getManagedRepositories() )
939 c.setLocation( removeExpressions( c.getLocation() ) );
940 c.setRefreshCronExpression( unescapeCronExpression( c.getRefreshCronExpression() ) );
946 private Configuration checkRepositoryLocations( Configuration config )
948 // additional check for [MRM-789], ensure that the location of the default repositories
949 // are not installed in the server installation
950 for ( ManagedRepositoryConfiguration repo : (List<ManagedRepositoryConfiguration>) config.getManagedRepositories() )
952 String repoPath = repo.getLocation();
953 File repoLocation = new File( repoPath );
955 if ( repoLocation.exists() && repoLocation.isDirectory() && !repoPath.endsWith(
956 "data/repositories/" + repo.getId() ) )
958 repo.setLocation( repoPath + "/data/repositories/" + repo.getId() );
965 public String getUserConfigFilename()
967 return userConfigFilename;
970 public String getAltConfigFilename()
972 return altConfigFilename;
976 public boolean isDefaulted()
978 return this.isConfigurationDefaulted;
981 public Registry getRegistry()
986 public void setRegistry( Registry registry )
988 this.registry = registry;
992 public void setUserConfigFilename( String userConfigFilename )
994 this.userConfigFilename = userConfigFilename;
997 public void setAltConfigFilename( String altConfigFilename )
999 this.altConfigFilename = altConfigFilename;