View Javadoc
1   package org.eclipse.aether.internal.impl;
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  
24  import javax.inject.Named;
25  import javax.inject.Singleton;
26  
27  import org.eclipse.aether.RepositorySystemSession;
28  import org.eclipse.aether.impl.UpdatePolicyAnalyzer;
29  import org.eclipse.aether.repository.RepositoryPolicy;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   */
35  @Singleton
36  @Named
37  public class DefaultUpdatePolicyAnalyzer
38      implements UpdatePolicyAnalyzer
39  {
40  
41      private static final Logger LOGGER = LoggerFactory.getLogger( DefaultUpdatePolicyAnalyzer.class );
42  
43      public DefaultUpdatePolicyAnalyzer()
44      {
45          // enables default constructor
46      }
47  
48      public String getEffectiveUpdatePolicy( RepositorySystemSession session, String policy1, String policy2 )
49      {
50          return ordinalOfUpdatePolicy( policy1 ) < ordinalOfUpdatePolicy( policy2 ) ? policy1 : policy2;
51      }
52  
53      @SuppressWarnings( { "checkstyle:magicnumber" } )
54      private int ordinalOfUpdatePolicy( String policy )
55      {
56          if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) )
57          {
58              return 1440;
59          }
60          else if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) )
61          {
62              return 0;
63          }
64          else if ( policy != null && policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
65          {
66              return getMinutes( policy );
67          }
68          else
69          {
70              // assume "never"
71              return Integer.MAX_VALUE;
72          }
73      }
74  
75      public boolean isUpdatedRequired( RepositorySystemSession session, long lastModified, String policy )
76      {
77          boolean checkForUpdates;
78  
79          if ( policy == null )
80          {
81              policy = "";
82          }
83  
84          if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) )
85          {
86              checkForUpdates = true;
87          }
88          else if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) )
89          {
90              Calendar cal = Calendar.getInstance();
91              cal.set( Calendar.HOUR_OF_DAY, 0 );
92              cal.set( Calendar.MINUTE, 0 );
93              cal.set( Calendar.SECOND, 0 );
94              cal.set( Calendar.MILLISECOND, 0 );
95  
96              checkForUpdates = cal.getTimeInMillis() > lastModified;
97          }
98          else if ( policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
99          {
100             int minutes = getMinutes( policy );
101 
102             Calendar cal = Calendar.getInstance();
103             cal.add( Calendar.MINUTE, -minutes );
104 
105             checkForUpdates = cal.getTimeInMillis() > lastModified;
106         }
107         else
108         {
109             // assume "never"
110             checkForUpdates = false;
111 
112             if ( !RepositoryPolicy.UPDATE_POLICY_NEVER.equals( policy ) )
113             {
114                 LOGGER.warn( "Unknown repository update policy '{}', assuming '{}'",
115                         policy, RepositoryPolicy.UPDATE_POLICY_NEVER );
116             }
117         }
118 
119         return checkForUpdates;
120     }
121 
122     @SuppressWarnings( { "checkstyle:magicnumber" } )
123     private int getMinutes( String policy )
124     {
125         int minutes;
126         try
127         {
128             String s = policy.substring( RepositoryPolicy.UPDATE_POLICY_INTERVAL.length() + 1 );
129             minutes = Integer.parseInt( s );
130         }
131         catch ( RuntimeException e )
132         {
133             minutes = 24 * 60;
134 
135             LOGGER.warn( "Non-parseable repository update policy '{}', assuming '{}:1440'",
136                     policy, RepositoryPolicy.UPDATE_POLICY_INTERVAL );
137         }
138         return minutes;
139     }
140 
141 }