001package org.eclipse.aether.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/**
023 * A policy controlling access to a repository.
024 */
025public final class RepositoryPolicy
026{
027
028    /**
029     * Never update locally cached data.
030     */
031    public static final String UPDATE_POLICY_NEVER = "never";
032
033    /**
034     * Always update locally cached data.
035     */
036    public static final String UPDATE_POLICY_ALWAYS = "always";
037
038    /**
039     * Update locally cached data once a day.
040     */
041    public static final String UPDATE_POLICY_DAILY = "daily";
042
043    /**
044     * Update locally cached data every X minutes as given by "interval:X".
045     */
046    public static final String UPDATE_POLICY_INTERVAL = "interval";
047
048    /**
049     * Verify checksums and fail the resolution if they do not match.
050     */
051    public static final String CHECKSUM_POLICY_FAIL = "fail";
052
053    /**
054     * Verify checksums and warn if they do not match.
055     */
056    public static final String CHECKSUM_POLICY_WARN = "warn";
057
058    /**
059     * Do not verify checksums.
060     */
061    public static final String CHECKSUM_POLICY_IGNORE = "ignore";
062
063    private final boolean enabled;
064
065    private final String updatePolicy;
066
067    private final String checksumPolicy;
068
069    /**
070     * Creates a new policy with checksum warnings and daily update checks.
071     */
072    public RepositoryPolicy()
073    {
074        this( true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN );
075    }
076
077    /**
078     * Creates a new policy with the specified settings.
079     * 
080     * @param enabled A flag whether the associated repository should be accessed or not.
081     * @param updatePolicy The update interval after which locally cached data from the repository is considered stale
082     *            and should be refetched, may be {@code null}.
083     * @param checksumPolicy The way checksum verification should be handled, may be {@code null}.
084     */
085    public RepositoryPolicy( boolean enabled, String updatePolicy, String checksumPolicy )
086    {
087        this.enabled = enabled;
088        this.updatePolicy = ( updatePolicy != null ) ? updatePolicy : "";
089        this.checksumPolicy = ( checksumPolicy != null ) ? checksumPolicy : "";
090    }
091
092    /**
093     * Indicates whether the associated repository should be contacted or not.
094     * 
095     * @return {@code true} if the repository should be contacted, {@code false} otherwise.
096     */
097    public boolean isEnabled()
098    {
099        return enabled;
100    }
101
102    /**
103     * Gets the update policy for locally cached data from the repository.
104     * 
105     * @return The update policy, never {@code null}.
106     */
107    public String getUpdatePolicy()
108    {
109        return updatePolicy;
110    }
111
112    /**
113     * Gets the policy for checksum validation.
114     * 
115     * @return The checksum policy, never {@code null}.
116     */
117    public String getChecksumPolicy()
118    {
119        return checksumPolicy;
120    }
121
122    @Override
123    public String toString()
124    {
125        StringBuilder buffer = new StringBuilder( 256 );
126        buffer.append( "enabled=" ).append( isEnabled() );
127        buffer.append( ", checksums=" ).append( getChecksumPolicy() );
128        buffer.append( ", updates=" ).append( getUpdatePolicy() );
129        return buffer.toString();
130    }
131
132    @Override
133    public boolean equals( Object obj )
134    {
135        if ( this == obj )
136        {
137            return true;
138        }
139
140        if ( obj == null || !getClass().equals( obj.getClass() ) )
141        {
142            return false;
143        }
144
145        RepositoryPolicy that = (RepositoryPolicy) obj;
146
147        return enabled == that.enabled && updatePolicy.equals( that.updatePolicy )
148            && checksumPolicy.equals( that.checksumPolicy );
149    }
150
151    @Override
152    public int hashCode()
153    {
154        int hash = 17;
155        hash = hash * 31 + ( enabled ? 1 : 0 );
156        hash = hash * 31 + updatePolicy.hashCode();
157        hash = hash * 31 + checksumPolicy.hashCode();
158        return hash;
159    }
160
161}