|
1 | 1 | /* |
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. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials are made available under the |
5 | 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
|
56 | 56 | import org.glassfish.jersey.internal.LocalizationMessages; |
57 | 57 | import org.glassfish.jersey.internal.OsgiRegistry; |
58 | 58 | 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; |
60 | 62 | import org.osgi.framework.Bundle; |
61 | 63 | import org.osgi.framework.FrameworkUtil; |
62 | 64 |
|
@@ -1469,22 +1471,25 @@ private static boolean checkTypeBounds(final Class type, final Type[] bounds) { |
1469 | 1471 |
|
1470 | 1472 | private static final Class<?> bundleReferenceClass = AccessController.doPrivileged( |
1471 | 1473 | 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>) () -> { |
1479 | 1475 | try { |
1480 | 1476 | if (bundleReferenceClass != null) { |
1481 | 1477 | return OsgiRegistry.getInstance(); |
1482 | 1478 | } |
1483 | | - } catch (final Exception e) { |
| 1479 | + } catch (final Throwable e) { |
1484 | 1480 | // Do nothing - instance is null. |
1485 | 1481 | } |
1486 | 1482 |
|
1487 | 1483 | 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(); |
1488 | 1493 | } |
1489 | 1494 |
|
1490 | 1495 | /** |
|
0 commit comments