This project has retired. For details please refer to its Attic page.
PropagateErrorsOnUpdateDownloadPolicy 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.springframework.stereotype.Service;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Properties;
30  
31  /**
32   * PropagateErrorsPolicy - a policy applied on error to determine how to treat the error.
33   */
34  @Service( "downloadErrorPolicy#propagate-errors-on-update" )
35  public class PropagateErrorsOnUpdateDownloadPolicy
36      implements DownloadErrorPolicy
37  {
38      /**
39       * Signifies any error should cause a failure whether the artifact is already present or not.
40       */
41      public static final String ALWAYS = "always";
42  
43      /**
44       * Signifies any error should cause a failure only if the artifact is not already present.
45       */
46      public static final String NOT_PRESENT = "artifact not already present";
47  
48      private List<String> options = new ArrayList<>( 2 );
49  
50      public PropagateErrorsOnUpdateDownloadPolicy()
51      {
52          options.add( ALWAYS );
53          options.add( NOT_PRESENT );
54      }
55  
56      @Override
57      public boolean applyPolicy( String policySetting, Properties request, File localFile, Exception exception,
58                                  Map<String, Exception> previousExceptions )
59          throws PolicyConfigurationException
60      {
61          if ( !options.contains( policySetting ) )
62          {
63              // Not a valid code.
64              throw new PolicyConfigurationException(
65                  "Unknown error policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
66                      options.iterator(), "," ) + "]" );
67          }
68  
69          if ( ALWAYS.equals( policySetting ) )
70          {
71              // throw ther exception regardless
72              return true;
73          }
74  
75          if ( NOT_PRESENT.equals( policySetting ) )
76          {
77              // cancel the exception if the file exists
78              return !localFile.exists();
79          }
80  
81          throw new PolicyConfigurationException(
82              "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
83      }
84  
85      @Override
86      public String getDefaultOption()
87      {
88          return NOT_PRESENT;
89      }
90  
91      @Override
92      public String getId()
93      {
94          return "propagate-errors-on-update";
95      }
96  
97      @Override
98      public String getName()
99      {
100         return "Return error when";
101     }
102 
103     @Override
104     public List<String> getOptions()
105     {
106         return options;
107     }
108 }