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

org.infinispan.util.ModuleProperties Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.util;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;

import org.infinispan.commands.ReplicableCommand;
import org.infinispan.commands.module.ModuleCommandExtensions;
import org.infinispan.commands.module.ModuleCommandFactory;
import org.infinispan.commands.module.ModuleCommandInitializer;
import org.infinispan.commands.remote.CacheRpcCommand;
import org.infinispan.commons.util.ServiceFinder;
import org.infinispan.factories.components.ModuleMetadataFileFinder;
import org.infinispan.lifecycle.ModuleLifecycle;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;

/**
 * The ModuleProperties class represents Infinispan's module service extensions
 *
 * @author Vladimir Blagojevic
 * @author Sanne Grinovero
 * @author Galder Zamarreño
 * @since 4.0
 */
public class ModuleProperties extends Properties {

   private static final long serialVersionUID = 2558131508076199744L;

   private static final Log log = LogFactory.getLog(ModuleProperties.class);

   private Map commandFactories;
   private Map commandInitializers;
   private Collection> moduleCommands;

   public static Collection resolveModuleLifecycles(ClassLoader cl) {
      return ServiceFinder.load(ModuleLifecycle.class, cl);
   }

   /**
    * Retrieves an Iterable containing metadata file finders declared by each module.
    * @param cl class loader to use
    * @return an Iterable of ModuleMetadataFileFinders
    */
   public static Iterable getModuleMetadataFiles(ClassLoader cl) {
      return ServiceFinder.load(ModuleMetadataFileFinder.class, cl);
   }

   @SuppressWarnings("unchecked")
   public void loadModuleCommandHandlers(ClassLoader cl) {
      Collection moduleCmdExtLoader =
            ServiceFinder.load(ModuleCommandExtensions.class, cl);

      if (moduleCmdExtLoader.iterator().hasNext()) {
         commandFactories = new HashMap(1);
         commandInitializers = new HashMap(1);
         moduleCommands = new HashSet>(1);
         for (ModuleCommandExtensions extension : moduleCmdExtLoader) {
            log.debugf("Loading module command extension SPI class: %s", extension);
            ModuleCommandFactory cmdFactory = extension.getModuleCommandFactory();
            ModuleCommandInitializer cmdInitializer = extension.getModuleCommandInitializer();
            for (Map.Entry> command :
                  cmdFactory.getModuleCommands().entrySet()) {
               byte id = command.getKey();
               if (commandFactories.containsKey(id))
                  throw new IllegalArgumentException(String.format(
                        "Cannot use id %d for commands, as it is already in use by %s",
                        id, commandFactories.get(id).getClass().getName()));

               commandFactories.put(id, cmdFactory);
               moduleCommands.add(command.getValue());
               commandInitializers.put(id, cmdInitializer);
            }
         }
      } else {
         log.debugf("No module command extensions to load");
         commandInitializers = Collections.emptyMap();
         commandFactories = Collections.emptyMap();
      }
   }

   public Collection> moduleCommands() {
      return moduleCommands;
   }

   public Map moduleCommandFactories() {
      return commandFactories;
   }

   public Map moduleCommandInitializers() {
      return commandInitializers;
   }

   @SuppressWarnings("unchecked")
   public Collection> moduleCacheRpcCommands() {
      Collection> cmds = moduleCommands();
      if (cmds == null || cmds.isEmpty())
         return Collections.emptySet();

      Collection> cacheRpcCmds = new HashSet>(2);
      for (Class moduleCmdClass : cmds) {
         if (CacheRpcCommand.class.isAssignableFrom(moduleCmdClass))
            cacheRpcCmds.add((Class) moduleCmdClass);
      }

      return cacheRpcCmds;
   }

   public Collection> moduleOnlyReplicableCommands() {
      Collection> cmds = moduleCommands();
      if (cmds == null || cmds.isEmpty())
         return Collections.emptySet();

      Collection> replicableOnlyCmds =
            new HashSet>(2);
      for (Class moduleCmdClass : cmds) {
         if (!CacheRpcCommand.class.isAssignableFrom(moduleCmdClass)) {
            replicableOnlyCmds.add(moduleCmdClass);
         }
      }
      return replicableOnlyCmds;
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy