1 package org.apache.maven.archiva.proxy;
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.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
25 import java.util.Calendar;
26 import java.util.HashMap;
30 * A proxied artifact repository - contains the artifact repository and additional information about
31 * the proxied repository.
33 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
35 public class ProxiedArtifactRepository
38 * Whether to cache failures or not.
40 private boolean cacheFailures;
43 * Whether failures on this repository cause the whole group to fail.
45 private boolean hardFail;
48 * Whether to use the network proxy for any requests.
50 private boolean useNetworkProxy;
53 * The artifact repository on the other end of the proxy.
55 private final ArtifactRepository repository;
58 * Cache of failures that have already occurred, containing paths from the repository root. The value given
59 * specifies when the failure should expire.
61 private Map/*<String,Long>*/ failureCache = new HashMap/*<String,Long>*/();
64 * A user friendly name for the repository.
68 public ProxiedArtifactRepository( ArtifactRepository repository )
70 this.repository = repository;
73 public boolean isHardFail()
78 public boolean isUseNetworkProxy()
80 return useNetworkProxy;
83 public boolean isCacheFailures()
88 public ArtifactRepository getRepository()
94 * Check if there is a previously cached failure for requesting the given path.
96 * @param path the path
97 * @return whether there is a failure
99 public boolean isCachedFailure( String path )
101 boolean failed = false;
104 Long time = (Long) failureCache.get( path );
107 if ( System.currentTimeMillis() < time.longValue() )
113 clearFailure( path );
121 * Add a failure to the cache.
123 * @param path the path that failed
124 * @param policy the policy for when the failure should expire
126 public void addFailure( String path, ArtifactRepositoryPolicy policy )
128 failureCache.put( path, new Long( calculateExpiryTime( policy ) ) );
131 private long calculateExpiryTime( ArtifactRepositoryPolicy policy )
133 String updatePolicy = policy.getUpdatePolicy();
135 if ( ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( updatePolicy ) )
139 else if ( ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY.equals( updatePolicy ) )
141 // Get midnight boundary
142 Calendar cal = Calendar.getInstance();
143 cal.set( Calendar.HOUR_OF_DAY, 0 );
144 cal.set( Calendar.MINUTE, 0 );
145 cal.set( Calendar.SECOND, 0 );
146 cal.set( Calendar.MILLISECOND, 0 );
147 cal.add( Calendar.DAY_OF_MONTH, 1 );
148 time = cal.getTime().getTime();
150 else if ( updatePolicy.startsWith( ArtifactRepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
152 String s = updatePolicy.substring( ArtifactRepositoryPolicy.UPDATE_POLICY_INTERVAL.length() + 1 );
153 int minutes = Integer.valueOf( s ).intValue();
154 Calendar cal = Calendar.getInstance();
155 cal.add( Calendar.MINUTE, minutes );
156 time = cal.getTime().getTime();
160 // else assume "never"
161 time = Long.MAX_VALUE;
169 * @param path the path that had previously failed
171 public void clearFailure( String path )
173 failureCache.remove( path );
176 public String getName()
181 public void setCacheFailures( boolean cacheFailures )
183 this.cacheFailures = cacheFailures;
186 public void setHardFail( boolean hardFail )
188 this.hardFail = hardFail;
191 public void setUseNetworkProxy( boolean useNetworkProxy )
193 this.useNetworkProxy = useNetworkProxy;
196 public void setName( String name )