use of org.springframework.aop.TargetSource in project spring-integration by spring-projects.
the class IntegrationMBeanExporter method enhanceHandlerMonitor.
private MessageHandlerMetrics enhanceHandlerMonitor(MessageHandlerMetrics monitor) {
MessageHandlerMetrics result = monitor;
if (monitor.getManagedName() != null && monitor.getManagedType() != null) {
return monitor;
}
// Assignment algorithm and bean id, with bean id pulled reflectively out of enclosing endpoint if possible
String[] names = this.applicationContext.getBeanNamesForType(AbstractEndpoint.class);
String name = null;
String endpointName = null;
String source = "endpoint";
Object endpoint = null;
for (String beanName : names) {
endpoint = this.applicationContext.getBean(beanName);
try {
Object field = extractTarget(getField(endpoint, "handler"));
if (field == monitor || this.extractTarget(this.handlerInAnonymousWrapper(field)) == monitor) {
name = beanName;
endpointName = beanName;
break;
}
} catch (Exception e) {
logger.trace("Could not get handler from bean = " + beanName);
}
}
if (name != null && endpoint != null && name.startsWith("_org.springframework.integration")) {
name = getInternalComponentName(name);
source = "internal";
}
if (name != null && endpoint != null && name.startsWith("org.springframework.integration")) {
Object target = endpoint;
if (endpoint instanceof Advised) {
TargetSource targetSource = ((Advised) endpoint).getTargetSource();
if (targetSource != null) {
try {
target = targetSource.getTarget();
} catch (Exception e) {
logger.error("Could not get handler from bean = " + name);
}
}
}
Object field = getField(target, "inputChannel");
if (field != null) {
if (!this.anonymousHandlerCounters.containsKey(field)) {
this.anonymousHandlerCounters.put(field, new AtomicLong());
}
AtomicLong count = this.anonymousHandlerCounters.get(field);
long total = count.incrementAndGet();
String suffix = "";
/*
* Short hack to makes sure object names are unique if more than one endpoint has the same input channel
*/
if (total > 1) {
suffix = "#" + total;
}
name = field + suffix;
source = "anonymous";
}
}
if (endpoint instanceof Lifecycle) {
// Wrap the monitor in a lifecycle so it exposes the start/stop operations
if (monitor instanceof MappingMessageRouterManagement) {
if (monitor instanceof TrackableComponent) {
result = new TrackableRouterMetrics((Lifecycle) endpoint, (MappingMessageRouterManagement) monitor);
} else {
result = new RouterMetrics((Lifecycle) endpoint, (MappingMessageRouterManagement) monitor);
}
} else {
if (monitor instanceof TrackableComponent) {
result = new LifecycleTrackableMessageHandlerMetrics((Lifecycle) endpoint, monitor);
} else {
result = new LifecycleMessageHandlerMetrics((Lifecycle) endpoint, monitor);
}
}
}
if (name == null) {
if (monitor instanceof NamedComponent) {
name = ((NamedComponent) monitor).getComponentName();
}
if (name == null) {
name = monitor.toString();
}
source = "handler";
}
if (endpointName != null) {
this.beansByEndpointName.put(name, endpointName);
}
monitor.setManagedType(source);
monitor.setManagedName(name);
return result;
}
use of org.springframework.aop.TargetSource in project cxf by apache.
the class SpringClassUnwrapper method getRealClass.
@Override
public Class<?> getRealClass(Object o) {
if (AopUtils.isAopProxy(o) && (o instanceof Advised)) {
Advised advised = (Advised) o;
try {
TargetSource targetSource = advised.getTargetSource();
final Object target;
try {
target = targetSource.getTarget();
} catch (BeanCreationException ex) {
// be active on the current thread yet
return getRealClassFromClass(targetSource.getTargetClass());
}
if (target == null) {
Class<?> targetClass = AopUtils.getTargetClass(o);
if (targetClass != null) {
return getRealClassFromClass(targetClass);
}
} else {
return getRealClass(target);
}
} catch (Exception ex) {
// ignore
}
} else if (ClassUtils.isCglibProxyClass(o.getClass())) {
return getRealClassFromClass(AopUtils.getTargetClass(o));
}
return o.getClass();
}
use of org.springframework.aop.TargetSource in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testProxyIsBoundBeforeTargetSourceInvoked.
@Test
public void testProxyIsBoundBeforeTargetSourceInvoked() {
final TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new DebugInterceptor());
pf.setExposeProxy(true);
final ITestBean proxy = (ITestBean) createProxy(pf);
Advised config = (Advised) proxy;
// This class just checks proxy is bound before getTarget() call
config.setTargetSource(new TargetSource() {
@Override
public Class<?> getTargetClass() {
return TestBean.class;
}
@Override
public boolean isStatic() {
return false;
}
@Override
public Object getTarget() throws Exception {
assertThat(AopContext.currentProxy()).isEqualTo(proxy);
return target;
}
@Override
public void releaseTarget(Object target) throws Exception {
}
});
// Just test anything: it will fail if context wasn't found
assertThat(proxy.getAge()).isEqualTo(0);
}
use of org.springframework.aop.TargetSource in project spring-framework by spring-projects.
the class AbstractAutoProxyCreator method postProcessBeforeInstantiation.
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
Object cacheKey = getCacheKey(beanClass, beanName);
if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return null;
}
}
// Create proxy here if we have a custom TargetSource.
// Suppresses unnecessary default instantiation of the target bean:
// The TargetSource will handle target instances in a custom fashion.
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
if (StringUtils.hasLength(beanName)) {
this.targetSourcedBeans.add(beanName);
}
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
return null;
}
Aggregations