예제 #1
0
 @Override
 public void afterPropertiesSet() throws Exception {
   // do not continue without configurers, it would not work
   if (getConfigurers() == null || getConfigurers().size() == 0) {
     throw new BeanDefinitionStoreException(
         "Cannot configure state machine due to missing configurers. Did you remember to use "
             + "@EnableStateMachine with a StateMachineConfigurerAdapter.");
   }
   for (AnnotationConfigurer<StateMachineConfig<S, E>, StateMachineConfigBuilder<S, E>>
       configurer : getConfigurers()) {
     Class<?> clazz = configurer.getClass();
     if (ClassUtils.getUserClass(clazz).getName().equals(clazzName)) {
       getBuilder().apply(configurer);
     }
   }
   StateMachineConfig<S, E> stateMachineConfig = getBuilder().getOrBuild();
   TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
   StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
   ConfigurationData<S, E> stateMachineConfigurationConfig =
       stateMachineConfig.getStateMachineConfigurationConfig();
   ObjectStateMachineFactory<S, E> stateMachineFactory =
       new ObjectStateMachineFactory<S, E>(
           new DefaultStateMachineModel<S, E>(
               stateMachineConfigurationConfig, stateMachineStates, stateMachineTransitions));
   stateMachineFactory.setBeanFactory(getBeanFactory());
   stateMachineFactory.setContextEventsEnabled(contextEvents);
   stateMachineFactory.setBeanName(beanName);
   stateMachineFactory.setHandleAutostartup(stateMachineConfigurationConfig.isAutoStart());
   StateMachine<S, E> stateMachine = stateMachineFactory.getStateMachine();
   this.lifecycle = (SmartLifecycle) stateMachine;
   this.disposableBean = (DisposableBean) stateMachine;
   setObject(stateMachine);
 }
    /**
     * Builds a {@link StateMachine}.
     *
     * @return the state machine
     */
    public StateMachine<S, E> build() {
      try {
        builder.apply(adapter);
        StateMachineConfig<S, E> stateMachineConfig = builder.getOrBuild();

        TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
        StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
        ConfigurationData<S, E> stateMachineConfigurationConfig =
            stateMachineConfig.getStateMachineConfigurationConfig();

        ObjectStateMachineFactory<S, E> stateMachineFactory = null;
        if (stateMachineConfig.getModel() != null
            && stateMachineConfig.getModel().getFactory() != null) {
          stateMachineFactory =
              new ObjectStateMachineFactory<S, E>(
                  new DefaultStateMachineModel<S, E>(stateMachineConfigurationConfig, null, null),
                  stateMachineConfig.getModel().getFactory());
        } else {
          stateMachineFactory =
              new ObjectStateMachineFactory<S, E>(
                  new DefaultStateMachineModel<S, E>(
                      stateMachineConfigurationConfig, stateMachineStates, stateMachineTransitions),
                  null);
        }

        stateMachineFactory.setHandleAutostartup(stateMachineConfigurationConfig.isAutoStart());

        if (stateMachineConfigurationConfig.getBeanFactory() != null) {
          stateMachineFactory.setBeanFactory(stateMachineConfigurationConfig.getBeanFactory());
        }
        if (stateMachineConfigurationConfig.getTaskExecutor() != null) {
          stateMachineFactory.setTaskExecutor(stateMachineConfigurationConfig.getTaskExecutor());
        } else {
          stateMachineFactory.setTaskExecutor(new SyncTaskExecutor());
        }
        if (stateMachineConfigurationConfig.getTaskScheduler() != null) {
          stateMachineFactory.setTaskScheduler(stateMachineConfigurationConfig.getTaskScheduler());
        } else {
          stateMachineFactory.setTaskScheduler(new ConcurrentTaskScheduler());
        }
        return stateMachineFactory.getStateMachine();
      } catch (Exception e) {
        throw new StateMachineException("Error building state machine", e);
      }
    }