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
42 * @plexus.component role="org.apache.maven.repository.configuration.ConfigurationStore" role-hint="default"
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();
89 configuration = reader.read( fileReader );
91 catch ( IOException e )
93 throw new ConfigurationStoreException( e.getMessage(), e );
95 catch ( XmlPullParserException e )
97 throw new ConfigurationStoreException( e.getMessage(), e );
101 IOUtil.close( fileReader );
104 return configuration;
107 public void storeConfiguration( Configuration configuration )
108 throws ConfigurationStoreException
110 for ( Iterator i = listeners.iterator(); i.hasNext(); )
112 ConfigurationChangeListener listener = (ConfigurationChangeListener) i.next();
114 listener.notifyOfConfigurationChange( configuration );
117 ConfigurationXpp3Writer writer = new ConfigurationXpp3Writer();
119 FileWriter fileWriter = null;
122 fileWriter = new FileWriter( file );
123 writer.write( fileWriter, configuration );
125 catch ( IOException e )
127 throw new ConfigurationStoreException( e.getMessage(), e );
131 IOUtil.close( fileWriter );