This project has retired. For details please refer to its Attic page.
PropagateErrorsDownloadPolicy xref
View Javadoc
1   package org.apache.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 org.apache.commons.lang.StringUtils;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.springframework.stereotype.Service;
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Properties;
32  
33  /**
34   * PropagateErrorsPolicy - a policy applied on error to determine how to treat the error.
35   */
36  @Service( "downloadErrorPolicy#propagate-errors" )
37  public class PropagateErrorsDownloadPolicy
38      implements DownloadErrorPolicy
39  {
40      private Logger log = LoggerFactory.getLogger( PropagateErrorsDownloadPolicy.class );
41  
42      /**
43       * Signifies any error should stop searching for other proxies.
44       */
45      public static final String STOP = "stop";
46  
47      /**
48       * Propagate errors at the end after all are gathered, if there was no successful download from other proxies.
49       */
50      public static final String QUEUE = "queue error";
51  
52      /**
53       * Ignore errors and treat as if it were not found.
54       */
55      public static final String IGNORE = "ignore";
56  
57      private List<String> options = new ArrayList<>( 3 );
58  
59      public PropagateErrorsDownloadPolicy()
60      {
61          options.add( STOP );
62          options.add( QUEUE );
63          options.add( IGNORE );
64      }
65  
66      @Override
67      public boolean applyPolicy( String policySetting, Properties request, File localFile, Exception exception,
68                                  Map<String, Exception> previousExceptions )
69          throws PolicyConfigurationException
70      {
71          if ( !options.contains( policySetting ) )
72          {
73              // Not a valid code.
74              throw new PolicyConfigurationException( "Unknown error policy setting [" + policySetting +
75                                                          "], valid settings are [" + StringUtils.join(
76                  options.iterator(), "," ) + "]" );
77          }
78  
79          if ( IGNORE.equals( policySetting ) )
80          {
81              // Ignore.
82              log.debug( "Error policy set to IGNORE." );
83              return false;
84          }
85  
86          String repositoryId = request.getProperty( "remoteRepositoryId" );
87          if ( STOP.equals( policySetting ) )
88          {
89              return true;
90          }
91  
92          if ( QUEUE.equals( policySetting ) )
93          {
94              previousExceptions.put( repositoryId, exception );
95              return true;
96          }
97  
98          throw new PolicyConfigurationException(
99              "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
100     }
101 
102     @Override
103     public String getDefaultOption()
104     {
105         return QUEUE;
106     }
107 
108     @Override
109     public String getId()
110     {
111         return "propagate-errors";
112     }
113 
114     @Override
115     public String getName()
116     {
117         return "On remote error";
118     }
119 
120     @Override
121     public List<String> getOptions()
122     {
123         return options;
124     }
125 }