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 ( ordinalOfChecksumPolicy( policy.getChecksumPolicy() ) < ordinalOfChecksumPolicy( 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 ordinalOfChecksumPolicy( 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     private int ordinalOfUpdatePolicy( String policy )
202     {
203         if ( ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) )
204         {
205             return 1440;
206         }
207         else if ( ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) )
208         {
209             return 0;
210         }
211         else if ( policy != null && policy.startsWith( ArtifactRepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
212         {
213             String s = policy.substring( UPDATE_POLICY_INTERVAL.length() + 1 );
214             return Integer.valueOf( s );
215         }
216         else
217         {
218             return Integer.MAX_VALUE;
219         }
220     }
221 
222 }