| /* |
| * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved. |
| * |
| * 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 com.sun.enterprise.iiop.security; |
| |
| import java.lang.ref.WeakReference; |
| import org.glassfish.enterprise.iiop.api.GlassFishORBHelper; |
| import org.glassfish.gms.bootstrap.GMSAdapterService; |
| import org.glassfish.hk2.api.ServiceLocator; |
| import org.glassfish.internal.api.Globals; |
| |
| import jakarta.inject.Inject; |
| import jakarta.inject.Provider; |
| |
| /** |
| * This class is a local utility class to provide for hk2 lookups during runtime. |
| * |
| * @author Sudarsan Sridhar |
| */ |
| public class Lookups { |
| |
| @Inject |
| private Provider<SecurityMechanismSelector> securityMechanismSelectorProvider; |
| |
| @Inject |
| private Provider<GlassFishORBHelper> glassFishORBHelperProvider; |
| |
| @Inject |
| private Provider<SecurityContextUtil> securityContextUtilProvider; |
| |
| @Inject |
| private Provider<GMSAdapterService> gmsAdapterServiceProvider; |
| |
| /** |
| * Static singleton {@link Habitat} instance. |
| */ |
| private static final ServiceLocator habitat = Globals.getDefaultHabitat(); |
| |
| /** |
| * Static singleton {@link Lookups} instance. Note that this is assigned lazily and may remain null if the |
| * {@link Habitat} can not be obtained. |
| */ |
| private static Lookups singleton; |
| |
| private static WeakReference<SecurityMechanismSelector> sms = new WeakReference<SecurityMechanismSelector>(null); |
| private static WeakReference<GlassFishORBHelper> orb = new WeakReference<GlassFishORBHelper>(null); |
| private static WeakReference<SecurityContextUtil> sc = new WeakReference<SecurityContextUtil>(null); |
| |
| private Lookups() { |
| } |
| |
| /** |
| * Check to see if the singleton {@link Lookups} reference has been assigned. If null, then attempt to obtain and assign |
| * the singleton {@link Lookups} instance. |
| * |
| * @return true if the singleton instance has been successfully assigned; false otherwise |
| */ |
| private static synchronized boolean checkSingleton() { |
| if (singleton == null && habitat != null) { |
| // Obtaining the singleton through the habitat will cause the injections to occur. |
| singleton = habitat.create(Lookups.class); |
| habitat.inject(singleton); |
| habitat.postConstruct(singleton); |
| } |
| return singleton != null; |
| } |
| |
| /** |
| * Get the {@link SecurityMechanismSelector}. |
| * |
| * @return the {@link SecurityMechanismSelector}; null if not available |
| */ |
| static SecurityMechanismSelector getSecurityMechanismSelector() { |
| if (sms.get() != null) { |
| return sms.get(); |
| } |
| return _getSecurityMechanismSelector(); |
| } |
| |
| private static synchronized SecurityMechanismSelector _getSecurityMechanismSelector() { |
| if (sms.get() == null && checkSingleton()) { |
| sms = new WeakReference<SecurityMechanismSelector>(singleton.securityMechanismSelectorProvider.get()); |
| } |
| return sms.get(); |
| } |
| |
| /** |
| * Get the {@link GlassFishORBHelper}. |
| * |
| * @return the {@link GlassFishORBHelper}; null if not available |
| */ |
| static GlassFishORBHelper getGlassFishORBHelper() { |
| if (orb.get() != null) { |
| return orb.get(); |
| } |
| return _getGlassFishORBHelper(); |
| } |
| |
| private static synchronized GlassFishORBHelper _getGlassFishORBHelper() { |
| if (orb.get() == null && checkSingleton()) { |
| orb = new WeakReference<GlassFishORBHelper>(singleton.glassFishORBHelperProvider.get()); |
| } |
| return orb.get(); |
| } |
| |
| /** |
| * Get the {@link SecurityContextUtil}. |
| * |
| * @return the {@link SecurityContextUtil}; null if not available |
| */ |
| static SecurityContextUtil getSecurityContextUtil() { |
| if (sc.get() != null) { |
| return sc.get(); |
| } |
| return _getSecurityContextUtil(); |
| } |
| |
| private static synchronized SecurityContextUtil _getSecurityContextUtil() { |
| if (sc.get() == null && checkSingleton()) { |
| sc = new WeakReference<SecurityContextUtil>(singleton.securityContextUtilProvider.get()); |
| } |
| return sc.get(); |
| } |
| |
| /** |
| * Get the {@link GMSAdapterService}. |
| * |
| * @return the {@link GMSAdapterService}; null if not available |
| */ |
| static GMSAdapterService getGMSAdapterService() { |
| return checkSingleton() ? singleton.gmsAdapterServiceProvider.get() : null; |
| } |
| } |