Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.jgroups.stack;
import org.jgroups.Event;
import org.jgroups.Global;
import org.jgroups.annotations.DeprecatedProperty;
import org.jgroups.annotations.LocalAddress;
import org.jgroups.annotations.Property;
import org.jgroups.conf.PropertyHelper;
import org.jgroups.conf.ProtocolConfiguration;
import org.jgroups.logging.Log;
import org.jgroups.logging.LogFactory;
import org.jgroups.protocols.TP;
import org.jgroups.util.AsciiString;
import org.jgroups.util.StackType;
import org.jgroups.util.Tuple;
import org.jgroups.util.Util;
import org.w3c.dom.Node;
import java.io.IOException;
import java.io.PushbackReader;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.*;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
/**
* The task if this class is to setup and configure the protocol stack. A string describing
* the desired setup, which is both the layering and the configuration of each layer, is
* given to the configurator which creates and configures the protocol stack and returns
* a reference to the top layer (Protocol).
* Future functionality will include the capability to dynamically modify the layering
* of the protocol stack and the properties of each layer.
* @author Bela Ban
* @author Richard Achmatowicz
*/
public class Configurator {
protected static final Log log=LogFactory.getLog(Configurator.class);
private final ProtocolStack stack;
public Configurator() {
stack=null;
}
public Configurator(ProtocolStack protocolStack) {
stack=protocolStack;
}
public Protocol setupProtocolStack(List config) throws Exception {
return setupProtocolStack(config, stack);
}
public Protocol setupProtocolStack(ProtocolStack copySource) throws Exception {
List protocols=copySource.copyProtocols(stack);
Collections.reverse(protocols);
return connectProtocols(protocols);
}
/**
* The configuration string has a number of entries, separated by a ':' (colon).
* Each entry consists of the name of the protocol, followed by an optional configuration
* of that protocol. The configuration is enclosed in parentheses, and contains entries
* which are name/value pairs connected with an assignment sign (=) and separated by
* a semicolon.
*
* The first entry defines the bottommost layer, the string is parsed
* left to right and the protocol stack constructed bottom up. Example: the string
* "UDP(in_port=5555):FRAG(frag_size=32000):DEBUG" results is the following stack:
*/
public static Protocol setupProtocolStack(List protocol_configs, ProtocolStack st) throws Exception {
List protocols=createProtocols(protocol_configs, st);
if(protocols == null)
return null;
// check InetAddress related features of stack
Map> inetAddressMap = createInetAddressMap(protocol_configs, protocols) ;
Collection addrs=getAddresses(inetAddressMap);
StackType ip_version=Util.getIpStackType(); // 0 = n/a, 4 = IPv4, 6 = IPv6
if(!addrs.isEmpty()) {
// check that all user-supplied InetAddresses have a consistent version:
// 1. If an addr is IPv6 and we have an IPv4 stack --> FAIL
// 2. If an address is an IPv4 class D (multicast) address and the stack is IPv6: FAIL
// Else pass
for(InetAddress addr: addrs) {
if(addr instanceof Inet6Address && ip_version == StackType.IPv4)
throw new IllegalArgumentException("found IPv6 address " + addr + " in an IPv4 stack");
if(addr instanceof Inet4Address && addr.isMulticastAddress() && ip_version == StackType.IPv6)
throw new Exception("found IPv4 multicast address " + addr + " in an IPv6 stack");
}
}
// process default values
setDefaultValues(protocol_configs, protocols, ip_version);
ensureValidBindAddresses(protocols);
// Fixes NPE with concurrent channel creation when using a shared stack (https://issues.jboss.org/browse/JGRP-1488)
Protocol top_protocol=protocols.get(protocols.size() - 1);
top_protocol.setUpProtocol(st);
return connectProtocols(protocols);
}
public static void setDefaultValues(List protocols) throws Exception {
if(protocols == null)
return;
// check InetAddress related features of stack
Collection addrs=getInetAddresses(protocols);
StackType ip_version=Util.getIpStackType(); // 0 = n/a, 4 = IPv4, 6 = IPv6
if(!addrs.isEmpty()) {
// check that all user-supplied InetAddresses have a consistent version:
// 1. If an addr is IPv6 and we have an IPv4 stack --> FAIL
// 2. If an address is an IPv4 class D (multicast) address and the stack is IPv6: FAIL
// Else pass
for(InetAddress addr : addrs) {
if(addr instanceof Inet6Address && ip_version == StackType.IPv4)
throw new IllegalArgumentException("found IPv6 address " + addr + " in an IPv4 stack");
if(addr instanceof Inet4Address && addr.isMulticastAddress() && ip_version == StackType.IPv6)
throw new Exception("found IPv4 multicast address " + addr + " in an IPv6 stack");
}
}
// process default values
setDefaultValues(protocols, ip_version);
}
/**
* 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:
*
"VERIFY_SUSPECT(timeout=1500)"
Note that no colons (:) have to be
* specified
* @param stack The protocol stack
* @return Protocol The newly created protocol
* @exception Exception Will be thrown when the new protocol cannot be created
*/
public static Protocol createProtocol(String prot_spec, ProtocolStack stack) throws Exception {
ProtocolConfiguration config;
Protocol prot;
if(prot_spec == null) throw new Exception("Configurator.createProtocol(): prot_spec is null");
// parse the configuration for this protocol
config=new ProtocolConfiguration(prot_spec);
// create an instance of the protocol class and configure it
prot=createLayer(stack, config);
prot.init();
return prot;
}
/* ------------------------------- Private Methods ------------------------------------- */
/**
* Creates a protocol stack by iterating through the protocol list and connecting
* adjacent layers. The list starts with the topmost layer and has the bottommost
* layer at the tail.
* @param protocol_list List of Protocol elements (from top to bottom)
* @return Protocol stack
*/
public static Protocol connectProtocols(List protocol_list) throws Exception {
Protocol current_layer=null, next_layer=null;
for(int i=0; i < protocol_list.size(); i++) {
current_layer=protocol_list.get(i);
if(i + 1 >= protocol_list.size())
break;
next_layer=protocol_list.get(i + 1);
next_layer.setDownProtocol(current_layer);
current_layer.setUpProtocol(next_layer);
if(current_layer instanceof TP) {
TP transport = (TP)current_layer;
if(transport.isSingleton()) {
ConcurrentMap up_prots=transport.getUpProtocols();
synchronized(up_prots) {
while(true) {
AsciiString key=new AsciiString(Global.DUMMY + System.currentTimeMillis());
if(up_prots.containsKey(key))
continue;
up_prots.put(key, next_layer);
break;
}
}
current_layer.setUpProtocol(null);
}
}
}
// basic protocol sanity check
sanityCheck(protocol_list);
return current_layer;
}
/**
* Get a string of the form "P1(config_str1):P2:P3(config_str3)" and return
* ProtocolConfigurations for it. That means, parse "P1(config_str1)", "P2" and
* "P3(config_str3)"
* @param config_str Configuration string
* @return Vector of strings
*/
private static List parseProtocols(String config_str) throws IOException {
List retval=new LinkedList<>();
PushbackReader reader=new PushbackReader(new StringReader(config_str));
int ch;
StringBuilder sb;
boolean running=true;
while(running) {
String protocol_name=readWord(reader);
sb=new StringBuilder();
sb.append(protocol_name);
ch=read(reader);
if(ch == -1) {
retval.add(sb.toString());
break;
}
if(ch == ':') { // no attrs defined
retval.add(sb.toString());
continue;
}
if(ch == '(') { // more attrs defined
reader.unread(ch);
String attrs=readUntil(reader, ')');
sb.append(attrs);
retval.add(sb.toString());
}
else {
retval.add(sb.toString());
}
while(true) {
ch=read(reader);
if(ch == ':') {
break;
}
if(ch == -1) {
running=false;
break;
}
}
}
reader.close();
return retval;
}
private static int read(Reader reader) throws IOException {
int ch=-1;
while((ch=reader.read()) != -1) {
if(!Character.isWhitespace(ch))
return ch;
}
return ch;
}
/**
* Return a number of ProtocolConfigurations in a vector
* @param configuration protocol-stack configuration string
* @return List of ProtocolConfigurations
*/
public static List parseConfigurations(String configuration) throws Exception {
List retval=new ArrayList<>();
List protocol_string=parseProtocols(configuration);
if(protocol_string == null)
return null;
for(String component_string: protocol_string) {
retval.add(new ProtocolConfiguration(component_string));
}
return retval;
}
public static String printConfigurations(Collection configs) {
StringBuilder sb=new StringBuilder();
boolean first=true;
for(ProtocolConfiguration config: configs) {
if(first)
first=false;
else
sb.append(":");
sb.append(config.getProtocolName());
if(!config.getProperties().isEmpty()) {
sb.append('(').append(config.propertiesToString()).append(')');
}
}
return sb.toString();
}
private static String readUntil(Reader reader, char c) throws IOException {
StringBuilder sb=new StringBuilder();
int ch;
while((ch=read(reader)) != -1) {
sb.append((char)ch);
if(ch == c)
break;
}
return sb.toString();
}
private static String readWord(PushbackReader reader) throws IOException {
StringBuilder sb=new StringBuilder();
int ch;
while((ch=read(reader)) != -1) {
if(Character.isLetterOrDigit(ch) || ch == '_' || ch == '.' || ch == '$') {
sb.append((char)ch);
}
else {
reader.unread(ch);
break;
}
}
return sb.toString();
}
/**
* Takes vector of ProtocolConfigurations, iterates through it, creates Protocol for
* each ProtocolConfiguration and returns all Protocols in a list.
* @param protocol_configs List of ProtocolConfigurations
* @param stack The protocol stack
* @return List of Protocols
*/
public static List createProtocols(List protocol_configs, final ProtocolStack stack) throws Exception {
List retval=new LinkedList<>();
ProtocolConfiguration protocol_config;
Protocol layer;
String singleton_name;
for(int i=0; i < protocol_configs.size(); i++) {
protocol_config=protocol_configs.get(i);
singleton_name=protocol_config.getProperties().get(Global.SINGLETON_NAME);
if(singleton_name != null && !singleton_name.trim().isEmpty()) {
Map> singleton_transports=ProtocolStack.getSingletonTransports();
synchronized(singleton_transports) {
if(i > 0) { // crude way to check whether protocol is a transport
throw new IllegalArgumentException("Property 'singleton_name' can only be used in a transport" +
" protocol (was used in " + protocol_config.getProtocolName() + ")");
}
Tuple val=singleton_transports.get(singleton_name);
layer=val != null? val.getVal1() : null;
if(layer != null) {
retval.add(layer);
}
else {
layer=createLayer(stack, protocol_config);
if(layer == null)
return null;
singleton_transports.put(singleton_name, new Tuple<>((TP)layer,new ProtocolStack.RefCounter((short)0,(short)0)));
retval.add(layer);
}
}
continue;
}
layer=createLayer(stack, protocol_config);
if(layer == null)
return null;
retval.add(layer);
}
return retval;
}
protected static Protocol createLayer(ProtocolStack stack, ProtocolConfiguration config) throws Exception {
String protocol_name=config.getProtocolName();
Map properties=new HashMap<>(config.getProperties());
Protocol retval=null;
if(protocol_name == null || properties == null)
return null;
String defaultProtocolName=ProtocolConfiguration.protocol_prefix + '.' + protocol_name;
Class> clazz=null;
try {
clazz=Util.loadClass(defaultProtocolName, stack != null? stack.getClass() : null);
}
catch(ClassNotFoundException e) {
}
if(clazz == null) {
try {
clazz=Util.loadClass(protocol_name, config.getClassLoader());
}
catch(ClassNotFoundException e) {
}
if(clazz == null)
throw new Exception(String.format(Util.getMessage("ProtocolLoadError"), protocol_name, defaultProtocolName));
}
try {
retval=(Protocol)clazz.newInstance();
if(stack != null)
retval.setProtocolStack(stack);
removeDeprecatedProperties(retval, properties);
// before processing Field and Method based properties, take dependencies specified
// with @Property.dependsUpon into account
AccessibleObject[] dependencyOrderedFieldsAndMethods = computePropertyDependencies(retval, properties) ;
for(AccessibleObject ordered: dependencyOrderedFieldsAndMethods) {
if (ordered instanceof Field) {
resolveAndAssignField(retval, (Field)ordered, properties) ;
}
else if (ordered instanceof Method) {
resolveAndInvokePropertyMethod(retval, (Method)ordered, properties) ;
}
}
List