INT-881, Added initial namespace support for auto-exporting SI components as OSGi services.

This commit is contained in:
Oleg Zhurakousky
2009-11-05 16:24:06 +00:00
parent 5ebd3e6d06
commit eb13d55218
17 changed files with 480 additions and 80 deletions

View File

@@ -15,21 +15,17 @@
*/
package org.springframework.integration.osgi.config.xml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.core.task.TaskExecutor;
import org.springframework.integration.channel.SubscribableChannel;
import org.springframework.integration.controlbus.ControlBus;
import org.springframework.integration.osgi.AbstractSIConfigBundleTestDeployer;
import org.springframework.integration.osgi.stubs.SIBundleContextStub;
import org.springframework.integration.util.ErrorHandlingTaskExecutor;
import org.springframework.intergration.osgi.IntegrationOSGiConstants;
/**
@@ -44,10 +40,12 @@ public class BusConfigParserTests extends AbstractSIConfigBundleTestDeployer {
ApplicationContext ac = this.deploySIConfig(bundleContext,
"org/springframework/integration/osgi/config/xml/",
"BusConfigParserTests-default.xml");
ControlBus controlBus = (ControlBus) ac.getBean("controlBus-DEFAULT_CONTROL_GROUP");
ControlBus controlBus = (ControlBus) ac.getBean(IntegrationOSGiConstants.DEFAULT_BUS_GROUP_NAME);
assertNotNull(controlBus);
assertTrue(controlBus.getBusName().equals(IntegrationOSGiConstants.DEFAULT_BUS_GROUP_NAME));
ServiceReference[] sr = bundleContext.getServiceReferences(ControlBus.class.getName(),
"(&(org.springframework.osgi.bean.name=controlBus-DEFAULT_CONTROL_GROUP))");
"(&(" + IntegrationOSGiConstants.OSGI_BEAN_NAME + "=" +
IntegrationOSGiConstants.DEFAULT_BUS_GROUP_NAME + "))");
assertNotNull(sr);
assertTrue(sr.length == 1);
controlBus = (ControlBus) bundleContext.getService(sr[0]);
@@ -59,7 +57,7 @@ public class BusConfigParserTests extends AbstractSIConfigBundleTestDeployer {
ApplicationContext ac = this.deploySIConfig(bundleContext,
"org/springframework/integration/osgi/config/xml/",
"BusConfigParserTests-overrideGroupName.xml");
ControlBus controlBus = (ControlBus) ac.getBean("controlBus-FOO");
ControlBus controlBus = (ControlBus) ac.getBean("FOO");
assertNotNull(controlBus);
ServiceReference sr = bundleContext.getServiceReference(ControlBus.class.getName());
assertNotNull(sr);

View File

@@ -0,0 +1,78 @@
/*
* 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.extender;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import org.junit.Test;
import org.mockito.Mockito;
import org.omg.CORBA.MARSHAL;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.integration.controlbus.ControlBus;
import org.springframework.integration.osgi.AbstractSIConfigBundleTestDeployer;
import org.springframework.integration.osgi.stubs.SIBundleContextStub;
import org.springframework.osgi.config.internal.adapter.OsgiServiceRegistrationListenerAdapter;
import org.springframework.osgi.mock.MockServiceReference;
import org.springframework.osgi.service.exporter.OsgiServiceRegistrationListener;
import org.springframework.osgi.service.exporter.support.OsgiServiceFactoryBean;
import org.springframework.osgi.util.OsgiServiceUtils;
/**
* TODO - insert COMMENT
* @author Oleg Zhurakousky
* @since 2.0
*/
public class BusParserProducersTests extends AbstractSIConfigBundleTestDeployer {
@SuppressWarnings("unchecked")
@Test
public void testBusWithSubscribersBusPresent() throws Exception {
SIBundleContextStub bundleContext = SIBundleContextStub.getInstance();
this.deploySIConfig(bundleContext,
"org/springframework/integration/osgi/config/xml/",
"BusConfigParserTests-default.xml");
ApplicationContext ac = this.deploySIConfig(bundleContext,
"org/springframework/integration/osgi/extender/",
"BusParserProducersTests.xml");
Map beans = ac.getBeansOfType(OsgiServiceFactoryBean.class);
assertTrue(beans.size() == 1);
OsgiServiceFactoryBean serviceExporter = (OsgiServiceFactoryBean) beans.values().toArray()[0];
assertTrue(serviceExporter.getTargetBeanName().equals("exportedChannel"));
DirectFieldAccessor serviceExporterAccessor = new DirectFieldAccessor(serviceExporter);
OsgiServiceRegistrationListener[] listeners =
(OsgiServiceRegistrationListener[]) serviceExporterAccessor.getPropertyValue("listeners");
assertTrue(listeners.length == 1);
ServiceReference sr =
bundleContext.getServiceReferences(null, "(&(org.springframework.osgi.bean.name=exportedChannel))")[0];
assertNotNull(sr);
}
@Test
public void testBusWithSubscribersBusNotPresent() throws Exception {
SIBundleContextStub bundleContext = SIBundleContextStub.getInstance();
this.deploySIConfig(bundleContext,
"org/springframework/integration/osgi/extender/",
"BusParserProducersTests.xml");
assertTrue(true); // if exception was not thrown we are ok
}
}

View File

@@ -0,0 +1,15 @@
<?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"
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">
<si-control:bus group-name="DEFAULT_CONTROL_GROUP" >
<si-control:manage-producer ref="exportedChannel"/>
</si-control:bus>
<si:publish-subscribe-channel id="exportedChannel"/>
</beans>

View File

@@ -25,6 +25,7 @@ import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mockito.Mockito;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceFactory;
@@ -46,10 +47,13 @@ public class SIBundleContextStub extends MockBundleContext {
private static SIBundleContextStub bundleContext = new SIBundleContextStub();
private Map<ServiceReference, Object> services = new HashMap<ServiceReference, Object>();
private Set<ServiceReference> serviceReferences = new HashSet<ServiceReference>();
private Map<MapBasedDictionary, ServiceListener> serviceListenerMap =
new HashMap<MapBasedDictionary, ServiceListener>();
/**
*
* @return
@@ -97,7 +101,7 @@ public class SIBundleContextStub extends MockBundleContext {
// });
// t.start();
}
log.debug("Service: " + ref + " is registered");
log.info("Registered SERVICE: " + ref);
return reg;
}
/**
@@ -128,7 +132,6 @@ public class SIBundleContextStub extends MockBundleContext {
/**
*
*/
@SuppressWarnings("unchecked")
public void addServiceListener(ServiceListener listener) {
MapBasedDictionary properties = new MapBasedDictionary();
serviceListenerMap.put(properties, listener);
@@ -147,9 +150,9 @@ public class SIBundleContextStub extends MockBundleContext {
for (Dictionary filter : serviceListenerMap.keySet()) {
MapBasedDictionary listenerFilter = new MapBasedDictionary(filter);
log.debug("Trying to match filter properties: " + listenerFilter);
log.trace("Trying to match filter properties: " + listenerFilter);
MapBasedDictionary inFilter = new MapBasedDictionary(properties);
log.debug("Current filter entry: " + inFilter);
log.trace("Current filter entry: " + inFilter);
boolean objecClassMatch = true;
if (listenerFilter.containsKey("objectClass")){
objecClassMatch = this.matchObjectClass(listenerFilter, inFilter);
@@ -172,9 +175,10 @@ public class SIBundleContextStub extends MockBundleContext {
return Arrays.binarySearch(interfaces, interfaze) >=0;
}
public void removeService(ServiceReference sr){
services.remove(sr);
serviceReferences.remove(sr);
public Map<MapBasedDictionary, ServiceListener> getServiceListenerMap() {
return serviceListenerMap;
}
public Map<ServiceReference, Object> getServices() {
return services;
}
}

View File

@@ -38,15 +38,15 @@ public class SIServiceRegistrationStub extends MockServiceRegistration {
public void setBundleContext(SIBundleContextStub context){
this.context = context;
}
public void unregister() {
ServiceReference ref = this.getReference();
DirectFieldAccessor refAccessor = new DirectFieldAccessor(ref);
Dictionary properties = (Dictionary) refAccessor.getPropertyValue("properties");
context.removeService(this.getReference());
Set<ServiceListener> listeners = context.getFilteredListeners(properties);
for (ServiceListener serviceListener : listeners) {
serviceListener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref));
}
}
// public void unregister() {
// ServiceReference ref = this.getReference();
// DirectFieldAccessor refAccessor = new DirectFieldAccessor(ref);
// Dictionary properties = (Dictionary) refAccessor.getPropertyValue("properties");
//
// context.removeService(this.getReference());
// Set<ServiceListener> listeners = context.getFilteredListeners(properties);
// for (ServiceListener serviceListener : listeners) {
// serviceListener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, ref));
// }
// }
}

View File

@@ -9,13 +9,16 @@
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<logger name="org.springframework.osgi">
<level value="trace" />
<level value="info" />
</logger>
<logger name="org.springframework.integration">
<level value="info" />
</logger>
<logger name="org.springframework.integration.osgi">
<level value="debug" />
</logger>