com.freebox.engeneering.application.system.webflow.spring.SpringFlowControllerContextLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.freebox.engeneering.application.system.webflow.spring.SpringFlowControllerContextLoader.java

Source

/*
 * Copyright 2012 Lexaden.com
 *
 *   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 com.freebox.engeneering.application.system.webflow.spring;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;

import com.freebox.engeneering.application.system.webflow.annotation.Placeholder;
import com.freebox.engeneering.application.system.webflow.annotation.State;
import com.lexaden.webflow.web.LayoutController;

/**
 * The class is used to load annotated spring beans.
 *
 * @author Aliaksei Papou
 */
public class SpringFlowControllerContextLoader {

    private Log LOGGER = LogFactory.getLog(SpringFlowControllerContextLoader.class);

    private SpringFlowControllerContext stateContext;

    public SpringFlowControllerContextLoader(SpringFlowControllerContext stateContext) {
        this.stateContext = stateContext;
    }

    /**
     * Scans beans annotated with State annotation and binds them to state context.
     *
     * @param context the spring application context
     */
    @SuppressWarnings("rawtypes")
    public void load(ApplicationContext context) {
        long start = System.currentTimeMillis();
        final String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            if (beanDefinitionName.endsWith("Context")) {
                final Object bean = context.getBean(beanDefinitionName);

                final Method[] methods = bean.getClass().getMethods();
                for (Method method : methods) {
                    final State state = AnnotationUtils.findAnnotation(method, State.class);
                    if (state != null) {
                        final String[] stateArray = state.value();
                        for (String stateName : stateArray) {
                            final Object bean1 = context.getBean(method.getName());
                            final Placeholder placeholder = AnnotationUtils.findAnnotation(method,
                                    Placeholder.class);
                            if (placeholder != null) {
                                stateContext.bindController(stateName, placeholder.value(), method.getName());
                            }
                            if (bean1 instanceof LayoutController) {
                                stateContext.bindLayoutController(stateName, (LayoutController) bean1);
                            }
                        }
                    }
                }
            }
        }
        long end = System.currentTimeMillis();
        LOGGER.info("took time: " + ((end - start) / 1000) + " seconds.");

    }

}