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.
89 * You will always get a list. In this order.
91 * <li>The Configured List</li>
92 * <li>The Default List</li>
93 * <li>A single item list of <code>"**/*"</code></li>
96 * @param id the id to lookup.
97 * @return the list of patterns.
99 public List<String> getFileTypePatterns( String id )
101 Configuration config = archivaConfiguration.getConfiguration();
102 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
103 RepositoryScanningConfiguration repositoryScanningConfiguration = config.getRepositoryScanning();
104 if ( repositoryScanningConfiguration != null )
107 (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(), selectedFiletype );
109 if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
111 return filetype.getPatterns();
114 List<String> defaultPatterns = defaultTypeMap.get( id );
116 if ( CollectionUtils.isEmpty( defaultPatterns ) )
118 return Collections.singletonList( "**/*" );
121 return defaultPatterns;
124 public synchronized boolean matchesArtifactPattern( String relativePath )
126 // Correct the slash pattern.
127 relativePath = relativePath.replace( '\\', '/' );
129 if ( artifactPatterns == null )
131 artifactPatterns = getFileTypePatterns( ARTIFACTS );
134 for ( String pattern : artifactPatterns )
136 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
147 public boolean matchesDefaultExclusions( String relativePath )
149 // Correct the slash pattern.
150 relativePath = relativePath.replace( '\\', '/' );
152 for ( String pattern : DEFAULT_EXCLUSIONS )
154 if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
166 public void initialize()
168 // TODO: why is this done by hand?
170 // TODO: ideally, this would be instantiated by configuration instead, and not need to be a component
172 String errMsg = "Unable to load default archiva configuration for FileTypes: ";
176 CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
178 // Configure commonsRegistry
179 Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
180 fld.setAccessible( true );
181 fld.set( commonsRegistry, new CombinedConfiguration() );
182 commonsRegistry.addConfigurationFromResource( "org/apache/archiva/configuration/default-archiva.xml" );
184 // Read configuration as it was intended.
185 ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
186 Configuration defaultConfig = configReader.read( commonsRegistry );
188 initialiseTypeMap( defaultConfig );
190 catch ( RegistryException e )
192 throw new RuntimeException( errMsg + e.getMessage(), e );
194 catch ( SecurityException e )
196 throw new RuntimeException( errMsg + e.getMessage(), e );
198 catch ( NoSuchFieldException e )
200 throw new RuntimeException( errMsg + e.getMessage(), e );
202 catch ( IllegalArgumentException e )
204 throw new RuntimeException( errMsg + e.getMessage(), e );
206 catch ( IllegalAccessException e )
208 throw new RuntimeException( errMsg + e.getMessage(), e );
211 this.archivaConfiguration.addChangeListener( this );
214 private void initialiseTypeMap( Configuration configuration )
216 defaultTypeMap.clear();
218 // Store the default file type declaration.
219 List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
220 for ( FileType filetype : filetypes )
222 List<String> patterns = defaultTypeMap.get( filetype.getId() );
223 if ( patterns == null )
225 patterns = new ArrayList<>( filetype.getPatterns().size() );
227 patterns.addAll( filetype.getPatterns() );
229 defaultTypeMap.put( filetype.getId(), patterns );
234 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
236 if ( propertyName.contains( "fileType" ) )
238 artifactPatterns = null;
240 initialiseTypeMap( archivaConfiguration.getConfiguration() );
245 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )