001package org.eclipse.aether.internal.impl; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.Calendar; 023 024import javax.inject.Named; 025 026import org.eclipse.aether.RepositorySystemSession; 027import org.eclipse.aether.impl.UpdatePolicyAnalyzer; 028import org.eclipse.aether.repository.RepositoryPolicy; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032/** 033 */ 034@Named 035public class DefaultUpdatePolicyAnalyzer 036 implements UpdatePolicyAnalyzer 037{ 038 039 private static final Logger LOGGER = LoggerFactory.getLogger( DefaultUpdatePolicyAnalyzer.class ); 040 041 public DefaultUpdatePolicyAnalyzer() 042 { 043 // enables default constructor 044 } 045 046 public String getEffectiveUpdatePolicy( RepositorySystemSession session, String policy1, String policy2 ) 047 { 048 return ordinalOfUpdatePolicy( policy1 ) < ordinalOfUpdatePolicy( policy2 ) ? policy1 : policy2; 049 } 050 051 @SuppressWarnings( { "checkstyle:magicnumber" } ) 052 private int ordinalOfUpdatePolicy( String policy ) 053 { 054 if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) ) 055 { 056 return 1440; 057 } 058 else if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) ) 059 { 060 return 0; 061 } 062 else if ( policy != null && policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) ) 063 { 064 return getMinutes( policy ); 065 } 066 else 067 { 068 // assume "never" 069 return Integer.MAX_VALUE; 070 } 071 } 072 073 public boolean isUpdatedRequired( RepositorySystemSession session, long lastModified, String policy ) 074 { 075 boolean checkForUpdates; 076 077 if ( policy == null ) 078 { 079 policy = ""; 080 } 081 082 if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) ) 083 { 084 checkForUpdates = true; 085 } 086 else if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) ) 087 { 088 Calendar cal = Calendar.getInstance(); 089 cal.set( Calendar.HOUR_OF_DAY, 0 ); 090 cal.set( Calendar.MINUTE, 0 ); 091 cal.set( Calendar.SECOND, 0 ); 092 cal.set( Calendar.MILLISECOND, 0 ); 093 094 checkForUpdates = cal.getTimeInMillis() > lastModified; 095 } 096 else if ( policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) ) 097 { 098 int minutes = getMinutes( policy ); 099 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}