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 * <p>
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
040    /**
041     * Whether LRM should split local and remote artifacts.
042     *
043     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
044     * @configurationType {@link java.lang.Boolean}
045     * @configurationDefaultValue {@link #DEFAULT_SPLIT}
046     */
047    public static final String CONFIG_PROP_SPLIT = EnhancedLocalRepositoryManagerFactory.CONFIG_PROPS_PREFIX + "split";
048
049    public static final boolean DEFAULT_SPLIT = false;
050
051    /**
052     * The prefix to use for locally installed artifacts.
053     *
054     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
055     * @configurationType {@link java.lang.String}
056     * @configurationDefaultValue {@link #DEFAULT_LOCAL_PREFIX}
057     */
058    public static final String CONFIG_PROP_LOCAL_PREFIX =
059            EnhancedLocalRepositoryManagerFactory.CONFIG_PROPS_PREFIX + "localPrefix";
060
061    public static final String DEFAULT_LOCAL_PREFIX = "installed";
062
063    /**
064     * Whether locally installed artifacts should be split by version (release/snapshot).
065     *
066     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
067     * @configurationType {@link java.lang.Boolean}
068     * @configurationDefaultValue {@link #DEFAULT_SPLIT_LOCAL}
069     */
070    public static final String CONFIG_PROP_SPLIT_LOCAL =
071            EnhancedLocalRepositoryManagerFactory.CONFIG_PROPS_PREFIX + "splitLocal";
072
073    public static final boolean DEFAULT_SPLIT_LOCAL = false;
074
075    /**
076     * The prefix to use for remotely cached artifacts.
077     *
078     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
079     * @configurationType {@link java.lang.String}
080     * @configurationDefaultValue {@link #DEFAULT_REMOTE_PREFIX}
081     */
082    public static final String CONFIG_PROP_REMOTE_PREFIX =
083            EnhancedLocalRepositoryManagerFactory.CONFIG_PROPS_PREFIX + "remotePrefix";
084
085    public static final String DEFAULT_REMOTE_PREFIX = "cached";
086
087    /**
088     * Whether cached artifacts should be split by version (release/snapshot).
089     *
090     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
091     * @configurationType {@link java.lang.Boolean}
092     * @configurationDefaultValue {@link #DEFAULT_SPLIT_REMOTE}
093     */
094    public static final String CONFIG_PROP_SPLIT_REMOTE =
095            EnhancedLocalRepositoryManagerFactory.CONFIG_PROPS_PREFIX + "splitRemote";
096
097    public static final boolean DEFAULT_SPLIT_REMOTE = false;
098
099    /**
100     * Whether cached artifacts should be split by origin repository (repository ID).
101     *
102     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
103     * @configurationType {@link java.lang.Boolean}
104     * @configurationDefaultValue {@link #DEFAULT_SPLIT_REMOTE_REPOSITORY}
105     */
106    public static final String CONFIG_PROP_SPLIT_REMOTE_REPOSITORY =
107            EnhancedLocalRepositoryManagerFactory.CONFIG_PROPS_PREFIX + "splitRemoteRepository";
108
109    public static final boolean DEFAULT_SPLIT_REMOTE_REPOSITORY = false;
110
111    /**
112     * For cached artifacts, if both splitRemote and splitRemoteRepository are set to true sets the splitting order:
113     * by default it is repositoryId/version (false) or version/repositoryId (true)
114     *
115     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
116     * @configurationType {@link java.lang.Boolean}
117     * @configurationDefaultValue {@link #DEFAULT_SPLIT_REMOTE_REPOSITORY_LAST}
118     */
119    public static final String CONFIG_PROP_SPLIT_REMOTE_REPOSITORY_LAST =
120            EnhancedLocalRepositoryManagerFactory.CONFIG_PROPS_PREFIX + "splitRemoteRepositoryLast";
121
122    public static final boolean DEFAULT_SPLIT_REMOTE_REPOSITORY_LAST = false;
123
124    /**
125     * The prefix to use for release artifacts.
126     *
127     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
128     * @configurationType {@link java.lang.String}
129     * @configurationDefaultValue {@link #DEFAULT_RELEASES_PREFIX}
130     */
131    public static final String CONFIG_PROP_RELEASES_PREFIX =
132            EnhancedLocalRepositoryManagerFactory.CONFIG_PROPS_PREFIX + "releasesPrefix";
133
134    public static final String DEFAULT_RELEASES_PREFIX = "releases";
135
136    /**
137     * The prefix to use for snapshot artifacts.
138     *
139     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
140     * @configurationType {@link java.lang.String}
141     * @configurationDefaultValue {@link #DEFAULT_SNAPSHOTS_PREFIX}
142     */
143    public static final String CONFIG_PROP_SNAPSHOTS_PREFIX =
144            EnhancedLocalRepositoryManagerFactory.CONFIG_PROPS_PREFIX + "snapshotsPrefix";
145
146    public static final String DEFAULT_SNAPSHOTS_PREFIX = "snapshots";
147
148    protected boolean isSplit(RepositorySystemSession session) {
149        return ConfigUtils.getBoolean(session, DEFAULT_SPLIT, CONFIG_PROP_SPLIT);
150    }
151
152    protected String getLocalPrefix(RepositorySystemSession session) {
153        return ConfigUtils.getString(session, DEFAULT_LOCAL_PREFIX, CONFIG_PROP_LOCAL_PREFIX);
154    }
155
156    protected boolean isSplitLocal(RepositorySystemSession session) {
157        return ConfigUtils.getBoolean(session, DEFAULT_SPLIT_LOCAL, CONFIG_PROP_SPLIT_LOCAL);
158    }
159
160    protected String getRemotePrefix(RepositorySystemSession session) {
161        return ConfigUtils.getString(session, DEFAULT_REMOTE_PREFIX, CONFIG_PROP_REMOTE_PREFIX);
162    }
163
164    protected boolean isSplitRemote(RepositorySystemSession session) {
165        return ConfigUtils.getBoolean(session, DEFAULT_SPLIT_REMOTE, CONFIG_PROP_SPLIT_REMOTE);
166    }
167
168    protected boolean isSplitRemoteRepository(RepositorySystemSession session) {
169        return ConfigUtils.getBoolean(session, DEFAULT_SPLIT_REMOTE_REPOSITORY, CONFIG_PROP_SPLIT_REMOTE_REPOSITORY);
170    }
171
172    protected boolean isSplitRemoteRepositoryLast(RepositorySystemSession session) {
173        return ConfigUtils.getBoolean(
174                session, DEFAULT_SPLIT_REMOTE_REPOSITORY_LAST, CONFIG_PROP_SPLIT_REMOTE_REPOSITORY_LAST);
175    }
176
177    protected String getReleasesPrefix(RepositorySystemSession session) {
178        return ConfigUtils.getString(session, DEFAULT_RELEASES_PREFIX, CONFIG_PROP_RELEASES_PREFIX);
179    }
180
181    protected String getSnapshotsPrefix(RepositorySystemSession session) {
182        return ConfigUtils.getString(session, DEFAULT_SNAPSHOTS_PREFIX, CONFIG_PROP_SNAPSHOTS_PREFIX);
183    }
184
185    /**
186     * Support class for composers: it defines protected members for all the predefined configuration values and
187     * provides default implementation for methods. Implementors may change it's behaviour by overriding methods.
188     */
189    @SuppressWarnings("checkstyle:parameternumber")
190    protected abstract static class LocalPathPrefixComposerSupport implements LocalPathPrefixComposer {
191        protected final boolean split;
192
193        protected final String localPrefix;
194
195        protected final boolean splitLocal;
196
197        protected final String remotePrefix;
198
199        protected final boolean splitRemote;
200
201        protected final boolean splitRemoteRepository;
202
203        protected final boolean splitRemoteRepositoryLast;
204
205        protected final String releasesPrefix;
206
207        protected final String snapshotsPrefix;
208
209        protected LocalPathPrefixComposerSupport(
210                boolean split,
211                String localPrefix,
212                boolean splitLocal,
213                String remotePrefix,
214                boolean splitRemote,
215                boolean splitRemoteRepository,
216                boolean splitRemoteRepositoryLast,
217                String releasesPrefix,
218                String snapshotsPrefix) {
219            this.split = split;
220            this.localPrefix = localPrefix;
221            this.splitLocal = splitLocal;
222            this.remotePrefix = remotePrefix;
223            this.splitRemote = splitRemote;
224            this.splitRemoteRepository = splitRemoteRepository;
225            this.splitRemoteRepositoryLast = splitRemoteRepositoryLast;
226            this.releasesPrefix = releasesPrefix;
227            this.snapshotsPrefix = snapshotsPrefix;
228        }
229
230        @Override
231        public String getPathPrefixForLocalArtifact(Artifact artifact) {
232            if (!split) {
233                return null;
234            }
235            String result = localPrefix;
236            if (splitLocal) {
237                result += "/" + (artifact.isSnapshot() ? snapshotsPrefix : releasesPrefix);
238            }
239            return result;
240        }
241
242        @Override
243        public String getPathPrefixForRemoteArtifact(Artifact artifact, RemoteRepository repository) {
244            if (!split) {
245                return null;
246            }
247            String result = remotePrefix;
248            if (!splitRemoteRepositoryLast && splitRemoteRepository) {
249                result += "/" + repository.getId();
250            }
251            if (splitRemote) {
252                result += "/" + (artifact.isSnapshot() ? snapshotsPrefix : releasesPrefix);
253            }
254            if (splitRemoteRepositoryLast && splitRemoteRepository) {
255                result += "/" + repository.getId();
256            }
257            return result;
258        }
259
260        @Override
261        public String getPathPrefixForLocalMetadata(Metadata metadata) {
262            if (!split) {
263                return null;
264            }
265            String result = localPrefix;
266            if (splitLocal) {
267                result += "/" + (isSnapshot(metadata) ? snapshotsPrefix : releasesPrefix);
268            }
269            return result;
270        }
271
272        @Override
273        public String getPathPrefixForRemoteMetadata(Metadata metadata, RemoteRepository repository) {
274            if (!split) {
275                return null;
276            }
277            String result = remotePrefix;
278            if (!splitRemoteRepositoryLast && splitRemoteRepository) {
279                result += "/" + repository.getId();
280            }
281            if (splitRemote) {
282                result += "/" + (isSnapshot(metadata) ? snapshotsPrefix : releasesPrefix);
283            }
284            if (splitRemoteRepositoryLast && splitRemoteRepository) {
285                result += "/" + repository.getId();
286            }
287            return result;
288        }
289
290        protected boolean isSnapshot(Metadata metadata) {
291            return !metadata.getVersion().isEmpty() && metadata.getVersion().endsWith("-SNAPSHOT");
292        }
293    }
294}