forked from bndtools/bnd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProjectResolver.java
204 lines (172 loc) · 5.96 KB
/
ProjectResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package biz.aQute.resolve;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.felix.resolver.Logger;
import org.osgi.framework.ServiceReference;
import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
import org.osgi.resource.Wire;
import org.osgi.service.log.LogService;
import org.osgi.service.resolver.ResolutionException;
import org.osgi.service.resolver.Resolver;
import org.slf4j.LoggerFactory;
import aQute.bnd.build.Container;
import aQute.bnd.build.Project;
import aQute.bnd.header.Attrs;
import aQute.bnd.header.Parameters;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.resource.CapReqBuilder;
import aQute.bnd.osgi.resource.ResourceUtils;
import aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability;
import aQute.bnd.service.Strategy;
/**
* This class provides resolving capabilities to a Project (and this a bndrun
* which is a Run which extends Project). This class is supposed to simplify the
* sometimes bewildering number of moving cogs in resolving. It is a processor
* and uses the facilities to provide the different logging schemes used.
* <p>
* See RunResolution for a replacement
*/
@Deprecated
public class ProjectResolver extends Processor implements ResolutionCallback {
private final static org.slf4j.Logger logger = LoggerFactory.getLogger(ProjectResolver.class);
private final class ReporterLogger extends Logger implements LogService {
ReporterLogger(int i) {
super(i);
}
@Override
protected void doLog(int level, String msg, Throwable throwable) {
switch (level) {
case Logger.LOG_ERROR : // error
if (throwable == null) {
error("%s", msg);
} else {
exception(throwable, "%s", msg);
}
break;
case Logger.LOG_WARNING :
if (throwable == null) {
warning("%s", msg);
} else {
warning("%s: %s", msg, throwable);
}
break;
case Logger.LOG_INFO :
logger.info("{}", msg, throwable);
break;
default :
logger.debug("{}", msg, throwable);
break;
}
}
@Override
public void log(ServiceReference sr, int level, String message) {
doLog(level, toString(sr) + message, null);
}
@Override
public void log(ServiceReference sr, int level, String message, Throwable exception) {
doLog(level, toString(sr) + message, exception);
}
private String toString(ServiceReference<?> sr) {
return "[" + sr.getProperty(org.osgi.framework.Constants.SERVICE_ID) + "] ";
}
}
private final Project project;
private Map<Resource, List<Wire>> resolution;
private final ReporterLogger log = new ReporterLogger(0);
private final Collection<ResolutionCallback> cbs = new ArrayList<>();
public ProjectResolver(Project project) {
super(project);
getSettings(project);
this.project = project;
}
public Map<Resource, List<Wire>> resolve() throws ResolutionException {
try (ResolverLogger logger = new ResolverLogger()) {
ResolveProcess resolve = new ResolveProcess();
Resolver resolver = new BndResolver(logger);
resolution = resolve.resolveRequired(this, project, this, resolver, cbs, log);
return resolution;
}
}
@Override
public void processCandidates(Requirement requirement, Set<Capability> wired, List<Capability> candidates) {
// System.out.println("Process candidates " + requirement + " " + wired
// + " " + candidates);
}
/**
* Get the run bundles from the resolution. Resolve if this has not happened
* yet.
*/
public List<Container> getRunBundles() throws Exception {
Map<Resource, List<Wire>> resolution = this.resolution;
if (resolution == null) {
resolution = resolve();
}
List<Container> containers = new ArrayList<>();
for (Resource r : resolution.keySet()) {
IdentityCapability identity = ResourceUtils.getIdentityCapability(r);
if (identity == null) {
error("Identity for %s not found", r);
continue;
}
Container bundle = project.getBundle(identity.osgi_identity(), identity.version()
.toString(), Strategy.EXACT, null);
if (bundle == null) {
error("Bundle for %s-%s not found", identity.osgi_identity(), identity.version());
continue;
}
containers.add(bundle);
}
return containers;
}
/**
* Validate the current project for resolving.
*
* @throws Exception
*/
public void validate() throws Exception {
BndrunResolveContext context = getContext();
String runrequires = getProperty(RUNREQUIRES);
if (runrequires == null || runrequires.isEmpty()) {
error("Requires the %s instruction to be set", RUNREQUIRES);
} else {
if (EMPTY_HEADER.equals(runrequires))
return;
exists(context, runrequires, "Initial requirement %s cannot be resolved to an entry in the repositories");
}
String framework = getProperty(RUNFW);
if (framework == null) {
error("No framework is set");
} else {
exists(context, framework, "Framework not found");
}
}
private void exists(BndrunResolveContext context, String framework, String msg) throws Exception {
Parameters p = new Parameters(framework, this);
for (Map.Entry<String, Attrs> e : p.entrySet()) {
exists(context, e.getKey(), e.getValue(), msg);
}
}
private void exists(BndrunResolveContext context, String namespace, Attrs attrs, String msg) throws Exception {
Requirement req = CapReqBuilder.getRequirementFrom(namespace, attrs);
List<Capability> caps = context.findProviders(req);
if (caps == null || caps.isEmpty())
error(msg, req);
}
public BndrunResolveContext getContext() {
return new BndrunResolveContext(this, project, this, log);
}
public IdentityCapability getResource(String bsn, String version) {
Requirement requirement = CapReqBuilder.createBundleRequirement(bsn, version)
.buildSyntheticRequirement();
List<Capability> result = getContext().findProviders(requirement);
if (result == null || result.isEmpty())
return null;
return ResourceUtils.getIdentityCapability(result.get(0)
.getResource());
}
}