Added HttpInvokerTargetAdapter (INT-93).

This commit is contained in:
Mark Fisher
2008-03-18 22:31:57 +00:00
parent cfa6ff79a7
commit 02f159dc61
3 changed files with 128 additions and 38 deletions

View File

@@ -0,0 +1,74 @@
/*
* 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;
import java.io.Serializable;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageHandlingException;
import org.springframework.remoting.RemoteAccessException;
/**
* A base class for remoting target adapters.
*
* @author Mark Fisher
*/
public abstract class AbstractRemotingTargetAdapter implements MessageHandler {
private final MessageHandler handlerProxy;
public AbstractRemotingTargetAdapter(String url) {
this.handlerProxy = this.createHandlerProxy(url);
}
/**
* Subclasses must implement this method. It will be invoked from the constructor.
*/
protected abstract MessageHandler createHandlerProxy(String url);
public final Message<?> handle(Message<?> message) {
this.verifySerializability(message);
try {
return this.handlerProxy.handle(message);
}
catch (RemoteAccessException e) {
throw new MessageHandlingException("unable to handle message remotely", e);
}
}
private void verifySerializability(Message<?> message) {
if (!(message.getPayload() instanceof Serializable)) {
throw new MessageHandlingException(message,
this.getClass().getName() + " expects a Serializable payload type " +
"but encountered '" + message.getPayload().getClass().getName() + "'");
}
for (String attributeName : message.getHeader().getAttributeNames()) {
Object attribute = message.getHeader().getAttribute(attributeName);
if (!(attribute instanceof Serializable)) {
throw new MessageHandlingException(message,
this.getClass().getName() + " expects Serializable attribute types " +
"but encountered '" + attribute.getClass().getName() + "' for the attribute '" +
attributeName + "'");
}
}
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.httpinvoker;
import org.springframework.integration.adapter.AbstractRemotingTargetAdapter;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
/**
* A target channel adapter for HttpInvoker-based remoting.
*
* @author Mark Fisher
*/
public class HttpInvokerTargetAdapter extends AbstractRemotingTargetAdapter {
public HttpInvokerTargetAdapter(String url) {
super(url);
}
@Override
protected MessageHandler createHandlerProxy(String url) {
HttpInvokerProxyFactoryBean proxyFactory = new HttpInvokerProxyFactoryBean();
proxyFactory.setServiceInterface(MessageHandler.class);
proxyFactory.setServiceUrl(url);
proxyFactory.afterPropertiesSet();
return (MessageHandler) proxyFactory.getObject();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -16,12 +16,8 @@
package org.springframework.integration.adapter.rmi;
import java.io.Serializable;
import org.springframework.integration.adapter.AbstractRemotingTargetAdapter;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageHandlingException;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;
/**
@@ -29,47 +25,23 @@ import org.springframework.remoting.rmi.RmiProxyFactoryBean;
*
* @author Mark Fisher
*/
public class RmiTargetAdapter implements MessageHandler {
private final MessageHandler handlerProxy;
public class RmiTargetAdapter extends AbstractRemotingTargetAdapter {
public RmiTargetAdapter(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();
this.handlerProxy = (MessageHandler) proxyFactory.getObject();
}
public Message<?> handle(Message<?> message) {
this.verifySerializability(message);
try {
return this.handlerProxy.handle(message);
}
catch (RemoteAccessException e) {
throw new MessageHandlingException("unable to handle message remotely", e);
}
}
private void verifySerializability(Message<?> message) {
if (!(message.getPayload() instanceof Serializable)) {
throw new MessageHandlingException(message,
"RmiTargetAdapter expects a Serializable payload type " +
"but encountered '" + message.getPayload().getClass().getName() + "'");
}
for (String attributeName : message.getHeader().getAttributeNames()) {
Object attribute = message.getHeader().getAttribute(attributeName);
if (!(attribute instanceof Serializable)) {
throw new MessageHandlingException(message,
"RmiTargetAdapter expects Serializable attribute types " +
"but encountered '" + attribute.getClass().getName() + "' for the attribute '" +
attributeName + "'");
}
}
return (MessageHandler) proxyFactory.getObject();
}
}