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.apache.maven.project;
20  
21  import java.io.File;
22  
23  import org.eclipse.aether.RepositorySystemSession;
24  import org.eclipse.aether.artifact.Artifact;
25  import org.eclipse.aether.metadata.Metadata;
26  import org.eclipse.aether.repository.LocalArtifactRegistration;
27  import org.eclipse.aether.repository.LocalArtifactRequest;
28  import org.eclipse.aether.repository.LocalArtifactResult;
29  import org.eclipse.aether.repository.LocalMetadataRegistration;
30  import org.eclipse.aether.repository.LocalMetadataRequest;
31  import org.eclipse.aether.repository.LocalMetadataResult;
32  import org.eclipse.aether.repository.LocalRepository;
33  import org.eclipse.aether.repository.LocalRepositoryManager;
34  import org.eclipse.aether.repository.RemoteRepository;
35  
36  /**
37   * @author Benjamin Bentmann
38   */
39  public class LegacyLocalRepositoryManager implements LocalRepositoryManager {
40  
41      private final LocalRepository repository;
42  
43      public LegacyLocalRepositoryManager(File basedir) {
44          this.repository = new LocalRepository(basedir.getAbsoluteFile(), "legacy");
45      }
46  
47      public LocalRepository getRepository() {
48          return repository;
49      }
50  
51      public String getPathForLocalArtifact(Artifact artifact) {
52          StringBuilder path = new StringBuilder(128);
53  
54          path.append(artifact.getGroupId()).append('/');
55  
56          path.append(artifact.getExtension()).append("s/");
57  
58          path.append(artifact.getArtifactId()).append('-').append(artifact.getVersion());
59  
60          if (artifact.getClassifier().length() > 0) {
61              path.append('-').append(artifact.getClassifier());
62          }
63  
64          path.append('.').append(artifact.getExtension());
65  
66          return path.toString();
67      }
68  
69      public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) {
70          return getPathForLocalArtifact(artifact);
71      }
72  
73      public String getPathForLocalMetadata(Metadata metadata) {
74          return getPath(metadata, "local");
75      }
76  
77      public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) {
78          return getPath(metadata, getRepositoryKey(repository, context));
79      }
80  
81      String getRepositoryKey(RemoteRepository repository, String context) {
82          return repository.getId();
83      }
84  
85      private String getPath(Metadata metadata, String repositoryKey) {
86          StringBuilder path = new StringBuilder(128);
87  
88          if (metadata.getGroupId().length() > 0) {
89              path.append(metadata.getGroupId().replace('.', '/')).append('/');
90  
91              if (metadata.getArtifactId().length() > 0) {
92                  path.append(metadata.getArtifactId()).append('/');
93  
94                  if (metadata.getVersion().length() > 0) {
95                      path.append(metadata.getVersion()).append('/');
96                  }
97              }
98          }
99  
100         path.append(insertRepositoryKey(metadata.getType(), repositoryKey));
101 
102         return path.toString();
103     }
104 
105     private String insertRepositoryKey(String filename, String repositoryKey) {
106         String result;
107         int idx = filename.indexOf('.');
108         if (idx < 0) {
109             result = filename + '-' + repositoryKey;
110         } else {
111             result = filename.substring(0, idx) + '-' + repositoryKey + filename.substring(idx);
112         }
113         return result;
114     }
115 
116     public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRequest request) {
117         String path = getPathForLocalArtifact(request.getArtifact());
118         File file = new File(getRepository().getBasedir(), path);
119 
120         LocalArtifactResult result = new LocalArtifactResult(request);
121         if (file.isFile()) {
122             result.setFile(file);
123             result.setAvailable(true);
124         }
125 
126         return result;
127     }
128 
129     public void add(RepositorySystemSession session, LocalArtifactRegistration request) {
130         // noop
131     }
132 
133     public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) {
134         LocalMetadataResult result = new LocalMetadataResult(request);
135 
136         String path;
137 
138         Metadata metadata = request.getMetadata();
139         String context = request.getContext();
140         RemoteRepository remote = request.getRepository();
141 
142         if (remote != null) {
143             path = getPathForRemoteMetadata(metadata, remote, context);
144         } else {
145             path = getPathForLocalMetadata(metadata);
146         }
147 
148         File file = new File(getRepository().getBasedir(), path);
149         if (file.isFile()) {
150             result.setFile(file);
151         }
152 
153         return result;
154     }
155 
156     public void add(RepositorySystemSession session, LocalMetadataRegistration request) {
157         // noop
158     }
159 
160     public String toString() {
161         return String.valueOf(getRepository());
162     }
163 }