Coverage Report - org.apache.maven.archiva.configuration.FileTypes
 
Classes in this File Line Coverage Branch Coverage Complexity
FileTypes
0%
0/64
0%
0/22
0
 
 1  
 package org.apache.maven.archiva.configuration;
 2  
 
 3  
 /*
 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
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 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
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.lang.reflect.Field;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Arrays;
 25  
 import java.util.Collections;
 26  
 import java.util.HashMap;
 27  
 import java.util.List;
 28  
 import java.util.Map;
 29  
 
 30  
 import org.apache.commons.collections.CollectionUtils;
 31  
 import org.apache.commons.collections.Predicate;
 32  
 import org.apache.commons.configuration.CombinedConfiguration;
 33  
 import org.apache.maven.archiva.common.utils.Slf4JPlexusLogger;
 34  
 import org.apache.maven.archiva.configuration.functors.FiletypeSelectionPredicate;
 35  
 import org.apache.maven.archiva.configuration.io.registry.ConfigurationRegistryReader;
 36  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 37  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
 38  
 import org.codehaus.plexus.registry.Registry;
 39  
 import org.codehaus.plexus.registry.RegistryException;
 40  
 import org.codehaus.plexus.registry.RegistryListener;
 41  
 import org.codehaus.plexus.registry.commons.CommonsConfigurationRegistry;
 42  
 import org.codehaus.plexus.util.SelectorUtils;
 43  
 
 44  
 /**
 45  
  * FileTypes 
 46  
  *
 47  
  * @version $Id: FileTypes.java 825390 2009-10-15 04:41:43Z brett $
 48  
  * 
 49  
  * @plexus.component role="org.apache.maven.archiva.configuration.FileTypes"
 50  
  */
 51  0
 public class FileTypes
 52  
     implements Initializable, RegistryListener
 53  
 {
 54  
     public static final String ARTIFACTS = "artifacts";
 55  
 
 56  
     public static final String AUTO_REMOVE = "auto-remove";
 57  
 
 58  
     public static final String INDEXABLE_CONTENT = "indexable-content";
 59  
 
 60  
     public static final String IGNORED = "ignored";
 61  
 
 62  
     /**
 63  
      * @plexus.requirement
 64  
      */
 65  
     private ArchivaConfiguration archivaConfiguration;
 66  
 
 67  
     /**
 68  
      * Map of default values for the file types.
 69  
      */
 70  0
     private Map<String, List<String>> defaultTypeMap = new HashMap<String, List<String>>();
 71  
 
 72  
     private List<String> artifactPatterns;
 73  
 
 74  
     /**
 75  
      * Default exclusions from artifact consumers that are using the file types. Note that this is simplistic in the
 76  
      * case of the support files (based on extension) as it is elsewhere - it may be better to match these to actual
 77  
      * artifacts and exclude later during scanning.
 78  
      */
 79  0
     public static final List<String> DEFAULT_EXCLUSIONS =
 80  
         Arrays.asList( "**/maven-metadata.xml", "**/maven-metadata-*.xml", "**/*.sha1", "**/*.asc", "**/*.md5",
 81  
                        "**/*.pgp", "**/.index/**", "**/.indexer/**" );
 82  
 
 83  
     public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
 84  
     {
 85  0
         this.archivaConfiguration = archivaConfiguration;
 86  0
     }
 87  
 
 88  
     /**
 89  
      * <p>
 90  
      * Get the list of patterns for a specified filetype.
 91  
      * </p>
 92  
      *
 93  
      * <p>
 94  
      * You will always get a list.  In this order.
 95  
      *   <ul>
 96  
      *     <li>The Configured List</li>
 97  
      *     <li>The Default List</li>
 98  
      *     <li>A single item list of <code>"**<span>/</span>*"</code></li>
 99  
      *   </ul>
 100  
      * </p>
 101  
      *
 102  
      * @param id the id to lookup.
 103  
      * @return the list of patterns.
 104  
      */
 105  
     public List<String> getFileTypePatterns( String id )
 106  
     {
 107  0
         Configuration config = archivaConfiguration.getConfiguration();
 108  0
         Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
 109  0
         FileType filetype = (FileType) CollectionUtils.find( config.getRepositoryScanning().getFileTypes(),
 110  
                                                              selectedFiletype );
 111  
 
 112  0
         if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
 113  
         {
 114  0
             return filetype.getPatterns();
 115  
         }
 116  
 
 117  0
         List<String> defaultPatterns = defaultTypeMap.get( id );
 118  
 
 119  0
         if ( CollectionUtils.isEmpty( defaultPatterns ) )
 120  
         {
 121  0
             return Collections.singletonList( "**/*" );
 122  
         }
 123  
 
 124  0
         return defaultPatterns;
 125  
     }
 126  
 
 127  
     public synchronized boolean matchesArtifactPattern( String relativePath )
 128  
     {
 129  
         // Correct the slash pattern.
 130  0
         relativePath = relativePath.replace( '\\', '/' );
 131  
 
 132  0
         if ( artifactPatterns == null )
 133  
         {
 134  0
             artifactPatterns = getFileTypePatterns( ARTIFACTS );
 135  
         }
 136  
 
 137  0
         for ( String pattern : artifactPatterns )
 138  
         {
 139  0
             if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
 140  
             {
 141  
                 // Found match
 142  0
                 return true;
 143  
             }
 144  
         }
 145  
 
 146  
         // No match.
 147  0
         return false;
 148  
     }
 149  
 
 150  
     public boolean matchesDefaultExclusions( String relativePath )
 151  
     {
 152  
         // Correct the slash pattern.
 153  0
         relativePath = relativePath.replace( '\\', '/' );
 154  
 
 155  0
         for ( String pattern : DEFAULT_EXCLUSIONS )
 156  
         {
 157  0
             if ( SelectorUtils.matchPath( pattern, relativePath, false ) )
 158  
             {
 159  
                 // Found match
 160  0
                 return true;
 161  
             }
 162  
         }
 163  
 
 164  
         // No match.
 165  0
         return false;
 166  
     }
 167  
 
 168  
     public void initialize()
 169  
         throws InitializationException
 170  
     {
 171  
         // TODO: why is this done by hand?
 172  
         
 173  
         // TODO: ideally, this would be instantiated by configuration instead, and not need to be a component
 174  
 
 175  0
         String errMsg = "Unable to load default archiva configuration for FileTypes: ";
 176  
 
 177  
         try
 178  
         {
 179  0
             CommonsConfigurationRegistry commonsRegistry = new CommonsConfigurationRegistry();
 180  
 
 181  
             // Configure commonsRegistry
 182  0
             Field fld = commonsRegistry.getClass().getDeclaredField( "configuration" );
 183  0
             fld.setAccessible( true );
 184  0
             fld.set( commonsRegistry, new CombinedConfiguration() );
 185  0
             commonsRegistry.enableLogging( new Slf4JPlexusLogger( FileTypes.class ) );
 186  0
             commonsRegistry.addConfigurationFromResource( "org/apache/maven/archiva/configuration/default-archiva.xml" );
 187  
 
 188  
             // Read configuration as it was intended.
 189  0
             ConfigurationRegistryReader configReader = new ConfigurationRegistryReader();
 190  0
             Configuration defaultConfig = configReader.read( commonsRegistry );
 191  
 
 192  0
             initialiseTypeMap( defaultConfig );
 193  
         }
 194  0
         catch ( RegistryException e )
 195  
         {
 196  0
             throw new InitializationException( errMsg + e.getMessage(), e );
 197  
         }
 198  0
         catch ( SecurityException e )
 199  
         {
 200  0
             throw new InitializationException( errMsg + e.getMessage(), e );
 201  
         }
 202  0
         catch ( NoSuchFieldException e )
 203  
         {
 204  0
             throw new InitializationException( errMsg + e.getMessage(), e );
 205  
         }
 206  0
         catch ( IllegalArgumentException e )
 207  
         {
 208  0
             throw new InitializationException( errMsg + e.getMessage(), e );
 209  
         }
 210  0
         catch ( IllegalAccessException e )
 211  
         {
 212  0
             throw new InitializationException( errMsg + e.getMessage(), e );
 213  0
         }
 214  
 
 215  0
         this.archivaConfiguration.addChangeListener( this );
 216  0
     }
 217  
 
 218  
     private void initialiseTypeMap( Configuration configuration )
 219  
     {
 220  0
         defaultTypeMap.clear();
 221  
 
 222  
         // Store the default file type declaration.
 223  0
         List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
 224  0
         for ( FileType filetype : filetypes )
 225  
         {
 226  0
             List<String> patterns = defaultTypeMap.get( filetype.getId() );
 227  0
             if ( patterns == null )
 228  
             {
 229  0
                 patterns = new ArrayList<String>();
 230  
             }
 231  0
             patterns.addAll( filetype.getPatterns() );
 232  
 
 233  0
             defaultTypeMap.put( filetype.getId(), patterns );
 234  0
         }
 235  0
     }
 236  
 
 237  
     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
 238  
     {
 239  0
         if ( propertyName.contains( "fileType" ) )
 240  
         {
 241  0
             artifactPatterns = null;
 242  
 
 243  0
             initialiseTypeMap( archivaConfiguration.getConfiguration() );
 244  
         }
 245  0
     }
 246  
 
 247  
     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
 248  
     {
 249  
         /* nothing to do */
 250  0
     }
 251  
 }