PluginLoader.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.monitor.engine.dynamic;

  21. import org.openspcoop2.core.commons.CoreException;
  22. import org.openspcoop2.core.plugins.constants.TipoPlugin;
  23. import org.openspcoop2.utils.resources.Loader;
  24. import org.slf4j.Logger;

  25. /**
  26.  * PluginLoader
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class PluginLoader implements IPluginLoader {

  33.     public static PluginLoader getInstance() {
  34.         return (PluginLoader) CorePluginLoader.getInstance();
  35.     }
  36.    
  37.     private PluginManager pluginManager = null;
  38.     private Loader loader = null;
  39.     private Logger log;
  40.    
  41.     public PluginLoader() {
  42.         // nop
  43.     }
  44.    
  45.     @Override
  46.     public void init(Loader loader, Logger log) {
  47.         this.loader = loader;
  48.         this.log = log;
  49.     }
  50.     @Override
  51.     public void init(Loader loader, Logger log, IRegistroPluginsReader registroPluginsReader, int expireSeconds) {
  52.         init(loader, log);
  53.         this.pluginManager = new PluginManager(registroPluginsReader, expireSeconds);
  54.     }
  55.    
  56.     @Override
  57.     public void close(Logger log) {
  58.         if(this.pluginManager!=null) {
  59.             try {
  60.                 this.pluginManager.close();
  61.             }catch(Throwable t) {
  62.                 if(log!=null) {
  63.                     log.error("PluginManager close failure: "+t.getMessage(),t);
  64.                 }
  65.             }
  66.         }
  67.     }
  68.    
  69.     public boolean isPluginManagerEnabled() {
  70.         return this.pluginManager!=null;
  71.     }
  72.     public void updateFromConsoleConfig(Logger log) throws Exception {
  73.         this.pluginManager.updateFromConsoleConfig(log);
  74.     }
  75.    
  76.    
  77.     // UTILITY
  78.    
  79.     @Override
  80.     public Class<?> getDynamicClass(String className, TipoPlugin tipoPlugin, String tipo) throws CoreException {
  81.         return getDynamicClass(className, tipoPlugin.getValue(), tipo);
  82.     }
  83.     @Override
  84.     public Class<?> getDynamicClass(String className, String tipoPlugin, String tipo) throws CoreException {
  85.         if(className==null) {
  86.             throw new CoreException("Class not found ("+getObjectName(tipoPlugin)+" type '"+tipo+"')");
  87.         }
  88.         Class<?> c = null;
  89.         if(this.isPluginManagerEnabled()) {
  90.             if(this.pluginManager==null) {
  91.                 throw new CoreException("Plugin manager not initialized");
  92.             }
  93.             try {
  94.                 c = this.pluginManager.findClass(this.log, tipoPlugin, className);
  95.             }catch(Exception e) {
  96.                 throw new CoreException("Class '"+className+"' not found in registry ("+getObjectName(tipoPlugin)+" type '"+tipo+"'): "+e.getMessage(),e);
  97.             }
  98.         }
  99.         else {
  100.             try {
  101.                 c = Class.forName(className);
  102.             }catch(Exception e) {
  103.                 throw new CoreException("Class '"+className+"' not found in classpath: "+e.getMessage(),e);
  104.             }  
  105.         }
  106.         if(c==null) {
  107.             throw new CoreException("Class '"+className+"' not found in registry ("+getObjectName(tipoPlugin)+" type '"+tipo+"')");
  108.         }
  109.         return c;
  110.     }
  111.    
  112.     @Override
  113.     public <T> T newInstance(Class<T> c, TipoPlugin tipoPlugin, String tipo) throws CoreException{
  114.         return newInstance(c, tipoPlugin.getValue(), tipo);
  115.     }
  116.     @Override
  117.     @SuppressWarnings("unchecked")
  118.     public <T> T newInstance(Class<T> c, String tipoPlugin, String tipo) throws CoreException{
  119.         try {
  120.             return (T) this.loader.newInstance(c);
  121.         }catch(Throwable e) {
  122.             throw new CoreException("Class '"+c.getClass().getName()+"' new instance error ("+getObjectName(tipoPlugin)+" type '"+tipo+"'): "+e.getMessage(),e);
  123.         }
  124.     }
  125.    
  126.     @Override
  127.     public Object newInstance(String customTipoClasse, String tipo) throws Exception {
  128.         Class<?> c = getDynamicClass(null, customTipoClasse, tipo);
  129.         return newInstance(c, customTipoClasse, tipo);
  130.     }
  131.    
  132.     protected String getObjectName(String tipoPluginParam) throws CoreException {
  133.        
  134.         TipoPlugin tipoPlugin = TipoPlugin.toEnumConstant(tipoPluginParam);
  135.        
  136.         if(tipoPlugin==null) {
  137.             return tipoPluginParam;
  138.         }
  139.        
  140.         switch (tipoPlugin) {
  141.         case CONNETTORE:
  142.             return "connector";
  143.         case AUTENTICAZIONE:
  144.             return "authentication";
  145.         case AUTORIZZAZIONE:
  146.             return "authorization";
  147.         case AUTORIZZAZIONE_CONTENUTI:
  148.             return "authorization content";
  149.         case INTEGRAZIONE:
  150.             return "integration";
  151.         case SERVICE_HANDLER:
  152.             return "service handler";
  153.         case MESSAGE_HANDLER:
  154.             return "message handler";
  155.         case BEHAVIOUR:
  156.             return "behaviour";
  157.         case RATE_LIMITING:
  158.             return "policy rate limiting";
  159.         case ALLARME:
  160.             return "alarm";
  161.         case TRANSAZIONE:
  162.             return "transaction library";
  163.         case RICERCA:
  164.             return "search library";
  165.         case STATISTICA:
  166.             return "stats library";
  167.         case TOKEN_DYNAMIC_DISCOVERY:
  168.             return "token dynamic discovery";
  169.         case TOKEN_VALIDAZIONE:
  170.             return "token validation policy";
  171.         case TOKEN_NEGOZIAZIONE:
  172.             return "token retrieve policy";
  173.         case ATTRIBUTE_AUTHORITY:
  174.             return "attribute authority";
  175.         }
  176.         throw new CoreException("?? Type '"+tipoPlugin+"' unsupported ??");
  177.     }
  178.    
  179. }