1 package com.atlassian.plugin.osgi.util;
2
3 import com.atlassian.plugin.PluginDependencies;
4 import com.atlassian.plugin.module.ContainerAccessor;
5 import org.osgi.framework.Bundle;
6 import org.osgi.framework.namespace.PackageNamespace;
7 import org.osgi.framework.wiring.BundleRevision;
8 import org.osgi.framework.wiring.BundleRevisions;
9 import org.osgi.framework.wiring.BundleWire;
10
11 import java.util.Collection;
12
13
14
15
16
17 public final class OsgiPluginUtil {
18
19 private static final PluginDependencies EMPTY_DEPS = new PluginDependencies();
20
21
22
23
24
25
26
27
28
29
30
31
32
33 public static ContainerAccessor createNonExistingPluginContainer(final String pluginKey) {
34 return new ContainerAccessor() {
35
36 @Override
37 public <T> T createBean(Class<T> clazz) {
38 throw new UnsupportedOperationException(String.format("Plugin '%s' has no container", pluginKey));
39 }
40
41 @Override
42 public <T> T injectBean(T bean) {
43 throw new UnsupportedOperationException(String.format("Plugin '%s' has no container", pluginKey));
44 }
45
46 @Override
47 public <T> T getBean(String id) {
48 throw new UnsupportedOperationException(String.format("Plugin '%s' has no container", pluginKey));
49 }
50
51 @Override
52 public <T> Collection<T> getBeansOfType(Class<T> interfaceClass) {
53 throw new UnsupportedOperationException(String.format("Plugin '%s' has no container", pluginKey));
54 }
55 };
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public static PluginDependencies getDependencies(final Bundle bundle) {
72 int state = bundle.getState();
73 if (state == Bundle.INSTALLED || state == Bundle.UNINSTALLED) {
74 return EMPTY_DEPS;
75 }
76
77
78
79 final PluginDependencies.Builder depsBuilder = PluginDependencies.builder();
80 if (bundle instanceof BundleRevisions) {
81 for (final BundleRevision bundleRevision : ((BundleRevisions) bundle).getRevisions()) {
82 for (final BundleWire requiredWire : bundleRevision.getWiring().getRequiredWires(null)) {
83 final String pluginKey = OsgiHeaderUtil.getPluginKey(requiredWire.getProviderWiring().getBundle());
84 String resolutionDirective = requiredWire.getRequirement().getDirectives().get(PackageNamespace.REQUIREMENT_RESOLUTION_DIRECTIVE);
85
86 if (resolutionDirective == null) {
87
88 resolutionDirective = PackageNamespace.RESOLUTION_MANDATORY;
89 }
90
91 switch (resolutionDirective) {
92 case PackageNamespace.RESOLUTION_OPTIONAL:
93 depsBuilder.withOptional(pluginKey);
94 break;
95 case PackageNamespace.RESOLUTION_DYNAMIC:
96 depsBuilder.withDynamic(pluginKey);
97 break;
98 default:
99
100 depsBuilder.withMandatory(pluginKey);
101 break;
102 }
103 }
104 }
105 }
106
107 return depsBuilder.build();
108 }
109 }