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);
}
}
}
}
}
}
}
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));
}
}
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");
}
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);
}
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;
}
Aggregations