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 033import static java.util.Objects.requireNonNull; 034 035/** 036 */ 037@Singleton 038@Named 039public class DefaultUpdatePolicyAnalyzer 040 implements UpdatePolicyAnalyzer 041{ 042 043 private static final Logger LOGGER = LoggerFactory.getLogger( DefaultUpdatePolicyAnalyzer.class ); 044 045 public DefaultUpdatePolicyAnalyzer() 046 { 047 // enables default constructor 048 } 049 050 public String getEffectiveUpdatePolicy( RepositorySystemSession session, String policy1, String policy2 ) 051 { 052 requireNonNull( session, "session cannot be null" ); 053 return ordinalOfUpdatePolicy( policy1 ) < ordinalOfUpdatePolicy( policy2 ) ? policy1 : policy2; 054 } 055 056 @SuppressWarnings( { "checkstyle:magicnumber" } ) 057 private int ordinalOfUpdatePolicy( String policy ) 058 { 059 if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) ) 060 { 061 return 1440; 062 } 063 else if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) ) 064 { 065 return 0; 066 } 067 else if ( policy != null && policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) ) 068 { 069 return getMinutes( policy ); 070 } 071 else 072 { 073 // assume "never" 074 return Integer.MAX_VALUE; 075 } 076 } 077 078 public boolean isUpdatedRequired( RepositorySystemSession session, long lastModified, String policy ) 079 { 080 requireNonNull( session, "session cannot be null" ); 081 boolean checkForUpdates; 082 083 if ( policy == null ) 084 { 085 policy = ""; 086 } 087 088 if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) ) 089 { 090 checkForUpdates = true; 091 } 092 else if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) ) 093 { 094 Calendar cal = Calendar.getInstance(); 095 cal.set( Calendar.HOUR_OF_DAY, 0 ); 096 cal.set( Calendar.MINUTE, 0 ); 097 cal.set( Calendar.SECOND, 0 ); 098 cal.set( Calendar.MILLISECOND, 0 ); 099 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}