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; 025import javax.inject.Singleton; 026 027import org.eclipse.aether.RepositorySystemSession; 028import org.eclipse.aether.impl.UpdatePolicyAnalyzer; 029import org.eclipse.aether.repository.RepositoryPolicy; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032 033/** 034 */ 035@Singleton 036@Named 037public class DefaultUpdatePolicyAnalyzer 038 implements UpdatePolicyAnalyzer 039{ 040 041 private static final Logger LOGGER = LoggerFactory.getLogger( DefaultUpdatePolicyAnalyzer.class ); 042 043 public DefaultUpdatePolicyAnalyzer() 044 { 045 // enables default constructor 046 } 047 048 public String getEffectiveUpdatePolicy( RepositorySystemSession session, String policy1, String policy2 ) 049 { 050 return ordinalOfUpdatePolicy( policy1 ) < ordinalOfUpdatePolicy( policy2 ) ? policy1 : policy2; 051 } 052 053 @SuppressWarnings( { "checkstyle:magicnumber" } ) 054 private int ordinalOfUpdatePolicy( String policy ) 055 { 056 if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) ) 057 { 058 return 1440; 059 } 060 else if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) ) 061 { 062 return 0; 063 } 064 else if ( policy != null && policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) ) 065 { 066 return getMinutes( policy ); 067 } 068 else 069 { 070 // assume "never" 071 return Integer.MAX_VALUE; 072 } 073 } 074 075 public boolean isUpdatedRequired( RepositorySystemSession session, long lastModified, String policy ) 076 { 077 boolean checkForUpdates; 078 079 if ( policy == null ) 080 { 081 policy = ""; 082 } 083 084 if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) ) 085 { 086 checkForUpdates = true; 087 } 088 else if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) ) 089 { 090 Calendar cal = Calendar.getInstance(); 091 cal.set( Calendar.HOUR_OF_DAY, 0 ); 092 cal.set( Calendar.MINUTE, 0 ); 093 cal.set( Calendar.SECOND, 0 ); 094 cal.set( Calendar.MILLISECOND, 0 ); 095 096 checkForUpdates = cal.getTimeInMillis() > lastModified; 097 } 098 else if ( policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) ) 099 { 100 int minutes = getMinutes( policy ); 101 102 Calendar cal = Calendar.getInstance(); 103 cal.add( Calendar.MINUTE, -minutes ); 104 105 checkForUpdates = cal.getTimeInMillis() > lastModified; 106 } 107 else 108 { 109 // assume "never" 110 checkForUpdates = false; 111 112 if ( !RepositoryPolicy.UPDATE_POLICY_NEVER.equals( policy ) ) 113 { 114 LOGGER.warn( "Unknown repository update policy '{}', assuming '{}'", 115 policy, RepositoryPolicy.UPDATE_POLICY_NEVER ); 116 } 117 } 118 119 return checkForUpdates; 120 } 121 122 @SuppressWarnings( { "checkstyle:magicnumber" } ) 123 private int getMinutes( String policy ) 124 { 125 int minutes; 126 try 127 { 128 String s = policy.substring( RepositoryPolicy.UPDATE_POLICY_INTERVAL.length() + 1 ); 129 minutes = Integer.parseInt( s ); 130 } 131 catch ( RuntimeException e ) 132 { 133 minutes = 24 * 60; 134 135 LOGGER.warn( "Non-parseable repository update policy '{}', assuming '{}:1440'", 136 policy, RepositoryPolicy.UPDATE_POLICY_INTERVAL ); 137 } 138 return minutes; 139 } 140 141}