Removing archiva-xml-tools dependency from archiva-configuration.

Loading default FileTypes using plexus-configuration way, not via direct load of the XML.



git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@589831 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joakim Erdfelt 2007-10-29 20:03:58 +00:00
parent 14b91e96be
commit ec8628ad93
2 changed files with 51 additions and 52 deletions

View File

@ -32,10 +32,6 @@
<groupId>org.apache.maven.archiva</groupId> <groupId>org.apache.maven.archiva</groupId>
<artifactId>archiva-common</artifactId> <artifactId>archiva-common</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.maven.archiva</groupId>
<artifactId>archiva-xml-tools</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.apache.maven.archiva</groupId> <groupId>org.apache.maven.archiva</groupId>
<artifactId>archiva-policies</artifactId> <artifactId>archiva-policies</artifactId>

View File

@ -19,19 +19,19 @@ package org.apache.maven.archiva.configuration;
* under the License. * under the License.
*/ */
import org.apache.commons.collections.Closure;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate; import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.StringUtils; import org.apache.commons.configuration.CombinedConfiguration;
import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate; import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
import org.apache.maven.archiva.xml.ElementTextListClosure; import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryReader;
import org.apache.maven.archiva.xml.XMLException; import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.apache.maven.archiva.xml.XMLReader;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.dom4j.Element; import org.codehaus.plexus.registry.RegistryException;
import org.codehaus.plexus.registry.commons.CommonsConfigurationRegistry;
import java.net.URL; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -46,6 +46,7 @@ import java.util.Map;
* @plexus.component role="org.apache.maven.archiva.configuration.FileTypes" * @plexus.component role="org.apache.maven.archiva.configuration.FileTypes"
*/ */
public class FileTypes public class FileTypes
extends AbstractLogEnabled
implements Initializable implements Initializable
{ {
public static final String ARTIFACTS = "artifacts"; public static final String ARTIFACTS = "artifacts";
@ -108,57 +109,59 @@ public class FileTypes
public void initialize() public void initialize()
throws InitializationException throws InitializationException
{ {
/* Initialize Default Type Map */
defaultTypeMap.clear(); defaultTypeMap.clear();
String errMsg = "Unable to load default archiva configuration for FileTypes: ";
try try
{ {
URL defaultArchivaXml = this.getClass() CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
.getResource( "/org/apache/maven/archiva/configuration/default-archiva.xml" );
XMLReader reader = new XMLReader( "configuration", defaultArchivaXml ); // Configure commonsRegistry
List<Element> resp = reader.getElementList( "//configuration/repositoryScanning/fileTypes/fileType" ); Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
fld.setAccessible( true );
fld.set( commonsRegistry, new CombinedConfiguration() );
commonsRegistry.enableLogging( getLogger() );
commonsRegistry.addConfigurationFromResource( "org/apache/maven/archiva/configuration/default-archiva.xml" );
// Read configuration as it was intended.
ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
Configuration defaultConfig = configReader.read( commonsRegistry );
CollectionUtils.forAllDo( resp, new AddFileTypeToDefaultMap() ); // Store the default file type declaration.
List<FileType> filetypes = defaultConfig.getRepositoryScanning().getFileTypes();
for ( FileType filetype : filetypes )
{
List<String> patterns = defaultTypeMap.get( filetype.getId() );
if ( patterns == null )
{
patterns = new ArrayList<String>();
}
patterns.addAll( filetype.getPatterns() );
defaultTypeMap.put( filetype.getId(), patterns );
}
} }
catch ( XMLException e ) catch ( RegistryException e )
{ {
throw new InitializationException( "Unable to setup default filetype maps.", e ); throw new InitializationException( errMsg + e.getMessage(), e );
} }
} catch ( SecurityException e )
class AddFileTypeToDefaultMap
implements Closure
{
public void execute( Object input )
{ {
if ( !( input instanceof Element ) ) throw new InitializationException( errMsg + e.getMessage(), e );
{ }
// Not an element. skip. catch ( NoSuchFieldException e )
return; {
} throw new InitializationException( errMsg + e.getMessage(), e );
}
Element elem = (Element) input; catch ( IllegalArgumentException e )
if ( !StringUtils.equals( "fileType", elem.getName() ) ) {
{ throw new InitializationException( errMsg + e.getMessage(), e );
// Not a 'fileType' element. skip. }
return; catch ( IllegalAccessException e )
} {
throw new InitializationException( errMsg + e.getMessage(), e );
String id = elem.elementText( "id" );
Element patternsElem = elem.element( "patterns" );
if ( patternsElem == null )
{
// No patterns. skip.
return;
}
List<Element> patternElemList = patternsElem.elements( "pattern" );
ElementTextListClosure elemTextList = new ElementTextListClosure();
CollectionUtils.forAllDo( patternElemList, elemTextList );
List<String> patterns = elemTextList.getList();
defaultTypeMap.put( id, patterns );
} }
} }
} }