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.maven.archiva.configuration.io.registry.ConfigurationRegistryReader;
23 import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryWriter;
24 import org.codehaus.plexus.logging.AbstractLogEnabled;
25 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
26 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
27 import org.codehaus.plexus.registry.Registry;
28 import org.codehaus.plexus.registry.RegistryException;
29 import org.codehaus.plexus.registry.RegistryListener;
30 import org.codehaus.plexus.util.StringUtils;
32 import java.util.Iterator;
35 * Implementation of configuration holder that retrieves it from the registry.
37 * @plexus.component role="org.apache.maven.archiva.configuration.ArchivaConfiguration"
39 public class DefaultArchivaConfiguration
40 extends AbstractLogEnabled
41 implements ArchivaConfiguration, RegistryListener, Initializable
44 * Plexus registry to read the configuration from.
46 * @plexus.requirement role-hint="commons-configuration"
48 private Registry registry;
51 * The configuration that has been converted.
53 private Configuration configuration;
55 private static final String KEY = "org.apache.maven.archiva";
57 public synchronized Configuration getConfiguration()
59 if ( configuration == null )
61 configuration = load();
66 private Configuration load()
68 // TODO: should this be the same as section? make sure unnamed sections still work (eg, sys properties)
69 Configuration config = new ConfigurationRegistryReader().read( registry.getSubset( KEY ) );
71 // TODO: for commons-configuration 1.3 only
72 for ( Iterator i = config.getRepositories().iterator(); i.hasNext(); )
74 RepositoryConfiguration c = (RepositoryConfiguration) i.next();
75 c.setUrl( removeExpressions( c.getUrl() ) );
81 public void save( Configuration configuration )
82 throws RegistryException
84 Registry section = registry.getSection( KEY + ".user" );
85 if ( section == null )
87 section = registry.getSection( KEY + ".base" );
89 new ConfigurationRegistryWriter().write( configuration, section );
92 this.configuration = configuration;
95 public void addChangeListener( RegistryListener listener )
97 Registry section = registry.getSection( KEY + ".user" );
98 if ( section != null )
100 section.addChangeListener( listener );
102 section = registry.getSection( KEY + ".base" );
103 if ( section != null )
105 section.addChangeListener( listener );
109 public void initialize()
110 throws InitializationException
112 registry.addChangeListener( this );
114 ConfigurationUpgrade upgrade = new ConfigurationUpgrade();
115 upgrade.setLogger( getLogger() );
116 if ( upgrade.perform() )
118 this.configuration = load();
122 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
124 // nothing to do here
127 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
129 configuration = null;
132 private String removeExpressions( String directory )
134 String value = StringUtils.replace( directory, "${appserver.base}", registry.getString( "appserver.base",
135 "${appserver.base}" ) );
136 value = StringUtils.replace( value, "${appserver.home}", registry.getString( "appserver.home",
137 "${appserver.home}" ) );