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.commons.io.IOUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.archiva.configuration.io.xpp3.ConfigurationXpp3Reader;
25 import org.apache.maven.archiva.configuration.io.xpp3.ConfigurationXpp3Writer;
26 import org.codehaus.plexus.logging.AbstractLogEnabled;
27 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30 import java.io.FileNotFoundException;
31 import java.io.FileReader;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.util.Iterator;
35 import java.util.LinkedList;
36 import java.util.List;
39 * Load and store the configuration. No synchronization is used, but it is unnecessary as the old configuration object
40 * can continue to be used.
42 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
43 * @todo would be great for plexus to do this for us - so the configuration would be a component itself rather than this store
44 * @todo would be good to monitor the store file for changes
45 * @todo support other implementations than XML file
48 public class DefaultConfigurationStore
49 extends AbstractLogEnabled
50 implements ConfigurationStore
53 * @plexus.configuration default-value="${configuration.store.file}"
58 * The cached configuration.
60 private Configuration configuration;
63 * List of listeners to configuration changes.
65 private List/*<ConfigurationChangeListener>*/ listeners = new LinkedList();
67 public Configuration getConfigurationFromStore()
68 throws ConfigurationStoreException
70 if ( configuration == null )
72 ConfigurationXpp3Reader reader = new ConfigurationXpp3Reader();
76 file = new File( System.getProperty( "user.home" ), "/.m2/archiva.xml" );
78 // migration for those with the old file
81 File file = new File( System.getProperty( "user.home" ), "/.m2/repository-manager.xml" );
84 getLogger().info( "Migrating " + file + " to " + this.file );
85 file.renameTo( this.file );
90 FileReader fileReader;
93 fileReader = new FileReader( file );
95 catch ( FileNotFoundException e )
97 getLogger().warn( "Configuration file: " + file + " not found. Using defaults." );
98 configuration = new Configuration();
102 getLogger().info( "Reading configuration from " + file );
105 configuration = reader.read( fileReader, false );
106 sanitizeConfiguration( configuration );
108 catch ( IOException e )
110 throw new ConfigurationStoreException( e.getMessage(), e );
112 catch ( XmlPullParserException e )
114 throw new ConfigurationStoreException( e.getMessage(), e );
118 IOUtils.closeQuietly( fileReader );
121 return configuration;
125 * Perform any Upgrades and Adjustments needed to bring configuration up to the
126 * current configuration format.
128 * @param config the configuration to upgrade and adjust.
130 private void sanitizeConfiguration( Configuration config )
132 Iterator it = config.getRepositories().iterator();
133 while ( it.hasNext() )
135 RepositoryConfiguration repo = (RepositoryConfiguration) it.next();
137 // Ensure that the repo.urlName is set.
138 if ( StringUtils.isEmpty( repo.getUrlName() ) )
140 repo.setUrlName( repo.getId() );
145 public void storeConfiguration( Configuration configuration )
146 throws ConfigurationStoreException, InvalidConfigurationException, ConfigurationChangeException
148 for ( Iterator i = listeners.iterator(); i.hasNext(); )
150 ConfigurationChangeListener listener = (ConfigurationChangeListener) i.next();
152 listener.notifyOfConfigurationChange( configuration );
155 ConfigurationXpp3Writer writer = new ConfigurationXpp3Writer();
157 getLogger().info( "Writing configuration to " + file );
158 FileWriter fileWriter = null;
161 //does file directory exist ?
162 if ( file.getParentFile() != null && !file.getParentFile().exists() )
164 file.getParentFile().mkdirs();
167 fileWriter = new FileWriter( file );
168 writer.write( fileWriter, configuration );
170 catch ( IOException e )
172 throw new ConfigurationStoreException( e.getMessage(), e );
176 IOUtils.closeQuietly( fileWriter );
180 public void addChangeListener( ConfigurationChangeListener listener )
182 listeners.add( listener );