All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.server.context.ServerInitialContextFactoryBuilder Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev05
Show newest version
package org.infinispan.server.context;

import java.io.Closeable;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
import javax.naming.spi.InitialContextFactoryBuilder;

import org.infinispan.commons.util.ServiceFinder;
import org.infinispan.commons.util.Util;


/**
 * @author Tristan Tarrant <[email protected]>
 * @since 11.0
 **/
public class ServerInitialContextFactoryBuilder implements InitialContextFactoryBuilder, Closeable {

   private final ConcurrentMap namedObjects;
   private final Map initialContextFactories;

   public ServerInitialContextFactoryBuilder() {
      namedObjects = new ConcurrentHashMap<>();
      initialContextFactories = ServiceFinder.load(InitialContextFactory.class, Thread.currentThread().getContextClassLoader()).stream().collect(Collectors.toMap(className(), Function.identity()));
   }

   @Override
   public InitialContextFactory createInitialContextFactory(Hashtable environment) throws NamingException {
      String className = environment != null ? (String) environment.get(Context.INITIAL_CONTEXT_FACTORY) : null;
      if (className == null) {
         return new ServerInitialContextFactory(namedObjects);
      }
      if (initialContextFactories.containsKey(className)) {
         return initialContextFactories.get(className);
      } else {
         return InitialContextFactory.class.cast(Util.getInstance(className, Thread.currentThread().getContextClassLoader()));
      }
   }

   @Override
   public void close() {
      // Closes any AutoCloseable objects stored in the context
      namedObjects.values().stream().filter(v -> v instanceof AutoCloseable).forEach(o -> Util.close((AutoCloseable) o));
   }

   static  Function className() {
      return t -> t.getClass().getName();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy