Migrated RMI adapter and parser code from "org.springframework.integration.adapter" to the new "org.springframework.integration.rmi" module, and added a dedicated spring-integration-rmi-1.0.xsd schema and RmiNamespaceHandler.

This commit is contained in:
Mark Fisher
2008-09-20 16:36:50 +00:00
parent 65e0ecc12f
commit 6ed4c1d7bf
16 changed files with 138 additions and 45 deletions

View File

@@ -1,90 +0,0 @@
/*
* 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.adapter.rmi;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.adapter.AbstractRemotingGateway;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.remoting.rmi.RmiServiceExporter;
import org.springframework.remoting.support.RemoteInvocationExecutor;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* A gateway adapter for RMI-based remoting.
*
* @author Mark Fisher
*/
public class RmiGateway extends AbstractRemotingGateway implements InitializingBean, MessageHandler {
public static final String SERVICE_NAME_PREFIX = "org.springframewok.integration.rmiGateway.";
private final String requestChannelName;
private volatile String registryHost;
private volatile int registryPort = Registry.REGISTRY_PORT;
private volatile RemoteInvocationExecutor remoteInvocationExecutor;
/**
* Create an RmiGateway that sends to the provided request channel.
*
* @param requestChannel the channel where messages will be sent, must not be
* <code>null</code>.
*/
public RmiGateway(MessageChannel requestChannel) {
super(requestChannel);
this.requestChannelName = requestChannel.getName();
Assert.isTrue(StringUtils.hasText(this.requestChannelName), "RmiGateway's request channel must have a name.");
}
public void setRegistryHost(String registryHost) {
this.registryHost = registryHost;
}
public void setRegistryPort(int registryPort) {
this.registryPort = registryPort;
}
public void setRemoteInvocationExecutor(RemoteInvocationExecutor remoteInvocationExecutor) {
this.remoteInvocationExecutor = remoteInvocationExecutor;
}
public void afterPropertiesSet() throws RemoteException {
RmiServiceExporter exporter = new RmiServiceExporter();
if (this.registryHost != null) {
exporter.setRegistryHost(this.registryHost);
}
exporter.setRegistryPort(this.registryPort);
if (this.remoteInvocationExecutor != null) {
exporter.setRemoteInvocationExecutor(this.remoteInvocationExecutor);
}
exporter.setService(this);
exporter.setServiceInterface(MessageHandler.class);
exporter.setServiceName(SERVICE_NAME_PREFIX + this.requestChannelName);
exporter.afterPropertiesSet();
}
}

View File

@@ -1,46 +0,0 @@
/*
* 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.adapter.rmi;
import org.springframework.integration.adapter.AbstractRemotingHandler;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;
/**
* A MessageHandler adapter for RMI-based remoting.
*
* @author Mark Fisher
*/
public class RmiHandler extends AbstractRemotingHandler {
public RmiHandler(String url) {
super(url);
}
@Override
public MessageHandler createHandlerProxy(String url) {
RmiProxyFactoryBean proxyFactory = new RmiProxyFactoryBean();
proxyFactory.setServiceInterface(MessageHandler.class);
proxyFactory.setServiceUrl(url);
proxyFactory.setLookupStubOnStartup(false);
proxyFactory.setRefreshStubOnConnectFailure(true);
proxyFactory.afterPropertiesSet();
return (MessageHandler) proxyFactory.getObject();
}
}

View File

@@ -1,55 +0,0 @@
/*
* 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.adapter.rmi.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.integration.adapter.config.AbstractRemotingGatewayParser;
import org.springframework.integration.adapter.rmi.RmiGateway;
import org.springframework.util.StringUtils;
/**
* Parser for the &lt;rmi-gateway/&gt; element.
*
* @author Mark Fisher
*/
public class RmiGatewayParser extends AbstractRemotingGatewayParser {
private static final String REMOTE_INVOCATION_EXECUTOR_ATTRIBUTE = "remote-invocation-executor";
@Override
protected Class<?> getBeanClass(Element element) {
return RmiGateway.class;
}
@Override
protected boolean isEligibleAttribute(String attributeName) {
return !attributeName.equals(REMOTE_INVOCATION_EXECUTOR_ATTRIBUTE)
&& super.isEligibleAttribute(attributeName);
}
@Override
protected void doPostProcess(BeanDefinitionBuilder builder, Element element) {
String executorRef = element.getAttribute(REMOTE_INVOCATION_EXECUTOR_ATTRIBUTE);
if (StringUtils.hasText(executorRef)) {
builder.addPropertyReference("remoteInvocationExecutor", executorRef);
}
}
}

View File

@@ -1,65 +0,0 @@
/*
* 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.adapter.rmi.config;
import java.rmi.registry.Registry;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.adapter.rmi.RmiGateway;
import org.springframework.integration.adapter.rmi.RmiHandler;
import org.springframework.util.StringUtils;
/**
* Parser for the &lt;rmi-handler/&gt; element.
*
* @author Mark Fisher
*/
public class RmiHandlerParser extends AbstractSingleBeanDefinitionParser {
@Override
protected Class<?> getBeanClass(Element element) {
return RmiHandler.class;
}
@Override
protected boolean shouldGenerateId() {
return false;
}
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String host = element.getAttribute("host");
String remoteChannel = element.getAttribute("remote-channel");
if (!(StringUtils.hasText(host) && StringUtils.hasText(remoteChannel))) {
throw new ConfigurationException("The 'host' and 'remote-channel' attributes are both required");
}
String portAttribute = element.getAttribute("port");
String port = StringUtils.hasText(portAttribute) ? portAttribute : "" + Registry.REGISTRY_PORT;
String url = "rmi://" + host + ":" + port + "/" + RmiGateway.SERVICE_NAME_PREFIX + remoteChannel;
builder.addConstructorArgValue(url);
}
}

View File

@@ -6,5 +6,3 @@ httpinvoker-handler=org.springframework.integration.adapter.httpinvoker.config.H
mail-target=org.springframework.integration.adapter.mail.config.MailTargetParser
polling-mail-source=org.springframework.integration.adapter.mail.config.PollingMailSourceParser
imap-idle-mail-source=org.springframework.integration.adapter.mail.config.SubscribableImapIdleMailSourceParser
rmi-gateway=org.springframework.integration.adapter.rmi.config.RmiGatewayParser
rmi-handler=org.springframework.integration.adapter.rmi.config.RmiHandlerParser

View File

@@ -1,139 +0,0 @@
/*
* 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.adapter.rmi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.rmi.RemoteException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessageHandlingException;
import org.springframework.integration.message.StringMessage;
import org.springframework.remoting.RemoteLookupFailureException;
import org.springframework.remoting.rmi.RmiServiceExporter;
/**
* @author Mark Fisher
*/
public class RmiHandlerTests {
private final RmiHandler handler = new RmiHandler("rmi://localhost:1099/testRemoteHandler");
@Before
public void createExporter() throws RemoteException {
RmiServiceExporter exporter = new RmiServiceExporter();
exporter.setService(new TestHandler());
exporter.setServiceInterface(MessageHandler.class);
exporter.setServiceName("testRemoteHandler");
exporter.afterPropertiesSet();
}
@Test
public void testSerializablePayload() throws RemoteException {
Message<?> replyMessage = handler.handle(new StringMessage("test"));
assertNotNull(replyMessage);
assertEquals("TEST", replyMessage.getPayload());
}
@Test
public void testSerializableAttribute() throws RemoteException {
Message<String> requestMessage = MessageBuilder.withPayload("test")
.setHeader("testAttribute", "foo").build();
Message<?> replyMessage = handler.handle(requestMessage);
assertNotNull(replyMessage);
assertEquals("foo", replyMessage.getHeaders().get("testAttribute"));
}
@Test(expected=MessageHandlingException.class)
public void testNonSerializablePayload() throws RemoteException {
NonSerializableTestObject payload = new NonSerializableTestObject();
Message<?> requestMessage = new GenericMessage<NonSerializableTestObject>(payload);
handler.handle(requestMessage);
}
@Test(expected=MessageHandlingException.class)
public void testNonSerializableAttribute() throws RemoteException {
Message<String> requestMessage = MessageBuilder.withPayload("test")
.setHeader("testAttribute", new NonSerializableTestObject()).build();
handler.handle(requestMessage);
}
@Test
public void testInvalidServiceName() throws RemoteException {
RmiHandler handler = new RmiHandler("rmi://localhost:1099/noSuchService");
boolean exceptionThrown = false;
try {
handler.handle(new StringMessage("test"));
}
catch (MessageHandlingException e) {
assertEquals(RemoteLookupFailureException.class, e.getCause().getClass());
exceptionThrown = true;
}
assertTrue(exceptionThrown);
}
@Test
public void testInvalidHost() {
RmiHandler handler = new RmiHandler("rmi://noSuchHost:1099/testRemoteHandler");
boolean exceptionThrown = false;
try {
handler.handle(new StringMessage("test"));
}
catch (MessageHandlingException e) {
assertEquals(RemoteLookupFailureException.class, e.getCause().getClass());
exceptionThrown = true;
}
assertTrue(exceptionThrown);
}
@Test
public void testInvalidUrl() throws RemoteException {
RmiHandler handler = new RmiHandler("invalid");
boolean exceptionThrown = false;
try {
handler.handle(new StringMessage("test"));
}
catch (MessageHandlingException e) {
assertEquals(RemoteLookupFailureException.class, e.getCause().getClass());
exceptionThrown = true;
}
assertTrue(exceptionThrown);
}
private static class TestHandler implements MessageHandler {
public Message<?> handle(Message<?> message) {
return new GenericMessage<String>(message.getPayload().toString().toUpperCase(), message.getHeaders());
}
}
private static class NonSerializableTestObject {
}
}

View File

@@ -1,97 +0,0 @@
/*
* 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.adapter.rmi.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.rmi.RmiGateway;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.MessageChannelTemplate;
/**
* @author Mark Fisher
*/
public class RmiGatewayParserTests {
@Test
public void testAdapterWithDefaults() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"rmiGatewayParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
RmiGateway gateway = (RmiGateway) context.getBean("gatewayWithDefaults");
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
assertEquals(true, accessor.getPropertyValue("expectReply"));
assertEquals(channel, accessor.getPropertyValue("requestChannel"));
MessageChannelTemplate template = (MessageChannelTemplate)
accessor.getPropertyValue("channelTemplate");
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
assertEquals(-1L, templateAccessor.getPropertyValue("sendTimeout"));
assertEquals(-1L, templateAccessor.getPropertyValue("receiveTimeout"));
}
@Test
public void testAdapterWithCustomProperties() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"rmiGatewayParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
RmiGateway gateway = (RmiGateway) context.getBean("gatewayWithCustomProperties");
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
assertEquals(false, accessor.getPropertyValue("expectReply"));
assertEquals(channel, accessor.getPropertyValue("requestChannel"));
MessageChannelTemplate template = (MessageChannelTemplate)
accessor.getPropertyValue("channelTemplate");
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
assertEquals(123L, templateAccessor.getPropertyValue("sendTimeout"));
assertEquals(456L, templateAccessor.getPropertyValue("receiveTimeout"));
}
@Test
public void testAdapterWithHost() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"rmiGatewayParserTests.xml", this.getClass());
RmiGateway gateway = (RmiGateway) context.getBean("gatewayWithHost");
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
assertEquals("localhost", accessor.getPropertyValue("registryHost"));
}
@Test
public void testAdapterWithPort() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"rmiGatewayParserTests.xml", this.getClass());
RmiGateway gateway = (RmiGateway) context.getBean("gatewayWithPort");
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
assertEquals(1234, accessor.getPropertyValue("registryPort"));
}
@Test
public void testAdapterWithRemoteInvocationExecutorReference() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"rmiGatewayParserTests.xml", this.getClass());
RmiGateway gateway = (RmiGateway) context.getBean("gatewayWithExecutorRef");
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
Object remoteInvocationExecutor = accessor.getPropertyValue("remoteInvocationExecutor");
assertNotNull(remoteInvocationExecutor);
assertEquals(StubRemoteInvocationExecutor.class, remoteInvocationExecutor.getClass());
}
}

View File

@@ -1,70 +0,0 @@
/*
* 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.adapter.rmi.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.rmi.RmiGateway;
import org.springframework.integration.adapter.rmi.RmiHandler;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class RmiHandlerParserTests {
private final QueueChannel testChannel = new QueueChannel();
@Before
public void exportRemoteHandler() throws Exception {
testChannel.setBeanName("testChannel");
RmiGateway gateway = new RmiGateway(testChannel);
gateway.setExpectReply(false);
gateway.afterPropertiesSet();
}
@Test
public void testRmiHandlerDirectly() {
ApplicationContext context = new ClassPathXmlApplicationContext("rmiHandlerParserTests.xml", this.getClass());
RmiHandler handler = (RmiHandler) context.getBean("handler");
handler.handle(new StringMessage("test"));
Message<?> result = testChannel.receive(1000);
assertNotNull(result);
assertEquals("test", result.getPayload());
}
@Test
public void testRmiHandlerWithEndpoint() {
ApplicationContext context = new ClassPathXmlApplicationContext("rmiHandlerParserTests.xml", this.getClass());
MessageChannel localChannel = (MessageChannel) context.getBean("localChannel");
localChannel.send(new StringMessage("test"));
Message<?> result = testChannel.receive(1000);
assertNotNull(result);
assertEquals("test", result.getPayload());
}
}

View File

@@ -1,26 +0,0 @@
/*
* 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.adapter.rmi.config;
import org.springframework.remoting.support.DefaultRemoteInvocationExecutor;
/**
* @author Mark Fisher
*/
public class StubRemoteInvocationExecutor extends DefaultRemoteInvocationExecutor {
}

View File

@@ -1,27 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<message-bus/>
<channel id="testChannel"/>
<rmi-gateway id="gatewayWithDefaults" request-channel="testChannel"/>
<rmi-gateway id="gatewayWithCustomProperties" request-channel="testChannel"
expect-reply="false" request-timeout="123" reply-timeout="456"/>
<rmi-gateway id="gatewayWithHost" request-channel="testChannel" registry-host="localhost"/>
<rmi-gateway id="gatewayWithPort" request-channel="testChannel" registry-port="1234"/>
<rmi-gateway id="gatewayWithExecutorRef" request-channel="testChannel" remote-invocation-executor="invocationExecutor"/>
<beans:bean id="invocationExecutor" class="org.springframework.integration.adapter.rmi.config.StubRemoteInvocationExecutor"/>
</beans:beans>

View File

@@ -1,18 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<message-bus/>
<channel id="localChannel"/>
<service-activator input-channel="localChannel" ref="handler"/>
<rmi-handler id="handler" remote-channel="testChannel" host="localhost"/>
</beans:beans>