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.collections.CollectionUtils;
23 import org.apache.commons.collections.Predicate;
24 import org.apache.commons.configuration.CombinedConfiguration;
25 import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
26 import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryReader;
27 import org.codehaus.plexus.logging.AbstractLogEnabled;
28 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
29 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
30 import org.codehaus.plexus.registry.RegistryException;
31 import org.codehaus.plexus.registry.commons.CommonsConfigurationRegistry;
33 import java.lang.reflect.Field;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
43 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
46 * @plexus.component role="org.apache.maven.archiva.configuration.FileTypes"
48 public class FileTypes
49 extends AbstractLogEnabled
50 implements Initializable
52 public static final String ARTIFACTS = "artifacts";
54 public static final String AUTO_REMOVE = "auto-remove";
56 public static final String INDEXABLE_CONTENT = "indexable-content";
58 public static final String IGNORED = "ignored";
63 private ArchivaConfiguration archivaConfiguration;
66 * Map of default values for the file types.
68 private Map<String, List<String>> defaultTypeMap = new HashMap<String, List<String>>();
72 * Get the list of patterns for a specified filetype.
76 * You will always get a list. In this order.
78 * <li>The Configured List</li>
79 * <li>The Default List</li>
80 * <li>A single item list of <code>"**<span>/</span>*"</code></li>
84 * @param id the id to lookup.
85 * @return the list of patterns.
87 public List<String> getFileTypePatterns( String id )
89 Configuration config = archivaConfiguration.getConfiguration();
90 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
91 FileType filetype = (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(),
94 if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
96 return filetype.getPatterns();
99 List<String> defaultPatterns = defaultTypeMap.get( id );
101 if ( CollectionUtils.isEmpty( defaultPatterns ) )
103 return Collections.singletonList( "**/*" );
106 return defaultPatterns;
109 public void initialize()
110 throws InitializationException
112 /* Initialize Default Type Map */
113 defaultTypeMap.clear();
115 String errMsg = "Unable to load default archiva configuration for FileTypes: ";
119 CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
121 // Configure commonsRegistry
122 Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
123 fld.setAccessible( true );
124 fld.set( commonsRegistry, new CombinedConfiguration() );
125 commonsRegistry.enableLogging( getLogger() );
126 commonsRegistry.addConfigurationFromResource( "org/apache/maven/archiva/configuration/default-archiva.xml" );
128 // Read configuration as it was intended.
129 ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
130 Configuration defaultConfig = configReader.read( commonsRegistry );
132 // Store the default file type declaration.
133 List<FileType> filetypes = defaultConfig.getRepositoryScanning().getFileTypes();
134 for ( FileType filetype : filetypes )
136 List<String> patterns = defaultTypeMap.get( filetype.getId() );
137 if ( patterns == null )
139 patterns = new ArrayList<String>();
141 patterns.addAll( filetype.getPatterns() );
143 defaultTypeMap.put( filetype.getId(), patterns );
146 catch ( RegistryException e )
148 throw new InitializationException( errMsg + e.getMessage(), e );
150 catch ( SecurityException e )
152 throw new InitializationException( errMsg + e.getMessage(), e );
154 catch ( NoSuchFieldException e )
156 throw new InitializationException( errMsg + e.getMessage(), e );
158 catch ( IllegalArgumentException e )
160 throw new InitializationException( errMsg + e.getMessage(), e );
162 catch ( IllegalAccessException e )
164 throw new InitializationException( errMsg + e.getMessage(), e );