]> source.dussan.org Git - archiva.git/blob
66f4c7230e8fb216d303fb96ca1c83dda1c392fa
[archiva.git] /
1 package org.apache.maven.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.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;
31
32 import java.util.Iterator;
33
34 /**
35  * Implementation of configuration holder that retrieves it from the registry.
36  *
37  * @plexus.component role="org.apache.maven.archiva.configuration.ArchivaConfiguration"
38  */
39 public class DefaultArchivaConfiguration
40     extends AbstractLogEnabled
41     implements ArchivaConfiguration, RegistryListener, Initializable
42 {
43     /**
44      * Plexus registry to read the configuration from.
45      *
46      * @plexus.requirement role-hint="commons-configuration"
47      */
48     private Registry registry;
49
50     /**
51      * The configuration that has been converted.
52      */
53     private Configuration configuration;
54
55     private static final String KEY = "org.apache.maven.archiva";
56
57     public synchronized Configuration getConfiguration()
58     {
59         if ( configuration == null )
60         {
61             configuration = load();
62         }
63         return configuration;
64     }
65
66     private Configuration load()
67     {
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 ) );
70
71         // TODO: for commons-configuration 1.3 only
72         for ( Iterator i = config.getRepositories().iterator(); i.hasNext(); )
73         {
74             RepositoryConfiguration c = (RepositoryConfiguration) i.next();
75             c.setUrl( removeExpressions( c.getUrl() ) );
76         }
77
78         return config;
79     }
80
81     public void save( Configuration configuration )
82         throws RegistryException
83     {
84         Registry section = registry.getSection( KEY + ".user" );
85         if ( section == null )
86         {
87             section = registry.getSection( KEY + ".base" );
88         }
89         new ConfigurationRegistryWriter().write( configuration, section );
90         section.save();
91
92         this.configuration = configuration;
93     }
94
95     public void addChangeListener( RegistryListener listener )
96     {
97         Registry section = registry.getSection( KEY + ".user" );
98         if ( section != null )
99         {
100             section.addChangeListener( listener );
101         }
102         section = registry.getSection( KEY + ".base" );
103         if ( section != null )
104         {
105             section.addChangeListener( listener );
106         }
107     }
108
109     public void initialize()
110         throws InitializationException
111     {
112         registry.addChangeListener( this );
113
114         ConfigurationUpgrade upgrade = new ConfigurationUpgrade();
115         upgrade.setLogger( getLogger() );
116         if ( upgrade.perform() )
117         {
118             this.configuration = load();
119         }
120     }
121
122     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
123     {
124         // nothing to do here
125     }
126
127     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
128     {
129         configuration = null;
130     }
131
132     private String removeExpressions( String directory )
133     {
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}" ) );
138         return value;
139     }
140
141 }