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.internal.impl;
020
021import org.eclipse.aether.RepositorySystemSession;
022import org.eclipse.aether.artifact.Artifact;
023import org.eclipse.aether.metadata.Metadata;
024import org.eclipse.aether.repository.RemoteRepository;
025import org.eclipse.aether.util.ConfigUtils;
026
027/**
028 * Support class for {@link LocalPathPrefixComposerFactory} implementations: it predefines and makes re-usable
029 * common configuration getters, and defines a support class for {@link LocalPathPrefixComposer} carrying same
030 * configuration and providing default implementation for all methods.
031 *
032 * Implementors should extend this class to implement custom split strategies. If one needs to alter default
033 * configuration, they should override any configuration getter from this class.
034 *
035 * @see DefaultLocalPathPrefixComposerFactory
036 * @since 1.8.1
037 */
038public abstract class LocalPathPrefixComposerFactorySupport implements LocalPathPrefixComposerFactory {
039    protected static final String CONF_PROP_SPLIT = "aether.enhancedLocalRepository.split";
040
041    protected static final boolean DEFAULT_SPLIT = false;
042
043    protected static final String CONF_PROP_LOCAL_PREFIX = "aether.enhancedLocalRepository.localPrefix";
044
045    protected static final String DEFAULT_LOCAL_PREFIX = "installed";
046
047    protected static final String CONF_PROP_SPLIT_LOCAL = "aether.enhancedLocalRepository.splitLocal";
048
049    protected static final boolean DEFAULT_SPLIT_LOCAL = false;
050
051    protected static final String CONF_PROP_REMOTE_PREFIX = "aether.enhancedLocalRepository.remotePrefix";
052
053    protected static final String DEFAULT_REMOTE_PREFIX = "cached";
054
055    protected static final String CONF_PROP_SPLIT_REMOTE = "aether.enhancedLocalRepository.splitRemote";
056
057    protected static final boolean DEFAULT_SPLIT_REMOTE = false;
058
059    protected static final String CONF_PROP_SPLIT_REMOTE_REPOSITORY =
060            "aether.enhancedLocalRepository.splitRemoteRepository";
061
062    protected static final boolean DEFAULT_SPLIT_REMOTE_REPOSITORY = false;
063
064    protected static final String CONF_PROP_SPLIT_REMOTE_REPOSITORY_LAST =
065            "aether.enhancedLocalRepository.splitRemoteRepositoryLast";
066
067    protected static final boolean DEFAULT_SPLIT_REMOTE_REPOSITORY_LAST = false;
068
069    protected static final String CONF_PROP_RELEASES_PREFIX = "aether.enhancedLocalRepository.releasesPrefix";
070
071    protected static final String DEFAULT_RELEASES_PREFIX = "releases";
072
073    protected static final String CONF_PROP_SNAPSHOTS_PREFIX = "aether.enhancedLocalRepository.snapshotsPrefix";
074
075    protected static final String DEFAULT_SNAPSHOTS_PREFIX = "snapshots";
076
077    protected boolean isSplit(RepositorySystemSession session) {
078        return ConfigUtils.getBoolean(session, DEFAULT_SPLIT, CONF_PROP_SPLIT);
079    }
080
081    protected String getLocalPrefix(RepositorySystemSession session) {
082        return ConfigUtils.getString(session, DEFAULT_LOCAL_PREFIX, CONF_PROP_LOCAL_PREFIX);
083    }
084
085    protected boolean isSplitLocal(RepositorySystemSession session) {
086        return ConfigUtils.getBoolean(session, DEFAULT_SPLIT_LOCAL, CONF_PROP_SPLIT_LOCAL);
087    }
088
089    protected String getRemotePrefix(RepositorySystemSession session) {
090        return ConfigUtils.getString(session, DEFAULT_REMOTE_PREFIX, CONF_PROP_REMOTE_PREFIX);
091    }
092
093    protected boolean isSplitRemote(RepositorySystemSession session) {
094        return ConfigUtils.getBoolean(session, DEFAULT_SPLIT_REMOTE, CONF_PROP_SPLIT_REMOTE);
095    }
096
097    protected boolean isSplitRemoteRepository(RepositorySystemSession session) {
098        return ConfigUtils.getBoolean(session, DEFAULT_SPLIT_REMOTE_REPOSITORY, CONF_PROP_SPLIT_REMOTE_REPOSITORY);
099    }
100
101    protected boolean isSplitRemoteRepositoryLast(RepositorySystemSession session) {
102        return ConfigUtils.getBoolean(
103                session, DEFAULT_SPLIT_REMOTE_REPOSITORY_LAST, CONF_PROP_SPLIT_REMOTE_REPOSITORY_LAST);
104    }
105
106    protected String getReleasesPrefix(RepositorySystemSession session) {
107        return ConfigUtils.getString(session, DEFAULT_RELEASES_PREFIX, CONF_PROP_RELEASES_PREFIX);
108    }
109
110    protected String getSnapshotsPrefix(RepositorySystemSession session) {
111        return ConfigUtils.getString(session, DEFAULT_SNAPSHOTS_PREFIX, CONF_PROP_SNAPSHOTS_PREFIX);
112    }
113
114    /**
115     * Support class for composers: it defines protected members for all the predefined configuration values and
116     * provides default implementation for methods. Implementors may change it's behaviour by overriding methods.
117     */
118    @SuppressWarnings("checkstyle:parameternumber")
119    protected abstract static class LocalPathPrefixComposerSupport implements LocalPathPrefixComposer {
120        protected final boolean split;
121
122        protected final String localPrefix;
123
124        protected final boolean splitLocal;
125
126        protected final String remotePrefix;
127
128        protected final boolean splitRemote;
129
130        protected final boolean splitRemoteRepository;
131
132        protected final boolean splitRemoteRepositoryLast;
133
134        protected final String releasesPrefix;
135
136        protected final String snapshotsPrefix;
137
138        protected LocalPathPrefixComposerSupport(
139                boolean split,
140                String localPrefix,
141                boolean splitLocal,
142                String remotePrefix,
143                boolean splitRemote,
144                boolean splitRemoteRepository,
145                boolean splitRemoteRepositoryLast,
146                String releasesPrefix,
147                String snapshotsPrefix) {
148            this.split = split;
149            this.localPrefix = localPrefix;
150            this.splitLocal = splitLocal;
151            this.remotePrefix = remotePrefix;
152            this.splitRemote = splitRemote;
153            this.splitRemoteRepository = splitRemoteRepository;
154            this.splitRemoteRepositoryLast = splitRemoteRepositoryLast;
155            this.releasesPrefix = releasesPrefix;
156            this.snapshotsPrefix = snapshotsPrefix;
157        }
158
159        @Override
160        public String getPathPrefixForLocalArtifact(Artifact artifact) {
161            if (!split) {
162                return null;
163            }
164            String result = localPrefix;
165            if (splitLocal) {
166                result += "/" + (artifact.isSnapshot() ? snapshotsPrefix : releasesPrefix);
167            }
168            return result;
169        }
170
171        @Override
172        public String getPathPrefixForRemoteArtifact(Artifact artifact, RemoteRepository repository) {
173            if (!split) {
174                return null;
175            }
176            String result = remotePrefix;
177            if (!splitRemoteRepositoryLast && splitRemoteRepository) {
178                result += "/" + repository.getId();
179            }
180            if (splitRemote) {
181                result += "/" + (artifact.isSnapshot() ? snapshotsPrefix : releasesPrefix);
182            }
183            if (splitRemoteRepositoryLast && splitRemoteRepository) {
184                result += "/" + repository.getId();
185            }
186            return result;
187        }
188
189        @Override
190        public String getPathPrefixForLocalMetadata(Metadata metadata) {
191            if (!split) {
192                return null;
193            }
194            String result = localPrefix;
195            if (splitLocal) {
196                result += "/" + (isSnapshot(metadata) ? snapshotsPrefix : releasesPrefix);
197            }
198            return result;
199        }
200
201        @Override
202        public String getPathPrefixForRemoteMetadata(Metadata metadata, RemoteRepository repository) {
203            if (!split) {
204                return null;
205            }
206            String result = remotePrefix;
207            if (!splitRemoteRepositoryLast && splitRemoteRepository) {
208                result += "/" + repository.getId();
209            }
210            if (splitRemote) {
211                result += "/" + (isSnapshot(metadata) ? snapshotsPrefix : releasesPrefix);
212            }
213            if (splitRemoteRepositoryLast && splitRemoteRepository) {
214                result += "/" + repository.getId();
215            }
216            return result;
217        }
218
219        protected boolean isSnapshot(Metadata metadata) {
220            return !metadata.getVersion().isEmpty() && metadata.getVersion().endsWith("-SNAPSHOT");
221        }
222    }
223}