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    private int ordinalOfUpdatePolicy( String policy )
052    {
053        if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) )
054        {
055            return 1440;
056        }
057        else if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) )
058        {
059            return 0;
060        }
061        else if ( policy != null && policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
062        {
063            return getMinutes( policy );
064        }
065        else
066        {
067            // assume "never"
068            return Integer.MAX_VALUE;
069        }
070    }
071
072    public boolean isUpdatedRequired( RepositorySystemSession session, long lastModified, String policy )
073    {
074        boolean checkForUpdates;
075
076        if ( policy == null )
077        {
078            policy = "";
079        }
080
081        if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) )
082        {
083            checkForUpdates = true;
084        }
085        else if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) )
086        {
087            Calendar cal = Calendar.getInstance();
088            cal.set( Calendar.HOUR_OF_DAY, 0 );
089            cal.set( Calendar.MINUTE, 0 );
090            cal.set( Calendar.SECOND, 0 );
091            cal.set( Calendar.MILLISECOND, 0 );
092
093            checkForUpdates = cal.getTimeInMillis() > lastModified;
094        }
095        else if ( policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
096        {
097            int minutes = getMinutes( policy );
098
099            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}