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;
Command handler that handles commands based on CommandHandler annotations on an aggregate. Those annotations may appear on methods, in which case a specific aggregate instance needs to be targeted by the command (see org.axonframework.domain.AggregateIdentifier), or on the constructor. The latter will create a new Aggregate instance, which is then stored in the repository.

Parameters:
<T> the type of aggregate this handler handles commands for
Author(s):
Allard Buijze
Since:
1.2
 
 public class AggregateAnnotationCommandHandler<T extends AggregateRootimplements Subscribable {
 
     private final CommandBus commandBus;
     private final AggregateCommandHandlerInspector<T> inspector;
     private final Repository<T> repository;
 
     private final Map<ClassCommandHandlerregisteredCommandHandlers = new HashMap<ClassCommandHandler>();
     private final CommandTargetResolver commandTargetResolver;

    
Initializes an AnnotationCommandHandler based on the annotations on given aggregateType, to be registered on the given commandBus.

Parameters:
aggregateType The type of aggregate
repository The repository providing access to aggregate instances
commandBus The command bus to register command handlers to
 
     public AggregateAnnotationCommandHandler(Class<T> aggregateTypeRepository<T> repositoryCommandBus commandBus) {
         this(aggregateTyperepositorycommandBusnew AnnotationCommandTargetResolver());
     }

    
Initializes an AnnotationCommandHandler based on the annotations on given aggregateType, to be registered on the given commandBus.

Parameters:
aggregateType The type of aggregate
repository The repository providing access to aggregate instances
commandBus The command bus to register command handlers to
commandTargetResolver The target resolution strategy
 
     public AggregateAnnotationCommandHandler(Class<T> aggregateTypeRepository<T> repositoryCommandBus commandBus,
                                              CommandTargetResolver commandTargetResolver) {
         this. = repository;
         this. = commandBus;
         this. = commandTargetResolver;
         this. = new AggregateCommandHandlerInspector<T>(aggregateType);
     }
 
     @SuppressWarnings({"unchecked"})
     @Override
     @PreDestroy
     public synchronized void unsubscribe() {
         for (Map.Entry<ClassCommandHandlerhandlerEntry : .entrySet()) {
             .unsubscribe(handlerEntry.getKey(), handlerEntry.getValue());
         }
     }
 
     @PostConstruct
     @Override
     public synchronized void subscribe() {
         for (final Handler commandHandler : .getHandlers()) {
             CommandHandler<Objecthandler = new CommandHandler<Object>() {
                 @Override
                 public Object handle(Object commandUnitOfWork unitOfWorkthrows Throwable {
                     T aggregate = loadAggregate(command);
                     return commandHandler.invoke(aggregatecommandunitOfWork);
                }
            };
            .subscribe(commandHandler.getParameterType(), handler);
            .put(commandHandler.getParameterType(), handler);
        }
        for (final ConstructorCommandHandler<T> handler : .getConstructorHandlers()) {
            .subscribe(handler.getCommandType(), new AnnotatedConstructorCommandHandler(handler));
        }
    }
    private T loadAggregate(Object command) {
        return .load(iv.getIdentifier(), iv.getVersion());
    }
    private class AnnotatedConstructorCommandHandler implements CommandHandler<Object> {
        private final ConstructorCommandHandler<T> handler;
            this. = handler;
        }
        @Override
        public Object handle(Object commandUnitOfWork unitOfWorkthrows Throwable {
            .add(.invoke(commandunitOfWork));
            return .;
        }
    }
New to GrepCode? Check out our FAQ X