1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.osgi.boot.utils.internal;
20
21 import java.lang.reflect.Field;
22 import java.lang.reflect.Method;
23 import java.util.List;
24
25 import org.eclipse.jetty.osgi.boot.utils.BundleClassLoaderHelper;
26 import org.osgi.framework.Bundle;
27
28
29
30
31
32 public class DefaultBundleClassLoaderHelper implements BundleClassLoaderHelper
33 {
34
35 private static boolean identifiedOsgiImpl = false;
36
37 private static boolean isEquinox = false;
38
39 private static boolean isFelix = false;
40
41 private static void init(Bundle bundle)
42 {
43 identifiedOsgiImpl = true;
44 try
45 {
46 isEquinox = bundle.getClass().getClassLoader().loadClass("org.eclipse.osgi.framework.internal.core.BundleHost") != null;
47 }
48 catch (Throwable t)
49 {
50 isEquinox = false;
51 }
52 if (!isEquinox)
53 {
54 try
55 {
56 isFelix = bundle.getClass().getClassLoader().loadClass("org.apache.felix.framework.BundleImpl") != null;
57 }
58 catch (Throwable t2)
59 {
60 isFelix = false;
61 }
62 }
63 }
64
65
66
67
68
69
70
71 public ClassLoader getBundleClassLoader(Bundle bundle)
72 {
73 String bundleActivator = (String) bundle.getHeaders().get("Bundle-Activator");
74 if (bundleActivator == null)
75 {
76 bundleActivator = (String) bundle.getHeaders().get("Jetty-ClassInBundle");
77 }
78 if (bundleActivator != null)
79 {
80 try
81 {
82 return bundle.loadClass(bundleActivator).getClassLoader();
83 }
84 catch (ClassNotFoundException e)
85 {
86
87
88 e.printStackTrace();
89 }
90 }
91
92 if (!identifiedOsgiImpl)
93 {
94 init(bundle);
95 }
96 if (isEquinox)
97 {
98 return internalGetEquinoxBundleClassLoader(bundle);
99 }
100 else if (isFelix) { return internalGetFelixBundleClassLoader(bundle); }
101 return null;
102 }
103
104 private static Method Equinox_BundleHost_getBundleLoader_method;
105
106 private static Method Equinox_BundleLoader_createClassLoader_method;
107
108 private static ClassLoader internalGetEquinoxBundleClassLoader(Bundle bundle)
109 {
110
111 try
112 {
113 if (Equinox_BundleHost_getBundleLoader_method == null)
114 {
115 Equinox_BundleHost_getBundleLoader_method =
116 bundle.getClass().getClassLoader().loadClass("org.eclipse.osgi.framework.internal.core.BundleHost").getDeclaredMethod("getBundleLoader", new Class[] {});
117 Equinox_BundleHost_getBundleLoader_method.setAccessible(true);
118 }
119 Object bundleLoader = Equinox_BundleHost_getBundleLoader_method.invoke(bundle, new Object[] {});
120 if (Equinox_BundleLoader_createClassLoader_method == null && bundleLoader != null)
121 {
122 Equinox_BundleLoader_createClassLoader_method =
123 bundleLoader.getClass().getClassLoader().loadClass("org.eclipse.osgi.internal.loader.BundleLoader").getDeclaredMethod("createClassLoader", new Class[] {});
124 Equinox_BundleLoader_createClassLoader_method.setAccessible(true);
125 }
126 return (ClassLoader) Equinox_BundleLoader_createClassLoader_method.invoke(bundleLoader, new Object[] {});
127 }
128 catch (Throwable t)
129 {
130 t.printStackTrace();
131 }
132 return null;
133 }
134
135 private static Field Felix_BundleImpl_m_modules_field;
136
137 private static Field Felix_ModuleImpl_m_classLoader_field;
138
139 private static ClassLoader internalGetFelixBundleClassLoader(Bundle bundle)
140 {
141
142 try
143 {
144
145
146 if (Felix_BundleImpl_m_modules_field == null)
147 {
148 Felix_BundleImpl_m_modules_field = bundle.getClass().getClassLoader().loadClass("org.apache.felix.framework.BundleImpl").getDeclaredField("m_modules");
149 Felix_BundleImpl_m_modules_field.setAccessible(true);
150 }
151
152
153 Object currentModuleImpl;
154 try
155 {
156 Object[] moduleArray = (Object[]) Felix_BundleImpl_m_modules_field.get(bundle);
157 currentModuleImpl = moduleArray[moduleArray.length - 1];
158 }
159 catch (Throwable t2)
160 {
161 @SuppressWarnings("unchecked")
162 List<Object> moduleArray = (List<Object>) Felix_BundleImpl_m_modules_field.get(bundle);
163 currentModuleImpl = moduleArray.get(moduleArray.size() - 1);
164 }
165
166 if (Felix_ModuleImpl_m_classLoader_field == null && currentModuleImpl != null)
167 {
168 Felix_ModuleImpl_m_classLoader_field = bundle.getClass().getClassLoader().loadClass("org.apache.felix.framework.ModuleImpl").getDeclaredField("m_classLoader");
169 Felix_ModuleImpl_m_classLoader_field.setAccessible(true);
170 }
171
172
173
174 ClassLoader cl = (ClassLoader) Felix_ModuleImpl_m_classLoader_field.get(currentModuleImpl);
175 if (cl == null)
176 {
177
178
179
180
181 bundle.loadClass("java.lang.Object");
182 cl = (ClassLoader) Felix_ModuleImpl_m_classLoader_field.get(currentModuleImpl);
183 return cl;
184 }
185 else
186 {
187 return cl;
188 }
189 }
190 catch (Throwable t)
191 {
192 t.printStackTrace();
193 }
194 return null;
195 }
196
197 }