-
Notifications
You must be signed in to change notification settings - Fork 21
/
OsgiExplorer.java
409 lines (324 loc) · 13.5 KB
/
OsgiExplorer.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package com.neva.felix.webconsole.plugins.search.core;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Splitter;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.primitives.Longs;
import com.neva.felix.webconsole.plugins.search.decompiler.DecompilerFactory;
import com.neva.felix.webconsole.plugins.search.decompiler.Decompilers;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.osgi.framework.*;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.metatype.MetaTypeInformation;
import org.osgi.service.metatype.MetaTypeService;
import org.osgi.service.metatype.ObjectClassDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class OsgiExplorer {
public static final String BUNDLE_STORAGE_PROP = "org.osgi.framework.storage";
public static final String BUNDLE_STORAGE_DIR_DEFAULT = "felix-cache";
public static final String BUNDLE_PATH_FORMAT = "%s/bundle%s";
public static final String BUNDLE_JAR_FILE = "bundle.jar";
public static final String JAR_EXT = "jar";
private static final Logger LOG = LoggerFactory.getLogger(OsgiExplorer.class);
private final MetaTypeService metaTypeService;
private final ConfigurationAdmin configAdmin;
private BundleContext context;
public OsgiExplorer(BundleContext context) {
this.context = context;
this.metaTypeService = service(MetaTypeService.class);
this.configAdmin = service(ConfigurationAdmin.class);
}
public File findDir(Long bundleId) {
return new File(String.format(BUNDLE_PATH_FORMAT, getBundleDir(), bundleId));
}
private String getBundleDir() {
return StringUtils.defaultIfBlank(context.getProperty(BUNDLE_STORAGE_PROP),
System.getProperty("user.dir") + "/" + BUNDLE_STORAGE_DIR_DEFAULT);
}
public File findJar(String bundleId) {
return findJar(Long.valueOf(bundleId));
}
public File findJar(Long bundleId) {
File jar = findJar(findDir(bundleId));
if (jar == null) {
//If jar not found through storage dir, try to resolve bundle location (Sling starter sources jars from maven repository)
Bundle bundle = findBundle(bundleId);
return new File(StringUtils.replace(bundle.getLocation(), "reference:file:", ""));
}
return jar;
}
public File findJar(File bundleDir) {
if (bundleDir.exists()) {
List<File> files = FluentIterable.from(FileUtils.listFiles(bundleDir, new String[]{JAR_EXT}, true))
.filter(file -> file.getName().equalsIgnoreCase(BUNDLE_JAR_FILE))
.toSortedList((f1, f2) -> f2.getAbsolutePath().compareTo(f1.getAbsolutePath()));
return Iterables.getFirst(files, null);
}
return null;
}
public BundleJar findBundleJar(String bundleId) {
BundleJar result = null;
File jar = findJar(bundleId);
Bundle bundle = findBundle(bundleId);
if (jar != null && bundle != null) {
result = new BundleJar(bundle, jar);
}
return result;
}
public Set<BundleJar> findBundleJars(List<String> bundleIds) {
Set<BundleJar> result = Sets.newLinkedHashSet();
for (String bundleId : bundleIds) {
BundleJar jar = findBundleJar(bundleId);
if (jar != null) {
result.add(jar);
}
}
return result;
}
public String proposeJarName(Long bundleId) {
return proposeJarName(String.valueOf(bundleId));
}
public String proposeJarName(String bundleId) {
String result = BUNDLE_JAR_FILE;
final Bundle bundle = context.getBundle(Long.valueOf(bundleId));
if (bundle != null) {
final String realName = FilenameUtils.getName(bundle.getLocation());
if (StringUtils.isNotEmpty(realName) && JAR_EXT.equalsIgnoreCase(FilenameUtils.getExtension(realName))) {
result = realName;
} else {
result = String.format("%s.%s", bundle.getSymbolicName(), JAR_EXT);
}
}
return result;
}
public String decompileClass(Decompilers type, BundleClass clazz) {
return decompileClass(type, clazz.getBundle().getBundleId(), clazz.getClassName());
}
public String decompileClass(Decompilers type, Long bundleId, String className) {
return decompileClass(type, findJar(bundleId), className);
}
public String decompileClass(Decompilers type, File jar, String className) {
String source = StringUtils.EMPTY;
try {
source = DecompilerFactory.get(type).decompile(jar, className, false);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return source;
}
public String decompileClass(Decompilers type, boolean showLineNumbers, File jar, String className) {
String source = StringUtils.EMPTY;
try {
source = DecompilerFactory.get(type).decompile(jar, className, showLineNumbers);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return source;
}
public List<BundleClass> findClasses(final Bundle bundle) {
return FluentIterable.from(new BundleScanner(bundle).findClassNames()).transform(new Function<String, BundleClass>() {
@Override
public BundleClass apply(String className) {
return new BundleClass(bundle, className);
}
}).toList();
}
public Iterable<BundleClass> findClasses(String name) {
if (StringUtils.isBlank(name)) {
return Collections.emptyList();
}
final List<BundleClass> results = Lists.newArrayList();
for (Bundle bundle : context.getBundles()) {
final BundleClass clazz = findClass(bundle, name);
if (clazz != null) {
results.add(clazz);
}
}
return results;
}
public List<BundleClass> findClasses(List<String> bundleIdAndClassNames) {
return FluentIterable.from(bundleIdAndClassNames).transform(new Function<String, BundleClass>() {
@Override
public BundleClass apply(String bundleIdAndClassName) {
List<String> parts = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(bundleIdAndClassName);
return findClass(parts.get(0), parts.get(1));
}
}).filter(Predicates.<BundleClass>notNull()).toList();
}
public Set<BundleClass> findClasses(List<String> bundleIds, List<String> bundleClasses) {
Set<BundleClass> result = Sets.newLinkedHashSet();
for (Bundle bundle : findBundles(bundleIds)) {
result.addAll(findClasses(bundle));
}
result.addAll(findClasses(bundleClasses));
return result;
}
public BundleClass findClass(Configuration configuration) {
BundleClass clazz = null;
Bundle bundle;
ServiceReference serviceReference = findServiceReference(configuration.getPid());
if (serviceReference == null) {
serviceReference = findServiceReference(configuration.getFactoryPid());
}
if (serviceReference != null) {
bundle = serviceReference.getBundle();
Object service = referenceService(serviceReference);
if (service != null) {
clazz = new BundleClass(bundle, service.getClass().getName());
}
} else {
bundle = findBundle(configuration);
}
String classPid = StringUtils.defaultIfBlank(configuration.getFactoryPid(), configuration.getPid());
if (clazz == null && bundle != null) {
clazz = findClass(bundle, classPid);
}
if (clazz != null) {
clazz = obtainClassMetadata(clazz, classPid);
}
return clazz;
}
private BundleClass obtainClassMetadata(BundleClass clazz, String classPid) {
if (clazz != null) {
ObjectClassDefinition definition = getClassDefinition(clazz.getBundle(), classPid);
if (definition != null) {
clazz = new MetaTypeBundleClass(clazz, definition);
}
}
return clazz;
}
public Bundle findBundle(String id) {
return findBundle(Longs.tryParse(id));
}
public Bundle findBundle(Long id) {
return id != null ? context.getBundle(id) : null;
}
public Iterable<Bundle> findBundles(List<String> ids) {
return FluentIterable.from(ids).transform(new Function<String, Bundle>() {
@Override
public Bundle apply(String id) {
return findBundle(id);
}
}).filter(Predicates.<Bundle>notNull());
}
public Bundle findBundle(final Configuration configuration) {
return FluentIterable.from(context.getBundles()).firstMatch(new Predicate<Bundle>() {
@Override
public boolean apply(Bundle bundle) {
return StringUtils.equals(configuration.getBundleLocation(), bundle.getLocation());
}
}).orNull();
}
public BundleClass findClass(ServiceReference serviceReference) {
BundleClass clazz = null;
Bundle bundle = serviceReference.getBundle();
String pid = (String) serviceReference.getProperty(Constants.SERVICE_PID);
Object service = referenceService(serviceReference);
if (service != null) {
clazz = new BundleClass(bundle, service.getClass().getName());
} else if (StringUtils.isNotBlank(pid)) {
clazz = findClass(bundle, pid);
}
if (clazz != null) {
clazz = obtainClassMetadata(clazz, pid);
}
return clazz;
}
public BundleClass findClass(String bundleId, String name) {
final Bundle bundle = findBundle(bundleId);
return bundle != null ? findClass(bundle, name) : null;
}
public BundleClass findClass(Bundle bundle, String name) {
for (final String className : new BundleScanner(bundle).findClassNames(name)) {
if (StringUtils.equals(className, name)) {
return new BundleClass(bundle, name);
}
}
return null;
}
public ObjectClassDefinition getClassDefinition(Bundle bundle, String pid) {
ObjectClassDefinition result = null;
if (bundle != null) {
try {
final MetaTypeInformation info = metaTypeService.getMetaTypeInformation(bundle);
if (info != null) {
result = info.getObjectClassDefinition(pid, null);
}
} catch (Throwable e) { //NOSONAR
// ignore
}
}
return result;
}
@SuppressWarnings("unchecked")
public <T> T service(Class<T> clazz) {
ServiceReference reference = context.getServiceReference(clazz.getName());
return reference != null ? (T) context.getService(reference) : null;
}
public Object findService(final String pid) {
return referenceService(findServiceReference(pid));
}
public Object referenceService(ServiceReference reference) {
Object service = null;
if (reference != null) {
try {
service = context.getService(reference);
} catch (NoClassDefFoundError e) { //NOSONAR
// ignore classloading issues etc
}
}
return service;
}
public ServiceReference findServiceReference(final String pid) {
ServiceReference result = null;
if (StringUtils.isNotBlank(pid)) {
try {
result = FluentIterable.from(context.getAllServiceReferences(null, null))
.firstMatch(new Predicate<ServiceReference>() {
@Override
public boolean apply(ServiceReference serviceReference) {
return StringUtils.equals(pid, (String) serviceReference.getProperty(Constants.SERVICE_PID));
}
}).orNull();
} catch (InvalidSyntaxException e) { // NOSONAR
// ignore
}
}
return result;
}
public Iterable<ServiceReference> getServiceReferences() {
try {
return FluentIterable.from(context.getAllServiceReferences(null, null));
} catch (InvalidSyntaxException e) {
LOG.error("Cannot obtain references during service search.", e);
}
return Collections.emptyList();
}
public Iterable<Configuration> getConfigurations() {
Iterable<Configuration> configurations = Collections.emptyList();
if (configAdmin != null) {
try {
configurations = FluentIterable.from(configAdmin.listConfigurations(null));
} catch (IOException | InvalidSyntaxException e) {
LOG.error("Cannot read configurations", e);
}
}
return configurations;
}
public BundleContext getContext() {
return context;
}
}