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