1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.geronimo.gshell.artifact.ivy;
21
22 import org.apache.geronimo.gshell.artifact.Artifact;
23 import org.apache.geronimo.gshell.artifact.ArtifactResolver;
24 import org.apache.geronimo.gshell.artifact.transfer.TransferListener;
25 import org.apache.ivy.Ivy;
26 import org.apache.ivy.core.module.descriptor.Configuration;
27 import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor;
28 import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor;
29 import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
30 import org.apache.ivy.core.module.id.ModuleRevisionId;
31 import org.apache.ivy.core.report.ArtifactDownloadReport;
32 import org.apache.ivy.core.report.ResolveReport;
33 import org.apache.ivy.core.resolve.ResolveOptions;
34 import org.apache.ivy.util.filter.Filter;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import java.util.LinkedHashSet;
39
40
41
42
43
44
45 public class ArtifactResolverImpl
46 implements ArtifactResolver
47 {
48 private final Logger log = LoggerFactory.getLogger(getClass());
49
50 private final Ivy ivy;
51
52 public ArtifactResolverImpl(final Ivy ivy) {
53 assert ivy != null;
54
55 this.ivy = ivy;
56 }
57
58 public void setTransferListener(final TransferListener listener) {
59 assert listener != null;
60
61
62 }
63
64 public Result resolve(final Request request) throws Failure {
65 assert request != null;
66
67 ResolveOptions options = new ResolveOptions();
68 options.setOutputReport(true);
69 options.setTransitive(true);
70
71 AndArtifactFilter filter = new AndArtifactFilter();
72 options.setArtifactFilter(filter);
73
74
75 filter.add(new IvyDependenciesFilter());
76
77 if (request.filter != null) {
78 log.debug("Filter: {}", request.filter);
79
80 filter.add(new Filter() {
81 public boolean accept(final Object obj) {
82 if (!(obj instanceof org.apache.ivy.core.module.descriptor.Artifact)) {
83 return false;
84 }
85
86 Artifact artifact = createArtifact((org.apache.ivy.core.module.descriptor.Artifact)obj);
87 return request.filter.accept(artifact);
88 }
89 });
90 }
91
92 ModuleDescriptor md = createModuleDescriptor(request);
93
94 Result result = new Result();
95
96 try {
97 ResolveReport resolveReport = ivy.resolve(md, options);
98 result.artifacts = new LinkedHashSet<Artifact>();
99
100 log.debug("Resolved:");
101
102 for (ArtifactDownloadReport downloadReport : resolveReport.getAllArtifactsReports()) {
103 Artifact artifact = createArtifact(downloadReport.getArtifact());
104 artifact.setFile(downloadReport.getLocalFile());
105
106 log.debug(" {}", artifact);
107
108 result.artifacts.add(artifact);
109 }
110 }
111 catch (Exception e) {
112 throw new Failure(e);
113 }
114
115 return result;
116 }
117
118 private Artifact createArtifact(final org.apache.ivy.core.module.descriptor.Artifact source) {
119 assert source != null;
120
121 ModuleRevisionId sourceId = source.getModuleRevisionId();
122
123 Artifact artifact = new Artifact();
124 artifact.setGroup(sourceId.getOrganisation());
125 artifact.setName(source.getName());
126 artifact.setVersion(sourceId.getRevision());
127 artifact.setType(source.getType());
128
129 return artifact;
130 }
131
132 private ModuleDescriptor createModuleDescriptor(final Request request) {
133 assert request != null;
134
135 log.debug("Artifact: {}", request.artifact);
136
137 ModuleRevisionId id = ModuleRevisionId.newInstance(request.artifact.getGroup(), request.artifact.getName(), request.artifact.getVersion());
138 DefaultModuleDescriptor md = new DefaultModuleDescriptor(id, "integration", null, true);
139 md.addConfiguration(new Configuration("default"));
140 md.setLastModified(System.currentTimeMillis());
141
142 if (request.artifacts != null) {
143 log.debug("Dependencies:");
144
145 for (Artifact artifact : request.artifacts) {
146 log.debug(" {}", artifact);
147
148 ModuleRevisionId depId = ModuleRevisionId.newInstance(artifact.getGroup(), artifact.getName(), artifact.getVersion());
149 DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, depId, false, false, true);
150 dd.addDependencyConfiguration("default", "default");
151 md.addDependency(dd);
152 }
153 }
154
155 return md;
156 }
157 }