001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.repository;
020
021/**
022 * A policy controlling access to a repository.
023 */
024public final class RepositoryPolicy {
025
026    /**
027     * Never update locally cached data.
028     */
029    public static final String UPDATE_POLICY_NEVER = "never";
030
031    /**
032     * Always update locally cached data.
033     */
034    public static final String UPDATE_POLICY_ALWAYS = "always";
035
036    /**
037     * Update locally cached data once a day.
038     */
039    public static final String UPDATE_POLICY_DAILY = "daily";
040
041    /**
042     * Update locally cached data every X minutes as given by "interval:X".
043     */
044    public static final String UPDATE_POLICY_INTERVAL = "interval";
045
046    /**
047     * Verify checksums and fail the resolution if they do not match.
048     */
049    public static final String CHECKSUM_POLICY_FAIL = "fail";
050
051    /**
052     * Verify checksums and warn if they do not match.
053     */
054    public static final String CHECKSUM_POLICY_WARN = "warn";
055
056    /**
057     * Do not verify checksums.
058     */
059    public static final String CHECKSUM_POLICY_IGNORE = "ignore";
060
061    private final boolean enabled;
062
063    private final String updatePolicy;
064
065    private final String checksumPolicy;
066
067    /**
068     * Creates a new policy with checksum warnings and daily update checks.
069     */
070    public RepositoryPolicy() {
071        this(true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN);
072    }
073
074    /**
075     * Creates a new policy with the specified settings.
076     *
077     * @param enabled A flag whether the associated repository should be accessed or not.
078     * @param updatePolicy The update interval after which locally cached data from the repository is considered stale
079     *            and should be refetched, may be {@code null}.
080     * @param checksumPolicy The way checksum verification should be handled, may be {@code null}.
081     */
082    public RepositoryPolicy(boolean enabled, String updatePolicy, String checksumPolicy) {
083        this.enabled = enabled;
084        this.updatePolicy = (updatePolicy != null) ? updatePolicy : "";
085        this.checksumPolicy = (checksumPolicy != null) ? checksumPolicy : "";
086    }
087
088    /**
089     * Indicates whether the associated repository should be contacted or not.
090     *
091     * @return {@code true} if the repository should be contacted, {@code false} otherwise.
092     */
093    public boolean isEnabled() {
094        return enabled;
095    }
096
097    /**
098     * Gets the update policy for locally cached data from the repository.
099     *
100     * @return The update policy, never {@code null}.
101     */
102    public String getUpdatePolicy() {
103        return updatePolicy;
104    }
105
106    /**
107     * Gets the policy for checksum validation.
108     *
109     * @return The checksum policy, never {@code null}.
110     */
111    public String getChecksumPolicy() {
112        return checksumPolicy;
113    }
114
115    @Override
116    public String toString() {
117        StringBuilder buffer = new StringBuilder(256);
118        buffer.append("enabled=").append(isEnabled());
119        buffer.append(", checksums=").append(getChecksumPolicy());
120        buffer.append(", updates=").append(getUpdatePolicy());
121        return buffer.toString();
122    }
123
124    @Override
125    public boolean equals(Object obj) {
126        if (this == obj) {
127            return true;
128        }
129
130        if (obj == null || !getClass().equals(obj.getClass())) {
131            return false;
132        }
133
134        RepositoryPolicy that = (RepositoryPolicy) obj;
135
136        return enabled == that.enabled
137                && updatePolicy.equals(that.updatePolicy)
138                && checksumPolicy.equals(that.checksumPolicy);
139    }
140
141    @Override
142    public int hashCode() {
143        int hash = 17;
144        hash = hash * 31 + (enabled ? 1 : 0);
145        hash = hash * 31 + updatePolicy.hashCode();
146        hash = hash * 31 + checksumPolicy.hashCode();
147        return hash;
148    }
149}