Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010-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.commandhandling.annotation;
 
 
 import java.util.Map;
Spring Bean post processor that automatically generates an adapter for each bean containing CommandHandler annotated methods.

Author(s):
Allard Buijze
Since:
0.5
 
 
     private CommandBus commandBus;
 
     @Override
     protected Class<?> getAdapterInterface() {
         return CommandHandler.class;
     }
 
     @Override
     protected boolean isPostProcessingCandidate(Class<?> targetClass) {
         return isNotCommandHandlerSubclass(targetClass) && hasCommandHandlerMethod(targetClass);
     }
 
     @Override
         adapter.subscribe();
         return adapter;
     }

    
 
     @SuppressWarnings({"unchecked"})
     @Override
     public void afterPropertiesSet() throws Exception {
         // if no CommandBus is set, find one in the application context
         if ( == null) {
             Map<StringCommandBusbeans = getApplicationContext().getBeansOfType(CommandBus.class);
             if (beans.size() != 1) {
                 throw new IllegalStateException("If no specific CommandBus is provided, the application context must "
                                                         + "contain exactly one bean of type CommandBus. The current application context has: "
                                                         + beans.size());
             }
             this. = beans.entrySet().iterator().next().getValue();
         }
     }
 
     private boolean isNotCommandHandlerSubclass(Class<?> beanClass) {
         return !CommandHandler.class.isAssignableFrom(beanClass) && !AggregateRoot.class.isAssignableFrom(beanClass);
     }
 
     private boolean hasCommandHandlerMethod(Class<?> beanClass) {
         final AtomicBoolean result = new AtomicBoolean(false);
         ReflectionUtils.doWithMethods(beanClassnew HasEventHandlerAnnotationMethodCallback(result));
         return result.get();
     }

    
Sets the event bus to which detected event listeners should be subscribed. If none is provided, the event bus will be automatically detected in the application context.

Parameters:
commandBus the event bus to subscribe detected event listeners to
 
     public void setCommandBus(CommandBus commandBus) {
         this. = commandBus;
     }
 
     private static final class HasEventHandlerAnnotationMethodCallback implements ReflectionUtils.MethodCallback {
 
         private final AtomicBoolean result;
 
         private HasEventHandlerAnnotationMethodCallback(AtomicBoolean result) {
            this. = result;
        }

        
        @Override
        public void doWith(Method methodthrows IllegalArgumentExceptionIllegalAccessException {
            if (method.isAnnotationPresent(org.axonframework.commandhandling.annotation.CommandHandler.class)) {
                .set(true);
            }
        }
    }
New to GrepCode? Check out our FAQ X