Example usage for org.springframework.beans.factory.config RuntimeBeanReference RuntimeBeanReference

List of usage examples for org.springframework.beans.factory.config RuntimeBeanReference RuntimeBeanReference

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config RuntimeBeanReference RuntimeBeanReference.

Prototype

public RuntimeBeanReference(Class<?> beanType) 

Source Link

Document

Create a new RuntimeBeanReference to a bean of the given type.

Usage

From source file:org.statefulj.framework.core.StatefulFactory.java

private void buildFramework(String statefulControllerBeanId, Class<?> statefulControllerClass,
        BeanDefinitionRegistry reg, Map<Class<?>, String> entityToRepositoryMappings,
        Map<String, EndpointBinder> binders, Map<Class<?>, PersistenceSupportBeanFactory> persistenceFactories)
        throws CannotCompileException, IllegalArgumentException, NotFoundException, IllegalAccessException,
        InvocationTargetException, ClassNotFoundException {

    // Determine the managed class
    ////  w  w  w  .  j a  v  a 2  s.com
    StatefulController scAnnotation = ReflectionUtils.getFirstClassAnnotation(statefulControllerClass,
            StatefulController.class);
    Class<?> managedClass = scAnnotation.clazz();

    // Is the the Controller and ManagedClass the same?  (DomainEntity)
    //
    boolean isDomainEntity = managedClass.equals(statefulControllerClass);

    // ReferenceFactory will generate all the necessary bean ids
    //
    ReferenceFactory referenceFactory = new ReferenceFactoryImpl(statefulControllerBeanId);

    // We need to map Transitions across all Methods
    //
    Map<String, Map<String, Method>> providersMappings = new HashMap<String, Map<String, Method>>();
    Map<Transition, Method> transitionMapping = new HashMap<Transition, Method>();
    Map<Transition, Method> anyMapping = new HashMap<Transition, Method>();
    Set<String> states = new HashSet<String>();
    Set<String> blockingStates = new HashSet<String>();

    // Fetch Repo info
    //
    String repoBeanId = getRepoId(entityToRepositoryMappings, managedClass);

    // Get the Persistence Factory
    //
    PersistenceSupportBeanFactory factory = null;
    BeanDefinition repoBeanDefinitionFactory = null;

    // If we don't have a repo mapped to this entity, fall back to memory persister
    //
    if (repoBeanId == null) {
        logger.warn("Unable to find Spring Data Repository for {}, using an in-memory persister",
                managedClass.getName());
        factory = this.memoryPersistenceFactory;
    } else {
        repoBeanDefinitionFactory = reg.getBeanDefinition(repoBeanId);
        Class<?> repoClassName = getClassFromBeanClassName(repoBeanDefinitionFactory);

        // Fetch the PersistenceFactory
        //
        factory = persistenceFactories.get(repoClassName);
    }

    // Map the Events and Transitions for the Controller
    //
    mapEventsTransitionsAndStates(statefulControllerClass, providersMappings, transitionMapping, anyMapping,
            states, blockingStates);

    // Do we have binders?
    //
    boolean hasBinders = (providersMappings.size() > 0);

    // Iterate thru the providers - building and registering each Binder
    //
    if (hasBinders) {
        for (Entry<String, Map<String, Method>> entry : providersMappings.entrySet()) {

            // Fetch the binder
            //
            EndpointBinder binder = binders.get(entry.getKey());

            // Check if we found the binder
            //
            if (binder == null) {
                logger.error("Unable to locate binder: {}", entry.getKey());
                throw new RuntimeException("Unable to locate binder: " + entry.getKey());
            }

            // Build out the Binder Class
            //
            Class<?> binderClass = binder.bindEndpoints(statefulControllerBeanId, statefulControllerClass,
                    factory.getIdType(), isDomainEntity, entry.getValue(), referenceFactory);

            // Add the new Binder Class to the Bean Registry
            //
            registerBinderBean(entry.getKey(), referenceFactory, binderClass, reg);
        }
    }

    // -- Build the FSM infrastructure --

    // Build out a set of States
    //
    List<RuntimeBeanReference> stateBeans = new ManagedList<RuntimeBeanReference>();
    for (String state : states) {
        logger.debug("Registering state \"{}\"", state);
        String stateId = registerState(referenceFactory, statefulControllerClass, state,
                blockingStates.contains(state), reg);
        stateBeans.add(new RuntimeBeanReference(stateId));
    }

    // Build out the Action classes and the Transitions
    //
    RuntimeBeanReference controllerRef = new RuntimeBeanReference(statefulControllerBeanId);
    int cnt = 1;
    List<String> transitionIds = new LinkedList<String>();
    for (Entry<Transition, Method> entry : anyMapping.entrySet()) {
        for (String state : states) {
            Transition t = entry.getKey();
            String from = state;
            String to = (t.to().equals(Transition.ANY_STATE)) ? state : entry.getKey().to();
            String transitionId = referenceFactory.getTransitionId(cnt++);
            boolean reload = t.reload();
            registerActionAndTransition(referenceFactory, statefulControllerClass, from, to, reload,
                    entry.getKey(), entry.getValue(), isDomainEntity, controllerRef, transitionId, reg);
            transitionIds.add(transitionId);
        }
    }
    for (Entry<Transition, Method> entry : transitionMapping.entrySet()) {
        Transition t = entry.getKey();
        boolean reload = t.reload();
        String transitionId = referenceFactory.getTransitionId(cnt++);
        registerActionAndTransition(referenceFactory, statefulControllerClass, entry.getKey().from(),
                entry.getKey().to(), reload, entry.getKey(), entry.getValue(), isDomainEntity, controllerRef,
                transitionId, reg);
        transitionIds.add(transitionId);
    }

    // Build out the Managed Entity Factory Bean
    //
    String factoryId = registerFactoryBean(referenceFactory, factory, scAnnotation, reg);

    // Build out the Managed Entity Finder Bean if we have endpoint binders; otherwise, it's not needed
    //
    String finderId = null;
    if (hasFinder(scAnnotation, repoBeanId)) {
        finderId = registerFinderBean(referenceFactory, factory, scAnnotation, repoBeanId, reg);
    }

    // Build out the Managed Entity State Persister Bean
    //
    String persisterId = registerPersisterBean(referenceFactory, factory, scAnnotation, managedClass,
            repoBeanId, repoBeanDefinitionFactory, stateBeans, reg);

    // Build out the FSM Bean
    //
    String fsmBeanId = registerFSM(referenceFactory, statefulControllerClass, scAnnotation, persisterId,
            managedClass, finderId, factory.getIdAnnotationType(), reg);

    // Build out the StatefulFSM Bean
    //
    String statefulFSMBeanId = registerStatefulFSMBean(referenceFactory, managedClass, fsmBeanId, factoryId,
            transitionIds, reg);

    // Build out the FSMHarness Bean if we have binders; otherwise, it's not needed
    //
    if (hasBinders) {
        registerFSMHarness(referenceFactory, factory, managedClass, statefulFSMBeanId, factoryId, finderId,
                repoBeanDefinitionFactory, reg);
    }
}

From source file:org.statefulj.framework.core.StatefulFactory.java

private void registerActionAndTransition(ReferenceFactory referenceFactory, Class<?> clazz, String from,
        String to, boolean reload, Transition transition, Method method, boolean isDomainEntity,
        RuntimeBeanReference controllerRef, String transitionId, BeanDefinitionRegistry reg) {

    // Remap to="Any" to to=from
    ////from w  ww.j a v  a  2  s.c  o m
    to = (Transition.ANY_STATE.equals(to)) ? from : to;

    logger.debug("Registered: {}({})->{}/{}", from, transition.event(), to,
            (method == null) ? "noop" : method.getName());

    // Build the Action Bean
    //
    RuntimeBeanReference actionRef = null;
    if (method != null) {
        String actionId = referenceFactory.getActionId(method);
        if (!reg.isBeanNameInUse(actionId)) {
            registerMethodInvocationAction(referenceFactory, method, isDomainEntity, controllerRef, reg,
                    actionId);
        }
        actionRef = new RuntimeBeanReference(actionId);
    }

    registerTransition(referenceFactory, from, to, reload, transition, transitionId, reg, actionRef);
}

From source file:org.statefulj.framework.core.StatefulFactory.java

/**
 * @param referenceFactory//from  ww w .  j a v a  2s  .c om
 * @param from
 * @param to
 * @param reload
 * @param transition
 * @param transitionId
 * @param reg
 * @param actionRef
 */
private void registerTransition(ReferenceFactory referenceFactory, String from, String to, boolean reload,
        Transition transition, String transitionId, BeanDefinitionRegistry reg,
        RuntimeBeanReference actionRef) {
    // Build the Transition Bean
    //
    BeanDefinition transitionBean = BeanDefinitionBuilder.genericBeanDefinition(TransitionImpl.class)
            .getBeanDefinition();

    String fromId = referenceFactory.getStateId(from);
    String toId = referenceFactory.getStateId(to);
    Pair<String, String> providerEvent = parseEvent(transition.event());

    ConstructorArgumentValues args = transitionBean.getConstructorArgumentValues();
    args.addIndexedArgumentValue(0, new RuntimeBeanReference(fromId));
    args.addIndexedArgumentValue(1, new RuntimeBeanReference(toId));
    args.addIndexedArgumentValue(2, providerEvent.getRight());
    args.addIndexedArgumentValue(3, actionRef);
    args.addIndexedArgumentValue(4,
            (transition.from().equals(Transition.ANY_STATE) && transition.to().equals(Transition.ANY_STATE)));
    args.addIndexedArgumentValue(5, reload);
    reg.registerBeanDefinition(transitionId, transitionBean);
}

From source file:org.statefulj.framework.core.StatefulFactory.java

/**
 * @param referenceFactory//  www .j a v a 2  s.  c  o m
 * @param method
 * @param isDomainEntity
 * @param controllerRef
 * @param reg
 * @param actionId
 */
private void registerMethodInvocationAction(ReferenceFactory referenceFactory, Method method,
        boolean isDomainEntity, RuntimeBeanReference controllerRef, BeanDefinitionRegistry reg,
        String actionId) {
    // Choose the type of invocationAction based off of
    // whether the controller is a DomainEntity
    //
    Class<?> methodInvocationAction = (isDomainEntity) ? DomainEntityMethodInvocationAction.class
            : MethodInvocationAction.class;

    BeanDefinition actionBean = BeanDefinitionBuilder.genericBeanDefinition(methodInvocationAction)
            .getBeanDefinition();

    ConstructorArgumentValues args = actionBean.getConstructorArgumentValues();
    args.addIndexedArgumentValue(0, method.getName());
    args.addIndexedArgumentValue(1, method.getParameterTypes());
    args.addIndexedArgumentValue(2, new RuntimeBeanReference(referenceFactory.getFSMId()));

    if (!isDomainEntity) {
        args.addIndexedArgumentValue(3, controllerRef);
    }

    reg.registerBeanDefinition(actionId, actionBean);
}

From source file:org.statefulj.framework.core.StatefulFactory.java

private String registerFSM(ReferenceFactory referenceFactory, Class<?> statefulControllerClass,
        StatefulController scAnnotation, String persisterId, Class<?> managedClass, String finderId,
        Class<? extends Annotation> idAnnotationType, BeanDefinitionRegistry reg) {
    int retryAttempts = scAnnotation.retryAttempts();
    int retryInterval = scAnnotation.retryInterval();

    String fsmBeanId = referenceFactory.getFSMId();
    BeanDefinition fsmBean = BeanDefinitionBuilder.genericBeanDefinition(FSM.class).getBeanDefinition();
    ConstructorArgumentValues args = fsmBean.getConstructorArgumentValues();
    args.addIndexedArgumentValue(0, fsmBeanId);
    args.addIndexedArgumentValue(1, new RuntimeBeanReference(persisterId));
    args.addIndexedArgumentValue(2, retryAttempts);
    args.addIndexedArgumentValue(3, retryInterval);
    args.addIndexedArgumentValue(4, managedClass);
    args.addIndexedArgumentValue(5, idAnnotationType);
    args.addIndexedArgumentValue(6, this.appContext);

    if (finderId != null) {
        args.addIndexedArgumentValue(7, new RuntimeBeanReference(finderId));
    }//from   w  w w .j  a  v a2  s  .  c om

    reg.registerBeanDefinition(fsmBeanId, fsmBean);
    return fsmBeanId;
}

From source file:org.statefulj.framework.core.StatefulFactory.java

private String registerStatefulFSMBean(ReferenceFactory referenceFactory, Class<?> statefulClass,
        String fsmBeanId, String factoryId, List<String> transitionIds, BeanDefinitionRegistry reg) {
    String statefulFSMBeanId = referenceFactory.getStatefulFSMId();
    BeanDefinition statefulFSMBean = BeanDefinitionBuilder.genericBeanDefinition(StatefulFSMImpl.class)
            .getBeanDefinition();//from   w  w  w .j a v a 2  s  .co m
    ConstructorArgumentValues args = statefulFSMBean.getConstructorArgumentValues();
    args.addIndexedArgumentValue(0, new RuntimeBeanReference(fsmBeanId));
    args.addIndexedArgumentValue(1, statefulClass);
    args.addIndexedArgumentValue(2, new RuntimeBeanReference(factoryId));
    reg.registerBeanDefinition(statefulFSMBeanId, statefulFSMBean);
    statefulFSMBean.setDependsOn(transitionIds.toArray(new String[] {}));
    return statefulFSMBeanId;
}