Search in sources :

Example 1 with Triple

use of org.jgroups.util.Triple in project JGroups by belaban.

the class ClassConfigurator method init.

protected static void init() throws Exception {
    // make sure we have a class for DocumentBuilderFactory
    Util.loadClass("javax.xml.parsers.DocumentBuilderFactory", ClassConfigurator.class);
    String magic_number_file = null, protocol_id_file = null;
    try {
        // PropertyPermission not granted if running in an untrusted environment with JNLP
        magic_number_file = Util.getProperty(new String[] { Global.MAGIC_NUMBER_FILE, "org.jgroups.conf.magicNumberFile" }, null, null, MAGIC_NUMBER_FILE);
        protocol_id_file = Util.getProperty(new String[] { Global.PROTOCOL_ID_FILE, "org.jgroups.conf.protocolIDFile" }, null, null, PROTOCOL_ID_FILE);
    } catch (SecurityException ex) {
    }
    // Read jg-magic-map.xml
    List<Triple<Short, String, Boolean>> mapping = readMappings(magic_number_file);
    for (Triple<Short, String, Boolean> tuple : mapping) {
        short m = tuple.getVal1();
        if (m >= MAX_MAGIC_VALUE)
            throw new IllegalArgumentException("ID " + m + " is bigger than MAX_MAGIC_VALUE (" + MAX_MAGIC_VALUE + "); increase MAX_MAGIC_VALUE");
        boolean external = tuple.getVal3();
        if (external) {
            if (magicMap[m] != null)
                alreadyInMagicMap(m, tuple.getVal2());
            continue;
        }
        Class clazz = Util.loadClass(tuple.getVal2(), ClassConfigurator.class);
        if (magicMap[m] != null)
            alreadyInMagicMap(m, clazz.getName());
        if (Constructable.class.isAssignableFrom(clazz)) {
            Constructable obj = (Constructable) clazz.newInstance();
            magicMap[m] = obj.create();
        } else {
            Supplier<? extends Object> supplier = (Supplier<Object>) () -> {
                try {
                    return clazz.newInstance();
                } catch (Throwable throwable) {
                    return null;
                }
            };
            magicMap[m] = supplier;
        }
        Object inst = magicMap[m].get();
        if (inst == null)
            continue;
        // test to confirm that the Constructable impl returns an instance of the correct type
        if (!inst.getClass().equals(clazz))
            throw new IllegalStateException(String.format("%s.create() returned the wrong class: %s\n", clazz.getSimpleName(), inst.getClass().getSimpleName()));
        // check that the IDs are the same
        if (inst instanceof Header)
            checkSameId((Header) inst, m);
        classMap.put(clazz, m);
    }
    // Read jg-protocol-ids.xml
    mapping = readMappings(protocol_id_file);
    for (Triple<Short, String, Boolean> tuple : mapping) {
        short m = tuple.getVal1();
        boolean external = tuple.getVal3();
        if (external) {
            if (protocol_names.containsKey(m))
                alreadyInProtocolsMap(m, tuple.getVal2());
            continue;
        }
        Class clazz = Util.loadClass(tuple.getVal2(), ClassConfigurator.class);
        if (protocol_ids.containsKey(clazz))
            alreadyInProtocolsMap(m, clazz.getName());
        protocol_ids.put(clazz, m);
        protocol_names.put(m, clazz);
    }
}
Also used : Constructable(org.jgroups.Constructable) Triple(org.jgroups.util.Triple) Header(org.jgroups.Header) Supplier(java.util.function.Supplier)

Example 2 with Triple

use of org.jgroups.util.Triple in project JGroups by belaban.

the class ClassConfigurator method parse.

protected static List<Triple<Short, String, Boolean>> parse(InputStream stream) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // for now
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(stream);
    NodeList class_list = document.getElementsByTagName("class");
    List<Triple<Short, String, Boolean>> list = new LinkedList<>();
    for (int i = 0; i < class_list.getLength(); i++) {
        if (class_list.item(i).getNodeType() == Node.ELEMENT_NODE) {
            list.add(parseClassData(class_list.item(i)));
        }
    }
    return list;
}
Also used : Triple(org.jgroups.util.Triple) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Example 3 with Triple

use of org.jgroups.util.Triple in project JGroups by belaban.

the class ClassConfigurator method parseClassData.

protected static Triple<Short, String, Boolean> parseClassData(Node protocol) {
    protocol.normalize();
    NamedNodeMap attrs = protocol.getAttributes();
    boolean external = false;
    String magicnumber = attrs.getNamedItem("id").getNodeValue();
    String clazzname = attrs.getNamedItem("name").getNodeValue();
    Node tmp = attrs.getNamedItem("external");
    if (tmp != null)
        external = Boolean.parseBoolean(tmp.getNodeValue());
    return new Triple<>(Short.valueOf(magicnumber), clazzname, external);
}
Also used : Triple(org.jgroups.util.Triple) NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node)

Aggregations

Triple (org.jgroups.util.Triple)3 Supplier (java.util.function.Supplier)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Constructable (org.jgroups.Constructable)1 Header (org.jgroups.Header)1 Document (org.w3c.dom.Document)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1