
org.fabric3.implementation.spring.runtime.component.SpringEventStreamHandler Maven / Gradle / Ivy
The newest version!
/*
* Fabric3
* Copyright (c) 2009-2015 Metaform Systems
*
* 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.
*
* Portions originally based on Apache Tuscany 2007
* licensed under the Apache 2.0 license.
*/
package org.fabric3.implementation.spring.runtime.component;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.fabric3.spi.container.channel.EventStreamHandler;
import org.fabric3.spi.container.invocation.WorkContextCache;
import org.fabric3.spi.model.type.java.JavaType;
import org.fabric3.spi.container.wire.InvocationRuntimeException;
import org.oasisopen.sca.ServiceRuntimeException;
import org.springframework.beans.BeansException;
/**
* Responsible for dispatching an event to a Spring bean.
*/
public class SpringEventStreamHandler implements EventStreamHandler {
private String beanName;
private String consumerName;
private JavaType type;
private SpringComponent component;
private ClassLoader targetTCCLClassLoader;
private Object beanProxy;
private Method beanMethod;
/**
* Constructor.
*
* @param beanName the name of the bean events are dispatched to
* @param methodName the name of the bean method events are dispatched to
* @param type the event type
* @param component the target component
*/
public SpringEventStreamHandler(String beanName, String methodName, JavaType type, SpringComponent component) {
this.beanName = beanName;
this.consumerName = methodName;
this.type = type;
this.component = component;
targetTCCLClassLoader = component.getClassLoader();
}
public void handle(Object event, boolean endOfBatch) {
try {
if (beanProxy == null) {
beanProxy = component.getBean(beanName);
if (beanProxy == null) {
throw new ServiceRuntimeException("Bean not found:" + beanName);
}
}
WorkContextCache.getAndResetThreadWorkContext();
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(targetTCCLClassLoader);
getConsumerMethod().invoke(beanProxy, event);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
Package pkg = cause.getClass().getPackage();
if (cause instanceof RuntimeException && pkg != null && pkg.getName().startsWith("org.springframework.")) {
// an exception generated by Spring - treat it as an infrastructure failure
throw new ServiceRuntimeException(cause);
}
// TODO Log
} catch (IllegalAccessException e) {
throw new InvocationRuntimeException(e);
} catch (BeansException e) {
// this should not happen at this point
throw new InvocationRuntimeException("Error invoking bean: " + beanName, e);
}
}
public void setNext(EventStreamHandler next) {
throw new IllegalStateException("This handler must be the last one in the handler sequence");
}
public EventStreamHandler getNext() {
return null;
}
/**
* Resolves the consumer method on the target bean after the Spring component has been started.
*
* @return the bean consumer method
*/
private Method getConsumerMethod() {
if (beanMethod != null) {
return beanMethod;
}
Class> beanClass = component.getBeanClass(beanName);
for (Method method : beanClass.getMethods()) {
Class>[] params = method.getParameterTypes();
// setters are only supported
if (params.length == 1 && method.getName().equals(consumerName) && params[0].isAssignableFrom(type.getType())) {
beanMethod = method;
return beanMethod;
}
}
throw new ServiceRuntimeException("Could not load method with type: " + type);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy