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 * plexus.component role="org.apache.maven.archiva.configuration.FileTypes"
52 @Service( "fileTypes" )
53 public class FileTypes
54 implements RegistryListener
56 public static final String ARTIFACTS = "artifacts";
58 public static final String AUTO_REMOVE = "auto-remove";
60 public static final String INDEXABLE_CONTENT = "indexable-content";
62 public static final String IGNORED = "ignored";
68 @Named( value = "archivaConfiguration#default" )
69 private ArchivaConfiguration archivaConfiguration;
72 * Map of default values for the file types.
74 private Map<String, List<String>> defaultTypeMap = new HashMap<String, List<String>>();
76 private List<String> artifactPatterns;
79 * Default exclusions from artifact consumers that are using the file types. Note that this is simplistic in the
80 * case of the support files (based on extension) as it is elsewhere - it may be better to match these to actual
81 * artifacts and exclude later during scanning.
83 public static final List<String> DEFAULT_EXCLUSIONS =
84 Arrays.asList( "**/maven-metadata.xml", "**/maven-metadata-*.xml", "**/*.sha1", "**/*.asc", "**/*.md5",
85 "**/*.pgp", "**/.index/**", "**/.indexer/**" );
87 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
89 this.archivaConfiguration = archivaConfiguration;
94 * Get the list of patterns for a specified filetype.
98 * You will always get a list. In this order.
100 * <li>The Configured List</li>
101 * <li>The Default List</li>
102 * <li>A single item list of <code>"**<span>/</span>*"</code></li>
106 * @param id the id to lookup.
107 * @return the list of patterns.
109 public List<String> getFileTypePatterns( String id )
111 Configuration config = archivaConfiguration.getConfiguration();
112 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
114 (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(), selectedFiletype );
116 if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
118 return filetype.getPatterns();
121 List<String> defaultPatterns = defaultTypeMap.get( id );
123 if ( CollectionUtils.isEmpty( defaultPatterns ) )
125 return Collections.singletonList( "**/*" );
128 return defaultPatterns;
131 public synchronized boolean matchesArtifactPattern( String relativePath )
133 // Correct the slash pattern.
134 relativePath = relativePath.replace( '\\', '/' );
136 if ( artifactPatterns == null )
138 artifactPatterns = getFileTypePatterns( ARTIFACTS );
141 for ( String pattern : artifactPatterns )
143 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
154 public boolean matchesDefaultExclusions( String relativePath )
156 // Correct the slash pattern.
157 relativePath = relativePath.replace( '\\', '/' );
159 for ( String pattern : DEFAULT_EXCLUSIONS )
161 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
173 public void initialize()
175 // TODO: why is this done by hand?
177 // TODO: ideally, this would be instantiated by configuration instead, and not need to be a component
179 String errMsg = "Unable to load default archiva configuration for FileTypes: ";
183 CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
185 // Configure commonsRegistry
186 Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
187 fld.setAccessible( true );
188 fld.set( commonsRegistry, new CombinedConfiguration() );
189 commonsRegistry.addConfigurationFromResource(
190 "org/apache/maven/archiva/configuration/default-archiva.xml" );
192 // Read configuration as it was intended.
193 ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
194 Configuration defaultConfig = configReader.read( commonsRegistry );
196 initialiseTypeMap( defaultConfig );
198 catch ( RegistryException e )
200 throw new RuntimeException( errMsg + e.getMessage(), e );
202 catch ( SecurityException e )
204 throw new RuntimeException( errMsg + e.getMessage(), e );
206 catch ( NoSuchFieldException e )
208 throw new RuntimeException( errMsg + e.getMessage(), e );
210 catch ( IllegalArgumentException e )
212 throw new RuntimeException( errMsg + e.getMessage(), e );
214 catch ( IllegalAccessException e )
216 throw new RuntimeException( errMsg + e.getMessage(), e );
219 this.archivaConfiguration.addChangeListener( this );
222 private void initialiseTypeMap( Configuration configuration )
224 defaultTypeMap.clear();
226 // Store the default file type declaration.
227 List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
228 for ( FileType filetype : filetypes )
230 List<String> patterns = defaultTypeMap.get( filetype.getId() );
231 if ( patterns == null )
233 patterns = new ArrayList<String>();
235 patterns.addAll( filetype.getPatterns() );
237 defaultTypeMap.put( filetype.getId(), patterns );
241 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
243 if ( propertyName.contains( "fileType" ) )
245 artifactPatterns = null;
247 initialiseTypeMap( archivaConfiguration.getConfiguration() );
251 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )