-
Notifications
You must be signed in to change notification settings - Fork 53
/
OSGiModuleImpl.java
executable file
·599 lines (523 loc) · 20.2 KB
/
OSGiModuleImpl.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
* Copyright (c) 2007, 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 static org.jvnet.hk2.osgiadapter.Logger.logger;
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.DescriptorFileFinder;
import org.glassfish.hk2.api.PopulatorPostProcessor;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.bootstrap.HK2Populator;
import org.glassfish.hk2.bootstrap.impl.URLDescriptorFileFinder;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.service.packageadmin.RequiredBundle;
import com.sun.enterprise.module.LifecyclePolicy;
import com.sun.enterprise.module.HK2Module;
import com.sun.enterprise.module.ModuleChangeListener;
import com.sun.enterprise.module.ModuleDefinition;
import com.sun.enterprise.module.ModuleDependency;
import com.sun.enterprise.module.ModuleMetadata;
import com.sun.enterprise.module.ModuleState;
import com.sun.enterprise.module.ModulesRegistry;
import com.sun.enterprise.module.ResolveError;
import com.sun.enterprise.module.bootstrap.BootException;
/**
* @author Sanjeeb.Sahoo@Sun.COM
*/
public class OSGiModuleImpl implements HK2Module {
private final ReentrantLock lock = new ReentrantLock();
private volatile Bundle bundle; // made volatile as it is accessed from multiple threads
private ModuleDefinition md;
private AbstractOSGiModulesRegistryImpl registry;
private boolean isTransientlyActive = false;
List<ActiveDescriptor> activeDescriptors;
/* TODO (Sahoo): Change hk2-apt to generate an equivalent BundleActivator
corresponding to LifecyclerPolicy class. That way, LifecyclePolicy class
will be invoked even when underlying OSGi bundle is stopped or started
using any OSGi bundle management tool.
*/
private LifecyclePolicy lifecyclePolicy;
private static final Enumeration<URL> EMPTY_URLS = new Enumeration<URL>() {
@Override
public boolean hasMoreElements() {
return false;
}
@Override
public URL nextElement() {
throw new NoSuchElementException();
}
};
public OSGiModuleImpl(AbstractOSGiModulesRegistryImpl registry, Bundle bundle, ModuleDefinition md) {
this.registry = registry;
this.bundle = bundle;
this.md = md;
}
@Override
public ModuleDefinition getModuleDefinition() {
return md;
}
@Override
public String getName() {
return md.getName();
}
@Override
public ModulesRegistry getRegistry() {
return registry;
}
@Override
public ModuleState getState() {
// We don't cache the module state locally. Instead we always map
// the underlying bundle's state to HK2 state. This avoids us
// from having to register a listener with OSGi to be updated with
// bundle state transitions.
return mapBundleStateToModuleState(bundle);
}
/* package */ static ModuleState mapBundleStateToModuleState(Bundle bundle)
{
ModuleState state;
switch (bundle.getState())
{
case Bundle.INSTALLED:
case Bundle.UNINSTALLED:
state = ModuleState.NEW;
break;
case Bundle.RESOLVED:
case Bundle.STARTING:
case Bundle.STOPPING:
state = ModuleState.RESOLVED;
break;
case Bundle.ACTIVE:
state = ModuleState.READY;
break;
default:
throw new RuntimeException(
"Does not know how to handle bundle with state [" +
bundle.getState() + "]");
}
return state;
}
@Override
public void resolve() throws ResolveError {
// Since OSGi bundle does not have a separate resolve method,
// we use the same implementation as start();
lock.lock();
try {
start();
} finally {
lock.unlock();
}
}
@Override
public void start() throws ResolveError {
lock.lock();
try {
int state = bundle.getState();
if (((Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING) & state) != 0) {
if (logger.isLoggable(Level.FINER)) {
logger.logp(Level.FINER, "OSGiModuleImpl", "start",
"Ignoring start of bundle {0} as it is in {1} state",
new Object[]{bundle, toString(bundle.getState())} );
}
return;
}
if (registry.getPackageAdmin().getBundleType(bundle) == PackageAdmin.BUNDLE_TYPE_FRAGMENT) {
if (logger.isLoggable(Level.FINER)) {
logger.logp(Level.FINER, "OSGiModuleImpl", "start",
"Ignoring start of bundle {0} as it is a fragment bundle",
new Object[]{bundle} );
}
return;
}
try {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
AccessController.doPrivileged(new PrivilegedExceptionAction(){
public Object run() throws BundleException
{
startBundle();
return null;
}
});
} catch (PrivilegedActionException e) {
throw (BundleException)e.getException();
}
} else {
startBundle();
}
isTransientlyActive = true;
if (logger.isLoggable(Level.FINE)) {
logger.logp(Level.FINE, "OSGiModuleImpl",
"start", "Started bundle {0}", bundle);
}
} catch (BundleException e) {
throw new ResolveError("Failed to start "+this,e);
}
// TODO(Sahoo): Remove this when hk2-apt generates equivalent BundleActivator
// if there is a LifecyclePolicy, then instantiate and invoke.
if (md.getLifecyclePolicyClassName()!=null) {
try {
Class<LifecyclePolicy> lifecyclePolicyClass =
(Class<LifecyclePolicy>) bundle.loadClass(md.getLifecyclePolicyClassName());
lifecyclePolicy = lifecyclePolicyClass.newInstance();
} catch(ClassNotFoundException e) {
throw new ResolveError("ClassNotFound : " + e.getMessage(), e);
} catch(java.lang.InstantiationException | IllegalAccessException e) {
throw new ResolveError(e);
}
}
if (lifecyclePolicy!=null) {
lifecyclePolicy.start(this);
}
} finally {
lock.unlock();
}
}
private void startBundle() throws BundleException {
BundleException exception = null;
for(int attempt = 1; attempt <= 3; attempt++) {
try {
if (attempt > 1) {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "Retrying start of {0} due to lock race condition", bundle);
}
jitter();
}
bundle.start(Bundle.START_TRANSIENT);
return;
} catch (BundleException be) {
exception = be;
// When starting bundles in multiple threads, Apache Felix may run in locking race
// condition throwing "Unable to acquire global lock" on one of competing threads.
// Best course of action is to retry bundle start rather than fail unrecoverably.
if (!be.getMessage().contains("Unable to acquire global lock")) {
break;
}
}
}
throw exception;
}
private void jitter() {
try {
Thread.sleep(new Random().nextInt(20));
} catch (InterruptedException ie) {
// nevermind
}
}
private String toString(int state)
{
String value;
switch (state) {
case Bundle.STARTING:
value = "STARTING";
break;
case Bundle.STOPPING:
value = "STOPPING";
break;
case Bundle.INSTALLED:
value = "INSTALLED";
break;
case Bundle.UNINSTALLED:
value = "UNINSTALLED";
break;
case Bundle.RESOLVED:
value = "RESOLVED";
break;
case Bundle.ACTIVE:
value = "ACTIVE";
break;
default:
value = "UNKNOWN STATE [" + state + "]";
logger.warning("No mapping exist for bundle state " + state);
}
return value;
}
@Override
public boolean stop() {
lock.lock();
try {
detach();
// Don't refresh packages, as we are not uninstalling the bundle.
// registry.getPackageAdmin().refreshPackages(new Bundle[]{bundle});
return true;
} finally {
lock.unlock();
}
}
public void detach() {
if (bundle.getState() != Bundle.ACTIVE) {
if (logger.isLoggable(Level.FINER)) {
logger.logp(Level.FINER, "OSGiModuleImpl", "detach",
"Ignoring stop of bundle {0} as it is in {1} state",
new Object[]{bundle, toString(bundle.getState())} );
}
return;
}
if (lifecyclePolicy!=null) {
lifecyclePolicy.stop(this);
lifecyclePolicy=null;
}
try {
bundle.stop();
if (logger.isLoggable(Level.FINE))
{
logger.logp(Level.FINE, "OSGiModuleImpl", "detach", "Stopped bundle = {0}", new Object[]{bundle});
}
// bundle.uninstall();
} catch (BundleException e) {
throw new RuntimeException(e);
}
}
@Override
public void uninstall() {
// This method is called when the hk2-osgi-adapter module is stopped.
// During that time, we need to stop all the modules, hence no sticky check is
// performed in this method.
try {
bundle.uninstall();
} catch (BundleException e) {
throw new RuntimeException(e);
}
registry.remove(this);
this.registry = null;
}
@Override
public void refresh() {
URI location = md.getLocations()[0];
File f = new File(location);
if (f.lastModified() > bundle.getLastModified()) {
try {
bundle.update();
registry.getPackageAdmin().refreshPackages(new Bundle[]{bundle});
} catch (BundleException e) {
throw new RuntimeException(e);
}
}
}
@Override
public ModuleMetadata getMetadata() {
return md.getMetadata();
}
@Override
public <T> Iterable<Class<? extends T>> getProvidersClass(
Class<T> serviceClass) {
return (Iterable)getProvidersClass(serviceClass.getName());
}
@Override
public Iterable<Class> getProvidersClass(String name) {
List<Class> r = new ArrayList<>();
for( String provider : getMetadata().getEntry(name).providerNames) {
try {
r.add(getClassLoader().loadClass(provider));
} catch (ClassNotFoundException e) {
logger.log(Level.SEVERE, "Failed to load "+provider+" from "+getName(),e);
}
}
return r;
}
@Override
public boolean hasProvider(Class serviceClass) {
String name = serviceClass.getName();
return getMetadata().getEntry(name).hasProvider();
}
@Override
public void addListener(ModuleChangeListener listener) {
registry.addModuleChangeListener(listener, this);
}
@Override
public void removeListener(ModuleChangeListener listener) {
registry.removeModuleChangeListener(listener);
}
@Override
public void dumpState(PrintStream writer) {
writer.print(toString());
}
/**
* Parses all the inhabitants descriptors of the given name in this module.
* @return
*/
List<ActiveDescriptor> parseInhabitants(String name, ServiceLocator serviceLocator, List<PopulatorPostProcessor> populatorPostProcessors) throws IOException, BootException {
DescriptorFileFinder dff = null;
final String path = "META-INF/hk2-locator/" + name;
URL entry = bundle.getEntry(path);
if (entry != null) {
dff = new URLDescriptorFileFinder(entry);
}
if (dff != null) {
final OSGiModuleImpl module = this;
ArrayList<PopulatorPostProcessor> allPostProcessors = new ArrayList<>();
allPostProcessors.add(new OsgiPopulatorPostProcessor(module));
if (populatorPostProcessors != null) {
allPostProcessors.addAll(populatorPostProcessors);
}
this.activeDescriptors = HK2Populator.populate(serviceLocator, dff, allPostProcessors);
}
return this.activeDescriptors;
}
/**
* This method is used as the parent loader of the class loader that we return in {@link #getClassLoader}
*/
private ClassLoader getParentLoader() {
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return Bundle.class.getClassLoader();
}
});
}
@Override
public ClassLoader getClassLoader() {
/*
* This is a delegating class loader.
* It always delegates to OSGi's 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.
* It overrides loadClass(), getResource() and getResources() as opposed to
* their findXYZ() equivalents so that the OSGi export control mechanism
* is enforced even for classes and resources available in the system/boot
* class loader.
*/
return new ClassLoader(getParentLoader()) {
private final ReentrantLock lock = new ReentrantLock();
@Override
protected Class<?> loadClass(final String name, boolean resolve) throws ClassNotFoundException {
lock.lock();
try {
//doprivileged needed for running with SecurityManager
return AccessController.doPrivileged(new PrivilegedExceptionAction<Class>() {
public Class run() throws ClassNotFoundException {
Class c = bundle.loadClass(name);
return c;
}
});
} catch (PrivilegedActionException e) {
throw (ClassNotFoundException)e.getException();
} finally {
lock.unlock();
}
}
@Override
public URL getResource(String name) {
URL result = bundle.getResource(name);
if (result != null) return result;
return null;
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
Enumeration<URL> resources = bundle.getResources(name);
if (resources==null) {
// This check is needed, because ClassLoader.getResources()
// expects us to return an empty enumeration.
resources = EMPTY_URLS;
}
return resources;
}
@Override
public String toString() {
return "Class Loader for Bundle [" + bundle.toString() + " ]";
}
};
}
@Override
public void addImport(HK2Module module) {
throw new UnsupportedOperationException("This method can't be implemented in OSGi environment");
}
@Override
public HK2Module addImport(ModuleDependency dependency) {
throw new UnsupportedOperationException("This method can't be implemented in OSGi environment");
}
@Override
public boolean isSticky() {
return true; // all modules are always sticky
}
@Override
public void setSticky(boolean sticky) {
// NOOP: It's not required in OSGi.
}
@Override
public List<HK2Module> getImports() {
List<HK2Module> result = new ArrayList<>();
RequiredBundle[] requiredBundles =
registry.getPackageAdmin().getRequiredBundles(bundle.getSymbolicName());
if (requiredBundles!=null) {
for(RequiredBundle rb : requiredBundles) {
HK2Module m = registry.getModule(rb.getBundle());
if (m!=null) {
// module is known to the module system
result.add(m);
} else {
// module is not known to us - may be the OSgi bundle depends on a native
// OSGi bundle
}
}
}
return result;
}
@Override
public boolean isShared() {
return true; // all OSGi bundles are always shared.
}
public Bundle getBundle() {
return bundle;
}
public boolean isTransientlyActive() {
return isTransientlyActive;
}
@Override
public String toString() {
return "OSGiModuleImpl:: Bundle = [" + bundle
+ "], State = [" + getState() + "]";
}
@Override
public int hashCode() {
return bundle.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof OSGiModuleImpl) {
return bundle.equals(OSGiModuleImpl.class.cast(obj).bundle);
}
return false;
}
protected void setBundle(Bundle bundle) {
/*
* This method is purposefully not made synchronized as this can be called like this:
*
* thread #1: is calling this.init() and has held the lock, but is waiting for Obr to deploy.
* thread #2: is deploying some bundles using Obr and as part of that is trying to call setBundle on this module.
*/
if (this.bundle != null && this.bundle != bundle) {
throw new RuntimeException("setBundle called with bundle [" + bundle + "] where as module [" + this +
"] is already associated with bundle [" + this.bundle + "]");
} else {
this.bundle = bundle;
logger.logp(Level.INFO, "OSGiModuleImpl", "setBundle", "module [{0}] is now associated with bundle [{1}]",
new Object[]{this, bundle});
}
}
}