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