content: 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.nio.channels.SocketChannel;
  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<SocketChannel, HttpRequestDeCoder> httpDeCoderMap = new ConcurrentHashMap<>();
      private final Map<String, Object> attrs = new ConcurrentHashMap<>();
      private boolean init;
      private List<Interceptor> interceptors;
      private ServerConfig serverConfig;
      private CheckRequestRunnable checkRequestRunnable;
  
      public ApplicationContext() {
      }
  
      public ApplicationContext(ServerConfig serverConfig) {
          this.serverConfig = serverConfig;
      }
  
      public Map<SocketChannel, HttpRequestDeCoder> getHttpDeCoderMap() {
          return httpDeCoderMap;
      }
  
      public Object getAttribute(String key) {
          return attrs.get(key);
      }
  
      public Map<String, Object> 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<Interceptor> 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<? extends Interceptor> 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);
              }
          }
      }
  }
  