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.archiva.common.FileTypeUtils;
23 import org.apache.archiva.configuration.functors.FiletypeSelectionPredicate;
24 import org.apache.archiva.configuration.io.registry.ConfigurationRegistryReader;
25 import org.apache.archiva.redback.components.registry.Registry;
26 import org.apache.archiva.redback.components.registry.RegistryException;
27 import org.apache.archiva.redback.components.registry.RegistryListener;
28 import org.apache.archiva.redback.components.registry.commons.CommonsConfigurationRegistry;
29 import org.apache.commons.collections.CollectionUtils;
30 import org.apache.commons.collections.Predicate;
31 import org.apache.commons.configuration.CombinedConfiguration;
32 import org.apache.tools.ant.types.selectors.SelectorUtils;
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;
49 public class FileTypes
50 implements RegistryListener
52 public static final String ARTIFACTS = "artifacts";
54 public static final String AUTO_REMOVE = "auto-remove";
56 public static final String INDEXABLE_CONTENT = "indexable-content";
58 public static final String IGNORED = "ignored";
61 @Named(value = "archivaConfiguration#default")
62 private ArchivaConfiguration archivaConfiguration;
65 * Map of default values for the file types.
67 private Map<String, List<String>> defaultTypeMap = new HashMap<>();
69 private List<String> artifactPatterns;
72 * Default exclusions from artifact consumers that are using the file types. Note that this is simplistic in the
73 * case of the support files (based on extension) as it is elsewhere - it may be better to match these to actual
74 * artifacts and exclude later during scanning.
78 public static final List<String> DEFAULT_EXCLUSIONS = FileTypeUtils.DEFAULT_EXCLUSIONS;
80 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
82 this.archivaConfiguration = archivaConfiguration;
86 * Get the list of patterns for a specified filetype.
87 * You will always get a list. In this order.
89 * <li>The Configured List</li>
90 * <li>The Default List</li>
91 * <li>A single item list of <code>"**/*"</code></li>
94 * @param id the id to lookup.
95 * @return the list of patterns.
97 public List<String> getFileTypePatterns( String id )
99 Configuration config = archivaConfiguration.getConfiguration();
100 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
101 RepositoryScanningConfiguration repositoryScanningConfiguration = config.getRepositoryScanning();
102 if ( repositoryScanningConfiguration != null )
105 (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(), selectedFiletype );
107 if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
109 return filetype.getPatterns();
112 List<String> defaultPatterns = defaultTypeMap.get( id );
114 if ( CollectionUtils.isEmpty( defaultPatterns ) )
116 return Collections.singletonList( "**/*" );
119 return defaultPatterns;
122 public synchronized boolean matchesArtifactPattern( String relativePath )
124 // Correct the slash pattern.
125 relativePath = relativePath.replace( '\\', '/' );
127 if ( artifactPatterns == null )
129 artifactPatterns = getFileTypePatterns( ARTIFACTS );
132 for ( String pattern : artifactPatterns )
134 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
145 public boolean matchesDefaultExclusions( String relativePath )
147 // Correct the slash pattern.
148 relativePath = relativePath.replace( '\\', '/' );
150 for ( String pattern : DEFAULT_EXCLUSIONS )
152 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
164 public void initialize()
166 // TODO: why is this done by hand?
168 // TODO: ideally, this would be instantiated by configuration instead, and not need to be a component
170 String errMsg = "Unable to load default archiva configuration for FileTypes: ";
174 CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
176 // Configure commonsRegistry
177 Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
178 fld.setAccessible( true );
179 fld.set( commonsRegistry, new CombinedConfiguration() );
180 commonsRegistry.addConfigurationFromResource( "org/apache/archiva/configuration/default-archiva.xml" );
182 // Read configuration as it was intended.
183 ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
184 Configuration defaultConfig = configReader.read( commonsRegistry );
186 initialiseTypeMap( defaultConfig );
188 catch ( RegistryException e )
190 throw new RuntimeException( errMsg + e.getMessage(), e );
192 catch ( SecurityException e )
194 throw new RuntimeException( errMsg + e.getMessage(), e );
196 catch ( NoSuchFieldException e )
198 throw new RuntimeException( errMsg + e.getMessage(), e );
200 catch ( IllegalArgumentException e )
202 throw new RuntimeException( errMsg + e.getMessage(), e );
204 catch ( IllegalAccessException e )
206 throw new RuntimeException( errMsg + e.getMessage(), e );
209 this.archivaConfiguration.addChangeListener( this );
212 private void initialiseTypeMap( Configuration configuration )
214 defaultTypeMap.clear();
216 // Store the default file type declaration.
217 List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
218 for ( FileType filetype : filetypes )
220 List<String> patterns = defaultTypeMap.get( filetype.getId() );
221 if ( patterns == null )
223 patterns = new ArrayList<>( filetype.getPatterns().size() );
225 patterns.addAll( filetype.getPatterns() );
227 defaultTypeMap.put( filetype.getId(), patterns );
232 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
234 if ( propertyName.contains( "fileType" ) )
236 artifactPatterns = null;
238 initialiseTypeMap( archivaConfiguration.getConfiguration() );
243 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )