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