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.registry.Registry;
28 import org.codehaus.plexus.registry.RegistryException;
29 import org.codehaus.plexus.registry.RegistryListener;
30 import org.codehaus.plexus.util.SelectorUtils;
31 import org.codehaus.redback.components.registry.commons.CommonsConfigurationRegistry;
32 import org.springframework.stereotype.Service;
34 import javax.annotation.PostConstruct;
35 import javax.inject.Inject;
36 import javax.inject.Named;
37 import java.lang.reflect.Field;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.HashMap;
42 import java.util.List;
50 @Service( "fileTypes" )
51 public class FileTypes
52 implements 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";
66 @Named( value = "archivaConfiguration#default" )
67 private ArchivaConfiguration archivaConfiguration;
70 * Map of default values for the file types.
72 private Map<String, List<String>> defaultTypeMap = new HashMap<String, List<String>>();
74 private List<String> artifactPatterns;
77 * Default exclusions from artifact consumers that are using the file types. Note that this is simplistic in the
78 * case of the support files (based on extension) as it is elsewhere - it may be better to match these to actual
79 * artifacts and exclude later during scanning.
81 public static final List<String> DEFAULT_EXCLUSIONS =
82 Arrays.asList( "**/maven-metadata.xml", "**/maven-metadata-*.xml", "**/*.sha1", "**/*.asc", "**/*.md5",
83 "**/*.pgp", "**/.index/**", "**/.indexer/**" );
85 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
87 this.archivaConfiguration = archivaConfiguration;
92 * Get the list of patterns for a specified filetype.
96 * You will always get a list. In this order.
98 * <li>The Configured List</li>
99 * <li>The Default List</li>
100 * <li>A single item list of <code>"**<span>/</span>*"</code></li>
104 * @param id the id to lookup.
105 * @return the list of patterns.
107 public List<String> getFileTypePatterns( String id )
109 Configuration config = archivaConfiguration.getConfiguration();
110 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
112 (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(), selectedFiletype );
114 if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
116 return filetype.getPatterns();
119 List<String> defaultPatterns = defaultTypeMap.get( id );
121 if ( CollectionUtils.isEmpty( defaultPatterns ) )
123 return Collections.singletonList( "**/*" );
126 return defaultPatterns;
129 public synchronized boolean matchesArtifactPattern( String relativePath )
131 // Correct the slash pattern.
132 relativePath = relativePath.replace( '\\', '/' );
134 if ( artifactPatterns == null )
136 artifactPatterns = getFileTypePatterns( ARTIFACTS );
139 for ( String pattern : artifactPatterns )
141 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
152 public boolean matchesDefaultExclusions( String relativePath )
154 // Correct the slash pattern.
155 relativePath = relativePath.replace( '\\', '/' );
157 for ( String pattern : DEFAULT_EXCLUSIONS )
159 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
171 public void initialize()
173 // TODO: why is this done by hand?
175 // TODO: ideally, this would be instantiated by configuration instead, and not need to be a component
177 String errMsg = "Unable to load default archiva configuration for FileTypes: ";
181 CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
183 // Configure commonsRegistry
184 Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
185 fld.setAccessible( true );
186 fld.set( commonsRegistry, new CombinedConfiguration() );
187 commonsRegistry.addConfigurationFromResource(
188 "org/apache/maven/archiva/configuration/default-archiva.xml" );
190 // Read configuration as it was intended.
191 ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
192 Configuration defaultConfig = configReader.read( commonsRegistry );
194 initialiseTypeMap( defaultConfig );
196 catch ( RegistryException e )
198 throw new RuntimeException( errMsg + e.getMessage(), e );
200 catch ( SecurityException e )
202 throw new RuntimeException( errMsg + e.getMessage(), e );
204 catch ( NoSuchFieldException e )
206 throw new RuntimeException( errMsg + e.getMessage(), e );
208 catch ( IllegalArgumentException e )
210 throw new RuntimeException( errMsg + e.getMessage(), e );
212 catch ( IllegalAccessException e )
214 throw new RuntimeException( errMsg + e.getMessage(), e );
217 this.archivaConfiguration.addChangeListener( this );
220 private void initialiseTypeMap( Configuration configuration )
222 defaultTypeMap.clear();
224 // Store the default file type declaration.
225 List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
226 for ( FileType filetype : filetypes )
228 List<String> patterns = defaultTypeMap.get( filetype.getId() );
229 if ( patterns == null )
231 patterns = new ArrayList<String>();
233 patterns.addAll( filetype.getPatterns() );
235 defaultTypeMap.put( filetype.getId(), patterns );
239 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
241 if ( propertyName.contains( "fileType" ) )
243 artifactPatterns = null;
245 initialiseTypeMap( archivaConfiguration.getConfiguration() );
249 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )