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.configuration.util.PathMatcher;
26 import org.apache.archiva.components.registry.Registry;
27 import org.apache.archiva.components.registry.RegistryException;
28 import org.apache.archiva.components.registry.RegistryListener;
29 import org.apache.archiva.components.registry.commons.CommonsConfigurationRegistry;
30 import org.apache.commons.collections4.CollectionUtils;
31 import org.apache.commons.collections4.IterableUtils;
32 import org.apache.commons.collections4.Predicate;
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;
70 * Map of default values for the file types.
72 private Map<String, List<String>> defaultTypeMap = new HashMap<>();
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;
91 * Get the list of patterns for a specified filetype.
92 * You will always get a list. In this order.
94 * <li>The Configured List</li>
95 * <li>The Default List</li>
96 * <li>A single item list of <code>"**/*"</code></li>
99 * @param id the id to lookup.
100 * @return the list of patterns.
102 public List<String> getFileTypePatterns( String id )
104 Configuration config = archivaConfiguration.getConfiguration();
105 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
106 RepositoryScanningConfiguration repositoryScanningConfiguration = config.getRepositoryScanning();
107 if ( repositoryScanningConfiguration != null )
110 IterableUtils.find( config.getRepositoryScanning().getFileTypes(), selectedFiletype );
112 if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
114 return filetype.getPatterns();
117 List<String> defaultPatterns = defaultTypeMap.get( id );
119 if ( CollectionUtils.isEmpty( defaultPatterns ) )
121 return Collections.singletonList( "**/*" );
124 return defaultPatterns;
127 public synchronized boolean matchesArtifactPattern( String relativePath )
129 // Correct the slash pattern.
130 relativePath = relativePath.replace( '\\', '/' );
132 if ( artifactPatterns == null )
134 artifactPatterns = getFileTypePatterns( ARTIFACTS );
137 for ( String pattern : artifactPatterns )
139 if ( PathMatcher.matchPath( pattern, relativePath, false ) )
150 public boolean matchesDefaultExclusions( String relativePath )
152 // Correct the slash pattern.
153 relativePath = relativePath.replace( '\\', '/' );
155 for ( String pattern : DEFAULT_EXCLUSIONS )
157 if ( PathMatcher.matchPath( pattern, relativePath, false ) )
169 public void initialize()
171 initialiseTypeMap( this.archivaConfiguration.getConfiguration() );
173 this.archivaConfiguration.addChangeListener( this );
176 private void initialiseTypeMap( Configuration configuration )
178 defaultTypeMap.clear();
180 // Store the default file type declaration.
181 List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
182 for ( FileType filetype : filetypes )
184 List<String> patterns = defaultTypeMap.get( filetype.getId() );
185 if ( patterns == null )
187 patterns = new ArrayList<>( filetype.getPatterns().size() );
189 patterns.addAll( filetype.getPatterns() );
191 defaultTypeMap.put( filetype.getId(), patterns );
196 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
198 if ( propertyName.contains( "fileType" ) )
200 artifactPatterns = null;
202 initialiseTypeMap( archivaConfiguration.getConfiguration() );
207 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )