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