1 package org.apache.maven.repository.manager.web.utils;
4 * Copyright 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.Configuration;
20 import org.apache.maven.repository.configuration.io.xpp3.ConfigurationXpp3Reader;
21 import org.apache.maven.repository.configuration.io.xpp3.ConfigurationXpp3Writer;
22 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
25 import java.io.FileNotFoundException;
26 import java.io.FileReader;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.io.Reader;
30 import java.io.Writer;
32 import java.util.Iterator;
36 * This class updates/sets the configuration values in the mrm-admin-config.xml file used
37 * for discovery and indexing.
39 * @plexus.component role="org.apache.maven.repository.manager.web.utils.ConfigurationManager"
41 public class ConfigurationManager
43 public static final String WEB_XML_FILE = "web.xml";
45 public static final String INDEX_CONFIG_FILE = "mrm-admin-config.xml";
47 public static final String CONFIGURATION = "configuration";
49 public static final String DISCOVER_SNAPSHOTS = "discoverSnapshots";
51 public static final String DISCOVERY_CRON_EXPRESSION = "discoveryCronExpression";
53 public static final String INDEXPATH = "indexPath";
55 public static final String MIN_INDEXPATH = "minimalIndexPath";
57 public static final String REPOSITORY_LAYOUT = "repositoryLayout";
59 public static final String REPOSITORY_DIRECTORY = "repositoryDirectory";
61 public static final String DISCOVERY_BLACKLIST_PATTERNS = "discoveryBlacklistPatterns";
63 private Configuration config;
65 private File plexusDescriptor;
68 * Method for updating the configuration in mrm-admin-config.xml
70 * @param map contains the fields and the values to be updated in the configuration
72 public void updateConfiguration( Map map )
75 File file = getConfigFile();
79 config = readXmlDocument( file );
81 catch ( XmlPullParserException de )
86 for ( Iterator iter = map.entrySet().iterator(); iter.hasNext(); )
88 Map.Entry entry = (Map.Entry) iter.next();
89 String name = (String) entry.getKey();
90 String value = (String) entry.getValue();
92 if ( name.equals( DISCOVERY_CRON_EXPRESSION ) )
94 config.setDiscoveryCronExpression( value );
96 if ( name.equals( REPOSITORY_LAYOUT ) )
98 config.setRepositoryLayout( value );
100 if ( name.equals( DISCOVER_SNAPSHOTS ) )
102 config.setDiscoverSnapshots( Boolean.valueOf( value ).booleanValue() );
104 if ( name.equals( REPOSITORY_DIRECTORY ) )
106 config.setRepositoryDirectory( value );
108 if ( name.equals( INDEXPATH ) )
110 config.setIndexPath( value );
112 if ( name.equals( MIN_INDEXPATH ) )
114 config.setMinimalIndexPath( value );
116 if ( name.equals( DISCOVERY_BLACKLIST_PATTERNS ) )
118 config.setDiscoveryBlackListPatterns( value );
122 writeXmlDocument( getConfigFile() );
126 * Method that gets the properties set in the mrm-admin-config.xml for the configuration fields
127 * used in the schedule, indexing and discovery
129 * @return a Map that contains the elements in the properties of the configuration object
131 public Configuration getConfiguration()
134 File file = getConfigFile();
135 config = new Configuration();
137 if ( !file.exists() )
139 writeXmlDocument( getConfigFile() );
145 config = readXmlDocument( file );
147 catch ( XmlPullParserException xe )
149 // TODO: fix error handling!
150 xe.printStackTrace();
158 * Method that reads the xml file and returns a Configuration object
160 * @param file the xml file to be read
161 * @return a Document object that represents the contents of the xml file
162 * @throws FileNotFoundException
163 * @throws IOException
164 * @throws XmlPullParserException
166 protected Configuration readXmlDocument( File file )
167 throws IOException, XmlPullParserException
169 ConfigurationXpp3Reader configReader = new ConfigurationXpp3Reader();
170 Reader reader = new FileReader( file );
171 Configuration config = configReader.read( reader );
177 * Method for writing the configuration into the xml file
179 * @param file the file where the document will be written to
181 protected void writeXmlDocument( File file )
184 Writer writer = new FileWriter( file );
185 ConfigurationXpp3Writer configWriter = new ConfigurationXpp3Writer();
186 configWriter.write( writer, config );
190 * Method that returns the mrm-admin-config.xml file
192 * @return a File that references the plexus.xml
194 protected File getConfigFile()
196 URL indexConfigXml = getClass().getClassLoader().getResource( "../" + INDEX_CONFIG_FILE );
198 if ( indexConfigXml != null )
200 plexusDescriptor = new File( indexConfigXml.getFile() );
204 URL xmlPath = getClass().getClassLoader().getResource( "../" + WEB_XML_FILE );
205 String path = xmlPath.getFile();
206 int lastIndex = path.lastIndexOf( '/' );
207 path = path.substring( 0, lastIndex + 1 );
208 path = path + INDEX_CONFIG_FILE;
209 plexusDescriptor = new File( path );
212 return plexusDescriptor;