View Javadoc

1   /*
2    *  Licensed under the Apache License, Version 2.0 (the "License");
3    *  you may not use this file except in compliance with the License.
4    *  You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    *  Unless required by applicable law or agreed to in writing, software
9    *  distributed under the License is distributed on an "AS IS" BASIS,
10   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   *  See the License for the specific language governing permissions and
12   *  limitations under the License.
13   */
14  /*
15   * This file has been modified by Chris Pheby in accordance with Section 4.2
16   * of the Apache Software License
17   */
18  package org.jadira.dependencynavigator.implementations.local;
19  
20  import org.apache.maven.model.Model;
21  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
22  import org.codehaus.plexus.context.Context;
23  import org.codehaus.plexus.context.ContextException;
24  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
25  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
26  import org.jadira.dependencynavigator.controller.DependencyResolutionException;
27  import org.jadira.dependencynavigator.implementations.Repository;
28  
29  import java.io.File;
30  import java.io.FileNotFoundException;
31  import java.io.FileReader;
32  import java.io.IOException;
33  
34  public class LocalDiskRepository implements Repository, Contextualizable {
35  
36      public static final String KEY_LOCAL_REPO_PATH = "local.repo";
37  
38      private File repositoryRoot;
39  
40      public Model resolveFile(String groupId, String artifactId, String version) {
41          String[] groups = groupId.split("\\.");
42          File artifactPomFile = new File(repositoryRoot.getPath());
43          for (int i = 0; i < groups.length; i++) {
44              artifactPomFile = new File(artifactPomFile, groups[i]);
45          }
46          artifactPomFile = new File(artifactPomFile, artifactId);
47          artifactPomFile = new File(artifactPomFile, version);
48          artifactPomFile = new File(artifactPomFile, artifactId + "-" + version + ".pom");
49  
50          if (!artifactPomFile.exists()) {
51              return null;
52          }
53  
54          MavenXpp3Reader pomReader = new MavenXpp3Reader();
55          try {
56              return pomReader.read(new FileReader(artifactPomFile));
57          } catch (FileNotFoundException e) {
58              throw new DependencyResolutionException(e);
59          } catch (IOException e) {
60              throw new DependencyResolutionException(e);
61          } catch (XmlPullParserException e) {
62              // throw new DependencyResolutionException(e);
63          }
64          return null;
65      }
66  
67      public File getRepositoryRoot() {
68          return this.repositoryRoot;
69      }
70  
71      public boolean canCreateSnapshot() {
72          return true;
73      }
74  
75      public void contextualize(Context context) throws ContextException {
76          String repoPath = (String) context.get(KEY_LOCAL_REPO_PATH);
77          if (repoPath == null || repoPath.trim().length() == 0) {
78              throw new ContextException("Missing configuration parameter: " + KEY_LOCAL_REPO_PATH);
79          }
80          repositoryRoot = new File(repoPath);
81      }
82  }