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.common.utils.Slf4JPlexusLogger;
26 import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
27 import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryReader;
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;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import java.lang.reflect.Field;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.List;
49 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
52 * @plexus.component role="org.apache.maven.archiva.configuration.FileTypes"
54 public class FileTypes
55 implements Initializable, RegistryListener
57 private Logger log = LoggerFactory.getLogger(FileTypes.class);
59 public static final String ARTIFACTS = "artifacts";
61 public static final String AUTO_REMOVE = "auto-remove";
63 public static final String INDEXABLE_CONTENT = "indexable-content";
65 public static final String IGNORED = "ignored";
70 private ArchivaConfiguration archivaConfiguration;
73 * Map of default values for the file types.
75 private Map<String, List<String>> defaultTypeMap = new HashMap<String, List<String>>();
77 private List<String> artifactPatterns;
80 * Default exclusions from artifact consumers that are using the file types. Note that this is simplistic in the
81 * case of the support files (based on extension) as it is elsewhere - it may be better to match these to actual
82 * artifacts and exclude later during scanning.
84 public static final List<String> DEFAULT_EXCLUSIONS = Arrays.asList( "**/maven-metadata.xml",
85 "**/maven-metadata-*.xml", "**/*.sha1",
86 "**/*.asc", "**/*.md5", "**/*.pgp" );
88 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
90 this.archivaConfiguration = archivaConfiguration;
95 * Get the list of patterns for a specified filetype.
99 * You will always get a list. In this order.
101 * <li>The Configured List</li>
102 * <li>The Default List</li>
103 * <li>A single item list of <code>"**<span>/</span>*"</code></li>
107 * @param id the id to lookup.
108 * @return the list of patterns.
110 public List<String> getFileTypePatterns( String id )
112 Configuration config = archivaConfiguration.getConfiguration();
113 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
114 FileType filetype = (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(),
117 if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
119 return filetype.getPatterns();
122 List<String> defaultPatterns = defaultTypeMap.get( id );
124 if ( CollectionUtils.isEmpty( defaultPatterns ) )
126 return Collections.singletonList( "**/*" );
129 return defaultPatterns;
132 public synchronized boolean matchesArtifactPattern( String relativePath )
134 // Correct the slash pattern.
135 relativePath = relativePath.replace( '\\', '/' );
137 if ( artifactPatterns == null )
139 artifactPatterns = getFileTypePatterns( ARTIFACTS );
142 for ( String pattern : artifactPatterns )
144 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
155 public boolean matchesDefaultExclusions( String relativePath )
157 // Correct the slash pattern.
158 relativePath = relativePath.replace( '\\', '/' );
160 for ( String pattern : DEFAULT_EXCLUSIONS )
162 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
173 public void initialize()
174 throws InitializationException
176 // TODO: why is this done by hand?
178 String errMsg = "Unable to load default archiva configuration for FileTypes: ";
182 CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
184 // Configure commonsRegistry
185 Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
186 fld.setAccessible( true );
187 fld.set( commonsRegistry, new CombinedConfiguration() );
188 commonsRegistry.enableLogging( new Slf4JPlexusLogger( FileTypes.class ) );
189 commonsRegistry.addConfigurationFromResource( "org/apache/maven/archiva/configuration/default-archiva.xml" );
191 // Read configuration as it was intended.
192 ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
193 Configuration defaultConfig = configReader.read( commonsRegistry );
195 initialiseTypeMap( defaultConfig );
197 catch ( RegistryException e )
199 throw new InitializationException( errMsg + e.getMessage(), e );
201 catch ( SecurityException e )
203 throw new InitializationException( errMsg + e.getMessage(), e );
205 catch ( NoSuchFieldException e )
207 throw new InitializationException( errMsg + e.getMessage(), e );
209 catch ( IllegalArgumentException e )
211 throw new InitializationException( errMsg + e.getMessage(), e );
213 catch ( IllegalAccessException e )
215 throw new InitializationException( errMsg + e.getMessage(), e );
218 this.archivaConfiguration.addChangeListener( this );
221 private void initialiseTypeMap( Configuration configuration )
223 defaultTypeMap.clear();
225 // Store the default file type declaration.
226 List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
227 for ( FileType filetype : filetypes )
229 List<String> patterns = defaultTypeMap.get( filetype.getId() );
230 if ( patterns == null )
232 patterns = new ArrayList<String>();
234 patterns.addAll( filetype.getPatterns() );
236 defaultTypeMap.put( filetype.getId(), patterns );
240 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
242 if ( propertyName.contains( "fileType" ) )
244 artifactPatterns = null;
246 initialiseTypeMap( archivaConfiguration.getConfiguration() );
250 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )