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