View Javadoc

1   package org.sonatype.aether.repository;
2   
3   /*******************************************************************************
4    * Copyright (c) 2010-2011 Sonatype, Inc.
5    * All rights reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    *   http://www.eclipse.org/legal/epl-v10.html
9    *******************************************************************************/
10  
11  /**
12   * A policy controlling access to a repository. <em>Note:</em> Instances of this class are immutable and the exposed
13   * mutators return new objects rather than changing the current instance.
14   * 
15   * @author Benjamin Bentmann
16   */
17  public final class RepositoryPolicy
18  {
19  
20      /**
21       * Never update locally cached data.
22       */
23      public static final String UPDATE_POLICY_NEVER = "never";
24  
25      /**
26       * Always update locally cached data.
27       */
28      public static final String UPDATE_POLICY_ALWAYS = "always";
29  
30      /**
31       * Update locally cached data once a day.
32       */
33      public static final String UPDATE_POLICY_DAILY = "daily";
34  
35      /**
36       * Update locally cached data every X minutes as given by "interval:X".
37       */
38      public static final String UPDATE_POLICY_INTERVAL = "interval";
39  
40      /**
41       * Verify checksums and fail the resolution if they do not match.
42       */
43      public static final String CHECKSUM_POLICY_FAIL = "fail";
44  
45      /**
46       * Verify checksums and warn if they do not match.
47       */
48      public static final String CHECKSUM_POLICY_WARN = "warn";
49  
50      /**
51       * Do not verify checksums.
52       */
53      public static final String CHECKSUM_POLICY_IGNORE = "ignore";
54  
55      private final boolean enabled;
56  
57      private final String updatePolicy;
58  
59      private final String checksumPolicy;
60  
61      /**
62       * Creates a new policy with checksum warnings and daily update checks.
63       */
64      public RepositoryPolicy()
65      {
66          this( true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN );
67      }
68  
69      /**
70       * Creates a new policy with the specified settings.
71       * 
72       * @param enabled A flag whether the associated repository should be accessed or not.
73       * @param updatePolicy The update interval after which locally cached data from the repository is considered stale
74       *            and should be refetched, may be {@code null}.
75       * @param checksumPolicy The way checksum verification should be handled, may be {@code null}.
76       */
77      public RepositoryPolicy( boolean enabled, String updatePolicy, String checksumPolicy )
78      {
79          this.enabled = enabled;
80          this.updatePolicy = ( updatePolicy != null ) ? updatePolicy : "";
81          this.checksumPolicy = ( checksumPolicy != null ) ? checksumPolicy : "";
82      }
83  
84      /**
85       * Indicates whether the associated repository should be contacted or not.
86       * 
87       * @return {@code true} if the repository should be contacted, {@code false} otherwise.
88       */
89      public boolean isEnabled()
90      {
91          return enabled;
92      }
93  
94      /**
95       * Sets the enabled flag for the associated repository.
96       * 
97       * @param enabled {@code true} if the repository should be contacted, {@code false} otherwise.
98       * @return The new policy, never {@code null}.
99       */
100     public RepositoryPolicy setEnabled( boolean enabled )
101     {
102         if ( this.enabled == enabled )
103         {
104             return this;
105         }
106         return new RepositoryPolicy( enabled, updatePolicy, checksumPolicy );
107     }
108 
109     /**
110      * Gets the update policy for locally cached data from the repository.
111      * 
112      * @return The update policy, never {@code null}.
113      */
114     public String getUpdatePolicy()
115     {
116         return updatePolicy;
117     }
118 
119     /**
120      * Sets the update policy for locally cached data from the repository. Well-known policies are
121      * {@link #UPDATE_POLICY_NEVER}, {@link #UPDATE_POLICY_ALWAYS}, {@link #UPDATE_POLICY_DAILY} and
122      * {@link #UPDATE_POLICY_INTERVAL}
123      * 
124      * @param updatePolicy The update policy, may be {@code null}.
125      * @return The new policy, never {@code null}.
126      */
127     public RepositoryPolicy setUpdatePolicy( String updatePolicy )
128     {
129         if ( this.updatePolicy.equals( updatePolicy ) )
130         {
131             return this;
132         }
133         return new RepositoryPolicy( enabled, updatePolicy, checksumPolicy );
134     }
135 
136     /**
137      * Gets the policy for checksum validation.
138      * 
139      * @return The checksum policy, never {@code null}.
140      */
141     public String getChecksumPolicy()
142     {
143         return checksumPolicy;
144     }
145 
146     /**
147      * Sets the policy for checksum validation. Well-known policies are {@link #CHECKSUM_POLICY_FAIL},
148      * {@link #CHECKSUM_POLICY_WARN} and {@link #CHECKSUM_POLICY_IGNORE}.
149      * 
150      * @param checksumPolicy The checksum policy, may be {@code null}.
151      * @return The new policy, never {@code null}.
152      */
153     public RepositoryPolicy setChecksumPolicy( String checksumPolicy )
154     {
155         if ( this.checksumPolicy.equals( checksumPolicy ) )
156         {
157             return this;
158         }
159         return new RepositoryPolicy( enabled, updatePolicy, checksumPolicy );
160     }
161 
162     @Override
163     public String toString()
164     {
165         StringBuilder buffer = new StringBuilder( 256 );
166         buffer.append( "enabled=" ).append( isEnabled() );
167         buffer.append( ", checksums=" ).append( getChecksumPolicy() );
168         buffer.append( ", updates=" ).append( getUpdatePolicy() );
169         return buffer.toString();
170     }
171 
172     @Override
173     public boolean equals( Object obj )
174     {
175         if ( this == obj )
176         {
177             return true;
178         }
179 
180         if ( obj == null || !getClass().equals( obj.getClass() ) )
181         {
182             return false;
183         }
184 
185         RepositoryPolicy that = (RepositoryPolicy) obj;
186 
187         return enabled == that.enabled && updatePolicy.equals( that.updatePolicy )
188             && checksumPolicy.equals( that.checksumPolicy );
189     }
190 
191     @Override
192     public int hashCode()
193     {
194         int hash = 17;
195         hash = hash * 31 + ( enabled ? 1 : 0 );
196         hash = hash * 31 + updatePolicy.hashCode();
197         hash = hash * 31 + checksumPolicy.hashCode();
198         return hash;
199     }
200 
201 }