]> source.dussan.org Git - archiva.git/blob
6373a503b2551a3a0f5ad54fefc72d5865043339
[archiva.git] /
1 package org.apache.archiva.configuration;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
50
51 import javax.annotation.PostConstruct;
52 import javax.inject.Inject;
53 import javax.inject.Named;
54 import java.io.File;
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;
64 import java.util.Map;
65 import java.util.Map.Entry;
66 import java.util.Set;
67
68 /**
69  * <p>
70  * Implementation of configuration holder that retrieves it from the registry.
71  * </p>
72  * <p>
73  * The registry layers and merges the 2 configuration files: user, and application server.
74  * </p>
75  * <p>
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.
79  * </p>
80  * <p>
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.
86  * </p>
87  * <p>
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.
90  * </p>
91  */
92 @Service ( "archivaConfiguration#default" )
93 public class DefaultArchivaConfiguration
94     implements ArchivaConfiguration, RegistryListener
95 {
96     private Logger log = LoggerFactory.getLogger( DefaultArchivaConfiguration.class );
97
98     /**
99      * Plexus registry to read the configuration from.
100      */
101     @Inject
102     @Named ( value = "commons-configuration" )
103     private Registry registry;
104
105     @Inject
106     private ComponentContainer componentContainer;
107
108     /**
109      * The configuration that has been converted.
110      */
111     private Configuration configuration;
112
113     /**
114      * see #initialize
115      *
116      * @todo these don't strictly belong in here
117      */
118     private Map<String, PreDownloadPolicy> prePolicies;
119
120     /**
121      * see #initialize
122      *
123      * @todo these don't strictly belong in here
124      */
125     private Map<String, PostDownloadPolicy> postPolicies;
126
127     /**
128      * see #initialize
129      *
130      * @todo these don't strictly belong in here
131      */
132     private Map<String, DownloadErrorPolicy> downloadErrorPolicies;
133
134
135     /**
136      * see #initialize
137      * default-value="${user.home}/.m2/archiva.xml"
138      */
139     private String userConfigFilename = "${user.home}/.m2/archiva.xml";
140
141     /**
142      * see #initialize
143      * default-value="${appserver.base}/conf/archiva.xml"
144      */
145     private String altConfigFilename = "${appserver.base}/conf/archiva.xml";
146
147     /**
148      * Configuration Listeners we've registered.
149      */
150     private Set<ConfigurationListener> listeners = new HashSet<>();
151
152     /**
153      * Registry Listeners we've registered.
154      */
155     private Set<RegistryListener> registryListeners = new HashSet<>();
156
157     /**
158      * Boolean to help determine if the configuration exists as a result of pulling in
159      * the default-archiva.xml
160      */
161     private boolean isConfigurationDefaulted = false;
162
163     private static final String KEY = "org.apache.archiva";
164
165     @Override
166     public Configuration getConfiguration()
167     {
168         return loadConfiguration();
169     }
170
171     private synchronized Configuration loadConfiguration()
172     {
173         if ( configuration == null )
174         {
175             configuration = load();
176             configuration = unescapeExpressions( configuration );
177             if ( isConfigurationDefaulted )
178             {
179                 configuration = checkRepositoryLocations( configuration );
180             }
181         }
182
183         return configuration;
184     }
185
186     @SuppressWarnings ( "unchecked" )
187     private Configuration load()
188     {
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 )
192         {
193             // a little autodetection of v1, even if version is omitted (this was previously allowed)
194             if ( subset.getSubset( "repositoryScanning" ).isEmpty() )
195             {
196                 // only for empty, or v < 1
197                 subset = readDefaultConfiguration();
198             }
199         }
200
201         Configuration config = new ConfigurationRegistryReader().read( subset );
202
203         config.getRepositoryGroups();
204         config.getRepositoryGroupsAsMap();
205         if ( !config.getRepositories().isEmpty() )
206         {
207             for ( V1RepositoryConfiguration r : config.getRepositories() ) 
208             {
209                 r.setScanned( r.isIndexed() );
210
211                 if ( StringUtils.startsWith( r.getUrl(), "file://" ) )
212                 {
213                     r.setLocation( r.getUrl().substring( 7 ) );
214                     config.addManagedRepository( r );
215                 }
216                 else if ( StringUtils.startsWith( r.getUrl(), "file:" ) )
217                 {
218                     r.setLocation( r.getUrl().substring( 5 ) );
219                     config.addManagedRepository( r );
220                 }
221                 else if ( StringUtils.isEmpty( r.getUrl() ) )
222                 {
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() ) )
227                     {
228                         r.setLocation( "file://${appserver.base}/repositories/" + r.getId() );
229                     }
230                     config.addManagedRepository( r );
231                 }
232                 else
233                 {
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 );
240                 }
241             }
242
243             // Prevent duplicate repositories from showing up.
244             config.getRepositories().clear();
245
246             registry.removeSubset( KEY + ".repositories" );
247         }
248
249         if ( !CollectionUtils.isEmpty( config.getRemoteRepositories() ) )
250         {
251             List<RemoteRepositoryConfiguration> remoteRepos = config.getRemoteRepositories();
252             for ( RemoteRepositoryConfiguration repo : remoteRepos )
253             {
254                 // [MRM-582] Remote Repositories with empty <username> and <password> fields shouldn't be created in configuration.
255                 if ( StringUtils.isBlank( repo.getUsername() ) )
256                 {
257                     repo.setUsername( null );
258                 }
259
260                 if ( StringUtils.isBlank( repo.getPassword() ) )
261                 {
262                     repo.setPassword( null );
263                 }
264             }
265         }
266
267         if ( !config.getProxyConnectors().isEmpty() )
268         {
269             // Fix Proxy Connector Settings.
270
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();
276
277             for ( ProxyConnectorConfiguration connector : proxyConnectorList )
278             {
279                 // Fix policies
280                 boolean connectorValid = true;
281
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();
287
288                 // Work thru policies. cleaning them up.
289                 for ( Entry<String, String> entry : policies.entrySet() )
290                 {
291                     String policyId = entry.getKey();
292                     String setting = entry.getValue();
293
294                     // Upgrade old policy settings.
295                     if ( "releases".equals( policyId ) || "snapshots".equals( policyId ) )
296                     {
297                         if ( "ignored".equals( setting ) )
298                         {
299                             setting = AbstractUpdatePolicy.ALWAYS;
300                         }
301                         else if ( "disabled".equals( setting ) )
302                         {
303                             setting = AbstractUpdatePolicy.NEVER;
304                         }
305                     }
306                     else if ( "cache-failures".equals( policyId ) )
307                     {
308                         if ( "ignored".equals( setting ) )
309                         {
310                             setting = CachedFailuresPolicy.NO;
311                         }
312                         else if ( "cached".equals( setting ) )
313                         {
314                             setting = CachedFailuresPolicy.YES;
315                         }
316                     }
317                     else if ( "checksum".equals( policyId ) )
318                     {
319                         if ( "ignored".equals( setting ) )
320                         {
321                             setting = ChecksumPolicy.IGNORE;
322                         }
323                     }
324
325                     // Validate existance of policy key.
326                     if ( policyExists( policyId ) )
327                     {
328                         Policy policy = findPolicy( policyId );
329                         // Does option exist?
330                         if ( !policy.getOptions().contains( setting ) )
331                         {
332                             setting = policy.getDefaultOption();
333                         }
334                         connector.addPolicy( policyId, setting );
335                     }
336                     else
337                     {
338                         // Policy key doesn't exist. Don't add it to golden version.
339                         log.warn( "Policy [{}] does not exist.", policyId );
340                     }
341                 }
342
343                 if ( connectorValid )
344                 {
345                     config.addProxyConnector( connector );
346                 }
347             }
348
349             // Normalize the order fields in the proxy connectors.
350             Map<String, java.util.List<ProxyConnectorConfiguration>> proxyConnectorMap =
351                 config.getProxyConnectorAsMap();
352
353             for ( List<ProxyConnectorConfiguration> connectors : proxyConnectorMap.values() )
354             {
355                 // Sort connectors by order field.
356                 Collections.sort( connectors, ProxyConnectorConfigurationOrderComparator.getInstance() );
357
358                 // Normalize the order field values.
359                 int order = 1;
360                 for ( ProxyConnectorConfiguration connector : connectors )
361                 {
362                     connector.setOrder( order++ );
363                 }
364             }
365         }
366
367         return config;
368     }
369
370     private Policy findPolicy( String policyId )
371     {
372         if ( MapUtils.isEmpty( prePolicies ) )
373         {
374             log.error( "No PreDownloadPolicies found!" );
375             return null;
376         }
377
378         if ( MapUtils.isEmpty( postPolicies ) )
379         {
380             log.error( "No PostDownloadPolicies found!" );
381             return null;
382         }
383
384         Policy policy;
385
386         policy = prePolicies.get( policyId );
387         if ( policy != null )
388         {
389             return policy;
390         }
391
392         policy = postPolicies.get( policyId );
393         if ( policy != null )
394         {
395             return policy;
396         }
397
398         policy = downloadErrorPolicies.get( policyId );
399         if ( policy != null )
400         {
401             return policy;
402         }
403
404         return null;
405     }
406
407     private boolean policyExists( String policyId )
408     {
409         if ( MapUtils.isEmpty( prePolicies ) )
410         {
411             log.error( "No PreDownloadPolicies found!" );
412             return false;
413         }
414
415         if ( MapUtils.isEmpty( postPolicies ) )
416         {
417             log.error( "No PostDownloadPolicies found!" );
418             return false;
419         }
420
421         return ( prePolicies.containsKey( policyId ) || postPolicies.containsKey( policyId )
422             || downloadErrorPolicies.containsKey( policyId ) );
423     }
424
425     private Registry readDefaultConfiguration()
426     {
427         // if it contains some old configuration, remove it (Archiva 0.9)
428         registry.removeSubset( KEY );
429
430         try
431         {
432             registry.addConfigurationFromResource( "org/apache/archiva/configuration/default-archiva.xml", KEY );
433             this.isConfigurationDefaulted = true;
434         }
435         catch ( RegistryException e )
436         {
437             throw new ConfigurationRuntimeException(
438                 "Fatal error: Unable to find the built-in default configuration and load it into the registry", e );
439         }
440         return registry.getSubset( KEY );
441     }
442
443     @SuppressWarnings ( "unchecked" )
444     @Override
445     public synchronized void save( Configuration configuration )
446         throws IndeterminateConfigurationException, RegistryException
447     {
448         Registry section = registry.getSection( KEY + ".user" );
449         Registry baseSection = registry.getSection( KEY + ".base" );
450         if ( section == null )
451         {
452             section = baseSection;
453             if ( section == null )
454             {
455                 section = createDefaultConfigurationFile();
456             }
457         }
458         else if ( baseSection != null )
459         {
460             Collection<String> keys = baseSection.getKeys();
461             boolean foundList = false;
462             for ( Iterator<String> i = keys.iterator(); i.hasNext() && !foundList; )
463             {
464                 String key = i.next();
465
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" ) )
472                 {
473                     foundList = true;
474                 }
475             }
476
477             if ( foundList )
478             {
479                 this.configuration = null;
480
481                 throw new IndeterminateConfigurationException(
482                     "Configuration can not be saved when it is loaded from two sources" );
483             }
484         }
485
486         // escape all cron expressions to handle ','
487         escapeCronExpressions( configuration );
488
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 )
491         {
492             section.removeSubset( "managedRepositories" );
493         }
494         if ( configuration.getRemoteRepositories().isEmpty() && section != null )
495         {
496             section.removeSubset( "remoteRepositories" );
497
498         }
499         if ( configuration.getProxyConnectors().isEmpty() && section != null )
500         {
501             section.removeSubset( "proxyConnectors" );
502         }
503         if ( configuration.getNetworkProxies().isEmpty() && section != null )
504         {
505             section.removeSubset( "networkProxies" );
506         }
507         if ( configuration.getLegacyArtifactPaths().isEmpty() && section != null )
508         {
509             section.removeSubset( "legacyArtifactPaths" );
510         }
511         if ( configuration.getRepositoryGroups().isEmpty() && section != null )
512         {
513             section.removeSubset( "repositoryGroups" );
514         }
515         if ( configuration.getRepositoryScanning() != null )
516         {
517             if ( configuration.getRepositoryScanning().getKnownContentConsumers().isEmpty() && section != null )
518             {
519                 section.removeSubset( "repositoryScanning.knownContentConsumers" );
520             }
521             if ( configuration.getRepositoryScanning().getInvalidContentConsumers().isEmpty() && section != null )
522             {
523                 section.removeSubset( "repositoryScanning.invalidContentConsumers" );
524             }
525         }
526
527         new ConfigurationRegistryWriter().write( configuration, section );
528         section.save();
529
530         this.configuration = unescapeExpressions( configuration );
531
532         triggerEvent( ConfigurationEvent.SAVED );
533     }
534
535     private void escapeCronExpressions( Configuration configuration )
536     {
537         for ( ManagedRepositoryConfiguration c : configuration.getManagedRepositories() )
538         {
539             c.setRefreshCronExpression( escapeCronExpression( c.getRefreshCronExpression() ) );
540         }
541
542
543     }
544
545     private Registry createDefaultConfigurationFile()
546         throws RegistryException
547     {
548         // TODO: may not be needed under commons-configuration 1.4 - check
549
550         String contents = "<configuration />";
551
552         String fileLocation = userConfigFilename;
553
554         if ( !writeFile( "user configuration", userConfigFilename, contents ) )
555         {
556             fileLocation = altConfigFilename;
557             if ( !writeFile( "alternative configuration", altConfigFilename, contents ) )
558             {
559                 throw new RegistryException(
560                     "Unable to create configuration file in either user [" + userConfigFilename + "] or alternative ["
561                         + altConfigFilename
562                         + "] locations on disk, usually happens when not allowed to write to those locations." );
563             }
564         }
565
566         // olamy hackish I know :-)
567         contents = "<configuration><xml fileName=\"" + fileLocation
568             + "\" config-forceCreate=\"true\" config-name=\"org.apache.archiva.user\"/>" + "</configuration>";
569
570         ( (CommonsConfigurationRegistry) registry ).setProperties( contents );
571
572         registry.initialize();
573
574         for ( RegistryListener regListener : registryListeners )
575         {
576             addRegistryChangeListener( regListener );
577         }
578
579         triggerEvent( ConfigurationEvent.SAVED );
580
581         Registry section = registry.getSection( KEY + ".user" );
582         return section == null ? new CommonsConfigurationRegistry( new BaseConfiguration() ) : section;
583     }
584
585     /**
586      * Attempts to write the contents to a file, if an IOException occurs, return false.
587      * <p>
588      * The file will be created if the directory to the file exists, otherwise this will return false.
589      *
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.
594      */
595     private boolean writeFile( String filetype, String path, String contents )
596     {
597         File file = new File( path );
598
599         try
600         {
601             // Check parent directory (if it is declared)
602             if ( file.getParentFile() != null )
603             {
604                 // Check that directory exists
605                 if ( ! file.getParentFile().isDirectory() )
606                 {
607                     // Directory to file must exist for file to be created
608                     return false;
609                 }
610             }
611
612             FileUtils.writeStringToFile( file, contents, "UTF-8" );
613             return true;
614         }
615         catch ( IOException e )
616         {
617             log.error( "Unable to create " + filetype + " file: " + e.getMessage(), e );
618             return false;
619         }
620     }
621
622     private void triggerEvent( int type )
623     {
624         ConfigurationEvent evt = new ConfigurationEvent( type );
625         for ( ConfigurationListener listener : listeners )
626         {
627             listener.configurationEvent( evt );
628         }
629     }
630
631     @Override
632     public void addListener( ConfigurationListener listener )
633     {
634         if ( listener == null )
635         {
636             return;
637         }
638
639         listeners.add( listener );
640     }
641
642     @Override
643     public void removeListener( ConfigurationListener listener )
644     {
645         if ( listener == null )
646         {
647             return;
648         }
649
650         listeners.remove( listener );
651     }
652
653     @Override
654     public void addChangeListener( RegistryListener listener )
655     {
656         addRegistryChangeListener( listener );
657
658         // keep track for later
659         registryListeners.add( listener );
660     }
661
662     private void addRegistryChangeListener( RegistryListener listener )
663     {
664         Registry section = registry.getSection( KEY + ".user" );
665         if ( section != null )
666         {
667             section.addChangeListener( listener );
668         }
669         section = registry.getSection( KEY + ".base" );
670         if ( section != null )
671         {
672             section.addChangeListener( listener );
673         }
674     }
675
676     @PostConstruct
677     public void initialize()
678     {
679
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
684         try
685         {
686             ExpressionEvaluator expressionEvaluator = new DefaultExpressionEvaluator();
687             expressionEvaluator.addExpressionSource( new SystemPropertyExpressionSource() );
688             String userConfigFileNameSysProps = System.getProperty( "archiva.user.configFileName" );
689             if ( StringUtils.isNotBlank( userConfigFileNameSysProps ) )
690             {
691                 userConfigFilename = userConfigFileNameSysProps;
692             }
693             else
694             {
695                 userConfigFilename = expressionEvaluator.expand( userConfigFilename );
696             }
697             altConfigFilename = expressionEvaluator.expand( altConfigFilename );
698             loadConfiguration();
699             handleUpgradeConfiguration();
700         }
701         catch ( IndeterminateConfigurationException | RegistryException e )
702         {
703             throw new RuntimeException( "failed during upgrade from previous version" + e.getMessage(), e );
704         }
705         catch ( EvaluatorException e )
706         {
707             throw new RuntimeException(
708                 "Unable to evaluate expressions found in " + "userConfigFilename or altConfigFilename.", e);
709         }
710         registry.addChangeListener( this );
711     }
712
713     /**
714      * upgrade from 1.3
715      */
716     private void handleUpgradeConfiguration()
717         throws RegistryException, IndeterminateConfigurationException
718     {
719
720         List<String> dbConsumers = Arrays.asList( "update-db-artifact", "update-db-repository-metadata" );
721
722         // remove database consumers if here
723         List<String> intersec =
724             ListUtils.intersection( dbConsumers, configuration.getRepositoryScanning().getKnownContentConsumers() );
725
726         if ( !intersec.isEmpty() )
727         {
728
729             List<String> knowContentConsumers =
730                 new ArrayList<>( configuration.getRepositoryScanning().getKnownContentConsumers().size() );
731             for ( String knowContentConsumer : configuration.getRepositoryScanning().getKnownContentConsumers() )
732             {
733                 if ( !dbConsumers.contains( knowContentConsumer ) )
734                 {
735                     knowContentConsumers.add( knowContentConsumer );
736                 }
737             }
738
739             configuration.getRepositoryScanning().setKnownContentConsumers( knowContentConsumers );
740         }
741
742         // ensure create-archiva-metadata is here
743         if ( !configuration.getRepositoryScanning().getKnownContentConsumers().contains( "create-archiva-metadata" ) )
744         {
745             List<String> knowContentConsumers =
746                 new ArrayList<>( configuration.getRepositoryScanning().getKnownContentConsumers() );
747             knowContentConsumers.add( "create-archiva-metadata" );
748             configuration.getRepositoryScanning().setKnownContentConsumers( knowContentConsumers );
749         }
750
751         // ensure duplicate-artifacts is here
752         if ( !configuration.getRepositoryScanning().getKnownContentConsumers().contains( "duplicate-artifacts" ) )
753         {
754             List<String> knowContentConsumers =
755                 new ArrayList<>( configuration.getRepositoryScanning().getKnownContentConsumers() );
756             knowContentConsumers.add( "duplicate-artifacts" );
757             configuration.getRepositoryScanning().setKnownContentConsumers( knowContentConsumers );
758         }
759         // save ??
760         //save( configuration );
761     }
762
763     @Override
764     public void reload()
765     {
766         this.configuration = null;
767         try
768         {
769             this.registry.initialize();
770         }
771         catch ( RegistryException e )
772         {
773             throw new ConfigurationRuntimeException( e.getMessage(), e );
774         }
775         this.initialize();
776     }
777
778     @Override
779     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
780     {
781         // nothing to do here
782     }
783
784     @Override
785     public synchronized void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
786     {
787         configuration = null;
788     }
789
790     private String removeExpressions( String directory )
791     {
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}" ) );
796         return value;
797     }
798
799     private String unescapeCronExpression( String cronExpression )
800     {
801         return StringUtils.replace( cronExpression, "\\,", "," );
802     }
803
804     private String escapeCronExpression( String cronExpression )
805     {
806         return StringUtils.replace( cronExpression, ",", "\\," );
807     }
808
809     private Configuration unescapeExpressions( Configuration config )
810     {
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() ) );
815         }
816
817         return config;
818     }
819
820     private Configuration checkRepositoryLocations( Configuration config )
821     {
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() )
825         {
826             String repoPath = repo.getLocation();
827             File repoLocation = new File( repoPath );
828
829             if ( repoLocation.exists() && repoLocation.isDirectory() && !repoPath.endsWith(
830                 "data/repositories/" + repo.getId() ) )
831             {
832                 repo.setLocation( repoPath + "/data/repositories/" + repo.getId() );
833             }
834         }
835
836         return config;
837     }
838
839     public String getUserConfigFilename()
840     {
841         return userConfigFilename;
842     }
843
844     public String getAltConfigFilename()
845     {
846         return altConfigFilename;
847     }
848
849     @Override
850     public boolean isDefaulted()
851     {
852         return this.isConfigurationDefaulted;
853     }
854
855     public Registry getRegistry()
856     {
857         return registry;
858     }
859
860     public void setRegistry( Registry registry )
861     {
862         this.registry = registry;
863     }
864
865
866     public void setUserConfigFilename( String userConfigFilename )
867     {
868         this.userConfigFilename = userConfigFilename;
869     }
870
871     public void setAltConfigFilename( String altConfigFilename )
872     {
873         this.altConfigFilename = altConfigFilename;
874     }
875 }