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      private int ordinalOfUpdatePolicy( String policy )
52      {
53          if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) )
54          {
55              return 1440;
56          }
57          else if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) )
58          {
59              return 0;
60          }
61          else if ( policy != null && policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
62          {
63              return getMinutes( policy );
64          }
65          else
66          {
67              // assume "never"
68              return Integer.MAX_VALUE;
69          }
70      }
71  
72      public boolean isUpdatedRequired( RepositorySystemSession session, long lastModified, String policy )
73      {
74          boolean checkForUpdates;
75  
76          if ( policy == null )
77          {
78              policy = "";
79          }
80  
81          if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) )
82          {
83              checkForUpdates = true;
84          }
85          else if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) )
86          {
87              Calendar cal = Calendar.getInstance();
88              cal.set( Calendar.HOUR_OF_DAY, 0 );
89              cal.set( Calendar.MINUTE, 0 );
90              cal.set( Calendar.SECOND, 0 );
91              cal.set( Calendar.MILLISECOND, 0 );
92  
93              checkForUpdates = cal.getTimeInMillis() > lastModified;
94          }
95          else if ( policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
96          {
97              int minutes = getMinutes( policy );
98  
99              Calendar cal = Calendar.getInstance();
100             cal.add( Calendar.MINUTE, -minutes );
101 
102             checkForUpdates = cal.getTimeInMillis() > lastModified;
103         }
104         else
105         {
106             // assume "never"
107             checkForUpdates = false;
108 
109             if ( !RepositoryPolicy.UPDATE_POLICY_NEVER.equals( policy ) )
110             {
111                 LOGGER.warn( "Unknown repository update policy '{}', assuming '{}'",
112                         policy, RepositoryPolicy.UPDATE_POLICY_NEVER );
113             }
114         }
115 
116         return checkForUpdates;
117     }
118 
119     private int getMinutes( String policy )
120     {
121         int minutes;
122         try
123         {
124             String s = policy.substring( RepositoryPolicy.UPDATE_POLICY_INTERVAL.length() + 1 );
125             minutes = Integer.valueOf( s );
126         }
127         catch ( RuntimeException e )
128         {
129             minutes = 24 * 60;
130 
131             LOGGER.warn( "Non-parseable repository update policy '{}', assuming '{}:1440'",
132                     policy, RepositoryPolicy.UPDATE_POLICY_INTERVAL );
133         }
134         return minutes;
135     }
136 
137 }