View Javadoc
1   package org.eclipse.aether.repository;
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  /**
23   * A policy controlling access to a repository.
24   */
25  public final class RepositoryPolicy
26  {
27  
28      /**
29       * Never update locally cached data.
30       */
31      public static final String UPDATE_POLICY_NEVER = "never";
32  
33      /**
34       * Always update locally cached data.
35       */
36      public static final String UPDATE_POLICY_ALWAYS = "always";
37  
38      /**
39       * Update locally cached data once a day.
40       */
41      public static final String UPDATE_POLICY_DAILY = "daily";
42  
43      /**
44       * Update locally cached data every X minutes as given by "interval:X".
45       */
46      public static final String UPDATE_POLICY_INTERVAL = "interval";
47  
48      /**
49       * Verify checksums and fail the resolution if they do not match.
50       */
51      public static final String CHECKSUM_POLICY_FAIL = "fail";
52  
53      /**
54       * Verify checksums and warn if they do not match.
55       */
56      public static final String CHECKSUM_POLICY_WARN = "warn";
57  
58      /**
59       * Do not verify checksums.
60       */
61      public static final String CHECKSUM_POLICY_IGNORE = "ignore";
62  
63      private final boolean enabled;
64  
65      private final String updatePolicy;
66  
67      private final String checksumPolicy;
68  
69      /**
70       * Creates a new policy with checksum warnings and daily update checks.
71       */
72      public RepositoryPolicy()
73      {
74          this( true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN );
75      }
76  
77      /**
78       * Creates a new policy with the specified settings.
79       * 
80       * @param enabled A flag whether the associated repository should be accessed or not.
81       * @param updatePolicy The update interval after which locally cached data from the repository is considered stale
82       *            and should be refetched, may be {@code null}.
83       * @param checksumPolicy The way checksum verification should be handled, may be {@code null}.
84       */
85      public RepositoryPolicy( boolean enabled, String updatePolicy, String checksumPolicy )
86      {
87          this.enabled = enabled;
88          this.updatePolicy = ( updatePolicy != null ) ? updatePolicy : "";
89          this.checksumPolicy = ( checksumPolicy != null ) ? checksumPolicy : "";
90      }
91  
92      /**
93       * Indicates whether the associated repository should be contacted or not.
94       * 
95       * @return {@code true} if the repository should be contacted, {@code false} otherwise.
96       */
97      public boolean isEnabled()
98      {
99          return enabled;
100     }
101 
102     /**
103      * Gets the update policy for locally cached data from the repository.
104      * 
105      * @return The update policy, never {@code null}.
106      */
107     public String getUpdatePolicy()
108     {
109         return updatePolicy;
110     }
111 
112     /**
113      * Gets the policy for checksum validation.
114      * 
115      * @return The checksum policy, never {@code null}.
116      */
117     public String getChecksumPolicy()
118     {
119         return checksumPolicy;
120     }
121 
122     @Override
123     public String toString()
124     {
125         StringBuilder buffer = new StringBuilder( 256 );
126         buffer.append( "enabled=" ).append( isEnabled() );
127         buffer.append( ", checksums=" ).append( getChecksumPolicy() );
128         buffer.append( ", updates=" ).append( getUpdatePolicy() );
129         return buffer.toString();
130     }
131 
132     @Override
133     public boolean equals( Object obj )
134     {
135         if ( this == obj )
136         {
137             return true;
138         }
139 
140         if ( obj == null || !getClass().equals( obj.getClass() ) )
141         {
142             return false;
143         }
144 
145         RepositoryPolicy that = (RepositoryPolicy) obj;
146 
147         return enabled == that.enabled && updatePolicy.equals( that.updatePolicy )
148             && checksumPolicy.equals( that.checksumPolicy );
149     }
150 
151     @Override
152     public int hashCode()
153     {
154         int hash = 17;
155         hash = hash * 31 + ( enabled ? 1 : 0 );
156         hash = hash * 31 + updatePolicy.hashCode();
157         hash = hash * 31 + checksumPolicy.hashCode();
158         return hash;
159     }
160 
161 }