1 package org.apache.maven.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 java.lang.reflect.Field;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
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.maven.archiva.common.utils.Slf4JPlexusLogger;
33 import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
34 import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryReader;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
36 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
37 import org.codehaus.plexus.registry.Registry;
38 import org.codehaus.plexus.registry.RegistryException;
39 import org.codehaus.plexus.registry.RegistryListener;
40 import org.codehaus.plexus.registry.commons.CommonsConfigurationRegistry;
41 import org.codehaus.plexus.util.SelectorUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
48 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
51 * @plexus.component role="org.apache.maven.archiva.configuration.FileTypes"
53 public class FileTypes
54 implements Initializable, RegistryListener
56 private Logger log = LoggerFactory.getLogger(FileTypes.class);
58 public static final String ARTIFACTS = "artifacts";
60 public static final String AUTO_REMOVE = "auto-remove";
62 public static final String INDEXABLE_CONTENT = "indexable-content";
64 public static final String IGNORED = "ignored";
69 private ArchivaConfiguration archivaConfiguration;
72 * Map of default values for the file types.
74 private Map<String, List<String>> defaultTypeMap = new HashMap<String, List<String>>();
76 private List<String> artifactPatterns;
78 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
80 this.archivaConfiguration = archivaConfiguration;
85 * 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>"**<span>/</span>*"</code></li>
97 * @param id the id to lookup.
98 * @return the list of patterns.
100 public List<String> getFileTypePatterns( String id )
102 Configuration config = archivaConfiguration.getConfiguration();
103 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
104 FileType filetype = (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(),
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 void initialize()
146 throws InitializationException
148 // TODO: why is this done by hand?
150 String errMsg = "Unable to load default archiva configuration for FileTypes: ";
154 CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
156 // Configure commonsRegistry
157 Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
158 fld.setAccessible( true );
159 fld.set( commonsRegistry, new CombinedConfiguration() );
160 commonsRegistry.enableLogging( new Slf4JPlexusLogger( FileTypes.class ) );
161 commonsRegistry.addConfigurationFromResource( "org/apache/maven/archiva/configuration/default-archiva.xml" );
163 // Read configuration as it was intended.
164 ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
165 Configuration defaultConfig = configReader.read( commonsRegistry );
167 initialiseTypeMap( defaultConfig );
169 catch ( RegistryException e )
171 throw new InitializationException( errMsg + e.getMessage(), e );
173 catch ( SecurityException e )
175 throw new InitializationException( errMsg + e.getMessage(), e );
177 catch ( NoSuchFieldException e )
179 throw new InitializationException( errMsg + e.getMessage(), e );
181 catch ( IllegalArgumentException e )
183 throw new InitializationException( errMsg + e.getMessage(), e );
185 catch ( IllegalAccessException e )
187 throw new InitializationException( errMsg + e.getMessage(), e );
190 this.archivaConfiguration.addChangeListener( this );
193 private void initialiseTypeMap( Configuration configuration )
195 defaultTypeMap.clear();
197 // Store the default file type declaration.
198 List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
199 for ( FileType filetype : filetypes )
201 List<String> patterns = defaultTypeMap.get( filetype.getId() );
202 if ( patterns == null )
204 patterns = new ArrayList<String>();
206 patterns.addAll( filetype.getPatterns() );
208 defaultTypeMap.put( filetype.getId(), patterns );
212 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
214 if ( propertyName.contains( "fileType" ) )
216 artifactPatterns = null;
218 initialiseTypeMap( archivaConfiguration.getConfiguration() );
222 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )