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.Inject;
025import javax.inject.Named;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.impl.UpdatePolicyAnalyzer;
029import org.eclipse.aether.repository.RepositoryPolicy;
030import org.eclipse.aether.spi.locator.Service;
031import org.eclipse.aether.spi.locator.ServiceLocator;
032import org.eclipse.aether.spi.log.Logger;
033import org.eclipse.aether.spi.log.LoggerFactory;
034import org.eclipse.aether.spi.log.NullLoggerFactory;
035
036/**
037 */
038@Named
039public class DefaultUpdatePolicyAnalyzer
040    implements UpdatePolicyAnalyzer, Service
041{
042
043    private Logger logger = NullLoggerFactory.LOGGER;
044
045    public DefaultUpdatePolicyAnalyzer()
046    {
047        // enables default constructor
048    }
049
050    @Inject
051    DefaultUpdatePolicyAnalyzer( LoggerFactory loggerFactory )
052    {
053        setLoggerFactory( loggerFactory );
054    }
055
056    public void initService( ServiceLocator locator )
057    {
058        setLoggerFactory( locator.getService( LoggerFactory.class ) );
059    }
060
061    public DefaultUpdatePolicyAnalyzer setLoggerFactory( LoggerFactory loggerFactory )
062    {
063        this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() );
064        return this;
065    }
066
067    public String getEffectiveUpdatePolicy( RepositorySystemSession session, String policy1, String policy2 )
068    {
069        return ordinalOfUpdatePolicy( policy1 ) < ordinalOfUpdatePolicy( policy2 ) ? policy1 : policy2;
070    }
071
072    private int ordinalOfUpdatePolicy( String policy )
073    {
074        if ( RepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) )
075        {
076            return 1440;
077        }
078        else if ( RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) )
079        {
080            return 0;
081        }
082        else if ( policy != null && policy.startsWith( RepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
083        {
084            return getMinutes( policy );
085        }
086        else
087        {
088            // assume "never"
089            return Integer.MAX_VALUE;
090        }
091    }
092
093    public boolean isUpdatedRequired( RepositorySystemSession session, long lastModified, String policy )
094    {
095        boolean checkForUpdates;
096
097        if ( policy == null )
098        {
099            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}