private static String getProtocolRegistrationName(String clusterName, String domain, Protocol p) { return domain + ":type=protocol,cluster=" + clusterName + ",protocol=" + p.getName(); }
protected static String printProtocols(Protocol[] protocols) { StringBuilder sb=new StringBuilder("["); boolean first=true; for(Protocol prot: protocols) { if(first) first=false; else sb.append(", "); sb.append(prot.getName()); } sb.append("]"); return sb.toString(); } }
/** Returns a given protocol or null if not found */ public <T extends Protocol> T findProtocol(String name) { T tmp=(T)top_prot; String prot_name; while(tmp != null) { prot_name=tmp.getName(); if(Objects.equals(prot_name, name)) return tmp; tmp=tmp.getDownProtocol(); } return null; }
public <T extends Protocol> List<T> findProtocols(String regexp) { List<T> retval=null; Pattern pattern=Pattern.compile(regexp); for(T prot=(T)top_prot; prot != null; prot=prot.getDownProtocol()) { String prot_name=prot.getName(); if(pattern.matcher(prot_name).matches()) { if(retval == null) retval=new ArrayList<>(); retval.add(prot); } } return retval; }
/** * @return Map<String,Map<key,val>> */ public Map<String,Object> dumpStats() { Map<String,Object> retval=new HashMap<>(); // no need to be sorted, we need order of protocols as in the config! for(Protocol p=top_prot; p != null; p=p.getDownProtocol()) { String prot_name=p.getName(); if(prot_name == null) continue; Map<String,Object> tmp=new TreeMap<>(); dumpStats(p, tmp, log); if(!tmp.isEmpty()) retval.put(prot_name, tmp); } return retval; }
public static Object getConvertedValue(Object obj, Field field, String value, boolean check_scope) throws Exception { if(obj == null) throw new IllegalArgumentException("Cannot get converted value: Object is null"); if(field == null) throw new IllegalArgumentException("Cannot get converted value: Field is null"); Property annotation=field.getAnnotation(Property.class); if(annotation == null) { throw new IllegalArgumentException("Cannot get property name for field " + field.getName() + " which is not annotated with @Property"); } String propertyName=field.getName(); String name=obj instanceof Protocol? ((Protocol)obj).getName() : obj.getClass().getName(); PropertyConverter propertyConverter=(PropertyConverter)annotation.converter().newInstance(); if(propertyConverter == null) { throw new Exception("Could not find property converter for field " + propertyName + " in " + name); } Object converted=null; try { String tmp=obj instanceof Protocol? ((Protocol)obj).getName() + "." + propertyName : propertyName; converted=propertyConverter.convert(obj, field.getType(), tmp, value, check_scope); } catch(Exception e) { throw new Exception("Conversion of " + propertyName + " in " + name + " with original property value " + value + " failed", e); } return converted; }
public String toString() { StringBuilder sb = new StringBuilder() ; sb.append("InetAddressInfo(") .append("protocol=" + protocol.getName()) .append(", propertyName=" + getPropertyName()) .append(", string value=" + getStringValue()) .append(", parameterized=" + isParameterized()); if (isParameterized()) sb.append(", baseType=" + getBaseType()) ; sb.append(")") ; return sb.toString(); } }
public static Object getConvertedValue(Object obj, Field field, Map<String, String> props, String prop, boolean check_scope) throws Exception { if (obj == null) throw new IllegalArgumentException("Cannot get converted value: Object is null") ; if (field == null) throw new IllegalArgumentException("Cannot get converted value: Field is null") ; if (props == null) throw new IllegalArgumentException("Cannot get converted value: Properties is null") ; Property annotation=field.getAnnotation(Property.class); if (annotation == null) { throw new IllegalArgumentException("Cannot get property name for field " + field.getName() + " which is not annotated with @Property") ; } String propertyName = getPropertyName(field, props) ; String name = obj instanceof Protocol? ((Protocol)obj).getName() : obj.getClass().getName(); PropertyConverter propertyConverter=(PropertyConverter)annotation.converter().newInstance(); if(propertyConverter == null) { throw new Exception("Could not find property converter for field " + propertyName + " in " + name); } Object converted = null ; try { String tmp=obj instanceof Protocol? ((Protocol)obj).getName() + "." + propertyName : propertyName; converted=propertyConverter.convert(obj, field.getType(), tmp, prop, check_scope); } catch(Exception e) { throw new Exception("Conversion of " + propertyName + " in " + name + " with original property value " + prop + " failed", e); } return converted ; }
public static void removeDeprecatedProperties(Object obj, Map<String,String> props) throws Exception { //traverse class hierarchy and find all deprecated properties for(Class<?> clazz=obj.getClass(); clazz != null; clazz=clazz.getSuperclass()) { if(clazz.isAnnotationPresent(DeprecatedProperty.class)) { DeprecatedProperty declaredAnnotation=clazz.getAnnotation(DeprecatedProperty.class); String[] deprecatedProperties=declaredAnnotation.names(); for(String propertyName : deprecatedProperties) { String propertyValue=props.get(propertyName); if(propertyValue != null) { if(log.isWarnEnabled()) { String name=obj instanceof Protocol? ((Protocol)obj).getName() : obj.getClass().getName(); log.warn(Util.getMessage("Deprecated"), name + "." + propertyName, "will be ignored"); } props.remove(propertyName); } } } } }
public <T extends Protocol> T setValue(String name, Object value) { if(name == null || value == null) return (T)this; Field field=Util.getField(getClass(), name); if(field == null) throw new IllegalArgumentException("field " + name + " not found"); Property prop=field.getAnnotation(Property.class); if(prop != null) { String deprecated_msg=prop.deprecatedMessage(); if(deprecated_msg != null && !deprecated_msg.isEmpty()) log.warn("Field " + getName() + "." + name + " is deprecated: " + deprecated_msg); } Util.setField(field, this, value); return (T)this; }
String name = obj instanceof Protocol? ((Protocol)obj).getName() : obj.getClass().getName(); PropertyConverter propertyConverter=(PropertyConverter)annotation.converter().newInstance(); if(propertyConverter == null) { String tmp=obj instanceof Protocol? ((Protocol)obj).getName() + "." + propertyName : propertyName; converted=propertyConverter.convert(obj, method.getParameterTypes()[0], tmp, prop, check_scope);
/** * Inserts an already created (and initialized) protocol into the protocol list. Sets the links * to the protocols above and below correctly and adjusts the linked list of protocols accordingly. * Note that this method may change the value of top_prot or bottom_prot. * @param prot The protocol to be inserted. Before insertion, a sanity check will ensure that none * of the existing protocols have the same name as the new protocol. * @param position Where to place the protocol with respect to the neighbor_prot (ABOVE, BELOW) * @param neighbor_prot The name of the neighbor protocol. An exception will be thrown if this name * is not found * @exception Exception Will be thrown when the new protocol cannot be created, or inserted. */ public void insertProtocol(Protocol prot, Position position, String neighbor_prot) throws Exception { if(neighbor_prot == null) throw new IllegalArgumentException("neighbor_prot is null"); Protocol neighbor=findProtocol(neighbor_prot); if(neighbor == null) throw new IllegalArgumentException("protocol " + neighbor_prot + " not found in " + printProtocolSpec(false)); if(position == Position.BELOW && neighbor instanceof TP) throw new IllegalArgumentException("Cannot insert protocol " + prot.getName() + " below transport protocol"); insertProtocolInStack(prot, neighbor, position); }
/** * Makes sure that all fields annotated with @LocalAddress is (1) an InetAddress and (2) a valid address on any * local network interface * @param protocols * @throws Exception */ public static void ensureValidBindAddresses(List<Protocol> protocols) throws Exception { for(Protocol protocol : protocols) { String protocolName=protocol.getName(); //traverse class hierarchy and find all annotated fields and add them to the list if annotated Field[] fields=Util.getAllDeclaredFieldsWithAnnotations(protocol.getClass(), LocalAddress.class); for(int i=0; i < fields.length; i++) { Object val=getValueFromProtocol(protocol, fields[i]); if(val == null) continue; if(!(val instanceof InetAddress)) throw new Exception("field " + protocolName + "." + fields[i].getName() + " is not an InetAddress"); Util.checkIfValidAddress((InetAddress)val, protocolName); } } }
short id=protocol.getId(); if(id > 0 && !ids.add(id)) throw new Exception("Protocol ID " + id + " (name=" + protocol.getName() + ") is duplicate; protocol IDs have to be unique"); removeProvidedUpServices(protocol, tmp); if(!tmp.isEmpty()) throw new Exception("events " + printEvents(tmp) + " are required by " + protocol.getName() + ", but not provided by any of the protocols below it"); removeProvidedDownServices(protocol, tmp); if(!tmp.isEmpty()) throw new Exception("events " + printEvents(tmp) + " are required by " + protocol.getName() + ", but not provided by any of the protocols above it");
String name=obj instanceof Protocol? ((Protocol)obj).getName() : obj.getClass().getName(); throw new Exception("Could not assign property " + propertyName + " in " + name + ", method is " + methodName + ", converted value is " + converted, e);
String name=obj instanceof Protocol? ((Protocol)obj).getName() : obj.getClass().getName(); throw new Exception("Property assignment of " + propertyName + " in " + name + " with original property value " + propertyValue + " and converted to " + converted
/** * Resolves and applies the specified properties to this protocol. * @param properties a map of property string values * @return this protocol * @throws Exception if any of the specified properties are unresolvable or unrecognized. */ public <T extends Protocol> T setProperties(Map<String, String> properties) throws Exception { // These Configurator methods are destructive, so make a defensive copy Map<String, String> copy = new HashMap<>(properties); Configurator.removeDeprecatedProperties(this, copy); Configurator.resolveAndAssignFields(this, copy); Configurator.resolveAndInvokePropertyMethods(this, copy); List<Object> objects = this.getConfigurableObjects(); if (objects != null) { for (Object object : objects) { Configurator.removeDeprecatedProperties(object, copy); Configurator.resolveAndAssignFields(object, copy); Configurator.resolveAndInvokePropertyMethods(object, copy); } } if (!copy.isEmpty()) { throw new IllegalArgumentException(String.format("Unrecognized %s properties: %s", this.getName(), copy.keySet())); } return (T)this; }
ProtocolConfiguration protocol_config = protocol_configs.get(i) ; Protocol protocol = protocols.get(i) ; String protocolName = protocol.getName();
String protocolName=protocol.getName();
public <T extends Protocol> T removeProtocol(T prot) { if(prot == null) return null; Protocol above=prot.getUpProtocol(), below=prot.getDownProtocol(); checkAndSwitchTop(prot, below); if(above != null) above.setDownProtocol(below); if(below != null) below.setUpProtocol(above); prot.setUpProtocol(null); prot.setDownProtocol(null); try { prot.stop(); } catch(Throwable t) { log.error(Util.getMessage("FailedStopping") + prot.getName() + ": " + t); } try { prot.destroy(); } catch(Throwable t) { log.error(Util.getMessage("FailedDestroying") + prot.getName() + ": " + t); } return prot; }