private void AddConfiguration(IClassHierarchy ns, ConfigurationBuilderImpl builder) { this.ClassHierarchy = this.ClassHierarchy.Merge(ns); if (ClassHierarchy is ClassHierarchyImpl || builder.ClassHierarchy is ClassHierarchyImpl) { if (ClassHierarchy is ClassHierarchyImpl && builder.ClassHierarchy is ClassHierarchyImpl) { ((ClassHierarchyImpl)ClassHierarchy).Parameterparser.MergeIn(((ClassHierarchyImpl)builder.ClassHierarchy).Parameterparser); } else { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException("Attempt to merge Java and non-Java class hierarchy! Not supported."), LOGGER); } } foreach (IClassNode cn in builder.BoundImpls.Keys) { IClassNode n = null; builder.BoundImpls.TryGetValue(cn, out n); if (n != null) { Bind(cn.GetFullName(), n.GetFullName()); } } foreach (IClassNode cn in builder.BoundConstructors.Keys) { IClassNode n = null; builder.BoundConstructors.TryGetValue(cn, out n); if (n != null) { Bind(cn.GetFullName(), n.GetFullName()); } } // The namedParameters set contains the strings that can be used to // instantiate new // named parameter instances. Create new ones where we can. foreach (INamedParameterNode np in builder.NamedParameters.Keys) { string v = null; builder.NamedParameters.TryGetValue(np, out v); Bind(np.GetFullName(), v); } foreach (IClassNode cn in builder.LegacyConstructors.Keys) { IConstructorDef cd = null; builder.LegacyConstructors.TryGetValue(cn, out cd); RegisterLegacyConstructor(cn, cd.GetArgs()); } foreach (KeyValuePair<INamedParameterNode, object> e in builder.BoundSetEntries) { string name = ((INamedParameterNode)e.Key).GetFullName(); if (e.Value is INode) { BindSetEntry(name, (INode)e.Value); } else if (e.Value is string) { BindSetEntry(name, (string)e.Value); } else { var ex = new IllegalStateException(string.Format(CultureInfo.CurrentCulture, "The value {0} set to the named parameter {1} is illegel.", e.Value, name)); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } } foreach (var p in builder.BoundLists) { BoundLists.Add(p.Key, p.Value); } }
private void WireUpInheritanceRelationships(AvroNode n) { if (n.classNode != null && !n.classNode.Equals("")) { AvroClassNode cn = (AvroClassNode)n.classNode; IClassNode iface = null; try { iface = (IClassNode)GetNode(n.fullName); } catch (NameResolutionException e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new IllegalStateException("When reading protocol buffer node " + n.fullName + " does not exist. Full record is " + n, e); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } foreach (string impl in cn.implFullNames) { try { iface.PutImpl((IClassNode)GetNode(impl)); } catch (NameResolutionException e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new IllegalStateException("When reading protocol buffer node " + n + " refers to non-existent implementation:" + impl); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } catch (InvalidCastException e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); try { var ex = new IllegalStateException( "When reading protocol buffer node " + n + " found implementation" + GetNode(impl) + " which is not a ClassNode!"); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } catch (NameResolutionException ne) { Utilities.Diagnostics.Exceptions.Caught(ne, Level.Error, LOGGER); var ex = new IllegalStateException( "Got 'cant happen' exception when producing error message for " + e); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } } } } }
private void BindNode(INode n, string value) { if (n is INamedParameterNode) { BindParameter((INamedParameterNode)n, value); } else if (n is IClassNode) { INode m = this.ClassHierarchy.GetNode(value); Bind((IClassNode)n, (IClassNode)m); } else { var ex = new IllegalStateException(string.Format("getNode() returned {0} which is neither a ClassNode nor a NamedParameterNode", n)); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } }
public object ParseDefaultValue(INamedParameterNode name) { string[] vals = name.GetDefaultInstanceAsStrings(); object[] ret = new object[vals.Length]; for (int i = 0; i < vals.Length; i++) { string val = vals[i]; try { ret[i] = Parse(name, val); } catch (ParseException e) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new ClassHierarchyException("Could not parse default value " + val, e); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } } if (name.IsSet()) { return new HashSet<object>(ret.ToList<object>()); } if (name.IsList()) { return new List<object>(ret.ToList<object>()); } if (ret.Length == 0) { return null; } if (ret.Length == 1) { return ret[0]; } var ec = new IllegalStateException("Multiple defaults for non-set named parameter! " + name.GetFullName()); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ec, LOGGER); return null; // this line would be never reached as Throw will throw an exception }
public object Parse(INamedParameterNode np, string value) { IClassNode iface = null; try { iface = (IClassNode)GetNode(np.GetFullArgName()); } catch (NameResolutionException e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new IllegalStateException("Could not parse validated named parameter argument type. NamedParameter is " + np.GetFullName() + " argument type is " + np.GetFullArgName(), e); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } Type clazz; string fullName; try { clazz = (Type)ClassForName(iface.GetFullName()); fullName = null; } catch (TypeLoadException e) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Warning, LOGGER); clazz = null; fullName = iface.GetFullName(); } object result = null; if (clazz != null) { result = Parameterparser.Parse(clazz, value); } else { result = Parameterparser.Parse(fullName, value); } if (result == null) { try { INode impl = GetNode(value); if (impl is IClassNode) { if (IsImplementation(iface, (IClassNode)impl)) { return impl; } } var ex = new ParseException( "Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-subclass " + impl.GetFullName()); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } catch (NameResolutionException ec) { Utilities.Diagnostics.Exceptions.Caught(ec, Level.Error, LOGGER); var ex = new ParseException( "Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-class " + value, ec); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } } return result; }
public INode BuildPathToNode(Type type) { INode parent = GetParentNode(type); Type argType = ReflectionUtilities.GetNamedParameterTargetOrNull(type); if (argType == null) { return NodeFactory.CreateClassNode(parent, type); } INamedParameterNode np = NodeFactory.CreateNamedParameterNode(parent, type, argType); if (Parameterparser.CanParse(ReflectionUtilities.GetAssemblyQualifiedName(argType))) { if (type.GetCustomAttribute<NamedParameterAttribute>().DefaultClass != null) { var e = new ClassHierarchyException("Named parameter " + ReflectionUtilities.GetAssemblyQualifiedName(type) + " defines default implementation for parsable type " + ReflectionUtilities.GetAssemblyQualifiedName(argType)); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } } if (!string.IsNullOrEmpty(np.GetAlias())) { IDictionary<string, string> mapping = null; _aliasLookupTable.TryGetValue(np.GetAliasLanguage().ToString(), out mapping); if (null == mapping) { mapping = new Dictionary<string, string>(); _aliasLookupTable.Add(np.GetAliasLanguage().ToString(), mapping); } try { mapping.Add(np.GetAlias(), np.GetFullName()); } catch (Exception) { var e = new TangApplicationException(string.Format(CultureInfo.CurrentCulture, "Duplicated alias {0} on named parameter {1}.", np.GetAlias(), np.GetFullName())); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } } string shortName = np.GetShortName(); if (shortName != null && !shortName.Equals(string.Empty)) { INamedParameterNode oldNode = null; shortNames.TryGetValue(shortName, out oldNode); if (oldNode != null) { if (oldNode.GetFullName().Equals(np.GetFullName())) { var ex = new IllegalStateException("Tried to double bind " + oldNode.GetFullName() + " to short name " + shortName); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } var e = new ClassHierarchyException("Named parameters " + oldNode.GetFullName() + " and " + np.GetFullName() + " have the same short name: " + shortName); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } shortNames.Add(shortName, np); } return np; }
public INode BuildPathToNode(Type type) { INode parent = GetParentNode(type); Type argType = ReflectionUtilities.GetNamedParameterTargetOrNull(type); if (argType == null) { return NodeFactory.CreateClassNode(parent, type); } INamedParameterNode np = NodeFactory.CreateNamedParameterNode(parent, type, argType); if(Parameterparser.CanParse(ReflectionUtilities.GetAssemblyQualifiedName(argType))) { if(type.GetCustomAttribute<NamedParameterAttribute>().DefaultClass != null) { var e = new ClassHierarchyException("Named parameter " + ReflectionUtilities.GetAssemblyQualifiedName(type) + " defines default implementation for parsable type " + ReflectionUtilities.GetAssemblyQualifiedName(argType)); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } } string shortName = np.GetShortName(); if (shortName != null && !shortName.Equals("")) { INamedParameterNode oldNode = null; shortNames.TryGetValue(shortName, out oldNode); if (oldNode != null) { if (oldNode.GetFullName().Equals(np.GetFullName())) { var ex = new IllegalStateException("Tried to double bind " + oldNode.GetFullName() + " to short name " + shortName); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } var e = new ClassHierarchyException("Named parameters " + oldNode.GetFullName() + " and " + np.GetFullName() + " have the same short name: " + shortName); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } shortNames.Add(shortName, np); } return np; }