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