1 package org.apache.maven.repository.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.repository.configuration.io.xpp3.ConfigurationXpp3Reader;
20 import org.apache.maven.repository.configuration.io.xpp3.ConfigurationXpp3Writer;
21 import org.codehaus.plexus.logging.AbstractLogEnabled;
22 import org.codehaus.plexus.util.IOUtil;
23 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
26 import java.io.FileNotFoundException;
27 import java.io.FileReader;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import java.util.Iterator;
31 import java.util.LinkedList;
32 import java.util.List;
35 * Load and store the configuration. No synchronization is used, but it is unnecessary as the old configuration object
36 * can continue to be used.
38 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
39 * @todo would be great for plexus to do this for us - so the configuration would be a component itself rather than this store
40 * @todo would be good to monitor the store file for changes
41 * @todo support other implementations than XML file
44 public class DefaultConfigurationStore
45 extends AbstractLogEnabled
46 implements ConfigurationStore
49 * @plexus.configuration default-value="${configuration.store.file}"
54 * The cached configuration.
56 private Configuration configuration;
59 * List of listeners to configuration changes.
61 private List/*<ConfigurationChangeListener>*/ listeners = new LinkedList();
63 public Configuration getConfigurationFromStore()
64 throws ConfigurationStoreException
66 if ( configuration == null )
68 ConfigurationXpp3Reader reader = new ConfigurationXpp3Reader();
72 file = new File( System.getProperty( "user.home" ), "/.m2/repository-manager.xml" );
75 FileReader fileReader;
78 fileReader = new FileReader( file );
80 catch ( FileNotFoundException e )
82 getLogger().warn( "Configuration file: " + file + " not found. Using defaults." );
83 configuration = new Configuration();
87 getLogger().info( "Reading configuration from " + file );
90 configuration = reader.read( fileReader, false );
92 catch ( IOException e )
94 throw new ConfigurationStoreException( e.getMessage(), e );
96 catch ( XmlPullParserException e )
98 throw new ConfigurationStoreException( e.getMessage(), e );
102 IOUtil.close( fileReader );
105 return configuration;
108 public void storeConfiguration( Configuration configuration )
109 throws ConfigurationStoreException, InvalidConfigurationException, ConfigurationChangeException
111 for ( Iterator i = listeners.iterator(); i.hasNext(); )
113 ConfigurationChangeListener listener = (ConfigurationChangeListener) i.next();
115 listener.notifyOfConfigurationChange( configuration );
118 ConfigurationXpp3Writer writer = new ConfigurationXpp3Writer();
120 getLogger().info( "Writing configuration to " + file );
121 FileWriter fileWriter = null;
124 file.getParentFile().mkdirs();
126 fileWriter = new FileWriter( file );
127 writer.write( fileWriter, configuration );
129 catch ( IOException e )
131 throw new ConfigurationStoreException( e.getMessage(), e );
135 IOUtil.close( fileWriter );
139 public void addChangeListener( ConfigurationChangeListener listener )
141 listeners.add( listener );