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.Registry;
31 import org.codehaus.plexus.registry.RegistryException;
32 import org.codehaus.plexus.registry.RegistryListener;
33 import org.codehaus.plexus.registry.commons.CommonsConfigurationRegistry;
34 import org.codehaus.plexus.util.SelectorUtils;
36 import java.lang.reflect.Field;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.List;
46 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
49 * @plexus.component role="org.apache.maven.archiva.configuration.FileTypes"
51 public class FileTypes
52 extends AbstractLogEnabled
53 implements Initializable, RegistryListener
55 public static final String ARTIFACTS = "artifacts";
57 public static final String AUTO_REMOVE = "auto-remove";
59 public static final String INDEXABLE_CONTENT = "indexable-content";
61 public static final String IGNORED = "ignored";
66 private ArchivaConfiguration archivaConfiguration;
69 * Map of default values for the file types.
71 private Map<String, List<String>> defaultTypeMap = new HashMap<String, List<String>>();
73 private List<String> artifactPatterns;
75 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
77 this.archivaConfiguration = archivaConfiguration;
82 * Get the list of patterns for a specified filetype.
86 * You will always get a list. In this order.
88 * <li>The Configured List</li>
89 * <li>The Default List</li>
90 * <li>A single item list of <code>"**<span>/</span>*"</code></li>
94 * @param id the id to lookup.
95 * @return the list of patterns.
97 public List<String> getFileTypePatterns( String id )
99 Configuration config = archivaConfiguration.getConfiguration();
100 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
101 FileType filetype = (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(),
104 if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
106 return filetype.getPatterns();
109 List<String> defaultPatterns = defaultTypeMap.get( id );
111 if ( CollectionUtils.isEmpty( defaultPatterns ) )
113 return Collections.singletonList( "**/*" );
116 return defaultPatterns;
119 public synchronized boolean matchesArtifactPattern( String relativePath )
121 // Correct the slash pattern.
122 relativePath = relativePath.replace( '\\', '/' );
124 if ( artifactPatterns == null )
126 artifactPatterns = getFileTypePatterns( ARTIFACTS );
129 for ( String pattern : artifactPatterns )
131 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
142 public void initialize()
143 throws InitializationException
145 // TODO: why is this done by hand?
147 String errMsg = "Unable to load default archiva configuration for FileTypes: ";
151 CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
153 // Configure commonsRegistry
154 Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
155 fld.setAccessible( true );
156 fld.set( commonsRegistry, new CombinedConfiguration() );
157 commonsRegistry.enableLogging( getLogger() );
158 commonsRegistry.addConfigurationFromResource( "org/apache/maven/archiva/configuration/default-archiva.xml" );
160 // Read configuration as it was intended.
161 ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
162 Configuration defaultConfig = configReader.read( commonsRegistry );
164 initialiseTypeMap( defaultConfig );
166 catch ( RegistryException e )
168 throw new InitializationException( errMsg + e.getMessage(), e );
170 catch ( SecurityException e )
172 throw new InitializationException( errMsg + e.getMessage(), e );
174 catch ( NoSuchFieldException e )
176 throw new InitializationException( errMsg + e.getMessage(), e );
178 catch ( IllegalArgumentException e )
180 throw new InitializationException( errMsg + e.getMessage(), e );
182 catch ( IllegalAccessException e )
184 throw new InitializationException( errMsg + e.getMessage(), e );
187 this.archivaConfiguration.addChangeListener( this );
190 private void initialiseTypeMap( Configuration configuration )
192 defaultTypeMap.clear();
194 // Store the default file type declaration.
195 List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
196 for ( FileType filetype : filetypes )
198 List<String> patterns = defaultTypeMap.get( filetype.getId() );
199 if ( patterns == null )
201 patterns = new ArrayList<String>();
203 patterns.addAll( filetype.getPatterns() );
205 defaultTypeMap.put( filetype.getId(), patterns );
209 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
211 if ( propertyName.contains( "fileType" ) )
213 artifactPatterns = null;
215 initialiseTypeMap( archivaConfiguration.getConfiguration() );
219 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )