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.commons.collections.CollectionUtils;
26 import org.apache.commons.collections.Predicate;
27 import org.apache.commons.configuration.CombinedConfiguration;
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.
83 public static final List<String> DEFAULT_EXCLUSIONS = FileTypeUtils.DEFAULT_EXCLUSIONS;
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( "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>( filetype.getPatterns().size() );
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 )