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   * @version $Id: ArtifactRepositoryPolicy.java 640549 2008-03-24 20:05:11Z bentmann $
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( boolean enabled, String updatePolicy, String checksumPolicy )
59      {
60          this.enabled = enabled;
61  
62          if ( updatePolicy == null )
63          {
64              updatePolicy = UPDATE_POLICY_DAILY;
65          }
66          this.updatePolicy = updatePolicy;
67  
68          if ( checksumPolicy == null )
69          {
70              checksumPolicy = CHECKSUM_POLICY_WARN;
71          }
72          this.checksumPolicy = checksumPolicy;
73      }
74  
75      public void setEnabled( boolean enabled )
76      {
77          this.enabled = enabled;
78      }
79  
80      public void setUpdatePolicy( String updatePolicy )
81      {
82          this.updatePolicy = updatePolicy;
83      }
84  
85      public void setChecksumPolicy( String checksumPolicy )
86      {
87          this.checksumPolicy = checksumPolicy;
88      }
89  
90      public boolean isEnabled()
91      {
92          return enabled;
93      }
94  
95      public String getUpdatePolicy()
96      {
97          return updatePolicy;
98      }
99  
100     public String getChecksumPolicy()
101     {
102         return checksumPolicy;
103     }
104 
105     public boolean checkOutOfDate( Date lastModified )
106     {
107         boolean checkForUpdates = false;
108 
109         if ( UPDATE_POLICY_ALWAYS.equals( updatePolicy ) )
110         {
111             checkForUpdates = true;
112         }
113         else if ( UPDATE_POLICY_DAILY.equals( updatePolicy ) )
114         {
115             // Get midnight boundary
116             Calendar cal = Calendar.getInstance();
117             cal.set( Calendar.HOUR_OF_DAY, 0 );
118             cal.set( Calendar.MINUTE, 0 );
119             cal.set( Calendar.SECOND, 0 );
120             cal.set( Calendar.MILLISECOND, 0 );
121             if ( cal.getTime().after( lastModified ) )
122             {
123                 checkForUpdates = true;
124             }
125         }
126         else if ( updatePolicy.startsWith( UPDATE_POLICY_INTERVAL ) )
127         {
128             String s = updatePolicy.substring( UPDATE_POLICY_INTERVAL.length() + 1 );
129             int minutes = Integer.valueOf( s ).intValue();
130             Calendar cal = Calendar.getInstance();
131             cal.add( Calendar.MINUTE, -minutes );
132             if ( cal.getTime().after( lastModified ) )
133             {
134                 checkForUpdates = true;
135             }
136         }
137         // else assume "never"
138         return checkForUpdates;
139     }
140 }