Github user pivotal-amurmann commented on a diff in the pull request: https://github.com/apache/geode/pull/707#discussion_r133246611 --- Diff: geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/ServerConnectionFactory.java --- @@ -22,59 +22,89 @@ import java.io.IOException; import java.net.Socket; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import java.util.ServiceLoader; -import javax.management.ServiceNotFoundException; /** * Creates instances of ServerConnection based on the connection mode provided. */ public class ServerConnectionFactory { - private static ClientProtocolMessageHandler protobufProtocolHandler; - private static final Object protocolLoadLock = new Object(); + private ClientProtocolMessageHandler protobufProtocolHandler; + private Map<String, Class<? extends StreamAuthenticator>> authenticators = null; - private static ClientProtocolMessageHandler findClientProtocolMessageHandler() { + public ServerConnectionFactory() {} + + private synchronized void initializeAuthenticatorsMap() { + if (authenticators != null) { + return; + } + authenticators = new HashMap<>(); + ServiceLoader<StreamAuthenticator> loader = ServiceLoader.load(StreamAuthenticator.class); + for (StreamAuthenticator streamAuthenticator : loader) { + authenticators.put(streamAuthenticator.implementationID(), streamAuthenticator.getClass()); + } + } + + private synchronized ClientProtocolMessageHandler initializeMessageHandler() { if (protobufProtocolHandler != null) { return protobufProtocolHandler; } + ServiceLoader<ClientProtocolMessageHandler> loader = + ServiceLoader.load(ClientProtocolMessageHandler.class); + Iterator<ClientProtocolMessageHandler> iterator = loader.iterator(); - synchronized (protocolLoadLock) { - if (protobufProtocolHandler != null) { - return protobufProtocolHandler; - } - - ServiceLoader<ClientProtocolMessageHandler> loader = - ServiceLoader.load(ClientProtocolMessageHandler.class); - Iterator<ClientProtocolMessageHandler> iterator = loader.iterator(); - - if (!iterator.hasNext()) { - throw new ServiceLoadingFailureException( - "ClientProtocolMessageHandler implementation not found in JVM"); - } + if (!iterator.hasNext()) { + throw new ServiceLoadingFailureException( + "There is no ClientProtocolMessageHandler implementation found in JVM"); + } - ClientProtocolMessageHandler returnValue = iterator.next(); + protobufProtocolHandler = iterator.next(); + return protobufProtocolHandler; + } - if (iterator.hasNext()) { + private StreamAuthenticator findStreamAuthenticator(String implementationID) { + if (authenticators == null) { + initializeAuthenticatorsMap(); + } + Class<? extends StreamAuthenticator> streamAuthenticatorClass = + authenticators.get(implementationID); + if (streamAuthenticatorClass == null) { + throw new ServiceLoadingFailureException( + "Could not find implementation for StreamAuthenticator with implementation ID " + + implementationID); + } else { + try { + return streamAuthenticatorClass.newInstance(); + } catch (InstantiationException | IllegalAccessException e) { throw new ServiceLoadingFailureException( - "Multiple service implementations found for ClientProtocolMessageHandler"); --- End diff -- Where are we now handling the case of having multiple implementations?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---