Package org.mom4j.xcp.impl

Source Code of org.mom4j.xcp.impl.XcpConfigImpl

package org.mom4j.xcp.impl;

import java.util.HashMap;

import org.mom4j.xcp.XcpConfig;
import org.mom4j.xcp.XcpHandler;
import org.mom4j.xcp.util.InstancePool;


public class XcpConfigImpl implements XcpConfig {
   
    private int         port;
    private int         workers;
   
    HashMap handlerMap;
   
    public XcpConfigImpl() {
        this.port        = XcpConfig.DEFAULT_PORT;
        this.workers     = XcpConfig.DEFAULT_WORKERS;
        this.handlerMap  = new HashMap();
    }
   
   
    public void setPort(int port) {
        if(port <= 0) {
            throw new IllegalArgumentException("port <= 0");
        }
        this.port = port;
    }
   
   
    public int getPort() {
        return this.port;
    }
   
   
    public void setWorkersCount(int num) {
        if(num < 1) {
            throw new IllegalArgumentException("num !> 1");
        }
        this.workers = num;
    }
   
   
    public void addXcpHandler(String protocol, Class xcphandler) {
        if(protocol == null || xcphandler == null) {
            throw new IllegalArgumentException("protocol || handler == null");
        }
        if(xcphandler.getName().endsWith(XcpDocumentHandler.class.getName())) {
            throw new IllegalArgumentException("XcpDocumentHandler not allowed!");
        }
        Object[] objects = new Object[this.workers];
        for(int i = 0; i < objects.length; i++) {
            try {
                objects[i] = xcphandler.newInstance();
            } catch(Exception ex) {
                //Referenced by XcpDocumentHandler, XcpConfigImpl is required on the
                //cllient side, so no logging ...
                ex.printStackTrace();
                throw new IllegalStateException(ex.getMessage());
            }
        }
        InstancePool pool = new InstancePool(objects);
       
        this.handlerMap.put(protocol, pool);
    }
   
   
    public XcpHandler getXcpHandler(String protocol) {
        XcpHandler   ret  = null;
        InstancePool pool = (InstancePool)this.handlerMap.get(protocol);
        if(pool != null) {
            ret = (XcpHandler)pool.getObject();
        }
        return ret;
    }
   
   
    public void releaseXcpHandler(String protocol, XcpHandler handler) {
        InstancePool pool = (InstancePool)this.handlerMap.get(protocol);
        if(pool != null) {
            pool.releaseObject(handler);
        }
    }
   
   
    public void removeXcpHandler(String protocol) {
        this.handlerMap.remove(protocol);
    }
   
   
    public int getWorkersCount() {
        return this.workers;
    }
   
}
TOP

Related Classes of org.mom4j.xcp.impl.XcpConfigImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.