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 java.lang.reflect.Field;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
30 import org.apache.commons.collections.CollectionUtils;
31 import org.apache.commons.collections.Predicate;
32 import org.apache.commons.configuration.CombinedConfiguration;
33 import org.apache.maven.archiva.common.utils.Slf4JPlexusLogger;
34 import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
35 import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryReader;
36 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
37 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
38 import org.codehaus.plexus.registry.Registry;
39 import org.codehaus.plexus.registry.RegistryException;
40 import org.codehaus.plexus.registry.RegistryListener;
41 import org.codehaus.plexus.registry.commons.CommonsConfigurationRegistry;
42 import org.codehaus.plexus.util.SelectorUtils;
49 * @plexus.component role="org.apache.maven.archiva.configuration.FileTypes"
51 public class FileTypes
52 implements Initializable, RegistryListener
54 public static final String ARTIFACTS = "artifacts";
56 public static final String AUTO_REMOVE = "auto-remove";
58 public static final String INDEXABLE_CONTENT = "indexable-content";
60 public static final String IGNORED = "ignored";
65 private ArchivaConfiguration archivaConfiguration;
68 * Map of default values for the file types.
70 private Map<String, List<String>> defaultTypeMap = new HashMap<String, List<String>>();
72 private List<String> artifactPatterns;
75 * Default exclusions from artifact consumers that are using the file types. Note that this is simplistic in the
76 * case of the support files (based on extension) as it is elsewhere - it may be better to match these to actual
77 * artifacts and exclude later during scanning.
79 public static final List<String> DEFAULT_EXCLUSIONS = Arrays.asList( "**/maven-metadata.xml",
80 "**/maven-metadata-*.xml", "**/*.sha1",
81 "**/*.asc", "**/*.md5", "**/*.pgp" );
83 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
85 this.archivaConfiguration = archivaConfiguration;
90 * Get the list of patterns for a specified filetype.
94 * You will always get a list. In this order.
96 * <li>The Configured List</li>
97 * <li>The Default List</li>
98 * <li>A single item list of <code>"**<span>/</span>*"</code></li>
102 * @param id the id to lookup.
103 * @return the list of patterns.
105 public List<String> getFileTypePatterns( String id )
107 Configuration config = archivaConfiguration.getConfiguration();
108 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
109 FileType filetype = (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(),
112 if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
114 return filetype.getPatterns();
117 List<String> defaultPatterns = defaultTypeMap.get( id );
119 if ( CollectionUtils.isEmpty( defaultPatterns ) )
121 return Collections.singletonList( "**/*" );
124 return defaultPatterns;
127 public synchronized boolean matchesArtifactPattern( String relativePath )
129 // Correct the slash pattern.
130 relativePath = relativePath.replace( '\\', '/' );
132 if ( artifactPatterns == null )
134 artifactPatterns = getFileTypePatterns( ARTIFACTS );
137 for ( String pattern : artifactPatterns )
139 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
150 public boolean matchesDefaultExclusions( String relativePath )
152 // Correct the slash pattern.
153 relativePath = relativePath.replace( '\\', '/' );
155 for ( String pattern : DEFAULT_EXCLUSIONS )
157 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
168 public void initialize()
169 throws InitializationException
171 // TODO: why is this done by hand?
173 // TODO: ideally, this would be instantiated by configuration instead, and not need to be a component
175 String errMsg = "Unable to load default archiva configuration for FileTypes: ";
179 CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
181 // Configure commonsRegistry
182 Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
183 fld.setAccessible( true );
184 fld.set( commonsRegistry, new CombinedConfiguration() );
185 commonsRegistry.enableLogging( new Slf4JPlexusLogger( FileTypes.class ) );
186 commonsRegistry.addConfigurationFromResource( "org/apache/maven/archiva/configuration/default-archiva.xml" );
188 // Read configuration as it was intended.
189 ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
190 Configuration defaultConfig = configReader.read( commonsRegistry );
192 initialiseTypeMap( defaultConfig );
194 catch ( RegistryException e )
196 throw new InitializationException( errMsg + e.getMessage(), e );
198 catch ( SecurityException e )
200 throw new InitializationException( errMsg + e.getMessage(), e );
202 catch ( NoSuchFieldException e )
204 throw new InitializationException( errMsg + e.getMessage(), e );
206 catch ( IllegalArgumentException e )
208 throw new InitializationException( errMsg + e.getMessage(), e );
210 catch ( IllegalAccessException e )
212 throw new InitializationException( errMsg + e.getMessage(), e );
215 this.archivaConfiguration.addChangeListener( this );
218 private void initialiseTypeMap( Configuration configuration )
220 defaultTypeMap.clear();
222 // Store the default file type declaration.
223 List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
224 for ( FileType filetype : filetypes )
226 List<String> patterns = defaultTypeMap.get( filetype.getId() );
227 if ( patterns == null )
229 patterns = new ArrayList<String>();
231 patterns.addAll( filetype.getPatterns() );
233 defaultTypeMap.put( filetype.getId(), patterns );
237 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
239 if ( propertyName.contains( "fileType" ) )
241 artifactPatterns = null;
243 initialiseTypeMap( archivaConfiguration.getConfiguration() );
247 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )