1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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 }