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

com.hibegin.http.server.ApplicationContext Maven / Gradle / Ivy

Go to download

Simple, flexible, less dependent, more extended. Less memory footprint, can quickly build Web project. Can quickly run embedded, Android devices

There is a newer version: 0.3.162
Show newest version
package com.hibegin.http.server;

import com.hibegin.common.util.LoggerUtil;
import com.hibegin.http.server.api.HttpRequestDeCoder;
import com.hibegin.http.server.api.Interceptor;
import com.hibegin.http.server.config.ServerConfig;
import com.hibegin.http.server.handler.CheckRequestRunnable;
import com.hibegin.http.server.util.FileCacheKit;

import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ApplicationContext {

    private static final Logger LOGGER = LoggerUtil.getLogger(ApplicationContext.class);
    private final Map httpDeCoderMap = new ConcurrentHashMap<>();
    private final Map attrs = new ConcurrentHashMap<>();
    private boolean init;
    private List interceptors;
    private ServerConfig serverConfig;
    private CheckRequestRunnable checkRequestRunnable;

    public ApplicationContext() {
    }

    public ApplicationContext(ServerConfig serverConfig) {
        this.serverConfig = serverConfig;
    }

    public Map getHttpDeCoderMap() {
        return httpDeCoderMap;
    }

    public Object getAttribute(String key) {
        return attrs.get(key);
    }

    public Map getAttrs() {
        return attrs;
    }

    public void setAttribute(String key, Object value) {
        attrs.put(key, value);
    }

    public ServerConfig getServerConfig() {
        return serverConfig;
    }

    public void setServerConfig(ServerConfig serverConfig) {
        this.serverConfig = serverConfig;
    }

    public List getInterceptors() {
        return interceptors;
    }

    public CheckRequestRunnable getCheckRequestRunnable() {
        return checkRequestRunnable;
    }

    public void init() {
        if (init) {
            return;
        }
        checkRequestRunnable = new CheckRequestRunnable(serverConfig, httpDeCoderMap);
        //清空tmp目录
        FileCacheKit.cleanByFlag(serverConfig.getPort());
        init = true;
        interceptors = new ArrayList<>();
        for (Class interceptorClazz : serverConfig.getInterceptors()) {
            try {
                Interceptor interceptor = interceptorClazz.getDeclaredConstructor().newInstance();
                interceptors.add(interceptor);
            } catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
                     InvocationTargetException e) {
                LOGGER.log(Level.SEVERE, "init interceptor error", e);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy