Search in sources :

Example 1 with Constructable

use of org.jgroups.Constructable 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)

Aggregations

Supplier (java.util.function.Supplier)1 Constructable (org.jgroups.Constructable)1 Header (org.jgroups.Header)1 Triple (org.jgroups.util.Triple)1