View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl;
20  
21  import org.eclipse.aether.RepositorySystemSession;
22  import org.eclipse.aether.artifact.Artifact;
23  import org.eclipse.aether.metadata.Metadata;
24  import org.eclipse.aether.repository.RemoteRepository;
25  import org.eclipse.aether.util.ConfigUtils;
26  
27  /**
28   * Support class for {@link LocalPathPrefixComposerFactory} implementations: it predefines and makes re-usable
29   * common configuration getters, and defines a support class for {@link LocalPathPrefixComposer} carrying same
30   * configuration and providing default implementation for all methods.
31   *
32   * Implementors should extend this class to implement custom split strategies. If one needs to alter default
33   * configuration, they should override any configuration getter from this class.
34   *
35   * @see DefaultLocalPathPrefixComposerFactory
36   * @since 1.8.1
37   */
38  public abstract class LocalPathPrefixComposerFactorySupport implements LocalPathPrefixComposerFactory {
39      protected static final String CONF_PROP_SPLIT = "aether.enhancedLocalRepository.split";
40  
41      protected static final boolean DEFAULT_SPLIT = false;
42  
43      protected static final String CONF_PROP_LOCAL_PREFIX = "aether.enhancedLocalRepository.localPrefix";
44  
45      protected static final String DEFAULT_LOCAL_PREFIX = "installed";
46  
47      protected static final String CONF_PROP_SPLIT_LOCAL = "aether.enhancedLocalRepository.splitLocal";
48  
49      protected static final boolean DEFAULT_SPLIT_LOCAL = false;
50  
51      protected static final String CONF_PROP_REMOTE_PREFIX = "aether.enhancedLocalRepository.remotePrefix";
52  
53      protected static final String DEFAULT_REMOTE_PREFIX = "cached";
54  
55      protected static final String CONF_PROP_SPLIT_REMOTE = "aether.enhancedLocalRepository.splitRemote";
56  
57      protected static final boolean DEFAULT_SPLIT_REMOTE = false;
58  
59      protected static final String CONF_PROP_SPLIT_REMOTE_REPOSITORY =
60              "aether.enhancedLocalRepository.splitRemoteRepository";
61  
62      protected static final boolean DEFAULT_SPLIT_REMOTE_REPOSITORY = false;
63  
64      protected static final String CONF_PROP_SPLIT_REMOTE_REPOSITORY_LAST =
65              "aether.enhancedLocalRepository.splitRemoteRepositoryLast";
66  
67      protected static final boolean DEFAULT_SPLIT_REMOTE_REPOSITORY_LAST = false;
68  
69      protected static final String CONF_PROP_RELEASES_PREFIX = "aether.enhancedLocalRepository.releasesPrefix";
70  
71      protected static final String DEFAULT_RELEASES_PREFIX = "releases";
72  
73      protected static final String CONF_PROP_SNAPSHOTS_PREFIX = "aether.enhancedLocalRepository.snapshotsPrefix";
74  
75      protected static final String DEFAULT_SNAPSHOTS_PREFIX = "snapshots";
76  
77      protected boolean isSplit(RepositorySystemSession session) {
78          return ConfigUtils.getBoolean(session, DEFAULT_SPLIT, CONF_PROP_SPLIT);
79      }
80  
81      protected String getLocalPrefix(RepositorySystemSession session) {
82          return ConfigUtils.getString(session, DEFAULT_LOCAL_PREFIX, CONF_PROP_LOCAL_PREFIX);
83      }
84  
85      protected boolean isSplitLocal(RepositorySystemSession session) {
86          return ConfigUtils.getBoolean(session, DEFAULT_SPLIT_LOCAL, CONF_PROP_SPLIT_LOCAL);
87      }
88  
89      protected String getRemotePrefix(RepositorySystemSession session) {
90          return ConfigUtils.getString(session, DEFAULT_REMOTE_PREFIX, CONF_PROP_REMOTE_PREFIX);
91      }
92  
93      protected boolean isSplitRemote(RepositorySystemSession session) {
94          return ConfigUtils.getBoolean(session, DEFAULT_SPLIT_REMOTE, CONF_PROP_SPLIT_REMOTE);
95      }
96  
97      protected boolean isSplitRemoteRepository(RepositorySystemSession session) {
98          return ConfigUtils.getBoolean(session, DEFAULT_SPLIT_REMOTE_REPOSITORY, CONF_PROP_SPLIT_REMOTE_REPOSITORY);
99      }
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 }