Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2011. Axon Framework
   *
   * 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.axonframework.util;
 
 
 import java.util.Map;

Abstract bean post processor that finds candidates for proxying. Typically used to wrap annotated beans with their respective interface implementations.

Author(s):
Allard Buijze
Since:
0.4
 
 
     private static final Logger logger = LoggerFactory.getLogger(AbstractAnnotationHandlerBeanPostProcessor.class);
 
     private final Map<StringSubscribablemanagedAdapters = new HashMap<StringSubscribable>();
     private ApplicationContext applicationContext;

    
 
     @Override
     public Object postProcessBeforeInitialization(Object beanString beanNamethrows BeansException {
         return bean;
     }

    
 
     @Override
     public Object postProcessAfterInitialization(final Object beanfinal String beanNamethrows BeansException {
         Class<?> targetClass = bean.getClass();
         if (isPostProcessingCandidate(targetClass)) {
             Subscribable adapter = initializeAdapterFor(bean);
             .put(beanNameadapter);
             return createAdapterProxy(targetClassbeanadaptergetAdapterInterface());
         }
         return bean;
     }

    
Returns the interface that the adapter implements to connect the annotated method to the actual interface definition. Calls to methods declared on this interface are passed to the adapter, while other method calls will be forwarded to the annotated object itself.

Note: this *must* be an interface. It may not be an (abstract) class.

Returns:
the interface that the adapter implements to connect the annotated method to the actual interface definition
 
     protected abstract Class<?> getAdapterInterface();

    
Indicates whether an object of the given targetClass should be post processed.

Parameters:
targetClass The type of bean
Returns:
true to post process bean of given type, false otherwise
 
     protected abstract boolean isPostProcessingCandidate(Class<?> targetClass);

    
 
     @Override
     public void postProcessBeforeDestruction(Object beanString beanNamethrows BeansException {
         if (.containsKey(beanName)) {
             try {
                 .get(beanName).unsubscribe();
            } catch (Exception e) {
                .error("An exception occurred while unsubscribing an event listener"e);
            } finally {
                .remove(beanName);
            }
        }
    }

    
Create an AnnotationEventListenerAdapter instance of the given bean. This adapter will receive all event handler calls to be handled by this bean.

Parameters:
bean The bean that the EventListenerAdapter has to adapt
Returns:
an event handler adapter for the given bean
    protected abstract Subscribable initializeAdapterFor(Object bean);
    private Object createAdapterProxy(Class targetClassObject annotatedHandlerfinal Object adapter,
                                      final Class<?> adapterInterface) {
        ProxyFactory pf = new ProxyFactory(annotatedHandler);
        pf.addAdvice(new AdapterIntroductionInterceptor(adapteradapterInterface));
        pf.addInterface(adapterInterface);
        pf.setProxyTargetClass(true);
        pf.setExposeProxy(true);
        return pf.getProxy(targetClass.getClassLoader());
    }

    
Returns the ApplicationContext this Bean Post Processor is registered in.

Returns:
the ApplicationContext this Bean Post Processor is registered in
        return ;
    }

    
    @Override
    public void setApplicationContext(ApplicationContext applicationContextthrows BeansException {
        this. = applicationContext;
    }
    private static class AdapterIntroductionInterceptor implements IntroductionInfoIntroductionInterceptor {
        private final Object adapter;
        private Class<?> adapterInterface;
        public AdapterIntroductionInterceptor(Object adapterClass<?> adapterInterface) {
            this. = adapter;
            this. = adapterInterface;
        }
        @Override
        public boolean implementsInterface(Class<?> intf) {
            return intf.equals();
        }
        @Override
        public Object invoke(MethodInvocation invocationthrows Throwable {
            if (invocation.getMethod().getDeclaringClass().equals()) {
                try {
                    return invocation.getMethod().invoke(invocation.getArguments());
                } catch (InvocationTargetException e) {
                    throw e.getCause();
                }
            }
            return invocation.proceed();
        }
        @Override
        public Class[] getInterfaces() {
            return new Class[]{};
        }
    }
New to GrepCode? Check out our FAQ X