⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configurator.java

📁 JGRoups源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        for(int i=0; i < req_list.size(); i++) {            req=(ProtocolReq)req_list.elementAt(i);            // check whether layers above this one provide corresponding down services            if(req.up_reqs != null) {                for(int j=0; j < req.up_reqs.size(); j++) {                    evt_type=((Integer)req.up_reqs.elementAt(j)).intValue();                    if(!providesDownServices(i, req_list, evt_type)) {                        throw new Exception("Configurator.sanityCheck(): event " +                                            Event.type2String(evt_type) + " is required by " +                                            req.name + ", but not provided by any of the layers above");                    }                }            }            // check whether layers below this one provide corresponding up services            if(req.down_reqs != null) {  // check whether layers above this one provide up_reqs                for(int j=0; j < req.down_reqs.size(); j++) {                    evt_type=((Integer)req.down_reqs.elementAt(j)).intValue();                    if(!providesUpServices(i, req_list, evt_type)) {                        throw new Exception("Configurator.sanityCheck(): event " +                                            Event.type2String(evt_type) + " is required by " +                                            req.name + ", but not provided by any of the layers below");                    }                }            }        }    }    /** Check whether any of the protocols 'below' end_index provide evt_type */    boolean providesUpServices(int end_index, Vector req_list, int evt_type) {        ProtocolReq req;        for(int i=0; i < end_index; i++) {            req=(ProtocolReq)req_list.elementAt(i);            if(req.providesUpService(evt_type))                return true;        }        return false;    }    /** Checks whether any of the protocols 'above' start_index provide evt_type */    boolean providesDownServices(int start_index, Vector req_list, int evt_type) {        ProtocolReq req;        for(int i=start_index; i < req_list.size(); i++) {            req=(ProtocolReq)req_list.elementAt(i);            if(req.providesDownService(evt_type))                return true;        }        return false;    }    /* --------------------------- End of Private Methods ---------------------------------- */    private static class ProtocolReq {        Vector up_reqs=null;        Vector down_reqs=null;        Vector up_provides=null;        Vector down_provides=null;        String name=null;        ProtocolReq(String name) {            this.name=name;        }        boolean providesUpService(int evt_type) {            int type;            if(up_provides != null) {                for(int i=0; i < up_provides.size(); i++) {                    type=((Integer)up_provides.elementAt(i)).intValue();                    if(type == evt_type)                        return true;                }            }            return false;        }        boolean providesDownService(int evt_type) {            int type;            if(down_provides != null) {                for(int i=0; i < down_provides.size(); i++) {                    type=((Integer)down_provides.elementAt(i)).intValue();                    if(type == evt_type)                        return true;                }            }            return false;        }        public String toString() {            StringBuffer ret=new StringBuffer();            ret.append('\n' + name + ':');            if(up_reqs != null)                ret.append("\nRequires from above: " + printUpReqs());            if(down_reqs != null)                ret.append("\nRequires from below: " + printDownReqs());            if(up_provides != null)                ret.append("\nProvides to above: " + printUpProvides());            if(down_provides != null)                ret.append("\nProvides to below: ").append(printDownProvides());            return ret.toString();        }        String printUpReqs() {            StringBuffer ret=new StringBuffer("[");            if(up_reqs != null) {                for(int i=0; i < up_reqs.size(); i++) {                    ret.append(Event.type2String(((Integer)up_reqs.elementAt(i)).intValue()) + ' ');                }            }            return ret.toString() + ']';        }        String printDownReqs() {            StringBuffer ret=new StringBuffer("[");            if(down_reqs != null) {                for(int i=0; i < down_reqs.size(); i++) {                    ret.append(Event.type2String(((Integer)down_reqs.elementAt(i)).intValue()) + ' ');                }            }            return ret.toString() + ']';        }        String printUpProvides() {            StringBuffer ret=new StringBuffer("[");            if(up_provides != null) {                for(int i=0; i < up_provides.size(); i++) {                    ret.append(Event.type2String(((Integer)up_provides.elementAt(i)).intValue()) + ' ');                }            }            return ret.toString() + ']';        }        String printDownProvides() {            StringBuffer ret=new StringBuffer("[");            if(down_provides != null) {                for(int i=0; i < down_provides.size(); i++)                    ret.append(Event.type2String(((Integer)down_provides.elementAt(i)).intValue()) +                               ' ');            }            return ret.toString() + ']';        }    }    /**     * Parses and encapsulates the specification for 1 protocol of the protocol stack, e.g.     * <code>UNICAST(timeout=5000)</code>     */    public class ProtocolConfiguration {        private String protocol_name=null;        private String properties_str=null;        private final Properties properties=new Properties();        private static final String protocol_prefix="org.jgroups.protocols";        /**         * Creates a new ProtocolConfiguration.         * @param config_str The configuration specification for the protocol, e.g.         *                   <pre>VERIFY_SUSPECT(timeout=1500)</pre>         */        public ProtocolConfiguration(String config_str) throws Exception {            setContents(config_str);        }        public String getProtocolName() {            return protocol_name;        }        public Properties getProperties() {            return properties;        }        void setContents(String config_str) throws Exception {            int index=config_str.indexOf('(');  // e.g. "UDP(in_port=3333)"            int end_index=config_str.lastIndexOf(')');            if(index == -1) {                protocol_name=config_str;            }            else {                if(end_index == -1) {                    throw new Exception("Configurator.ProtocolConfiguration.setContents(): closing ')' " +                                        "not found in " + config_str + ": properties cannot be set !");                }                else {                    properties_str=config_str.substring(index + 1, end_index);                    protocol_name=config_str.substring(0, index);                }            }            /* "in_port=5555;out_port=6666" */            if(properties_str != null) {                Vector components=parseComponentStrings(properties_str, ";");                if(components.size() > 0) {                    for(int i=0; i < components.size(); i++) {                        String name, value, comp=(String)components.elementAt(i);                        index=comp.indexOf('=');                        if(index == -1) {                            throw new Exception("Configurator.ProtocolConfiguration.setContents(): " +                                                "'=' not found in " + comp);                        }                        name=comp.substring(0, index);                        value=comp.substring(index + 1, comp.length());                        properties.put(name, value);                    }                }            }        }        private Protocol createLayer(ProtocolStack prot_stack) throws Exception {            Protocol retval=null;            if(protocol_name == null)                return null;            String defaultProtocolName=protocol_prefix + '.' + protocol_name;            Class clazz=null;            try {                clazz=Util.loadClass(defaultProtocolName, this.getClass());            }            catch(ClassNotFoundException e) {            }            if(clazz == null) {                try {                    clazz=Util.loadClass(protocol_name, this.getClass());                }                catch(ClassNotFoundException e) {                }                if(clazz == null) {                    throw new Exception("unable to load class for protocol " + protocol_name +                            " (either as an absolute - " + protocol_name + " - or relative - " +                            defaultProtocolName + " - package name)!");                }            }            try {                retval=(Protocol)clazz.newInstance();                if(retval == null)                    throw new Exception("creation of instance for protocol " + protocol_name + "failed !");                retval.setProtocolStack(prot_stack);                if(properties != null)                    if(!retval.setPropertiesInternal(properties))                        return null;                // retval.init(); // moved to after creation of *all* protocols            }            catch(InstantiationException inst_ex) {                log.error("an instance of " + protocol_name + " could not be created. Please check that it implements" +                        " interface Protocol and that is has a public empty constructor !");                throw inst_ex;            }            return retval;        }        public String toString() {            StringBuffer retval=new StringBuffer();            retval.append("Protocol: ");            if(protocol_name == null)                retval.append("<unknown>");            else                retval.append(protocol_name);            if(properties != null)                retval.append("(" + properties + ')');            return retval.toString();        }    }    public static void main(String args[]) {        if(args.length != 1) {            System.err.println("Configurator <string>");            System.exit(0);        }        String config_str=args[0];        Configurator conf=new Configurator();        Vector protocol_configs;        Vector protocols=null;        Protocol protocol_stack;        try {            protocol_configs=conf.parseConfigurations(config_str);            protocols=conf.createProtocols(protocol_configs, null);            if(protocols == null)                return;            protocol_stack=conf.connectProtocols(protocols);            Thread.sleep(3000);            conf.stopProtocolStack(protocol_stack);            // conf.stopProtocolStackInternal(protocol_stack);        }        catch(Exception e) {            System.err.println(e);        }        System.err.println(protocols);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -