View Javadoc

1   package org.apache.maven.artifact.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  import java.util.Calendar;
23  import java.util.Date;
24  import java.util.TimeZone;
25  
26  /**
27   * Describes a set of policies for a repository to use under certain conditions.
28   *
29   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
30   */
31  public class ArtifactRepositoryPolicy
32  {
33      public static final String UPDATE_POLICY_NEVER = "never";
34  
35      public static final String UPDATE_POLICY_ALWAYS = "always";
36  
37      public static final String UPDATE_POLICY_DAILY = "daily";
38  
39      public static final String UPDATE_POLICY_INTERVAL = "interval";
40  
41      public static final String CHECKSUM_POLICY_FAIL = "fail";
42  
43      public static final String CHECKSUM_POLICY_WARN = "warn";
44  
45      public static final String CHECKSUM_POLICY_IGNORE = "ignore";
46  
47      private boolean enabled;
48  
49      private String updatePolicy;
50  
51      private String checksumPolicy;
52  
53      public ArtifactRepositoryPolicy()
54      {
55          this( true, null, null );
56      }
57  
58      public ArtifactRepositoryPolicy( ArtifactRepositoryPolicy policy )
59      {
60          this( policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy() );
61      }
62  
63      public ArtifactRepositoryPolicy( boolean enabled, String updatePolicy, String checksumPolicy )
64      {
65          this.enabled = enabled;
66  
67          if ( updatePolicy == null )
68          {
69              updatePolicy = UPDATE_POLICY_DAILY;
70          }
71          this.updatePolicy = updatePolicy;
72  
73          if ( checksumPolicy == null )
74          {
75              checksumPolicy = CHECKSUM_POLICY_WARN;
76          }
77          this.checksumPolicy = checksumPolicy;
78      }
79  
80      public void setEnabled( boolean enabled )
81      {
82          this.enabled = enabled;
83      }
84  
85      public void setUpdatePolicy( String updatePolicy )
86      {
87          if ( updatePolicy != null )
88          {
89              this.updatePolicy = updatePolicy;
90          }
91      }
92  
93      public void setChecksumPolicy( String checksumPolicy )
94      {
95          if ( checksumPolicy != null )
96          {
97              this.checksumPolicy = checksumPolicy;
98          }
99      }
100 
101     public boolean isEnabled()
102     {
103         return enabled;
104     }
105 
106     public String getUpdatePolicy()
107     {
108         return updatePolicy;
109     }
110 
111     public String getChecksumPolicy()
112     {
113         return checksumPolicy;
114     }
115 
116     public boolean checkOutOfDate( Date lastModified )
117     {
118         boolean checkForUpdates = false;
119 
120         if ( UPDATE_POLICY_ALWAYS.equals( updatePolicy ) )
121         {
122             checkForUpdates = true;
123         }
124         else if ( UPDATE_POLICY_DAILY.equals( updatePolicy ) )
125         {
126             // Get local midnight boundary
127             Calendar cal = Calendar.getInstance();
128 
129             cal.set( Calendar.HOUR_OF_DAY, 0 );
130             cal.set( Calendar.MINUTE, 0 );
131             cal.set( Calendar.SECOND, 0 );
132             cal.set( Calendar.MILLISECOND, 0 );
133 
134             if ( cal.getTime().after( lastModified ) )
135             {
136                 checkForUpdates = true;
137             }
138         }
139         else if ( updatePolicy.startsWith( UPDATE_POLICY_INTERVAL ) )
140         {
141             String s = updatePolicy.substring( UPDATE_POLICY_INTERVAL.length() + 1 );
142             int minutes = Integer.valueOf( s );
143             Calendar cal = Calendar.getInstance();
144             cal.add( Calendar.MINUTE, -minutes );
145             if ( cal.getTime().after( lastModified ) )
146             {
147                 checkForUpdates = true;
148             }
149         }
150         // else assume "never"
151         return checkForUpdates;
152     }
153 
154     @Override
155     public String toString()
156     {
157         StringBuilder buffer = new StringBuilder( 64 );
158         buffer.append( "{enabled=" );
159         buffer.append( enabled );
160         buffer.append( ", checksums=" );
161         buffer.append( checksumPolicy );
162         buffer.append( ", updates=" );
163         buffer.append( updatePolicy );
164         buffer.append( "}" );
165         return buffer.toString();
166     }
167 
168     public void merge( ArtifactRepositoryPolicy policy )
169     {
170         if ( policy != null && policy.isEnabled() )
171         {
172             setEnabled( true );
173 
174             if ( ordinalOfChecksumPolicy( policy.getChecksumPolicy() ) < ordinalOfChecksumPolicy( getChecksumPolicy() ) )
175             {
176                 setChecksumPolicy( policy.getChecksumPolicy() );
177             }
178 
179             if ( ordinalOfUpdatePolicy( policy.getUpdatePolicy() ) < ordinalOfUpdatePolicy( getUpdatePolicy() ) )
180             {
181                 setUpdatePolicy( policy.getUpdatePolicy() );
182             }
183         }
184     }
185 
186     private int ordinalOfChecksumPolicy( String policy )
187     {
188         if ( ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
189         {
190             return 2;
191         }
192         else if ( ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
193         {
194             return 0;
195         }
196         else
197         {
198             return 1;
199         }
200     }
201 
202     private int ordinalOfUpdatePolicy( String policy )
203     {
204         if ( ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) )
205         {
206             return 1440;
207         }
208         else if ( ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) )
209         {
210             return 0;
211         }
212         else if ( policy != null && policy.startsWith( ArtifactRepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
213         {
214             String s = policy.substring( UPDATE_POLICY_INTERVAL.length() + 1 );
215             return Integer.valueOf( s );
216         }
217         else
218         {
219             return Integer.MAX_VALUE;
220         }
221     }
222 
223 }