1 package org.eclipse.aether.internal.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23
24 import static java.util.Objects.requireNonNull;
25
26 import java.util.Objects;
27 import java.util.SortedSet;
28 import java.util.TreeSet;
29
30 import org.eclipse.aether.RepositorySystemSession;
31 import org.eclipse.aether.artifact.Artifact;
32 import org.eclipse.aether.metadata.Metadata;
33 import org.eclipse.aether.repository.LocalArtifactRegistration;
34 import org.eclipse.aether.repository.LocalArtifactRequest;
35 import org.eclipse.aether.repository.LocalArtifactResult;
36 import org.eclipse.aether.repository.LocalMetadataRegistration;
37 import org.eclipse.aether.repository.LocalMetadataRequest;
38 import org.eclipse.aether.repository.LocalMetadataResult;
39 import org.eclipse.aether.repository.LocalRepository;
40 import org.eclipse.aether.repository.LocalRepositoryManager;
41 import org.eclipse.aether.repository.RemoteRepository;
42
43
44
45
46 class SimpleLocalRepositoryManager
47 implements LocalRepositoryManager
48 {
49
50 private final LocalRepository repository;
51
52 private final LocalPathComposer localPathComposer;
53
54 SimpleLocalRepositoryManager( File basedir, String type, LocalPathComposer localPathComposer )
55 {
56 requireNonNull( basedir, "base directory cannot be null" );
57 repository = new LocalRepository( basedir.getAbsoluteFile(), type );
58 this.localPathComposer = requireNonNull( localPathComposer );
59 }
60
61 @Override
62 public LocalRepository getRepository()
63 {
64 return repository;
65 }
66
67 @Override
68 public String getPathForLocalArtifact( Artifact artifact )
69 {
70 requireNonNull( artifact, "artifact cannot be null" );
71 return localPathComposer.getPathForArtifact( artifact, true );
72 }
73
74 @Override
75 public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
76 {
77 requireNonNull( artifact, "artifact cannot be null" );
78 requireNonNull( repository, "repository cannot be null" );
79 return localPathComposer.getPathForArtifact( artifact, false );
80 }
81
82 @Override
83 public String getPathForLocalMetadata( Metadata metadata )
84 {
85 requireNonNull( metadata, "metadata cannot be null" );
86 return localPathComposer.getPathForMetadata( metadata, "local" );
87 }
88
89 @Override
90 public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
91 {
92 requireNonNull( metadata, "metadata cannot be null" );
93 requireNonNull( repository, "repository cannot be null" );
94 return localPathComposer.getPathForMetadata( metadata, getRepositoryKey( repository, context ) );
95 }
96
97
98
99
100
101
102 protected String getRepositoryKey( RemoteRepository repository, String context )
103 {
104 String key;
105
106 if ( repository.isRepositoryManager() )
107 {
108
109
110 StringBuilder buffer = new StringBuilder( 128 );
111
112 buffer.append( repository.getId() );
113
114 buffer.append( '-' );
115
116 SortedSet<String> subKeys = new TreeSet<>();
117 for ( RemoteRepository mirroredRepo : repository.getMirroredRepositories() )
118 {
119 subKeys.add( mirroredRepo.getId() );
120 }
121
122 SimpleDigest digest = new SimpleDigest();
123 digest.update( context );
124 for ( String subKey : subKeys )
125 {
126 digest.update( subKey );
127 }
128 buffer.append( digest.digest() );
129
130 key = buffer.toString();
131 }
132 else
133 {
134
135
136 key = repository.getId();
137 }
138
139 return key;
140 }
141
142 @Override
143 public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request )
144 {
145 requireNonNull( session, "session cannot be null" );
146 requireNonNull( request, "request cannot be null" );
147 Artifact artifact = request.getArtifact();
148 LocalArtifactResult result = new LocalArtifactResult( request );
149
150 String path;
151 File file;
152
153
154 if ( Objects.equals( artifact.getVersion(), artifact.getBaseVersion() ) )
155 {
156 path = getPathForLocalArtifact( artifact );
157 file = new File( getRepository().getBasedir(), path );
158 if ( file.isFile() )
159 {
160 result.setFile( file );
161 result.setAvailable( true );
162 }
163 }
164
165 if ( !result.isAvailable() )
166 {
167 for ( RemoteRepository repository : request.getRepositories() )
168 {
169 path = getPathForRemoteArtifact( artifact, repository, request.getContext() );
170 file = new File( getRepository().getBasedir(), path );
171 if ( file.isFile() )
172 {
173 result.setFile( file );
174 result.setAvailable( true );
175 break;
176 }
177 }
178
179 }
180
181 return result;
182 }
183
184 @Override
185 public void add( RepositorySystemSession session, LocalArtifactRegistration request )
186 {
187 requireNonNull( session, "session cannot be null" );
188 requireNonNull( request, "request cannot be null" );
189
190 }
191
192 @Override
193 public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
194 {
195 requireNonNull( session, "session cannot be null" );
196 requireNonNull( request, "request cannot be null" );
197 LocalMetadataResult result = new LocalMetadataResult( request );
198
199 String path;
200
201 Metadata metadata = request.getMetadata();
202 String context = request.getContext();
203 RemoteRepository remote = request.getRepository();
204
205 if ( remote != null )
206 {
207 path = getPathForRemoteMetadata( metadata, remote, context );
208 }
209 else
210 {
211 path = getPathForLocalMetadata( metadata );
212 }
213
214 File file = new File( getRepository().getBasedir(), path );
215 if ( file.isFile() )
216 {
217 result.setFile( file );
218 }
219
220 return result;
221 }
222
223 @Override
224 public void add( RepositorySystemSession session, LocalMetadataRegistration request )
225 {
226 requireNonNull( session, "session cannot be null" );
227 requireNonNull( request, "request cannot be null" );
228
229 }
230
231 @Override
232 public String toString()
233 {
234 return String.valueOf( getRepository() );
235 }
236 }