
org.fabric3.implementation.spring.runtime.component.SpringInvoker 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.
*/
package org.fabric3.implementation.spring.runtime.component;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.fabric3.spi.container.invocation.Message;
import org.fabric3.spi.container.wire.Interceptor;
import org.fabric3.spi.container.wire.InvocationRuntimeException;
import org.oasisopen.sca.ServiceRuntimeException;
import org.springframework.beans.BeansException;
/**
* Invokes a target Spring bean. When the bean is invoked, the thread context classloader will be set to the classloader for the contribution that
* contains the application context.
*/
public class SpringInvoker implements Interceptor {
private String beanName;
private SpringComponent component;
private Object beanProxy;
private Method beanMethod;
private ClassLoader targetTCCLClassLoader;
public SpringInvoker(String beanName, Method beanMethod, SpringComponent component) {
this.beanName = beanName;
this.beanMethod = beanMethod;
this.component = component;
targetTCCLClassLoader = component.getClassLoader();
}
public Message invoke(Message msg) {
try {
if (beanProxy == null) {
beanProxy = component.getBean(beanName);
if (beanProxy == null) {
throw new ServiceRuntimeException("Bean not found:" + beanName);
}
}
Object body = msg.getBody();
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(targetTCCLClassLoader);
msg.setBody(beanMethod.invoke(beanProxy, (Object[]) body));
} 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
cause = new ServiceRuntimeException(cause);
}
msg.setBodyWithFault(cause);
} 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);
}
return msg;
}
public void setNext(Interceptor next) {
throw new IllegalStateException("This interceptor must be the last one in an target interceptor chain");
}
public Interceptor getNext() {
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy