BehaviourLoader.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.pdd.core.behaviour;

  21. import org.openspcoop2.core.commons.CoreException;
  22. import org.openspcoop2.core.config.PortaApplicativaBehaviour;
  23. import org.openspcoop2.core.config.constants.TipoBehaviour;
  24. import org.openspcoop2.pdd.config.dynamic.PddPluginLoader;
  25. import org.openspcoop2.pdd.core.CostantiPdD;
  26. import org.openspcoop2.pdd.core.PdDContext;
  27. import org.openspcoop2.pdd.core.behaviour.built_in.load_balance.LoadBalancerBehaviour;
  28. import org.openspcoop2.pdd.core.behaviour.built_in.multi_deliver.MultiDeliverBehaviour;
  29. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  30. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  31. import org.openspcoop2.protocol.sdk.state.IState;

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

  40.     public static IBehaviour newInstance(PortaApplicativaBehaviour behaviour, MsgDiagnostico msgDiag,
  41.             PdDContext pddContext, IProtocolFactory<?> protocolFactory, IState state) throws CoreException{
  42.        
  43.         if(behaviour==null || behaviour.getNome()==null || "".equals(behaviour.getNome())) {
  44.             throw new CoreException("Behaviour undefined");
  45.         }
  46.        
  47.         IBehaviour behaviourImpl = null;
  48.         String tipoDiagBehaviour = null;
  49.         try {
  50.             TipoBehaviour bt = TipoBehaviour.toEnumConstant(behaviour.getNome(), false);
  51.             switch (bt) {
  52.             case CONSEGNA_MULTIPLA:
  53.             case CONSEGNA_CONDIZIONALE:
  54.             case CONSEGNA_CON_NOTIFICHE:
  55.                
  56.                 behaviourImpl = new MultiDeliverBehaviour(bt, state);
  57.                
  58.                 tipoDiagBehaviour = bt.getLabel();
  59.                
  60.                 break;
  61.                
  62.             case CONSEGNA_LOAD_BALANCE:
  63.                
  64.                 behaviourImpl = new LoadBalancerBehaviour(state);
  65.                
  66.                 tipoDiagBehaviour = bt.getLabel();
  67.                
  68.                 break;
  69.                
  70.             case CUSTOM:
  71.                
  72.                 behaviourImpl = (IBehaviour) PddPluginLoader.getInstance().newBehaviour(behaviour.getNome());
  73.                
  74.                 tipoDiagBehaviour = behaviour.getNome();
  75.                
  76.                 break;
  77.    
  78.             }
  79.         }catch(Exception e) {
  80.             throw new CoreException(e.getMessage(),e);
  81.         }
  82.        
  83.         if(behaviourImpl==null) {
  84.             throw new CoreException("Init Behaviour '"+behaviour.getNome()+"' failed");
  85.         }
  86.        
  87.         if(msgDiag!=null) {
  88.             msgDiag.addKeyword(CostantiPdD.KEY_TIPO_BEHAVIOUR, tipoDiagBehaviour);
  89.         }
  90.        
  91.         behaviourImpl.init(pddContext, protocolFactory);
  92.        
  93.         if(behaviourImpl instanceof AbstractBehaviour) {
  94.             ((AbstractBehaviour)behaviourImpl).initMsgDiagnostico(msgDiag);
  95.         }
  96.        
  97.         return behaviourImpl;
  98.        
  99.     }
  100.    
  101. }