1 package org.apache.maven.archiva.configuration;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.archiva.configuration.io.xpp3.ConfigurationXpp3Reader;
20 import org.apache.maven.archiva.configuration.io.xpp3.ConfigurationXpp3Writer;
21 import org.codehaus.plexus.logging.AbstractLogEnabled;
22 import org.codehaus.plexus.util.IOUtil;
23 import org.codehaus.plexus.util.StringUtils;
24 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
27 import java.io.FileNotFoundException;
28 import java.io.FileReader;
29 import java.io.FileWriter;
30 import java.io.IOException;
31 import java.util.Iterator;
32 import java.util.LinkedList;
33 import java.util.List;
36 * Load and store the configuration. No synchronization is used, but it is unnecessary as the old configuration object
37 * can continue to be used.
39 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
40 * @todo would be great for plexus to do this for us - so the configuration would be a component itself rather than this store
41 * @todo would be good to monitor the store file for changes
42 * @todo support other implementations than XML file
45 public class DefaultConfigurationStore
46 extends AbstractLogEnabled
47 implements ConfigurationStore
50 * @plexus.configuration default-value="${configuration.store.file}"
55 * The cached configuration.
57 private Configuration configuration;
60 * List of listeners to configuration changes.
62 private List/*<ConfigurationChangeListener>*/ listeners = new LinkedList();
64 public Configuration getConfigurationFromStore()
65 throws ConfigurationStoreException
67 if ( configuration == null )
69 ConfigurationXpp3Reader reader = new ConfigurationXpp3Reader();
73 file = new File( System.getProperty( "user.home" ), "/.m2/archiva.xml" );
75 // migration for those with the old file
78 File file = new File( System.getProperty( "user.home" ), "/.m2/repository-manager.xml" );
81 getLogger().info( "Migrating " + file + " to " + this.file );
82 file.renameTo( this.file );
87 FileReader fileReader;
90 fileReader = new FileReader( file );
92 catch ( FileNotFoundException e )
94 getLogger().warn( "Configuration file: " + file + " not found. Using defaults." );
95 configuration = new Configuration();
99 getLogger().info( "Reading configuration from " + file );
102 configuration = reader.read( fileReader, false );
103 sanitizeConfiguration( configuration );
105 catch ( IOException e )
107 throw new ConfigurationStoreException( e.getMessage(), e );
109 catch ( XmlPullParserException e )
111 throw new ConfigurationStoreException( e.getMessage(), e );
115 IOUtil.close( fileReader );
118 return configuration;
122 * Perform any Upgrades and Adjustments needed to bring configuration up to the
123 * current configuration format.
125 * @param config the configuration to upgrade and adjust.
127 private void sanitizeConfiguration( Configuration config )
129 Iterator it = config.getRepositories().iterator();
130 while ( it.hasNext() )
132 RepositoryConfiguration repo = (RepositoryConfiguration) it.next();
134 // Ensure that the repo.urlName is set.
135 if ( StringUtils.isEmpty( repo.getUrlName() ) )
137 repo.setUrlName( repo.getId() );
142 public void storeConfiguration( Configuration configuration )
143 throws ConfigurationStoreException, InvalidConfigurationException, ConfigurationChangeException
145 for ( Iterator i = listeners.iterator(); i.hasNext(); )
147 ConfigurationChangeListener listener = (ConfigurationChangeListener) i.next();
149 listener.notifyOfConfigurationChange( configuration );
152 ConfigurationXpp3Writer writer = new ConfigurationXpp3Writer();
154 getLogger().info( "Writing configuration to " + file );
155 FileWriter fileWriter = null;
158 file.getParentFile().mkdirs();
160 fileWriter = new FileWriter( file );
161 writer.write( fileWriter, configuration );
163 catch ( IOException e )
165 throw new ConfigurationStoreException( e.getMessage(), e );
169 IOUtil.close( fileWriter );
173 public void addChangeListener( ConfigurationChangeListener listener )
175 listeners.add( listener );