All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ars.spring.context.ApplicationContextConfiguration Maven / Gradle / Ivy

The newest version!
package ars.spring.context;

import java.util.Map;
import java.util.List;
import java.util.Locale;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.lang.reflect.Method;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.framework.Advised;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;

import ars.util.Jsons;
import ars.util.Dates;
import ars.util.Strings;
import ars.util.Servers;
import ars.util.ObjectAdapter;
import ars.invoke.Router;
import ars.invoke.Channel;
import ars.invoke.Context;
import ars.invoke.Invoker;
import ars.invoke.Messager;
import ars.invoke.Cacheable;
import ars.invoke.StandardRouter;
import ars.invoke.local.Api;
import ars.invoke.local.Apis;
import ars.invoke.local.Function;
import ars.invoke.local.LocalInvoker;
import ars.invoke.event.InvokeEvent;
import ars.invoke.event.InvokeListener;
import ars.invoke.request.SessionFactory;
import ars.invoke.request.CacheSessionFactory;

/**
 * 应用上下文配置
 *
 * @author wuyongqiang
 */
public class ApplicationContextConfiguration extends StandardRouter implements Context, ApplicationContextAware,
    ApplicationListener {
    private String pattern; // 资源地址匹配模式
    private Invoker invoker; // 资源调用对象
    private Messager messager; // 消息处理对象
    private SessionFactory sessionFactory; // 会话工厂
    private ApplicationContext applicationContext; // 应用上下文对象
    private boolean initialized, destroied; // Spring容器启动/销毁标记

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public Invoker getInvoker() {
        return invoker;
    }

    public void setInvoker(Invoker invoker) {
        this.invoker = invoker;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void setExecutor(ExecutorService executor) {
        Servers.setExecutor(executor);
    }

    public void setDatePattern(String pattern) {
        Dates.setDatePattern(pattern);
    }

    public void setDatetimePattern(String pattern) {
        Dates.setDatetimePattern(pattern);
    }

    public void setDatenanoPattern(String pattern) {
        Dates.setDatenanoPattern(pattern);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextRefreshedEvent) {
            this.initialize();
        } else if (event instanceof ContextClosedEvent) {
            this.destroy();
        }
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public void initialize() {
        if (!this.initialized) {
            synchronized (this) {
                if (!this.initialized) {
                    if (this.invoker == null) {
                        this.logger.info("Initialization default invoker with class {}", LocalInvoker.class.getName());
                        this.invoker = new LocalInvoker();
                    }
                    if (this.sessionFactory == null) {
                        this.logger.info("Initialization default session factory with class {}",
                            CacheSessionFactory.class.getName());
                        this.sessionFactory = new CacheSessionFactory();
                    }

                    // 设置json序列化对象适配器
                    this.logger.info("Register json object adapters");
                    ObjectAdapter[] objectAdapters = this.applicationContext.getBeansOfType(ObjectAdapter.class)
                        .values().toArray(new ObjectAdapter[0]);
                    Jsons.setObjectAdapters(objectAdapters);

                    // 注册系统接口资源
                    this.logger.info("Register local api resources");
                    Collection entities = this.applicationContext.getBeansWithAnnotation(Api.class).values();
                    for (Object entity : entities) {
                        Class type = entity.getClass();
                        String classApi = Apis.getApi(Apis.getApiClass(type));
                        Method[] methods = Apis.getApiMethods(type);
                        for (Method method : methods) {
                            String methodApi = Apis.getApi(method);
                            String api = Strings.replace(new StringBuilder(classApi).append('/').append(methodApi),
                                "//", "/");
                            if (this.pattern == null || Strings.matches(api, this.pattern)) {
                                this.register(api, this.invoker,
                                    new Function(entity, method, Apis.getArguments(method)));
                            }
                        }
                    }

                    // 注册事件监听器
                    this.logger.info("Register invoke listeners");
                    Map> listeners = new HashMap>();
                    try {
                        for (Entry entry : this.applicationContext.getBeansOfType(
                            InvokeListener.class).entrySet()) {
                            InvokeListener target = null;
                            InvokeListener listener = entry.getValue();
                            if (AopUtils.isAopProxy(listener)) {
                                target = (InvokeListener) ((Advised) listener).getTargetSource().getTarget();
                            }
                            Class type = null;
                            for (Method method : (target == null ? listener : target).getClass().getMethods()) {
                                if (method.getName().equals("onInvokeEvent")
                                    && (type == null || type == InvokeEvent.class)) {
                                    type = method.getParameterTypes()[0];
                                }
                            }
                            List groups = listeners.get(type);
                            if (groups == null) {
                                groups = new LinkedList();
                                listeners.put(type, groups);
                            }
                            groups.add(listener);
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    if (!listeners.isEmpty()) {
                        for (Entry> entry : listeners.entrySet()) {
                            this.setListeners(entry.getKey(), entry.getValue().toArray(new InvokeListener[0]));
                        }
                    }

                    // 设置可缓存资源规则
                    this.logger.info("Register cache configuration");
                    Map cacheables = this.applicationContext.getBeansOfType(Cacheable.class);
                    if (!cacheables.isEmpty()) {
                        this.setCacheables(cacheables.values().toArray(new Cacheable[0]));
                    }

                    // 设置请求通道上下文
                    this.logger.info("Initialization channel context");
                    Collection channels = this.applicationContext.getBeansOfType(Channel.class).values();
                    for (Channel channel : channels) {
                        if (channel.getContext() == null) {
                            channel.setContext(this);
                        }
                    }
                    super.initialize();
                    this.initialized = true;
                }
            }
        }
    }

    @Override
    public Router getRouter() {
        return this;
    }

    @Override
    public Messager getMessager() {
        if (this.messager == null) {
            synchronized (this) {
                if (this.messager == null) {
                    this.messager = new Messager() {

                        @Override
                        public String format(Locale locale, String key, Object[] args) {
                            return this.format(locale, key, args, key);
                        }

                        @Override
                        public String format(Locale locale, String key, Object[] args, String text) {
                            return applicationContext.getMessage(key, args, text, locale);
                        }

                    };
                }
            }
        }
        return this.messager;
    }

    @Override
    public SessionFactory getSessionFactory() {
        return this.sessionFactory;
    }

    @SuppressWarnings("unchecked")
    @Override
    public  T getBean(Class type) {
        if (type == null) {
            throw new IllegalArgumentException("Type must not be null");
        }
        if (ApplicationContext.class.isAssignableFrom(type)) {
            return (T) this.applicationContext;
        }
        try {
            return this.applicationContext.getBean(type);
        } catch (NoSuchBeanDefinitionException e) {
            return null;
        }
    }

    @Override
    public void destroy() {
        if (!this.destroied) {
            synchronized (this) {
                if (!this.destroied) {
                    super.destroy();
                    this.logger.info("Destroy session factory");
                    this.sessionFactory.destroy();
                    this.logger.info("Destroy thread server");
                    Servers.destroy();
                    this.destroied = true;
                }
            }
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy