BasicLoader.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 java.util.List;

  22. import org.openspcoop2.monitor.sdk.condition.Context;
  23. import org.openspcoop2.monitor.sdk.constants.StatisticType;
  24. import org.openspcoop2.monitor.sdk.exceptions.SearchException;
  25. import org.openspcoop2.monitor.sdk.parameters.Parameter;
  26. import org.openspcoop2.monitor.sdk.plugins.ISearchArguments;
  27. import org.openspcoop2.monitor.sdk.plugins.ISearchProcessing;
  28. import org.openspcoop2.monitor.sdk.plugins.IStatisticProcessing;
  29. import org.openspcoop2.utils.LoggerWrapperFactory;
  30. import org.openspcoop2.utils.Utilities;
  31. import org.slf4j.Logger;

  32. /**
  33.  * BasicLoader
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class BasicLoader implements IDynamicLoader{

  40.     private String tipoPlugin;
  41.     private String tipo;
  42.     private String className;
  43.     private Class<?> c;
  44.     private Logger log;

  45.     protected BasicLoader(String tipoPlugin, String tipo, String className, Class<?> c, Logger log) {
  46.         this.tipoPlugin = tipoPlugin;
  47.         this.tipo = tipo;
  48.         this.className = className;
  49.         this.c = c;
  50.         if(log==null) {
  51.             this.log = LoggerWrapperFactory.getLogger(BasicLoader.class);
  52.         }
  53.         else {
  54.             this.log = log;
  55.         }
  56.     }

  57.     @Override
  58.     public String getTipoPlugin() {
  59.         return this.tipoPlugin;
  60.     }

  61.     @Override
  62.     public String getTipo() {
  63.         return this.tipo;
  64.     }
  65.    
  66.     @Override
  67.     public String getClassName() throws SearchException{
  68.         if(this.c!=null){
  69.             return this.c.getName();
  70.         }
  71.         throw new SearchException("Class undefined");
  72.     }
  73.    
  74.     @Override
  75.     public String getClassSimpleName() throws SearchException{
  76.         if(this.c!=null){
  77.             return this.c.getSimpleName();
  78.         }
  79.         throw new SearchException("Class undefined");
  80.     }
  81.    
  82.     @Override
  83.     public Object newInstance() throws SearchException{
  84.         try{
  85.             return Utilities.newInstance(this.c);
  86.         }catch (Exception e) {
  87.             throw new SearchException(e.getMessage(),e);
  88.         }
  89.     }
  90.    
  91.     @Override
  92.     public List<Parameter<?>> getParameters(Context context) throws SearchException{
  93.         try{
  94.            
  95.             Object obj = Utilities.newInstance(this.c);

  96.             if(obj instanceof ISearchProcessing){              
  97.                 return ((ISearchProcessing) obj).getParameters(context);
  98.             }else if(obj instanceof IStatisticProcessing){
  99.                 return ((IStatisticProcessing) obj).getParameters(context);
  100.             }else if(obj instanceof ISearchArguments){
  101.                 return ((ISearchArguments) obj).getParameters(context);
  102.             }else{
  103.                 String iface = ISearchArguments.class.getName();
  104.                 throw new SearchException("La classe ["+this.className+"] non implementa l'interfaccia ["+iface+"]");
  105.             }
  106.            
  107.         }
  108. //      catch(ClassNotFoundException cnfe){
  109. //          BasicLoader.log.error("Impossibile caricare il plugin. La classe indicata ["+this.className+"] non esiste.",cnfe);
  110. //          throw new SearchException("Impossibile caricare il plugin. La classe indicata ["+this.className+"] non esiste.");
  111. //      }
  112.         catch (SearchException re) {
  113.             throw re;
  114.         }catch (Exception e) {
  115.             this.log.error("Impossibile ottenere informazioni per il rendereng dal Loader: "+e.getMessage(),e);
  116.             throw new SearchException(e.getMessage(),e);
  117.         }
  118.     }
  119.    
  120.     @Override
  121.     public void updateRendering(Parameter<?> parameter, Context context) throws SearchException{
  122.         try{
  123.             Object obj = Utilities.newInstance(this.c);

  124.             if(obj instanceof ISearchProcessing){              
  125.                 ((ISearchProcessing) obj).updateRendering(parameter, context);
  126.             }else if(obj instanceof IStatisticProcessing){
  127.                 ((IStatisticProcessing) obj).updateRendering(parameter, context);
  128.             }else if(obj instanceof ISearchArguments){
  129.                 ((ISearchArguments) obj).updateRendering(parameter, context);
  130.             }else{
  131.                 String iface = ISearchArguments.class.getName();
  132.                 throw new SearchException("La classe ["+this.className+"] non implementa l'interfaccia ["+iface+"]");
  133.             }
  134.         }
  135. //      catch(ClassNotFoundException cnfe){
  136. //          BasicLoader.log.error("Impossibile caricare il plugin. La classe indicata ["+this.className+"] non esiste.",cnfe);
  137. //          throw new SearchException("Impossibile caricare il plugin. La classe indicata ["+this.className+"] non esiste.");
  138. //      }
  139.         catch (SearchException re) {
  140.             throw re;
  141.         }catch (Exception e) {
  142.             this.log.error("Impossibile ottenere informazioni per il rendereng dal Loader: "+e.getMessage(),e);
  143.             throw new SearchException(e.getMessage(),e);
  144.         }
  145.     }

  146.     @Override
  147.     public void valueSelectedListener(Parameter<?> parameter, Context context) {
  148.         try{
  149.             Object obj = Utilities.newInstance(this.c);

  150.             if(obj instanceof ISearchProcessing){
  151.                 ((ISearchProcessing) obj).onChangeValue(parameter, context);
  152.             }else if(obj instanceof IStatisticProcessing){
  153.                 ((IStatisticProcessing) obj).onChangeValue(parameter, context);
  154.             }else if(obj instanceof ISearchArguments){
  155.                 ((ISearchArguments) obj).onChangeValue(parameter, context);
  156.             }else{
  157.                 String iface = ISearchArguments.class.getName();
  158.                 throw new SearchException("La classe ["+this.className+"] non implementa l'interfaccia ["+iface+"]");
  159.             }
  160.         }
  161. //      catch(ClassNotFoundException cnfe){
  162. //          BasicLoader.log.error("Impossibile caricare il plugin. La classe indicata ["+this.className+"] non esiste.",cnfe);
  163. //          throw new RuntimeException("Impossibile caricare il plugin. La classe indicata ["+this.className+"] non esiste.");
  164. //      }
  165.         catch (Exception e) {
  166.             this.log.error(e.getMessage(),e);
  167.         }

  168.     }

  169.     @Override
  170.     public List<StatisticType> getEnabledStatisticType(Context context)
  171.             throws SearchException {
  172.         try{
  173.             Object obj = Utilities.newInstance(this.c);

  174.             if(obj instanceof IStatisticProcessing){
  175.                 return ((IStatisticProcessing) obj).getEnabledStatisticType();
  176.             }else{
  177.                 String iface = ISearchArguments.class.getName();
  178.                 throw new SearchException("La classe ["+this.className+"] non implementa l'interfaccia ["+iface+"]");
  179.             }
  180.         }
  181. //      catch(ClassNotFoundException cnfe){
  182. //          BasicLoader.log.error("Impossibile caricare il plugin. La classe indicata ["+this.className+"] non esiste.",cnfe);
  183. //          throw new SearchException("Impossibile caricare il plugin. La classe indicata ["+this.className+"] non esiste.");
  184. //      }
  185.         catch (SearchException re) {
  186.             throw re;
  187.         }catch (Exception e) {
  188.             this.log.error(e.getMessage(),e);
  189.             throw new SearchException(e.getMessage(),e);
  190.         }

  191.     }

  192. }