001    package org.apache.maven.artifact.repository;
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    
022    import java.util.Calendar;
023    import java.util.Date;
024    import java.util.TimeZone;
025    
026    /**
027     * Describes a set of policies for a repository to use under certain conditions.
028     *
029     * @author <a href="mailto:brett@apache.org">Brett Porter</a>
030     */
031    public class ArtifactRepositoryPolicy
032    {
033        public static final String UPDATE_POLICY_NEVER = "never";
034    
035        public static final String UPDATE_POLICY_ALWAYS = "always";
036    
037        public static final String UPDATE_POLICY_DAILY = "daily";
038    
039        public static final String UPDATE_POLICY_INTERVAL = "interval";
040    
041        public static final String CHECKSUM_POLICY_FAIL = "fail";
042    
043        public static final String CHECKSUM_POLICY_WARN = "warn";
044    
045        public static final String CHECKSUM_POLICY_IGNORE = "ignore";
046    
047        private boolean enabled;
048    
049        private String updatePolicy;
050    
051        private String checksumPolicy;
052    
053        public ArtifactRepositoryPolicy()
054        {
055            this( true, null, null );
056        }
057    
058        public ArtifactRepositoryPolicy( ArtifactRepositoryPolicy policy )
059        {
060            this( policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy() );
061        }
062    
063        public ArtifactRepositoryPolicy( boolean enabled, String updatePolicy, String checksumPolicy )
064        {
065            this.enabled = enabled;
066    
067            if ( updatePolicy == null )
068            {
069                updatePolicy = UPDATE_POLICY_DAILY;
070            }
071            this.updatePolicy = updatePolicy;
072    
073            if ( checksumPolicy == null )
074            {
075                checksumPolicy = CHECKSUM_POLICY_WARN;
076            }
077            this.checksumPolicy = checksumPolicy;
078        }
079    
080        public void setEnabled( boolean enabled )
081        {
082            this.enabled = enabled;
083        }
084    
085        public void setUpdatePolicy( String updatePolicy )
086        {
087            if ( updatePolicy != null )
088            {
089                this.updatePolicy = updatePolicy;
090            }
091        }
092    
093        public void setChecksumPolicy( String checksumPolicy )
094        {
095            if ( checksumPolicy != null )
096            {
097                this.checksumPolicy = checksumPolicy;
098            }
099        }
100    
101        public boolean isEnabled()
102        {
103            return enabled;
104        }
105    
106        public String getUpdatePolicy()
107        {
108            return updatePolicy;
109        }
110    
111        public String getChecksumPolicy()
112        {
113            return checksumPolicy;
114        }
115    
116        public boolean checkOutOfDate( Date lastModified )
117        {
118            boolean checkForUpdates = false;
119    
120            if ( UPDATE_POLICY_ALWAYS.equals( updatePolicy ) )
121            {
122                checkForUpdates = true;
123            }
124            else if ( UPDATE_POLICY_DAILY.equals( updatePolicy ) )
125            {
126                // Get local midnight boundary
127                Calendar cal = Calendar.getInstance();
128    
129                cal.set( Calendar.HOUR_OF_DAY, 0 );
130                cal.set( Calendar.MINUTE, 0 );
131                cal.set( Calendar.SECOND, 0 );
132                cal.set( Calendar.MILLISECOND, 0 );
133    
134                if ( cal.getTime().after( lastModified ) )
135                {
136                    checkForUpdates = true;
137                }
138            }
139            else if ( updatePolicy.startsWith( UPDATE_POLICY_INTERVAL ) )
140            {
141                String s = updatePolicy.substring( UPDATE_POLICY_INTERVAL.length() + 1 );
142                int minutes = Integer.valueOf( s );
143                Calendar cal = Calendar.getInstance();
144                cal.add( Calendar.MINUTE, -minutes );
145                if ( cal.getTime().after( lastModified ) )
146                {
147                    checkForUpdates = true;
148                }
149            }
150            // else assume "never"
151            return checkForUpdates;
152        }
153    
154        @Override
155        public String toString()
156        {
157            StringBuilder buffer = new StringBuilder( 64 );
158            buffer.append( "{enabled=" );
159            buffer.append( enabled );
160            buffer.append( ", checksums=" );
161            buffer.append( checksumPolicy );
162            buffer.append( ", updates=" );
163            buffer.append( updatePolicy );
164            buffer.append( "}" );
165            return buffer.toString();
166        }
167    
168        public void merge( ArtifactRepositoryPolicy policy )
169        {
170            if ( policy != null && policy.isEnabled() )
171            {
172                setEnabled( true );
173    
174                if ( ordinalOfChecksumPolicy( policy.getChecksumPolicy() ) < ordinalOfChecksumPolicy( getChecksumPolicy() ) )
175                {
176                    setChecksumPolicy( policy.getChecksumPolicy() );
177                }
178    
179                if ( ordinalOfUpdatePolicy( policy.getUpdatePolicy() ) < ordinalOfUpdatePolicy( getUpdatePolicy() ) )
180                {
181                    setUpdatePolicy( policy.getUpdatePolicy() );
182                }
183            }
184        }
185    
186        private int ordinalOfChecksumPolicy( String policy )
187        {
188            if ( ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
189            {
190                return 2;
191            }
192            else if ( ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
193            {
194                return 0;
195            }
196            else
197            {
198                return 1;
199            }
200        }
201    
202        private int ordinalOfUpdatePolicy( String policy )
203        {
204            if ( ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) )
205            {
206                return 1440;
207            }
208            else if ( ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) )
209            {
210                return 0;
211            }
212            else if ( policy != null && policy.startsWith( ArtifactRepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
213            {
214                String s = policy.substring( UPDATE_POLICY_INTERVAL.length() + 1 );
215                return Integer.valueOf( s );
216            }
217            else
218            {
219                return Integer.MAX_VALUE;
220            }
221        }
222    
223    }