PluginManager.java

  1. /*  This file is part of Openrouteservice.
  2.  *
  3.  *  Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
  4.  *  GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
  5.  *  of the License, or (at your option) any later version.

  6.  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  7.  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8.  *  See the GNU Lesser General Public License for more details.

  9.  *  You should have received a copy of the GNU Lesser General Public License along with this library;
  10.  *  if not, see <https://www.gnu.org/licenses/>.
  11.  */
  12. package org.heigit.ors.plugins;

  13. import org.apache.log4j.Logger;

  14. import java.lang.reflect.InvocationTargetException;
  15. import java.util.*;

  16. public class PluginManager<T extends Plugin> {
  17.     private static final Logger LOGGER = Logger.getLogger(PluginManager.class.getName());

  18.     private final ServiceLoader<T> loader;
  19.     private final Object lockObj;
  20.     private static final Map<String, Object> pluginMgrCache = new HashMap<>();

  21.     @SuppressWarnings("unchecked")
  22.     public static synchronized <T extends Plugin> PluginManager<T> getPluginManager(Class<?> cls) throws Exception {
  23.         PluginManager<T> pmgr = null;
  24.         pmgr = (PluginManager<T>) pluginMgrCache.get(cls.getName());
  25.         if (pmgr == null) {
  26.             pmgr = new PluginManager<>(cls);
  27.             pluginMgrCache.put(cls.getName(), pmgr);
  28.         }
  29.         return pmgr;
  30.     }

  31.     @SuppressWarnings("unchecked")
  32.     public PluginManager(Class<?> cls) throws Exception {
  33.         if (cls.equals(getClass()))
  34.             throw new Exception("Wrong class parameter");
  35.         loader = (ServiceLoader<T>) ServiceLoader.load(cls);
  36.         lockObj = new Object();
  37.     }

  38.     public List<T> createInstances(Map<String, Map<String, String>> parameters) {
  39.         List<T> result = new ArrayList<>(parameters.size());
  40.         if (!parameters.isEmpty()) {
  41.             for (Map.Entry<String, Map<String, String>> storageEntry : parameters.entrySet()) {
  42.                 T instance = createInstance(storageEntry.getKey(), storageEntry.getValue());

  43.                 if (instance != null) {
  44.                     result.add(instance);
  45.                 } else
  46.                     LOGGER.warn("'%s' was not found.".formatted(storageEntry.getKey()));
  47.             }
  48.         }
  49.         return result;
  50.     }

  51.     @SuppressWarnings("unchecked")
  52.     public T createInstance(String name, Map<String, String> params) {
  53.         T instance = null;
  54.         try {
  55.             // ServiceLoader is not threadsafe
  56.             synchronized (lockObj) {
  57.                 Iterator<T> entries = loader.iterator();
  58.                 while (entries.hasNext()) {
  59.                     T entry = entries.next();
  60.                     if (entry.getName().equalsIgnoreCase(name)) {
  61.                         instance = ((Class<T>) entry.getClass()).getDeclaredConstructor().newInstance();
  62.                         instance.setParameters(params);
  63.                         break;
  64.                     }
  65.                 }
  66.             }
  67.         } catch (ServiceConfigurationError | InstantiationException | IllegalAccessException | NoSuchMethodException |
  68.                  InvocationTargetException se) {
  69.             instance = null;
  70.             LOGGER.error(se);
  71.         }
  72.         return instance;
  73.     }
  74. }