1 package org.apache.maven.repository.proxy;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.repository.ArtifactRepository;
20 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
22 import java.util.Calendar;
23 import java.util.HashMap;
27 * A proxied artifact repository - contains the artifact repository and additional information about
28 * the proxied repository.
30 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
32 public class ProxiedArtifactRepository
35 * Whether to cache failures or not.
37 private boolean cacheFailures;
40 * Whether failures on this repository cause the whole group to fail.
42 private boolean hardFail;
45 * Whether to use the network proxy for any requests.
47 private boolean useNetworkProxy;
50 * The artifact repository on the other end of the proxy.
52 private final ArtifactRepository repository;
55 * Cache of failures that have already occurred, containing paths from the repository root. The value given
56 * specifies when the failure should expire.
58 private Map/*<String,Long>*/ failureCache = new HashMap/*<String,Long>*/();
61 * A user friendly name for the repository.
65 public ProxiedArtifactRepository( ArtifactRepository repository )
67 this.repository = repository;
70 public boolean isHardFail()
75 public boolean isUseNetworkProxy()
77 return useNetworkProxy;
80 public boolean isCacheFailures()
85 public ArtifactRepository getRepository()
91 * Check if there is a previously cached failure for requesting the given path.
93 * @param path the path
94 * @return whether there is a failure
96 public boolean isCachedFailure( String path )
98 boolean failed = false;
101 Long time = (Long) failureCache.get( path );
104 if ( System.currentTimeMillis() < time.longValue() )
110 clearFailure( path );
118 * Add a failure to the cache.
120 * @param path the path that failed
121 * @param policy the policy for when the failure should expire
123 public void addFailure( String path, ArtifactRepositoryPolicy policy )
125 failureCache.put( path, new Long( calculateExpiryTime( policy ) ) );
128 private long calculateExpiryTime( ArtifactRepositoryPolicy policy )
130 String updatePolicy = policy.getUpdatePolicy();
132 if ( ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( updatePolicy ) )
136 else if ( ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY.equals( updatePolicy ) )
138 // Get midnight boundary
139 Calendar cal = Calendar.getInstance();
140 cal.set( Calendar.HOUR_OF_DAY, 0 );
141 cal.set( Calendar.MINUTE, 0 );
142 cal.set( Calendar.SECOND, 0 );
143 cal.set( Calendar.MILLISECOND, 0 );
144 cal.add( Calendar.DAY_OF_MONTH, 1 );
145 time = cal.getTime().getTime();
147 else if ( updatePolicy.startsWith( ArtifactRepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
149 String s = updatePolicy.substring( ArtifactRepositoryPolicy.UPDATE_POLICY_INTERVAL.length() + 1 );
150 int minutes = Integer.valueOf( s ).intValue();
151 Calendar cal = Calendar.getInstance();
152 cal.add( Calendar.MINUTE, minutes );
153 time = cal.getTime().getTime();
157 // else assume "never"
158 time = Long.MAX_VALUE;
166 * @param path the path that had previously failed
168 public void clearFailure( String path )
170 failureCache.remove( path );
173 public String getName()
178 public void setCacheFailures( boolean cacheFailures )
180 this.cacheFailures = cacheFailures;
183 public void setHardFail( boolean hardFail )
185 this.hardFail = hardFail;
188 public void setUseNetworkProxy( boolean useNetworkProxy )
190 this.useNetworkProxy = useNetworkProxy;
193 public void setName( String name )