1 package org.apache.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.archiva.common.FileTypeUtils;
26 import org.apache.archiva.configuration.functors.FiletypeSelectionPredicate;
27 import org.apache.archiva.configuration.io.registry.ConfigurationRegistryReader;
28 import org.codehaus.plexus.registry.Registry;
29 import org.codehaus.plexus.registry.RegistryException;
30 import org.codehaus.plexus.registry.RegistryListener;
31 import org.codehaus.plexus.util.SelectorUtils;
32 import org.codehaus.redback.components.registry.commons.CommonsConfigurationRegistry;
33 import org.springframework.stereotype.Service;
35 import javax.annotation.PostConstruct;
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import java.lang.reflect.Field;
39 import java.util.ArrayList;
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.
82 public static final List<String> DEFAULT_EXCLUSIONS = FileTypeUtils.DEFAULT_EXCLUSIONS;
84 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
86 this.archivaConfiguration = archivaConfiguration;
91 * Get the list of patterns for a specified filetype.
95 * You will always get a list. In this order.
97 * <li>The Configured List</li>
98 * <li>The Default List</li>
99 * <li>A single item list of <code>"**<span>/</span>*"</code></li>
103 * @param id the id to lookup.
104 * @return the list of patterns.
106 public List<String> getFileTypePatterns( String id )
108 Configuration config = archivaConfiguration.getConfiguration();
109 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
111 (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(), selectedFiletype );
113 if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
115 return filetype.getPatterns();
118 List<String> defaultPatterns = defaultTypeMap.get( id );
120 if ( CollectionUtils.isEmpty( defaultPatterns ) )
122 return Collections.singletonList( "**/*" );
125 return defaultPatterns;
128 public synchronized boolean matchesArtifactPattern( String relativePath )
130 // Correct the slash pattern.
131 relativePath = relativePath.replace( '\\', '/' );
133 if ( artifactPatterns == null )
135 artifactPatterns = getFileTypePatterns( ARTIFACTS );
138 for ( String pattern : artifactPatterns )
140 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
151 public boolean matchesDefaultExclusions( String relativePath )
153 // Correct the slash pattern.
154 relativePath = relativePath.replace( '\\', '/' );
156 for ( String pattern : DEFAULT_EXCLUSIONS )
158 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
170 public void initialize()
172 // TODO: why is this done by hand?
174 // TODO: ideally, this would be instantiated by configuration instead, and not need to be a component
176 String errMsg = "Unable to load default archiva configuration for FileTypes: ";
180 CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
182 // Configure commonsRegistry
183 Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
184 fld.setAccessible( true );
185 fld.set( commonsRegistry, new CombinedConfiguration() );
186 commonsRegistry.addConfigurationFromResource(
187 "org/apache/archiva/configuration/default-archiva.xml" );
189 // Read configuration as it was intended.
190 ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
191 Configuration defaultConfig = configReader.read( commonsRegistry );
193 initialiseTypeMap( defaultConfig );
195 catch ( RegistryException e )
197 throw new RuntimeException( errMsg + e.getMessage(), e );
199 catch ( SecurityException e )
201 throw new RuntimeException( errMsg + e.getMessage(), e );
203 catch ( NoSuchFieldException e )
205 throw new RuntimeException( errMsg + e.getMessage(), e );
207 catch ( IllegalArgumentException e )
209 throw new RuntimeException( errMsg + e.getMessage(), e );
211 catch ( IllegalAccessException e )
213 throw new RuntimeException( errMsg + e.getMessage(), e );
216 this.archivaConfiguration.addChangeListener( this );
219 private void initialiseTypeMap( Configuration configuration )
221 defaultTypeMap.clear();
223 // Store the default file type declaration.
224 List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
225 for ( FileType filetype : filetypes )
227 List<String> patterns = defaultTypeMap.get( filetype.getId() );
228 if ( patterns == null )
230 patterns = new ArrayList<String>();
232 patterns.addAll( filetype.getPatterns() );
234 defaultTypeMap.put( filetype.getId(), patterns );
238 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
240 if ( propertyName.contains( "fileType" ) )
242 artifactPatterns = null;
244 initialiseTypeMap( archivaConfiguration.getConfiguration() );
248 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )