Skip to content

Commit 1fe6b75

Browse files
jansupolsenivam
authored andcommittedJan 6, 2022
Cache a lazy reference to OSGi registry instance
Signed-off-by: jansupol <jan.supol@oracle.com>
1 parent 21115d4 commit 1fe6b75

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed
 

‎core-common/src/main/java/org/glassfish/jersey/internal/util/ReflectionHelper.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -56,7 +56,9 @@
5656
import org.glassfish.jersey.internal.LocalizationMessages;
5757
import org.glassfish.jersey.internal.OsgiRegistry;
5858
import org.glassfish.jersey.internal.util.collection.ClassTypePair;
59-
59+
import org.glassfish.jersey.internal.util.collection.LazyValue;
60+
import org.glassfish.jersey.internal.util.collection.Value;
61+
import org.glassfish.jersey.internal.util.collection.Values;
6062
import org.osgi.framework.Bundle;
6163
import org.osgi.framework.FrameworkUtil;
6264

@@ -1469,22 +1471,25 @@ private static boolean checkTypeBounds(final Class type, final Type[] bounds) {
14691471

14701472
private static final Class<?> bundleReferenceClass = AccessController.doPrivileged(
14711473
classForNamePA("org.osgi.framework.BundleReference", null));
1472-
1473-
/**
1474-
* Returns an {@link OsgiRegistry} instance.
1475-
*
1476-
* @return an {@link OsgiRegistry} instance or {@code null} if the class cannot be instantiated (not in OSGi environment).
1477-
*/
1478-
public static OsgiRegistry getOsgiRegistryInstance() {
1474+
private static final LazyValue<Object> osgiInstance = Values.lazy((Value<Object>) () -> {
14791475
try {
14801476
if (bundleReferenceClass != null) {
14811477
return OsgiRegistry.getInstance();
14821478
}
1483-
} catch (final Exception e) {
1479+
} catch (final Throwable e) {
14841480
// Do nothing - instance is null.
14851481
}
14861482

14871483
return null;
1484+
});
1485+
1486+
/**
1487+
* Returns an {@link OsgiRegistry} instance.
1488+
*
1489+
* @return an {@link OsgiRegistry} instance or {@code null} if the class cannot be instantiated (not in OSGi environment).
1490+
*/
1491+
public static OsgiRegistry getOsgiRegistryInstance() {
1492+
return (OsgiRegistry) osgiInstance.get();
14881493
}
14891494

14901495
/**

0 commit comments

Comments
 (0)