-
Notifications
You must be signed in to change notification settings - Fork 53
/
AbstractOSGiModulesRegistryImpl.java
executable file
·408 lines (350 loc) · 15.2 KB
/
AbstractOSGiModulesRegistryImpl.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
/*
* Copyright (c) 2012, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Payara Services Ltd.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.jvnet.hk2.osgiadapter;
import com.sun.enterprise.module.*;
import com.sun.enterprise.module.bootstrap.BootException;
import com.sun.enterprise.module.common_impl.AbstractModulesRegistryImpl;
import com.sun.enterprise.module.common_impl.CompositeEnumeration;
import org.glassfish.hk2.api.*;
import org.glassfish.hk2.api.Filter;
import org.glassfish.hk2.utilities.DescriptorImpl;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
import org.osgi.framework.*;
import org.osgi.service.packageadmin.PackageAdmin;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import static org.glassfish.hk2.utilities.ServiceLocatorUtilities.createDynamicConfiguration;
import static org.jvnet.hk2.osgiadapter.Logger.logger;
/**
* @author sanjeeb.sahoo@oracle.com
*/
public abstract class AbstractOSGiModulesRegistryImpl extends AbstractModulesRegistryImpl {
/**
* OSGi BundleContext - used to install/uninstall, start/stop bundles
*/
BundleContext bctx;
protected PackageAdmin pa;
private Map<ModuleChangeListener, BundleListener> moduleChangeListeners = new HashMap<>();
private Map<ModuleLifecycleListener, BundleListener> moduleLifecycleListeners = new HashMap<>();
private final ReentrantLock lock = new ReentrantLock();
protected AbstractOSGiModulesRegistryImpl(BundleContext bctx) {
super(null);
this.bctx = bctx;
ServiceReference ref = bctx.getServiceReference(PackageAdmin.class.getName());
pa = PackageAdmin.class.cast(bctx.getService(ref));
}
@Override
public void shutdown() {
modules.clear();
for (Repository repo : repositories.values()) {
try {
repo.shutdown();
} catch(IOException e) {
java.util.logging.Logger.getAnonymousLogger().log(Level.SEVERE, "Error while closing repository " + repo, e);
// swallows
}
}
// don't try to stop the system bundle, as we may be embedded inside
// something like Eclipse.
}
@Override
public List<ActiveDescriptor> parseInhabitants(
HK2Module module, String name, ServiceLocator serviceLocator, List<PopulatorPostProcessor> postProcessors)
throws IOException, BootException {
OSGiModuleImpl osgiModuleImpl = (OSGiModuleImpl) module;
List<ActiveDescriptor> activeDescriptors;
Map<String, List<Descriptor>> descriptorMap = module.getModuleDefinition().getMetadata().getDescriptors();
List<Descriptor> descriptors = descriptorMap.get(name);
if (descriptors == null) {
activeDescriptors = osgiModuleImpl.parseInhabitants(name, serviceLocator, postProcessors);
if (activeDescriptors != null) {
// use the copy constructor to create (nonactive) descriptor for serialization into the cache
descriptors = new ArrayList<>();
for (Descriptor d : activeDescriptors) {
descriptors.add(new DescriptorImpl(d));
}
module.getModuleDefinition().getMetadata().addDescriptors(name, descriptors);
}
} else {
activeDescriptors = new ArrayList<>();
DynamicConfiguration dcs = createDynamicConfiguration(serviceLocator);
for (Descriptor descriptor : descriptors) {
DescriptorImpl di = (descriptor instanceof DescriptorImpl) ? (DescriptorImpl) descriptor : new DescriptorImpl(descriptor) ;
// set the hk2loader
DescriptorImpl descriptorImpl = new OsgiPopulatorPostProcessor(osgiModuleImpl).process(serviceLocator, di);
if (descriptorImpl != null) {
activeDescriptors.add(dcs.bind(descriptorImpl, false));
}
}
dcs.commit();
}
return activeDescriptors;
}
@Override
public ModulesRegistry createChild() {
throw new UnsupportedOperationException("Not Yet Implemented"); // TODO(Sahoo)
}
@Override
public void detachAll() {
lock.lock();
try {
for (HK2Module m : modules.values()) {
m.detach();
}
} finally {
lock.unlock();
}
}
/**
* Sets the classloader parenting the class loaders created by the modules
* associated with this registry.
* @param parent parent class loader
*/
@Override
public void setParentClassLoader(ClassLoader parent) {
throw new UnsupportedOperationException("This method can't be implemented in OSGi environment");
}
/**
* Returns the parent class loader parenting the class loaders created
* by modules associated with this registry.
* @return the parent classloader
*/
@Override
public ClassLoader getParentClassLoader() {
return Bundle.class.getClassLoader();
}
/**
* Returns a ClassLoader capable of loading classes from a set of modules identified
* by their module definition and also load new urls.
*
* @param parent the parent class loader for the returned class loader instance
* @param mds module definitions for all modules this classloader should be
* capable of loading
* @param urls urls to be added to the module classloader
* @return class loader instance
* @throws com.sun.enterprise.module.ResolveError if one of the provided module
* definition cannot be resolved
*/
@Override
public ClassLoader getModulesClassLoader(final ClassLoader parent,
Collection<ModuleDefinition> mds,
URL[] urls) throws ResolveError {
final List<ClassLoader> delegateCLs = new ArrayList<>();
final List<HK2Module> delegateModules = new ArrayList<>();
for (ModuleDefinition md : mds) {
HK2Module m = makeModuleFor(md.getName(), md.getVersion());
delegateModules.add(m);
delegateCLs.add(m.getClassLoader());
}
return new URLClassLoader(urls!=null?urls:new URL[0], parent) {
/*
* This is a delegating class loader.
* This extends URLClassLoader, because web layer (Jasper to be specific)
* expects it to be a URLClassLoader so that it can extract Classpath information
* used for javac.
* It always delegates to a chain of OSGi bundle's class loader.
* ClassLoader.defineClass() is never called in the context of this class.
* There will never be a class for which getClassLoader()
* would return this class loader.
*/
@Override
public URL[] getURLs() {
List<URL> result = new ArrayList<>();
if (parent instanceof URLClassLoader) {
URL[] parentURLs = URLClassLoader.class.cast(parent).getURLs();
result.addAll(Arrays.asList(parentURLs));
}
for (HK2Module m : delegateModules) {
ModuleDefinition md = m.getModuleDefinition();
URI[] uris = md.getLocations();
URL[] urls = new URL[uris.length];
for (int i = 0; i < uris.length; ++i) {
try {
urls[i] = uris[i].toURL();
} catch (MalformedURLException e) {
logger.log(Level.WARNING, "Exception {0} while converting {1} to URL", new Object[]{e, uris[i]});
}
}
result.addAll(Arrays.asList(urls));
}
return result.toArray(new URL[0]);
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
for (ClassLoader delegate : delegateCLs) {
try {
return delegate.loadClass(name);
} catch(ClassNotFoundException e) {
// This is expected, so ignore
}
}
throw new ClassNotFoundException(name);
}
@Override
public URL findResource(String name) {
URL resource = null;
for (ClassLoader delegate : delegateCLs) {
resource = delegate.getResource(name);
if (resource != null) {
return resource;
}
}
return resource;
}
@Override
public Enumeration<URL> findResources(String name) throws IOException {
List<Enumeration<URL>> enumerators = new ArrayList<>();
for (ClassLoader delegate : delegateCLs) {
Enumeration<URL> enumerator = delegate.getResources(name);
enumerators.add(enumerator);
}
return new CompositeEnumeration(enumerators);
}
};
}
/**
* Returns a ClassLoader capable of loading classes from a set of modules identified
* by their module definition
*
* @param parent the parent class loader for the returned class loader instance
* @param defs module definitions for all modules this classloader should be
* capable of loading classes from
* @return class loader instance
* @throws ResolveError if one of the provided module
* definition cannot be resolved
*/
@Override
public ClassLoader getModulesClassLoader(ClassLoader parent,
Collection<ModuleDefinition> defs)
throws ResolveError {
return getModulesClassLoader(parent, defs, null);
}
@Override
public HK2Module find(Class clazz) {
Bundle b = pa.getBundle(clazz);
if (b!=null) {
return getModule(b);
}
return null;
}
public PackageAdmin getPackageAdmin() {
return pa;
}
public void addModuleChangeListener(final ModuleChangeListener listener, final OSGiModuleImpl module) {
BundleListener bundleListener = new BundleListener() {
public void bundleChanged(BundleEvent event) {
if ((event.getBundle() == module.getBundle()) &&
((event.getType() & BundleEvent.UPDATED) == BundleEvent.UPDATED)) {
listener.changed(module);
}
}
};
bctx.addBundleListener(bundleListener);
moduleChangeListeners.put(listener, bundleListener);
}
public boolean removeModuleChangeListener(ModuleChangeListener listener) {
BundleListener bundleListener = moduleChangeListeners.remove(listener);
if (bundleListener!= null) {
bctx.removeBundleListener(bundleListener);
return true;
}
return false;
}
@Override
public void register(final ModuleLifecycleListener listener) {
// This is purposefully made an asynchronous bundle listener
BundleListener bundleListener = new BundleListener() {
public void bundleChanged(BundleEvent event) {
switch (event.getType()) {
case BundleEvent.INSTALLED:
listener.moduleInstalled(getModule(event.getBundle()));
break;
case BundleEvent.UPDATED:
listener.moduleUpdated(getModule(event.getBundle()));
break;
case BundleEvent.RESOLVED:
listener.moduleResolved(getModule(event.getBundle()));
break;
case BundleEvent.STARTED:
listener.moduleStarted(getModule(event.getBundle()));
break;
case BundleEvent.STOPPED:
listener.moduleStopped(getModule(event.getBundle()));
break;
}
}
};
bctx.addBundleListener(bundleListener);
moduleLifecycleListeners.put(listener, bundleListener);
}
@Override
public void unregister(ModuleLifecycleListener listener) {
BundleListener bundleListener = moduleLifecycleListeners.remove(listener);
if (bundleListener!=null) {
bctx.removeBundleListener(bundleListener);
}
}
/*package*/ HK2Module getModule(Bundle bundle) {
return modules.get(new OSGiModuleId(bundle));
}
@Override
public void remove(HK2Module module) {
super.remove(module);
if (!(module instanceof OSGiModuleImpl)) {
return;
}
OSGiModuleImpl oModule = (OSGiModuleImpl) module;
Bundle bundle = oModule.getBundle();
String bsn = bundle.getSymbolicName();
String version = bundle.getVersion().toString();
Set<ServiceLocator> locators = getAllServiceLocators();
for (ServiceLocator locator : locators) {
if (!ServiceLocatorState.RUNNING.equals(locator.getState())) continue;
ServiceLocatorUtilities.removeFilter(locator, new RemoveFilter(bsn, version));
}
}
private static class RemoveFilter implements Filter {
private final String bsn;
private final String version;
private RemoveFilter(String bsn, String version) {
this.bsn = bsn;
this.version = version;
}
private static String getMetadataValue(Descriptor d, String key) {
Map<String, List<String>> metadata = d.getMetadata();
List<String> values = metadata.get(key);
if (values == null || values.size() <= 0) {
return null;
}
return values.get(0);
}
@Override
public boolean matches(Descriptor d) {
String dBSN = getMetadataValue(d, OsgiPopulatorPostProcessor.BUNDLE_SYMBOLIC_NAME);
if (dBSN == null || !dBSN.equals(bsn)) return false;
String dVersion = getMetadataValue(d, OsgiPopulatorPostProcessor.BUNDLE_VERSION);
if (dVersion == null) return false;
return dVersion.equals(version);
}
}
}