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