1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.update.version;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.text.ParseException;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.apache.commons.digester3.annotations.rules.BeanPropertySetter;
32 import org.apache.commons.digester3.annotations.rules.CallMethod;
33 import org.apache.commons.digester3.annotations.rules.CallParam;
34 import org.apache.commons.digester3.annotations.rules.ObjectCreate;
35 import org.apache.commons.digester3.annotations.rules.SetProperty;
36 import org.apache.commons.lang3.builder.ToStringBuilder;
37 import org.apache.ivy.Ivy;
38 import org.apache.ivy.core.cache.ArtifactOrigin;
39 import org.apache.ivy.core.module.descriptor.Artifact;
40 import org.apache.ivy.core.module.id.ModuleRevisionId;
41 import org.apache.ivy.core.report.ArtifactDownloadReport;
42 import org.apache.ivy.core.resolve.DownloadOptions;
43 import org.apache.ivy.core.resolve.ResolveOptions;
44 import org.apache.ivy.core.resolve.ResolvedModuleRevision;
45 import org.apache.ivy.core.settings.IvySettings;
46 import org.efaps.update.Profile;
47 import org.efaps.update.util.InstallationException;
48
49
50
51
52
53
54
55
56 @ObjectCreate(pattern = "install/dependencies/dependency")
57 public class Dependency
58 {
59
60
61
62 @BeanPropertySetter(pattern = "install/dependencies/dependency/groupId")
63 private String groupId;
64
65
66
67
68
69 @BeanPropertySetter(pattern = "install/dependencies/dependency/artifactId")
70 private String artifactId;
71
72
73
74
75 @BeanPropertySetter(pattern = "install/dependencies/dependency/version")
76 private String version;
77
78
79
80
81
82
83 private File jarFile;
84
85
86
87
88 @SetProperty(pattern = "install/dependencies/dependency/", attributeName = "order")
89 private Integer order;
90
91
92
93
94 private final Set<String> profileNames = new HashSet<String>();
95
96
97
98
99
100
101
102
103 public void resolve()
104 throws InstallationException
105 {
106 final IvySettings ivySettings = new IvySettings();
107 try {
108 ivySettings.load(this.getClass().getResource("/org/efaps/update/version/ivy.xml"));
109 } catch (final IOException e) {
110 throw new InstallationException("IVY setting file could not be read", e);
111 } catch (final ParseException e) {
112 throw new InstallationException("IVY setting file could not be parsed", e);
113 }
114
115 final Ivy ivy = Ivy.newInstance(ivySettings);
116 ivy.getLoggerEngine().pushLogger(new IvyOverSLF4JLogger());
117
118 final Map<String, String> attr = new HashMap<String, String>();
119 attr.put("changing", "true");
120
121 final ModuleRevisionId modRevId = ModuleRevisionId.newInstance(this.groupId,
122 this.artifactId,
123 this.version,
124 attr);
125
126 final ResolveOptions options = new ResolveOptions();
127 options.setConfs(new String[] {"runtime"});
128
129
130 final ResolvedModuleRevision resModRev = ivy.findModule(modRevId);
131
132 Artifact dw = null;
133 for (final Artifact artifact : resModRev.getDescriptor().getAllArtifacts()) {
134 if ("jar".equals(artifact.getType())) {
135 dw = artifact;
136 break;
137 }
138 }
139
140 final DownloadOptions dwOptions = new DownloadOptions();
141
142 final ArtifactOrigin ao = resModRev.getArtifactResolver().locate(dw);
143 resModRev.getArtifactResolver().getRepositoryCacheManager().clean();
144
145 final ArtifactDownloadReport adw = resModRev.getArtifactResolver().download(ao, dwOptions);
146
147 this.jarFile = adw.getLocalFile();
148 }
149
150
151
152
153
154
155
156 public File getJarFile()
157 {
158 return this.jarFile;
159 }
160
161
162
163
164
165
166 public Integer getOrder()
167 {
168 return this.order;
169 }
170
171
172
173
174
175
176
177 public void setOrder(final Integer _order)
178 {
179 this.order = _order;
180 }
181
182
183
184
185
186
187 public String getGroupId()
188 {
189 return this.groupId;
190 }
191
192
193
194
195
196
197
198
199 public void setGroupId(final String _groupId)
200 {
201 this.groupId = _groupId;
202 }
203
204
205
206
207
208
209
210 public String getArtifactId()
211 {
212 return this.artifactId;
213 }
214
215
216
217
218
219
220
221
222 public void setArtifactId(final String _artifactId)
223 {
224 this.artifactId = _artifactId;
225 }
226
227
228
229
230
231
232
233 public String getVersion()
234 {
235 return this.version;
236 }
237
238
239
240
241
242
243
244 public void setVersion(final String _version)
245 {
246 this.version = _version;
247 }
248
249
250
251
252 @CallMethod(pattern = "install/dependencies/dependency/profiles/profile")
253 public void addProfileName(@CallParam(pattern = "install/dependencies/dependency/profiles/profile",
254 attributeName = "name") final String _name)
255 {
256 this.profileNames.add(_name);
257 }
258
259
260
261
262
263
264
265
266 public Set<Profile> getProfiles()
267 {
268 final Set<Profile> ret = new HashSet<Profile>();
269 if (this.profileNames.isEmpty()) {
270 ret.add(Profile.getDefaultProfile());
271 } else {
272 for (final String name : this.profileNames) {
273 ret.add(Profile.getProfile(name));
274 }
275 }
276 return ret;
277 }
278
279
280
281
282
283
284 @Override
285 public String toString()
286 {
287 return new ToStringBuilder(this)
288 .append("groupId", this.groupId)
289 .append("artifactId", this.artifactId)
290 .append("version", this.version)
291 .append("order", this.order)
292 .append("jarFile", this.jarFile)
293 .toString();
294 }
295 }