Search in sources :

Example 1 with EnterpriseBeanstalkManager

use of org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalkManager in project wso2-synapse by wso2.

the class EJBMediatorTest method testInitAndMediate.

/**
 * Initializing EJBMediator and Mediating a messageContext
 */
@Test
public void testInitAndMediate() throws Exception {
    SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class);
    ServerConfigurationInformation configurationInformation = new ServerConfigurationInformation();
    ServerContextInformation contextInformation = new ServerContextInformation(configurationInformation);
    Mockito.when(synapseEnvironment.getServerContextInformation()).thenReturn(contextInformation);
    try {
        ejbMediator.init(synapseEnvironment);
        Assert.fail("executed successfully when exception is expected");
    } catch (Exception ex) {
        Assert.assertEquals("assert exception class", SynapseException.class, ex.getClass());
        Assert.assertEquals("assert exception message", "Initialization failed. BeanstalkManager not found.", ex.getMessage());
    }
    ejbMediator.setBeanstalkName(BEANSTALK_NAME);
    EnterpriseBeanstalkManager beanstalkManager = Mockito.mock(EnterpriseBeanstalkManager.class);
    contextInformation.addProperty(EnterpriseBeanstalkConstants.BEANSTALK_MANAGER_PROP_NAME, beanstalkManager);
    try {
        ejbMediator.init(synapseEnvironment);
        Assert.fail("executed successfully when exception is expected");
    } catch (Exception ex) {
        Assert.assertEquals("assert exception class", SynapseException.class, ex.getClass());
        Assert.assertEquals("assert exception message", "Initialization failed. '" + BEANSTALK_NAME + "' " + "beanstalk not found.", ex.getMessage());
    }
    beanstalk = PowerMockito.mock(EnterpriseBeanstalk.class);
    Value beanId = new Value(VALUE);
    ejbMediator.setBeanId(beanId);
    Object obj = new Object();
    PowerMockito.when(beanstalk.getEnterpriseBean(any(String.class), any(String.class), any(String.class))).thenReturn(obj);
    PowerMockito.when(beanstalkManager.getBeanstalk(BEANSTALK_NAME)).thenReturn(beanstalk);
    PowerMockito.mockStatic(BeanUtils.class);
    PowerMockito.when(BeanUtils.invokeInstanceMethod(any(Object.class), any(Method.class), any(Object[].class))).thenReturn(new Object());
    ejbMediator.init(synapseEnvironment);
    String samplePayload = "<test>value</test>";
    Map<String, Entry> properties = new HashMap<>();
    Axis2MessageContext messageContext = TestUtils.getAxis2MessageContext(samplePayload, properties);
    ejbMediator.setClassName(CLASS_NAME);
    ejbMediator.setJndiName(JNDI_NAME);
    Assert.assertTrue("mediation must be successful", ejbMediator.mediate(messageContext));
}
Also used : SynapseException(org.apache.synapse.SynapseException) HashMap(java.util.HashMap) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) EnterpriseBeanstalkManager(org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalkManager) ServerConfigurationInformation(org.apache.synapse.ServerConfigurationInformation) Method(java.lang.reflect.Method) ExpectedException(org.junit.rules.ExpectedException) SynapseException(org.apache.synapse.SynapseException) EnterpriseBeanstalk(org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalk) Entry(org.apache.synapse.config.Entry) ServerContextInformation(org.apache.synapse.ServerContextInformation) Value(org.apache.synapse.mediators.Value) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with EnterpriseBeanstalkManager

use of org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalkManager in project wso2-synapse by wso2.

the class Axis2SynapseController method stop.

/**
 * Cleanup the axis2 environment and stop the synapse environment.
 */
public void stop() {
    try {
        // stop tasks
        SynapseTaskManager synapseTaskManager = synapseEnvironment.getTaskManager();
        if (synapseTaskManager.isInitialized()) {
            synapseTaskManager.cleanup();
        }
        EnterpriseBeanstalkManager manager = (EnterpriseBeanstalkManager) serverContextInformation.getProperty(EnterpriseBeanstalkConstants.BEANSTALK_MANAGER_PROP_NAME);
        if (manager != null) {
            manager.destroy();
        }
        // stop the listener manager
        if (listenerManager != null) {
            listenerManager.stop();
        }
        // detach the synapse handlers
        if (configurationContext != null) {
            List<Phase> inflowPhases = configurationContext.getAxisConfiguration().getInFlowPhases();
            for (Phase inPhase : inflowPhases) {
                // we are interested about the Dispatch phase in the inflow
                if (PhaseMetadata.PHASE_DISPATCH.equals(inPhase.getPhaseName())) {
                    List<HandlerDescription> synapseHandlers = new ArrayList<HandlerDescription>();
                    for (Handler handler : inPhase.getHandlers()) {
                        if (SynapseDispatcher.NAME.equals(handler.getName()) || SynapseMustUnderstandHandler.NAME.equals(handler.getName())) {
                            synapseHandlers.add(handler.getHandlerDesc());
                        }
                    }
                    for (HandlerDescription handlerMD : synapseHandlers) {
                        inPhase.removeHandler(handlerMD);
                    }
                }
            }
        } else {
            handleException("Couldn't detach the Synapse handlers, " + "ConfigurationContext not found.");
        }
        // continue stopping the axis2 environment if we created it
        if (serverConfigurationInformation.isCreateNewInstance() && configurationContext != null && configurationContext.getAxisConfiguration() != null) {
            Map<String, AxisService> serviceMap = configurationContext.getAxisConfiguration().getServices();
            for (AxisService svc : serviceMap.values()) {
                svc.setActive(false);
            }
            // stop all modules
            Map<String, AxisModule> moduleMap = configurationContext.getAxisConfiguration().getModules();
            for (AxisModule mod : moduleMap.values()) {
                if (mod.getModule() != null && !"synapse".equals(mod.getName())) {
                    mod.getModule().shutdown(configurationContext);
                }
            }
        }
    } catch (AxisFault e) {
        log.error("Error stopping the Axis2 Environment");
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) EnterpriseBeanstalkManager(org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalkManager) SecretCallbackHandler(org.wso2.securevault.secret.SecretCallbackHandler)

Example 3 with EnterpriseBeanstalkManager

use of org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalkManager in project wso2-synapse by wso2.

the class Axis2SynapseController method initEnterpriseBeanstalkHolder.

private synchronized void initEnterpriseBeanstalkHolder(ServerContextInformation serverContextInformation) {
    if (serverContextInformation.getProperty(EnterpriseBeanstalkConstants.BEANSTALK_MANAGER_PROP_NAME) == null) {
        EnterpriseBeanstalkManager beanstalkHolder = new EnterpriseBeanstalkManager();
        Properties synapseProperties = SynapsePropertiesLoader.reloadSynapseProperties();
        beanstalkHolder.init(synapseProperties);
        serverContextInformation.addProperty(EnterpriseBeanstalkConstants.BEANSTALK_MANAGER_PROP_NAME, beanstalkHolder);
    }
}
Also used : EnterpriseBeanstalkManager(org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalkManager)

Example 4 with EnterpriseBeanstalkManager

use of org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalkManager in project wso2-synapse by wso2.

the class EJBMediator method init.

/**
 * @param se SynapseEnvironment to be used for initialization
 */
public void init(SynapseEnvironment se) {
    EnterpriseBeanstalkManager beanstalkManager = (EnterpriseBeanstalkManager) se.getServerContextInformation().getProperty(EnterpriseBeanstalkConstants.BEANSTALK_MANAGER_PROP_NAME);
    if (beanstalkManager == null) {
        throw new SynapseException("Initialization failed. BeanstalkManager not found.");
    }
    beanstalk = beanstalkManager.getBeanstalk(beanstalkName);
    if (beanstalk == null) {
        throw new SynapseException("Initialization failed. '" + beanstalkName + "' beanstalk not found.");
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) EnterpriseBeanstalkManager(org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalkManager)

Aggregations

EnterpriseBeanstalkManager (org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalkManager)4 SynapseException (org.apache.synapse.SynapseException)2 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 AxisFault (org.apache.axis2.AxisFault)1 ServerConfigurationInformation (org.apache.synapse.ServerConfigurationInformation)1 ServerContextInformation (org.apache.synapse.ServerContextInformation)1 EnterpriseBeanstalk (org.apache.synapse.commons.beanstalk.enterprise.EnterpriseBeanstalk)1 Entry (org.apache.synapse.config.Entry)1 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 Value (org.apache.synapse.mediators.Value)1 Test (org.junit.Test)1 ExpectedException (org.junit.rules.ExpectedException)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 SecretCallbackHandler (org.wso2.securevault.secret.SecretCallbackHandler)1