INT-881, Finished up with initial version of SI Service Extender. Added some code improvements. Finished with lifecycle binding listeners

This commit is contained in:
Oleg Zhurakousky
2009-11-18 23:32:14 +00:00
parent 1ed307765e
commit cc42ab4ee4
15 changed files with 364 additions and 173 deletions

View File

@@ -40,11 +40,11 @@ public class BusConfigParserTests extends AbstractSIConfigBundleTestDeployer {
ApplicationContext ac = this.deploySIConfig(bundleContext,
"org/springframework/integration/osgi/config/xml/",
"BusConfigParserTests-default.xml");
ControlBus controlBus = (ControlBus) ac.getBean(ControlBusOSGiUtils.DEFAULT_BUS_GROUP_NAME);
ControlBus controlBus = (ControlBus) ac.getBean(ControlBusOSGiConfigUtils.DEFAULT_BUS_GROUP_NAME);
assertNotNull(controlBus);
ServiceReference[] sr = bundleContext.getServiceReferences(ControlBus.class.getName(),
"(&(" + IntegrationOSGiConstants.OSGI_BEAN_NAME + "=" +
ControlBusOSGiUtils.DEFAULT_BUS_GROUP_NAME + "))");
ControlBusOSGiConfigUtils.DEFAULT_BUS_GROUP_NAME + "))");
assertNotNull(sr);
assertTrue(sr.length == 1);
controlBus = (ControlBus) bundleContext.getService(sr[0]);

View File

@@ -12,5 +12,12 @@
<si-service:config>
<si-service:import name="channelA" si-type="publish-subscribe-channel" control-bus="DEFAULT_CONTROL_GROUP"/>
</si-service:config>
<si:service-activator id="sampleActivator" input-channel="channelA" auto-startup="false">
<bean class="org.springframework.integration.osgi.config.xml.ConfigParserImporterTests$TestBean"/>
</si:service-activator>
<si:gateway id="sampleGateway" service-interface="java.io.Serializable" default-request-channel="channelA"/>
</beans>

View File

@@ -16,12 +16,14 @@
package org.springframework.integration.osgi.config.xml;
import static org.junit.Assert.assertNotNull;
import junit.framework.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.osgi.framework.BundleContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.Lifecycle;
import org.springframework.integration.channel.SubscribableChannel;
import org.springframework.integration.controlbus.ControlBus;
import org.springframework.integration.core.Message;
@@ -48,6 +50,7 @@ public class ConfigParserImporterTests extends AbstractSIConfigBundleTestDeploye
SubscribableChannel channel = ac.getBean("channelA", SubscribableChannel.class);
assertNotNull(channel);
}
@Test(expected=ServiceUnavailableException.class)
public void testBasicSIServiceConfigNoBackingServiceError() throws Exception {
BundleContext bundleContext = SIBundleContextStub.getNewInstance();
@@ -81,6 +84,39 @@ public class ConfigParserImporterTests extends AbstractSIConfigBundleTestDeploye
channel.send(message);
Mockito.verify(handler, Mockito.times(1)).handleMessage(message);
}
@Test
public void testBasicSIServiceConfigWithBackingServiceLate() throws Exception {
BundleContext bundleContext = SIBundleContextStub.getNewInstance();
ApplicationContext ac = this.deploySIConfig(bundleContext,
"org/springframework/integration/osgi/config/xml/",
"ConfigParserImporterTests-default.xml");
SubscribableChannel channel = ac.getBean("channelA", SubscribableChannel.class);
assertNotNull(channel);
Lifecycle sampleActivator = (Lifecycle) ac.getBean("sampleActivator");
Assert.assertTrue(!sampleActivator.isRunning());
try {
channel.send(new StringMessage("hello"));
Assert.fail("Should have failed with ServiceUnavailableException");
} catch (ServiceUnavailableException e) {}
ConfigurableApplicationContext exporterAC = this.deploySIConfig(bundleContext,
"org/springframework/integration/osgi/config/xml/",
"ConfigParserImporterTests-exporter.xml");
Assert.assertTrue(sampleActivator.isRunning());
try {
channel.send(new StringMessage("hello"));
} catch (Exception e) {
Assert.fail("Should not have failed");
}
exporterAC.close();
Assert.assertTrue(!sampleActivator.isRunning());
exporterAC = this.deploySIConfig(bundleContext,
"org/springframework/integration/osgi/config/xml/",
"ConfigParserImporterTests-exporter.xml");
Assert.assertTrue(sampleActivator.isRunning());
}
@Test
public void testControlBusAttributeWithBusPresent() throws Exception {
BundleContext bundleContext = SIBundleContextStub.getNewInstance();
@@ -100,7 +136,11 @@ public class ConfigParserImporterTests extends AbstractSIConfigBundleTestDeploye
MessageHandler handler = Mockito.mock(MessageHandler.class);
bus.subscribe(handler);
exporterAC.close();
// should be 3 notification messages sent to the bus
// should be 1 notification message sent to the bus
Mockito.verify(handler, Mockito.times(1)).handleMessage((Message<?>) Mockito.any());
}
public static class TestBean{
public void foo(Message<?> message){}
}
}

View File

@@ -0,0 +1,10 @@
package org.springframework.integration.osgi.demo;
import org.springframework.integration.core.Message;
public class SampleActivator {
public void handle(Message<?> message){
System.out.println("Message: " + message);
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.osgi.demo;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.channel.SubscribableChannel;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.osgi.AbstractSIConfigBundleTestDeployer;
import org.springframework.integration.osgi.stubs.SIBundleContextStub;
import org.springframework.osgi.context.support.OsgiBundleXmlApplicationContext;
/**
* Simple demo shows how Spring-DM namespace support could be applied without using
* real OSGi runtime, by simply stubbing out OSGi BundleContext and passing it to
* OsgiBundleXmlApplicationContext
*
* @author Oleg Zhurakousky
* @since 2.0
*/
public class SpringDMnoOSGiDemo extends AbstractSIConfigBundleTestDeployer {
public static void main(String[] args) throws Exception {
new SpringDMnoOSGiDemo().runDemo();
}
/**
*
*/
public void runDemo() throws Exception {
BundleContext bundleContext = SIBundleContextStub.getNewInstance();
ApplicationContext serviceExporterAC = this.deploySIConfig(bundleContext,
"org/springframework/integration/osgi/extender/",
"ac-service-exporter.xml");
ApplicationContext serviceImporterAC = this.deploySIConfig(bundleContext,
"org/springframework/integration/osgi/extender/",
"ac-service-importer.xml");
// get channel from the exporter AC and send message
SubscribableChannel channel = serviceExporterAC.getBean("input", SubscribableChannel.class);
channel.send(new StringMessage("Hello String-DM without OSGi"));
// the output coming out of Service Activator defined in importer AC should display the message
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si-control="http://www.springframework.org/schema/integration/integration-control-bus"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration/integration-control-bus http://www.springframework.org/schema/integration/integration-control-bus/spring-integration-bus-1.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<si:publish-subscribe-channel id="input"/>
<osgi:service ref="input" interface="org.springframework.integration.channel.SubscribableChannel"/>
</beans>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:si-control="http://www.springframework.org/schema/integration/integration-control-bus"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration/integration-control-bus http://www.springframework.org/schema/integration/integration-control-bus/spring-integration-bus-1.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<osgi:reference id="input" interface="org.springframework.integration.channel.SubscribableChannel"/>
<si:service-activator input-channel="input">
<bean class="org.springframework.integration.osgi.extender.SampleActivator"/>
</si:service-activator>
</beans>