Coverage Report - org.apache.maven.archiva.policies.CachedFailuresPolicy
 
Classes in this File Line Coverage Branch Coverage Complexity
CachedFailuresPolicy
0%
0/21
0%
0/8
0
 
 1  
 package org.apache.maven.archiva.policies;
 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.io.File;
 23  
 import java.util.ArrayList;
 24  
 import java.util.List;
 25  
 import java.util.Properties;
 26  
 
 27  
 import org.apache.commons.lang.StringUtils;
 28  
 import org.apache.maven.archiva.policies.urlcache.UrlFailureCache;
 29  
 import org.slf4j.Logger;
 30  
 import org.slf4j.LoggerFactory;
 31  
 
 32  
 /**
 33  
  * {@link PreDownloadPolicy} to check if the requested url has failed before.
 34  
  *
 35  
  * @version $Id: CachedFailuresPolicy.java 718864 2008-11-19 06:33:35Z brett $
 36  
  * @plexus.component role="org.apache.maven.archiva.policies.PreDownloadPolicy"
 37  
  * role-hint="cache-failures"
 38  
  */
 39  
 public class CachedFailuresPolicy
 40  
     implements PreDownloadPolicy
 41  
 {
 42  0
     private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class );
 43  
     
 44  
     /**
 45  
      * The NO policy setting means that the the existence of old failures is <strong>not</strong> checked.
 46  
      * All resource requests are allowed thru to the remote repo.
 47  
      */
 48  
     public static final String NO = "no";
 49  
 
 50  
     /**
 51  
      * The YES policy setting means that the existence of old failures is checked, and will
 52  
      * prevent the request from being performed against the remote repo.
 53  
      */
 54  
     public static final String YES = "yes";
 55  
 
 56  
     /**
 57  
      * @plexus.requirement
 58  
      */
 59  
     private UrlFailureCache urlFailureCache;
 60  
 
 61  0
     private List<String> options = new ArrayList<String>();
 62  
 
 63  
     public CachedFailuresPolicy()
 64  0
     {
 65  0
         options.add( NO );
 66  0
         options.add( YES );
 67  0
     }
 68  
 
 69  
     public void applyPolicy( String policySetting, Properties request, File localFile )
 70  
         throws PolicyViolationException, PolicyConfigurationException
 71  
     {
 72  0
         if ( !options.contains( policySetting ) )
 73  
         {
 74  
             // Not a valid code.
 75  0
             throw new PolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting +
 76  
                 "], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" );
 77  
         }
 78  
 
 79  0
         if ( NO.equals( policySetting ) )
 80  
         {
 81  
             // Skip.
 82  0
             log.debug( "OK to fetch, check-failures policy set to NO." );
 83  0
             return;
 84  
         }
 85  
 
 86  0
         String url = request.getProperty( "url" );
 87  
 
 88  0
         if ( StringUtils.isNotBlank( url ) )
 89  
         {
 90  0
             if ( urlFailureCache.hasFailedBefore( url ) )
 91  
             {
 92  0
                 throw new PolicyViolationException(
 93  
                     "NO to fetch, check-failures detected previous failure on url: " + url );
 94  
             }
 95  
         }
 96  
 
 97  0
         log.debug( "OK to fetch, check-failures detected no issues." );
 98  0
     }
 99  
 
 100  
     public String getDefaultOption()
 101  
     {
 102  0
         return NO;
 103  
     }
 104  
 
 105  
     public String getId()
 106  
     {
 107  0
         return "cache-failures";
 108  
     }
 109  
 
 110  
     public String getName()
 111  
     {
 112  0
         return "Cache failures";
 113  
     }
 114  
 
 115  
     public List<String> getOptions()
 116  
     {
 117  0
         return options;
 118  
     }
 119  
 }