Search in sources :

Example 6 with ProtocolConfiguration

use of org.jgroups.conf.ProtocolConfiguration in project JGroups by belaban.

the class Configurator method setDefaultValues.

/*
    * Method which processes @Property.default() values, associated with the annotation
    * using the defaultValue= attribute. This method does the following:
    * - locate all properties which have no user value assigned
    * - if the defaultValue attribute is not "", generate a value for the field using the
    * property converter for that property and assign it to the field
    */
public static void setDefaultValues(List<ProtocolConfiguration> protocol_configs, List<Protocol> protocols, StackType ip_version) throws Exception {
    InetAddress default_ip_address = Util.getNonLoopbackAddress();
    if (default_ip_address == null) {
        log.warn(Util.getMessage("OnlyLoopbackFound"), ip_version);
        default_ip_address = Util.getLocalhost(ip_version);
    }
    for (int i = 0; i < protocol_configs.size(); i++) {
        ProtocolConfiguration protocol_config = protocol_configs.get(i);
        Protocol protocol = protocols.get(i);
        String protocolName = protocol.getName();
        // regenerate the Properties which were destroyed during basic property processing
        Map<String, String> properties = new HashMap<>(protocol_config.getProperties());
        Method[] methods = Util.getAllDeclaredMethodsWithAnnotations(protocol.getClass(), Property.class);
        for (int j = 0; j < methods.length; j++) {
            if (isSetPropertyMethod(methods[j], protocol.getClass())) {
                String propertyName = PropertyHelper.getPropertyName(methods[j]);
                Object propertyValue = getValueFromProtocol(protocol, propertyName);
                if (propertyValue == null) {
                    // if propertyValue is null, check if there is a we can use
                    Property annotation = methods[j].getAnnotation(Property.class);
                    // get the default value for the method- check for InetAddress types
                    String defaultValue = null;
                    if (InetAddressInfo.isInetAddressRelated(methods[j])) {
                        defaultValue = ip_version == StackType.IPv4 ? annotation.defaultValueIPv4() : annotation.defaultValueIPv6();
                        if (defaultValue != null && !defaultValue.isEmpty()) {
                            Object converted = null;
                            try {
                                if (defaultValue.equalsIgnoreCase(Global.NON_LOOPBACK_ADDRESS))
                                    converted = default_ip_address;
                                else
                                    converted = PropertyHelper.getConvertedValue(protocol, methods[j], properties, defaultValue, true);
                                methods[j].invoke(protocol, converted);
                            } catch (Exception e) {
                                throw new Exception("default could not be assigned for method " + propertyName + " in " + protocolName + " with default " + defaultValue, e);
                            }
                            log.debug("set property " + protocolName + "." + propertyName + " to default value " + converted);
                        }
                    }
                }
            }
        }
        // traverse class hierarchy and find all annotated fields and add them to the list if annotated
        Field[] fields = Util.getAllDeclaredFieldsWithAnnotations(protocol.getClass(), Property.class);
        for (int j = 0; j < fields.length; j++) {
            String propertyName = PropertyHelper.getPropertyName(fields[j], properties);
            Object propertyValue = getValueFromProtocol(protocol, fields[j]);
            if (propertyValue == null) {
                // add to collection of @Properties with no user specified value
                Property annotation = fields[j].getAnnotation(Property.class);
                // get the default value for the field - check for InetAddress types
                String defaultValue = null;
                if (InetAddressInfo.isInetAddressRelated(fields[j])) {
                    defaultValue = ip_version == StackType.IPv4 ? annotation.defaultValueIPv4() : annotation.defaultValueIPv6();
                    if (defaultValue != null && !defaultValue.isEmpty()) {
                        // condition for invoking converter
                        if (defaultValue != null || !PropertyHelper.usesDefaultConverter(fields[j])) {
                            Object converted = null;
                            try {
                                if (defaultValue.equalsIgnoreCase(Global.NON_LOOPBACK_ADDRESS))
                                    converted = default_ip_address;
                                else
                                    converted = PropertyHelper.getConvertedValue(protocol, fields[j], properties, defaultValue, true);
                                if (converted != null)
                                    Util.setField(fields[j], protocol, converted);
                            } catch (Exception e) {
                                throw new Exception("default could not be assigned for field " + propertyName + " in " + protocolName + " with default value " + defaultValue, e);
                            }
                            log.debug("set property " + protocolName + "." + propertyName + " to default value " + converted);
                        }
                    }
                }
            }
        }
    }
}
Also used : ProtocolConfiguration(org.jgroups.conf.ProtocolConfiguration) InetAddress(java.net.InetAddress) Property(org.jgroups.annotations.Property) DeprecatedProperty(org.jgroups.annotations.DeprecatedProperty)

Example 7 with ProtocolConfiguration

use of org.jgroups.conf.ProtocolConfiguration in project JGroups by belaban.

the class ProtocolStack method initComponents.

public static void initComponents(Protocol p, ProtocolConfiguration cfg) throws Exception {
    // copy the props and weed out properties from non-components
    Map<String, String> properties = cfg != null ? cfg.getProperties() : new HashMap<>();
    Map<String, String> props = new HashMap<>();
    // first a bit of sanity checking: no duplicate components in p
    final Map<String, Class<?>> prefixes = new HashMap<>();
    final Ref<Exception> ex = new Ref<>(null);
    Util.forAllComponentTypes(p.getClass(), (cl, prefix) -> {
        if (ex.isSet())
            return;
        if (prefix == null || prefix.trim().isEmpty()) {
            ex.set(new IllegalArgumentException(String.format("component (class=%s) in %s must have a prefix", cl.getSimpleName(), p.getName())));
            return;
        }
        if (prefixes.containsKey(prefix))
            ex.set(new IllegalArgumentException(String.format("multiple components (class=%s) in %s have same prefix '%s'", cl.getSimpleName(), p.getName(), prefix)));
        else
            prefixes.put(prefix, cl);
    });
    if (ex.isSet())
        throw ex.get();
    Util.forAllComponentTypes(p.getClass(), (c, prefix) -> {
        String key = prefix + ".";
        properties.entrySet().stream().filter(e -> e.getKey().startsWith(key)).forEach(e -> props.put(e.getKey(), e.getValue()));
    });
    ex.set(null);
    // StackType ip_version=Util.getIpStackType();
    InetAddress resolved_addr = p.getTransport() != null ? p.getTransport().getBindAddress() : null;
    final StackType ip_version = resolved_addr instanceof Inet6Address ? StackType.IPv6 : StackType.IPv4;
    Util.forAllComponents(p, (comp, prefix) -> {
        try {
            if (ex.isSet())
                return;
            Map<String, String> m = new HashMap<>();
            String key = prefix + ".";
            props.entrySet().stream().filter(e -> e.getKey().startsWith(key)).forEach(e -> m.put(e.getKey().substring(prefix.length() + 1), e.getValue()));
            props.keySet().removeIf(k -> k.startsWith(key));
            if (!m.isEmpty()) {
                Configurator.initializeAttrs(comp, m, ip_version);
                if (!m.isEmpty()) {
                    String fmt = "the following properties in %s:%s (%s) are not recognized: %s";
                    ex.set(new IllegalArgumentException(String.format(fmt, p.getName(), prefix, comp.getClass().getSimpleName(), m)));
                }
            }
            Configurator.setDefaultAddressValues(comp, ip_version);
            if (comp instanceof Lifecycle)
                ((Lifecycle) comp).init();
        } catch (Exception e) {
            throw new IllegalArgumentException(String.format("failed initializing component %s in protocol %s: %s", comp.getClass().getSimpleName(), p, e));
        }
    });
    if (ex.isSet())
        throw ex.get();
    if (!props.isEmpty()) {
        String fmt = "configuration error: the following component properties in %s are not recognized: %s";
        throw new IllegalArgumentException(String.format(fmt, p.getName(), props));
    }
}
Also used : Property(org.jgroups.annotations.Property) MessageBatch(org.jgroups.util.MessageBatch) java.util(java.util) Util(org.jgroups.util.Util) ProtocolConfiguration(org.jgroups.conf.ProtocolConfiguration) Field(java.lang.reflect.Field) InetAddress(java.net.InetAddress) Inet6Address(java.net.Inet6Address) ReflectUtils(org.jgroups.jmx.ReflectUtils) Entry(java.util.Map.Entry) org.jgroups(org.jgroups) ClassConfigurator(org.jgroups.conf.ClassConfigurator) StackType(org.jgroups.util.StackType) Pattern(java.util.regex.Pattern) PropertyConverter(org.jgroups.conf.PropertyConverter) Method(java.lang.reflect.Method) TP(org.jgroups.protocols.TP) Ref(org.jgroups.util.Ref) Inet6Address(java.net.Inet6Address) Ref(org.jgroups.util.Ref) StackType(org.jgroups.util.StackType) InetAddress(java.net.InetAddress)

Example 8 with ProtocolConfiguration

use of org.jgroups.conf.ProtocolConfiguration in project JGroups by belaban.

the class ProtocolConfigurationTest method testConfigurableObject.

/*
	 * Checks InetAddress and IpAddress processing 
	 */
public void testConfigurableObject() throws Exception {
    List<ProtocolConfiguration> protocol_configs = new ArrayList<>();
    List<Protocol> protocols = new ArrayList<>();
    // create the layer described by INETADDRESSES
    protocol = Configurator.createProtocol(configurableObjectsProps, stack);
    ProtocolConfiguration cfg = new ProtocolConfiguration(configurableObjectsProps);
    ProtocolStack.initComponents(protocol, cfg);
    // process the defaults (want this eventually)
    protocol_configs.add(new ProtocolConfiguration(configurableObjectsProps));
    protocols.add(protocol);
    // get the value which should have been assigned a default
    List<Object> configObjs = protocol.getComponents();
    assert configObjs.size() == 1;
    Object configObj = configObjs.get(0);
    assert configObj instanceof ConfigurableObject;
    assert ((ConfigurableObject) configObj).getStringProp().equals("test");
}
Also used : ProtocolConfiguration(org.jgroups.conf.ProtocolConfiguration) ArrayList(java.util.ArrayList) Protocol(org.jgroups.stack.Protocol)

Example 9 with ProtocolConfiguration

use of org.jgroups.conf.ProtocolConfiguration in project JGroups by belaban.

the class InetAddressChecksTest method testIPVersionCheckingConsistentVersion.

/*
	 * Checks IP version mechanism for consistent version processing
	 */
public void testIPVersionCheckingConsistentVersion() throws Exception {
    List<ProtocolConfiguration> protocol_configs = new ArrayList<>();
    List<Protocol> protocols = new ArrayList<>();
    // create the layer described by IPCHECK
    protocol = Configurator.createProtocol(ipCheckConsistentProps, stack);
    // process the defaults
    protocol_configs.add(new ProtocolConfiguration(ipCheckConsistentProps));
    protocols.add(protocol);
    Map<String, Map<String, InetAddressInfo>> inetAddressMap = null;
    inetAddressMap = Configurator.createInetAddressMap(protocol_configs, protocols);
    Collection<InetAddress> addrs = Configurator.getAddresses(inetAddressMap);
    determineIpVersionFromAddresses(addrs);
    // get the value which should have been assigned a default
    InetAddress a = ((IPCHECK) protocol).getInetAddress1();
    System.out.println("value of inetAddress1 = " + a);
    InetAddress b = ((IPCHECK) protocol).getInetAddress2();
    System.out.println("value of inetAddress2 = " + b);
    InetAddress c = ((IPCHECK) protocol).getInetAddress3();
    System.out.println("value of inetAddress3 = " + c);
}
Also used : ProtocolConfiguration(org.jgroups.conf.ProtocolConfiguration) Protocol(org.jgroups.stack.Protocol) InetAddress(java.net.InetAddress)

Example 10 with ProtocolConfiguration

use of org.jgroups.conf.ProtocolConfiguration in project JGroups by belaban.

the class Configurator method createProtocol.

/**
 * Creates a new protocol given the protocol specification. Initializes the properties and starts the
 * up and down handler threads.
 * @param prot_spec The specification of the protocol. Same convention as for specifying a protocol stack.
 *                  An exception will be thrown if the class cannot be created. Example:
 *                  <pre>"VERIFY_SUSPECT(timeout=1500)"</pre> Note that no colons (:) have to be
 *                  specified
 * @param stack     The protocol stack
 * @return Protocol The newly created protocol
 * @throws Exception Will be thrown when the new protocol cannot be created
 */
public static Protocol createProtocol(String prot_spec, ProtocolStack stack, boolean init_attrs) throws Exception {
    if (prot_spec == null)
        throw new Exception("Configurator.createProtocol(): prot_spec is null");
    // parse the configuration for this protocol
    ProtocolConfiguration config = new ProtocolConfiguration(prot_spec);
    // create an instance of the protocol class and configure it
    Protocol prot = createLayer(stack, config);
    if (init_attrs)
        initializeAttrs(prot, config, Util.getIpStackType());
    prot.init();
    return prot;
}
Also used : ProtocolConfiguration(org.jgroups.conf.ProtocolConfiguration)

Aggregations

ProtocolConfiguration (org.jgroups.conf.ProtocolConfiguration)12 InetAddress (java.net.InetAddress)6 Protocol (org.jgroups.stack.Protocol)4 Property (org.jgroups.annotations.Property)3 Inet6Address (java.net.Inet6Address)2 ArrayList (java.util.ArrayList)2 TP (org.jgroups.protocols.TP)2 StackType (org.jgroups.util.StackType)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 java.util (java.util)1 Entry (java.util.Map.Entry)1 Pattern (java.util.regex.Pattern)1 org.jgroups (org.jgroups)1 DeprecatedProperty (org.jgroups.annotations.DeprecatedProperty)1 ClassConfigurator (org.jgroups.conf.ClassConfigurator)1 PropertyConverter (org.jgroups.conf.PropertyConverter)1 XmlNode (org.jgroups.conf.XmlNode)1 ReflectUtils (org.jgroups.jmx.ReflectUtils)1 MessageBatch (org.jgroups.util.MessageBatch)1