1 package org.apache.maven.archiva.policies;
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.commons.lang.StringUtils;
23 import org.apache.maven.archiva.common.spring.SpringFactory;
24 import org.apache.maven.archiva.policies.urlcache.UrlFailureCache;
25 import org.codehaus.plexus.logging.AbstractLogEnabled;
26 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
27 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Properties;
35 * {@link PreDownloadPolicy} to check if the requested url has failed before.
37 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
39 * @plexus.component role="org.apache.maven.archiva.policies.PreDownloadPolicy"
40 * role-hint="cache-failures"
42 public class CachedFailuresPolicy
43 extends AbstractLogEnabled
44 implements PreDownloadPolicy, Initializable
47 * The NO policy setting means that the the existence of old failures is <strong>not</strong> checked.
48 * All resource requests are allowed thru to the remote repo.
50 public static final String NO = "no";
53 * The YES policy setting means that the existence of old failures is checked, and will
54 * prevent the request from being performed against the remote repo.
56 public static final String YES = "yes";
58 private UrlFailureCache urlFailureCache;
60 private List<String> options = new ArrayList<String>();
65 private SpringFactory springFactory;
67 public CachedFailuresPolicy()
73 public void applyPolicy( String policySetting, Properties request, File localFile )
74 throws PolicyViolationException, PolicyConfigurationException
76 if ( !options.contains( policySetting ) )
79 throw new PolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting +
80 "], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" );
83 if ( NO.equals( policySetting ) )
86 getLogger().debug( "OK to fetch, check-failures policy set to NO." );
90 String url = request.getProperty( "url" );
92 if ( StringUtils.isNotBlank( url ) )
94 if ( urlFailureCache.hasFailedBefore( url ) )
96 throw new PolicyViolationException(
97 "NO to fetch, check-failures detected previous failure on url: " + url );
101 getLogger().debug( "OK to fetch, check-failures detected no issues." );
104 public String getDefaultOption()
109 public String getId()
111 return "cache-failures";
114 public List<String> getOptions()
119 public void initialize()
120 throws InitializationException
122 urlFailureCache = (UrlFailureCache) springFactory.lookup( "urlFailureCache" );