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

org.nico.cat.server.container.Container Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
package org.nico.cat.server.container;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nico.cat.server.container.module.ApiModule;
import org.nico.cat.server.container.module.FilterModule;
import org.nico.cat.server.container.module.ListenerModule;
import org.nico.cat.server.container.moudle.realize.entry.Api;
import org.nico.cat.server.container.moudle.realize.entry.Filter;
import org.nico.cat.server.container.moudle.realize.entry.Listener;
import org.nico.cat.server.request.Request;
import org.nico.cat.server.request.extra.Session;
import org.nico.cat.server.response.Response;
import org.nico.log.Logging;
import org.nico.log.LoggingHelper;

/** 
 * Save the container for the service configuration information.
 * 
 * @author nico
 * @version createTime:2018年1月8日 下午9:55:26
 */
public abstract class Container {

	private static Container instance;
	
	private Container(){
		welcomes = new ArrayList();
		parameters = new HashMap();
		apis = new ArrayList();
		filters = new ArrayList();
		listeners = new ArrayList();
		apiMap = new HashMap();
		filterMap = new HashMap();
		sessionMap = new HashMap();
		requestContainer = new ThreadLocal();
		responseContainer = new ThreadLocal();
	}
	
	public static Container getInstance(){
		try {    
            if(instance != null){
            }else{  
                Thread.sleep(300);  
                synchronized (Container.class) {  
                    if(instance == null){
                        instance = new CatContainer();  
                    }  
                }  
            }   
        } catch (InterruptedException e) {   
            e.printStackTrace();  
        }  
        return instance; 
	}
	
	protected List welcomes;
	
	protected Map parameters;
	
	protected List apis;
	
	protected List filters;
	
	protected List listeners;
	
	protected Map apiMap;
	
	protected Map filterMap;
	
	protected Map sessionMap;
	
	protected ThreadLocal requestContainer;
	
	protected ThreadLocal responseContainer;
	
	public List getWelcomes() {
		return welcomes;
	}

	public Map getParameters() {
		return parameters;
	}

	public List getApis() {
		return apis;
	}

	public List getFilters() {
		return filters;
	}

	public List getListeners() {
		return listeners;
	}
	
	public Api getApi(String uri){
		return apiMap.get(uri);
	}
	
	public Filter getFilter(String uri){
		return filterMap.get(uri);
	}
	
	public Session getSession(String sessionId){
		return sessionMap.get(sessionId);
	}
	
	public boolean containsApi(String uri){
		return apiMap.containsKey(uri);
	}
	
	public boolean containsFilter(String uri){
		return filterMap.containsKey(uri);
	}
	
	public boolean containsSession(String sessionId){
		return sessionMap.containsKey(sessionId);
	}
	
	public abstract void appendWelcome(String welcome);
	
	public abstract void appendApiModule(ApiModule api);
	
	public abstract void appendFilterModule(FilterModule filter);
	
	public abstract void appendListenerModule(ListenerModule listener);
	
	public abstract void putParameter(String key, String value);
	
	public abstract void putApi(String uri, Api api);
	
	public abstract void putFilter(String uri, Filter filter);
	
	public abstract void putSession(String sessionId, Session session);
	
	public abstract void setActivityRequest(Request request);
	
	public abstract Request getActivityRequest();
	
	public abstract void setActivityResponse(Response response);
	
	public abstract Response getActivityResponse();
	
	public abstract void init();
	
	/**
	 * Simple implements Container
	 * 
	 * @author nico
	 * @version createTime:2018年1月9日 下午8:20:27
	 */
	private static class CatContainer extends Container{
		
		private Logging logging = LoggingHelper.getLogging(CatContainer.class);
		
		@Override
		public void appendWelcome(String welcome){
			 synchronized (this) {
				 if(welcomes == null) welcomes = new ArrayList();
			 }
			 welcomes.add(welcome);
		}

		@Override
		public void appendApiModule(ApiModule api) {
			synchronized (this) {
				 if(apis == null) apis = new ArrayList();
			 }
			apis.add(api);
		}

		@Override
		public void appendFilterModule(FilterModule filter) {
			synchronized (this) {
				 if(filters == null) filters = new ArrayList();
			 }
			filters.add(filter);
		}

		@Override
		public void appendListenerModule(ListenerModule listener) {
			synchronized (this) {
				 if(listeners == null) listeners = new ArrayList();
			 }
			listeners.add(listener);
		}

		@Override
		public void init() {
			if(listeners != null){
				for(ListenerModule module: listeners){
					try {
						logging.info("Listener " + module.getHandler() + " init");
						Listener listener = module.getHandler().newInstance();
						listener.init(this.parameters, module);
					} catch (Exception e) {
						logging.error(e);
					}
				}
			}
		}
		
		@Override
		public void putParameter(String key, String value) {
			 synchronized (this) {
				 if(parameters == null) parameters = new HashMap();
			 }
			 parameters.put(key, value);
		}
		
		@Override
		public void putApi(String uri, Api api){
			synchronized (this) {
				 if(apiMap == null) apiMap = new HashMap();
			 }
			apiMap.put(uri, api);
		}
		
		@Override
		public void putFilter(String uri, Filter filter){
			synchronized (this) {
				 if(filterMap == null) filterMap = new HashMap();
			 }
			filterMap.put(uri, filter);
		}

		@Override
		public void putSession(String sessionId, Session session) {
			synchronized (this) {
				 if(sessionMap == null) sessionMap = new HashMap();
			 }
			sessionMap.put(sessionId, session);
		}

		@Override
		public void setActivityRequest(Request request) {
			requestContainer.set(request);
		}

		@Override
		public Request getActivityRequest() {
			return requestContainer.get();
		}

		@Override
		public void setActivityResponse(Response response) {
			responseContainer.set(response);
		}

		@Override
		public Response getActivityResponse() {
			return responseContainer.get();
		}
		
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy