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

@@ -0,0 +1,90 @@
/*
* 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.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

@@ -0,0 +1,46 @@
/*
* 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.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

@@ -0,0 +1,55 @@
/*
* 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.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.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

@@ -0,0 +1,65 @@
/*
* 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.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.rmi.RmiGateway;
import org.springframework.integration.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

@@ -0,0 +1,33 @@
/*
* 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.rmi.config;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
/**
* Namespace handler for Spring Integration's <em>rmi</em> namespace.
*
* @author Mark Fisher
*/
public class RmiNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
this.registerBeanDefinitionParser("rmi-gateway", new RmiGatewayParser());
this.registerBeanDefinitionParser("rmi-handler", new RmiHandlerParser());
}
}

View File

@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/integration/rmi"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
targetNamespace="http://www.springframework.org/schema/integration/rmi"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
<xsd:annotation>
<xsd:documentation><![CDATA[
Defines the configuration elements for Spring Integration's RMI adapters.
]]></xsd:documentation>
</xsd:annotation>
<xsd:element name="rmi-gateway">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines an RMI-based gateway adapter.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:ID"/>
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="expect-reply" type="xsd:boolean" default="true"/>
<xsd:attribute name="request-channel" type="xsd:string" use="required"/>
<xsd:attribute name="reply-channel" type="xsd:string"/>
<xsd:attribute name="request-timeout" type="xsd:long"/>
<xsd:attribute name="reply-timeout" type="xsd:long"/>
<xsd:attribute name="registry-host" type="xsd:string"/>
<xsd:attribute name="registry-port" type="xsd:integer"/>
<xsd:attribute name="remote-invocation-executor" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="rmi-handler">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines an RMI-based MessageHandler adapter.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:string"/>
<xsd:attribute name="host" type="xsd:string" use="required"/>
<xsd:attribute name="port" type="xsd:integer"/>
<xsd:attribute name="remote-channel" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration/rmi=org.springframework.integration.rmi.config.RmiNamespaceHandler

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration/rmi/spring-integration-rmi-1.0.xsd=org/springframework/integration/rmi/config/spring-integration-rmi-1.0.xsd