Exemplo n.º 1
0
        public void BindParameter(INamedParameterNode name, string value)
        {
            /* Parse and discard value; this is just for type checking, skip for now*/
            if (this.ClassHierarchy is ICsClassHierarchy)
            {
                ((ICsClassHierarchy)ClassHierarchy).Parse(name, value);
            }

            if (name.IsSet())
            {
                BindSetEntry((INamedParameterNode)name, value);
            }
            else
            {
                try
                {
                    NamedParameters.Add(name, value);
                }
                catch (ArgumentException e)
                {
                    var msg = string.Format(CultureInfo.InvariantCulture, DuplicatedEntryForNamedParamater + "try to bind [{0}] to [{1}], but the configuration has been set for it.", value, name.GetFullName());
                    Utilities.Diagnostics.Exceptions.Throw(new ArgumentException(msg + e.Message), LOGGER);
                }
            }
        }
        private static ClassHierarchyProto.Node SerializeNode(INode n)
        {
            IList <ClassHierarchyProto.Node> children = new List <ClassHierarchyProto.Node>();

            foreach (INode child in n.GetChildren())
            {
                children.Add(SerializeNode(child));
            }


            if (n is IClassNode)
            {
                IClassNode cn = (IClassNode)n;
                IList <IConstructorDef> injectable = cn.GetInjectableConstructors();
                IList <IConstructorDef> all        = cn.GetAllConstructors();
                IList <IConstructorDef> others     = new List <IConstructorDef>(all);

                foreach (var c in injectable)
                {
                    others.Remove(c);
                }

                IList <ClassHierarchyProto.ConstructorDef> injectableConstructors = new List <ClassHierarchyProto.ConstructorDef>();
                foreach (IConstructorDef inj in injectable)
                {
                    injectableConstructors.Add(SerializeConstructorDef(inj));
                }

                IList <ClassHierarchyProto.ConstructorDef> otherConstructors = new List <ClassHierarchyProto.ConstructorDef>();
                foreach (IConstructorDef other in others)
                {
                    otherConstructors.Add(SerializeConstructorDef(other));
                }

                List <string> implFullNames = new List <string>();
                foreach (IClassNode impl in cn.GetKnownImplementations())
                {
                    implFullNames.Add(impl.GetFullName());
                }

                return(NewClassNode(cn.GetName(), cn.GetFullName(),
                                    cn.IsInjectionCandidate(), cn.IsExternalConstructor(), cn.IsUnit(),
                                    injectableConstructors, otherConstructors, implFullNames, children));
            }
            else if (n is INamedParameterNode)
            {
                INamedParameterNode np = (INamedParameterNode)n;
                return(NewNamedParameterNode(np.GetName(), np.GetFullName(),
                                             np.GetSimpleArgName(), np.GetFullArgName(), np.IsSet(), np.GetDocumentation(),
                                             np.GetShortName(), np.GetDefaultInstanceAsStrings(), children));
            }
            else if (n is IPackageNode)
            {
                return(NewPackageNode(n.GetName(), n.GetFullName(), children));
            }
            else
            {
                throw new IllegalStateException("Encountered unknown type of Node: " + n);
            }
        }
Exemplo n.º 3
0
        private object ParseBoundNamedParameter(INamedParameterNode np)
        {
            object        ret;
            ISet <Object> boundSet = this.configuration.GetBoundSet((INamedParameterNode)np);

            if (boundSet.Count != 0)
            {
                ISet <object> ret2 = new MonotonicSet <object>();
                foreach (Object o in boundSet)
                {
                    if (o is string)
                    {
                        try
                        {
                            ret2.Add(this.classHierarchy.Parse(np, (string)o));
                        }
                        catch (ParseException e)
                        {
                            throw new IllegalStateException("Could not parse " + o + " which was passed into " + np + " FIXME: Parsability is not currently checked by bindSetEntry(Node,String)");
                        }
                    }
                    else if (o is INode)
                    {
                        ret2.Add(o);
                    }
                    else
                    {
                        throw new IllegalStateException("Unexpected object " + o + " in bound set.  Should consist of nodes and strings");
                    }
                }

                return(ret2);
            }
            else if (namedParameterInstances.ContainsKey(np))
            {
                namedParameterInstances.TryGetValue(np, out ret);
            }
            else
            {
                string value = this.configuration.GetNamedParameter(np);
                if (value == null)
                {
                    ret = null;
                }
                else
                {
                    try
                    {
                        ret = this.classHierarchy.Parse(np, value);
                        namedParameterInstances.Add(np, ret);
                    }
                    catch (BindException e)
                    {
                        throw new IllegalStateException(
                                  "Could not parse pre-validated value", e);
                    }
                }
            }
            return(ret);
        }
Exemplo n.º 4
0
        public string GetNamedParameter(INamedParameterNode np)
        {
            string v = null;
            Builder.NamedParameters.TryGetValue(np, out v);

            return v;
        }
Exemplo n.º 5
0
        public IList <object> GetBoundList(INamedParameterNode np)
        {
            IList <object> list;

            Builder.BoundLists.TryGetValue(np, out list);
            return(list);
        }
Exemplo n.º 6
0
        private static InjectorImpl Copy(InjectorImpl old, IConfiguration[] configurations)
        {
            InjectorImpl i;

            try
            {
                IConfigurationBuilder cb = old.configuration.newBuilder();
                foreach (IConfiguration c in configurations)
                {
                    cb.AddConfiguration(c);
                }
                i = new InjectorImpl(cb.Build());
            }
            catch (BindException e)
            {
                throw new IllegalStateException("Unexpected error copying configuration!", e);
            }

            foreach (IClassNode cn in old.instances.Keys)
            {
                if (cn.GetFullName().Equals(ReflectionUtilities.GetFullName(typeof(IInjector))) ||
                    cn.GetFullName().Equals(ReflectionUtilities.GetFullName(typeof(InjectorImpl))))
                {
                    // This would imply that we're treating injector as a singleton somewhere.  It should be copied fresh each time.
                    throw new IllegalStateException("");
                }
                try
                {
                    IClassNode new_cn = (IClassNode)i.classHierarchy.GetNode(cn.GetFullName());

                    object o = null;
                    old.instances.TryGetValue(cn, out o);
                    if (o != null)
                    {
                        i.instances.Add(new_cn, o);
                    }
                }
                catch (BindException e)
                {
                    throw new IllegalStateException("Could not resolve name "
                                                    + cn.GetFullName() + " when copying injector", e);
                }
            }

            foreach (INamedParameterNode np in old.namedParameterInstances.Keys)
            {
                // if (!builder.namedParameters.containsKey(np)) {
                Object o = null;
                old.namedParameterInstances.TryGetValue(np, out o);
                INamedParameterNode new_np = (INamedParameterNode)i.classHierarchy.GetNode(np.GetFullName());
                i.namedParameterInstances.Add(new_np, o);
            }

            // Fork the aspect (if any)
            if (old.aspect != null)
            {
                i.BindAspect(old.aspect.CreateChildAspect());
            }
            return(i);
        }
Exemplo n.º 7
0
        public static InjectionPlan Deserialize(IClassHierarchy ch, InjectionPlanProto.InjectionPlan ip)
        {
            string fullName = ip.name;

            if (ip.constructor != null)
            {
                InjectionPlanProto.Constructor cons = ip.constructor;
                IClassNode cn = (IClassNode)ch.GetNode(fullName);

                InjectionPlanProto.InjectionPlan[] protoBufArgs = cons.args.ToArray();

                IClassNode[] cnArgs = new IClassNode[protoBufArgs.Length];

                for (int i = 0; i < protoBufArgs.Length; i++)
                {
                    INode no = ch.GetNode(protoBufArgs[i].name);
                    if (no is IClassNode)
                    {
                        cnArgs[i] = (IClassNode)no;
                    }
                    else if (no is INamedParameterNode)
                    {
                        INamedParameterNode np = (INamedParameterNode)no;
                        cnArgs[i] = (IClassNode)ch.GetNode(np.GetFullArgName());
                    }
                }

                InjectionPlan[] ipArgs = new InjectionPlan[protoBufArgs.Length];

                for (int i = 0; i < protoBufArgs.Length; i++)
                {
                    ipArgs[i] = (InjectionPlan)Deserialize(ch, protoBufArgs[i]);
                }

                IConstructorDef constructor = cn.GetConstructorDef(cnArgs);
                return(new Constructor(cn, constructor, ipArgs));
            }
            if (ip.instance != null)
            {
                InjectionPlanProto.Instance ins = ip.instance;
                object instance = Parse(ip.name, ins.value);
                return(new CsInstance(ch.GetNode(ip.name), instance));
            }
            if (ip.subplan != null)
            {
                InjectionPlanProto.Subplan         subplan       = ip.subplan;
                InjectionPlanProto.InjectionPlan[] protoBufPlans = subplan.plans.ToArray();

                InjectionPlan[] subPlans = new InjectionPlan[protoBufPlans.Length];
                for (int i = 0; i < protoBufPlans.Length; i++)
                {
                    subPlans[i] = (InjectionPlan)Deserialize(ch, protoBufPlans[i]);
                }
                INode n = ch.GetNode(fullName);
                return(new Subplan(n, subPlans));
            }
            Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Encountered unknown type of InjectionPlan: " + ip), LOGGER);
            return(null);
        }
Exemplo n.º 8
0
        public string GetNamedParameter(INamedParameterNode np)
        {
            string v = null;

            Builder.NamedParameters.TryGetValue(np, out v);

            return(v);
        }
Exemplo n.º 9
0
        public void TestNamedParameterWithAlias()
        {
            var ns = TangFactory.GetTang().GetDefaultClassHierarchy();
            INamedParameterNode cls = (INamedParameterNode)ns.GetNode(typeof(NamedParameterWithAlias).AssemblyQualifiedName);

            Assert.True(cls.GetAlias().Equals("org.apache.REEF.tang.tests.classHierarchy.NamedParameterWithAlias"));
            Assert.True(cls.GetAliasLanguage().ToString().Equals(Language.Java.ToString()));
        }
Exemplo n.º 10
0
        private static Org.Apache.REEF.Tang.Protobuf.Node SerializeNode(INode n)
        {
            IList <Org.Apache.REEF.Tang.Protobuf.Node> children = new List <Org.Apache.REEF.Tang.Protobuf.Node>();

            foreach (INode child in n.GetChildren())
            {
                children.Add(SerializeNode(child));
            }

            if (n is IClassNode)
            {
                IClassNode cn = (IClassNode)n;
                IList <IConstructorDef> injectable = cn.GetInjectableConstructors();
                IList <IConstructorDef> all        = cn.GetAllConstructors();
                IList <IConstructorDef> others     = new List <IConstructorDef>(all);

                foreach (var c in injectable)
                {
                    others.Remove(c);
                }

                IList <Org.Apache.REEF.Tang.Protobuf.ConstructorDef> injectableConstructors = new List <Org.Apache.REEF.Tang.Protobuf.ConstructorDef>();
                foreach (IConstructorDef inj in injectable)
                {
                    injectableConstructors.Add(SerializeConstructorDef(inj));
                }

                IList <Org.Apache.REEF.Tang.Protobuf.ConstructorDef> otherConstructors = new List <Org.Apache.REEF.Tang.Protobuf.ConstructorDef>();
                foreach (IConstructorDef other in others)
                {
                    otherConstructors.Add(SerializeConstructorDef(other));
                }

                List <string> implFullNames = new List <string>();
                foreach (IClassNode impl in cn.GetKnownImplementations())
                {
                    implFullNames.Add(impl.GetFullName());  // we use class fully qualifed name
                }

                return(NewClassNode(cn.GetName(), cn.GetFullName(),
                                    cn.IsInjectionCandidate(), cn.IsExternalConstructor(), cn.IsUnit(),
                                    injectableConstructors, otherConstructors, implFullNames, children));
            }
            if (n is INamedParameterNode)
            {
                INamedParameterNode np = (INamedParameterNode)n;
                return(NewNamedParameterNode(np.GetName(), np.GetFullName(),
                                             np.GetSimpleArgName(), np.GetFullArgName(), np.IsSet(), np.IsList(), np.GetDocumentation(),
                                             np.GetShortName(), np.GetDefaultInstanceAsStrings(), children, np.GetAlias(), np.GetAliasLanguage()));
            }
            if (n is IPackageNode)
            {
                return(NewPackageNode(n.GetName(), n.GetFullName(), children));
            }
            Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Encountered unknown type of Node: " + n), LOGGER);
            return(null);
        }
Exemplo n.º 11
0
        public object Parse(INamedParameterNode np, string value)
        {
            IClassNode iface;

            try
            {
                iface = (IClassNode)GetNode(np.GetFullArgName());
            }
            catch (NameResolutionException e)
            {
                throw new IllegalStateException("Could not parse validated named parameter argument type.  NamedParameter is " + np.GetFullName() + " argument type is " + np.GetFullArgName());
            }
            Type   clazz;
            String fullName;

            try
            {
                clazz    = (Type)ClassForName(iface.GetFullName());
                fullName = null;
            }
            catch (TypeLoadException e)
            {
                clazz    = null;
                fullName = iface.GetFullName();
            }
            try
            {
                if (clazz != null)
                {
                    return(parameterParser.Parse(clazz, value));
                }
                else
                {
                    return(parameterParser.Parse(fullName, value));
                }
            }
            catch (UnsupportedOperationException e)
            {
                try
                {
                    INode impl = GetNode(value);
                    if (impl is IClassNode)
                    {
                        if (IsImplementation(iface, (IClassNode)impl))
                        {
                            return(impl);
                        }
                    }
                    throw new ParseException("Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-subclass " + impl.GetFullName(), e);
                }
                catch (NameResolutionException e2)
                {
                    throw new ParseException("Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-class " + value, e);
                }
            }
        }
Exemplo n.º 12
0
        public void BindList(INamedParameterNode iface, IList <string> impl)
        {
            IList <object> l = new List <object>();

            foreach (var n in impl)
            {
                l.Add((object)n);
            }
            BoundLists.Add(iface, l);
        }
Exemplo n.º 13
0
 private object ParseElementsInCollection(INamedParameterNode np, ICollection <object> boundSet, ICollection <INode> ret3, ICollection <object> ret2)
 {
     foreach (object o in boundSet)
     {
         if (o is string)
         {
             try
             {
                 var r = this.classHierarchy.Parse(np, (string)o);
                 if (r is INode)
                 {
                     ret3.Add((INode)r);
                 }
                 else
                 {
                     ret2.Add(r);
                 }
             }
             catch (ParseException e)
             {
                 Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);
                 var ex =
                     new IllegalStateException("Could not parse " + o + " which was passed into " + np +
                                               " FIXME: Parsability is not currently checked by bindSetEntry(Node,String)");
                 Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
             }
         }
         else if (o is INode)
         {
             var o2 = o as INode;
             ret3.Add(o2);
         }
         else
         {
             var ex =
                 new IllegalStateException("Unexpected object " + o +
                                           " in bound set.  Should consist of nodes and strings");
             Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
         }
     }
     if (ret2.Count > 0 && ret3.Count == 0)
     {
         return(ret2);
     }
     if (ret3.Count > 0 && ret2.Count == 0)
     {
         return(ret3);
     }
     if (ret2.Count > 0 && ret3.Count > 0)
     {
         Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ApplicationException("Set contains different types of object"), LOGGER);
     }
     return(ret2);
 }
Exemplo n.º 14
0
        public void TestNamedParameterWithAliasRoundTrip()
        {
            var ns = TangFactory.GetTang().GetDefaultClassHierarchy();
            INamedParameterNode node1 = (INamedParameterNode)ns.GetNode(typeof(NamedParameterWithAlias).AssemblyQualifiedName);

            var ns1   = new ProtocolBufferClassHierarchy(ProtocolBufferClassHierarchy.Serialize(ns));
            var node2 = ns1.GetNode(typeof(NamedParameterWithAlias).AssemblyQualifiedName);

            Assert.True(node2 is INamedParameterNode);
            Assert.True(((INamedParameterNode)node2).GetAliasLanguage().ToString().Equals(Language.Java.ToString()));
            Assert.True(((INamedParameterNode)node2).GetFullName().Equals(typeof(NamedParameterWithAlias).AssemblyQualifiedName));
            Assert.True(((INamedParameterNode)node2).GetAlias().Equals("org.apache.REEF.tang.tests.classHierarchy.NamedParameterWithAlias"));
        }
Exemplo n.º 15
0
        private object ParseBoundNamedParameter(INamedParameterNode np)
        {
            ISet <object> boundSet = this.configuration.GetBoundSet((INamedParameterNode)np);

            if (boundSet.Count != 0)
            {
                ISet <INode>  ret3 = new MonotonicSet <INode>();
                ISet <object> ret2 = new MonotonicSet <object>();
                return(ParseElementsInCollection(np, boundSet, ret3, ret2));
            }

            IList <object> boundList = this.configuration.GetBoundList((INamedParameterNode)np);

            if (boundList != null && boundList.Count != 0)
            {
                IList <INode>  ret3 = new List <INode>();
                IList <object> ret2 = new List <object>();
                return(ParseElementsInCollection(np, boundList, ret3, ret2));
            }

            object ret = null;

            if (namedParameterInstances.ContainsKey(np))
            {
                namedParameterInstances.TryGetValue(np, out ret);
            }
            else
            {
                string value = this.configuration.GetNamedParameter(np);
                if (value == null)
                {
                    ret = null;
                }
                else
                {
                    try
                    {
                        ret = this.classHierarchy.Parse(np, value);
                        namedParameterInstances.Add(np, ret);
                    }
                    catch (BindException e)
                    {
                        Org.Apache.REEF.Utilities.Diagnostics.Exceptions.CaughtAndThrow(new IllegalStateException(
                                                                                            "Could not parse pre-validated value", e), Level.Error, LOGGER);
                    }
                }
            }
            return(ret);
        }
Exemplo n.º 16
0
        public IClassHierarchy Merge(IClassHierarchy ch)
        {
            if (this == ch)
            {
                return(this);
            }

            if (!(ch is ProtocolBufferClassHierarchy))
            {
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new NotSupportedException(
                                                                           "Cannot merge ExternalClassHierarchies yet!"), LOGGER);
            }

            ProtocolBufferClassHierarchy pch = (ProtocolBufferClassHierarchy)ch;

            foreach (var pair in pch.lookupTable)
            {
                if (!this.lookupTable.ContainsKey(pair.Key))
                {
                    this.lookupTable.Add(pair);
                }
            }

            foreach (INode n in ch.GetNamespace().GetChildren())
            {
                if (!rootNode.Contains(n.GetFullName()))
                {
                    if (n is INamedParameterNode)
                    {
                        INamedParameterNode np = (INamedParameterNode)n;
                        new NamedParameterNodeImpl(this.rootNode, np.GetName(),
                                                   np.GetFullName(), np.GetFullArgName(), np.GetSimpleArgName(),
                                                   np.IsSet(), np.IsList(), np.GetDocumentation(), np.GetShortName(),
                                                   np.GetDefaultInstanceAsStrings().ToArray());
                    }
                    else if (n is IClassNode)
                    {
                        IClassNode cn = (IClassNode)n;
                        new ClassNodeImpl(rootNode, cn.GetName(), cn.GetFullName(),
                                          cn.IsUnit(), cn.IsInjectionCandidate(),
                                          cn.IsExternalConstructor(), cn.GetInjectableConstructors(),
                                          cn.GetAllConstructors(), cn.GetDefaultImplementation());
                    }
                }
            }

            return(this);
        }
Exemplo n.º 17
0
        public void BindParameter(INamedParameterNode name, String value)
        {
            /* Parse and discard value; this is just for type checking, skip for now*/
            //if(classHierarchy is ICsClassHierarchy)
            //{
            //    ((ICsClassHierarchy)classHierarchy).Parse(name, value);
            //}

            //if(name.IsSet())
            //{
            //    BindSetEntry((INamedParameterNode)name, value);
            //}
            //else
            //{
            NamedParameters.Add(name, value);
            //}
        }
Exemplo n.º 18
0
        public void BindParameter(INamedParameterNode name, String value)
        {
            /* Parse and discard value; this is just for type checking, skip for now*/
            if (this.ClassHierarchy is ICsClassHierarchy)
            {
                ((ICsClassHierarchy)ClassHierarchy).Parse(name, value);
            }

            if (name.IsSet())
            {
                BindSetEntry((INamedParameterNode)name, value);
            }
            else
            {
                NamedParameters.Add(name, value);
            }
        }
Exemplo n.º 19
0
        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);
        }
Exemplo n.º 20
0
        public INode BuildPathToNode(Type type)
        {
            INode parent = GetParentNode(type);

            Type argType = GetNamedParameterTargetOrNull(type);

            if (argType == null)
            {
                return(NodeFactory.CreateClassNode(parent, type));
            }
            else
            {
                INamedParameterNode np = NodeFactory.CreateNamedParameterNode(parent, type, argType);

                //TODO
                //if(parameterParser.canParse(ReflectionUtilities.getFullName(argType))) {
                //    if(clazz.getAnnotation(NamedParameter.class).default_class() != Void.class) {
                //      throw new ClassHierarchyException("Named parameter " + ReflectionUtilities.getFullName(clazz) + " defines default implementation for parsable type " + ReflectionUtilities.getFullName(argType));
                //    }
                //}

                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()))
                        {
                            throw new IllegalStateException("Tried to double bind "
                                                            + oldNode.GetFullName() + " to short name " + shortName);
                        }
                        throw new ClassHierarchyException("Named parameters " + oldNode.GetFullName()
                                                          + " and " + np.GetFullName() + " have the same short name: "
                                                          + shortName);
                    }
                    shortNames.Add(shortName, np);
                }
                return(np);
            }
        }
Exemplo n.º 21
0
        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
        }
Exemplo n.º 22
0
 private void AddAlias(INamedParameterNode np)
 {
     if (!string.IsNullOrEmpty(np.GetAlias()))
     {
         IDictionary <string, string> mapping = null;
         _aliasLookupTable.TryGetValue(np.GetAliasLanguage().ToString(), out mapping);
         if (mapping == null)
         {
             mapping = new Dictionary <string, string>();
             _aliasLookupTable.Add(np.GetAliasLanguage().ToString(), mapping);
         }
         try
         {
             mapping.Add(np.GetAlias(), np.GetFullName());
         }
         catch (Exception)
         {
             var e = new ApplicationException(string.Format(CultureInfo.CurrentCulture, "Duplicated alias {0} on named parameter {1}.", np.GetAlias(), np.GetFullName()));
             Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
         }
     }
 }
Exemplo n.º 23
0
        public void TestNodeInjectAndBindVolatileInstance()
        {
            ICsClassHierarchy   classH   = TangFactory.GetTang().GetDefaultClassHierarchy();
            INamedParameterNode np       = (INamedParameterNode)classH.GetNode(typeof(ListOfClasses));
            IList <INode>       injected = new List <INode>();

            injected.Add(classH.GetNode(typeof(TestSetInjection.Integer)));
            injected.Add(classH.GetNode(typeof(TestSetInjection.Float)));

            IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindList(np, injected);

            IInjector i = TangFactory.GetTang().NewInjector(cb.Build());

            i.BindVolatileInstance(GenericType <TestSetInjection.Integer> .Class, new TestSetInjection.Integer(42));
            i.BindVolatileInstance(GenericType <TestSetInjection.Float> .Class, new TestSetInjection.Float(42.0001f));
            IList <INumber> actual = ((PoolListClass)i.GetInstance(typeof(PoolListClass))).Numbers;

            Assert.True(actual.Contains(new TestSetInjection.Integer(42)));
            Assert.True(actual.Contains(new TestSetInjection.Float(42.0001f)));
        }
Exemplo n.º 24
0
        public void TestStringInjectConfigurationBuilder()
        {
            ICsClassHierarchy   classH   = TangFactory.GetTang().GetDefaultClassHierarchy();
            INamedParameterNode np       = (INamedParameterNode)classH.GetNode(typeof(StringList));
            IList <string>      injected = new List <string>();

            injected.Add("hi");
            injected.Add("hello");
            injected.Add("bye");

            IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindList(np, injected);

            IInjector      i      = TangFactory.GetTang().NewInjector(cb.Build());
            IList <string> actual = ((StringClass)i.GetInstance(typeof(StringClass))).StringList;

            Assert.True(actual.Contains("hi"));
            Assert.True(actual.Contains("hello"));
            Assert.True(actual.Contains("bye"));
            Assert.Equal(actual.Count, 3);
        }
Exemplo n.º 25
0
 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)
         {
             throw new ClassHierarchyException("Could not parse default value", e);
         }
     }
     if (name.IsSet())
     {
         return(new HashSet <object>(ret.ToList <object>()));
     }
     else
     {
         if (ret.Length == 0)
         {
             return(null);
         }
         else if (ret.Length == 1)
         {
             return(ret[0]);
         }
         else
         {
             throw new IllegalStateException("Multiple defaults for non-set named parameter! " + name.GetFullName());
         }
     }
 }
Exemplo n.º 26
0
        // void BindVolatileParameterNoCopy(Class<? extends Name<T>> c, T o)
        void BindVolatileParameterNoCopy <U, T>(GenericType <U> c, T o)
            where U : Name <T>
        {
            INode n = this.classHierarchy.GetNode(typeof(U));

            if (n is INamedParameterNode)
            {
                INamedParameterNode np = (INamedParameterNode)n;
                object old             = this.configuration.GetNamedParameter(np);
                if (old != null)
                {
                    // XXX need to get the binding site here!
                    var ex = new BindException(
                        "Attempt to re-bind named parameter " + ReflectionUtilities.GetAssemblyQualifiedName(typeof(U)) + ".  Old value was [" + old
                        + "] new value is [" + o + "]");
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                }
                try
                {
                    namedParameterInstances.Add(np, o);
                }
                catch (ArgumentException e)
                {
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);
                    var ex = new BindException(
                        "Attempt to re-bind named parameter " + ReflectionUtilities.GetAssemblyQualifiedName(typeof(U)) + ".  Old value was [" + old
                        + "] new value is [" + o + "]");
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                }
            }
            else
            {
                var ex = new ArgumentException("Expected Name, got " + typeof(U) + " (probably a class)");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }
        }
Exemplo n.º 27
0
        public void TestIntListWithNamedParameter()
        {
            ICsClassHierarchy   classH   = TangFactory.GetTang().GetDefaultClassHierarchy();
            INamedParameterNode np       = (INamedParameterNode)classH.GetNode(typeof(IntListClass.NamedIntList));
            IList <string>      injected = new List <string>();

            injected.Add("1");
            injected.Add("2");
            injected.Add("3");

            IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindList(np, injected);

            IInjector    i = TangFactory.GetTang().NewInjector(cb.Build());
            IntListClass o = i.GetInstance <IntListClass>();

            IList <int> expected = new List <int>();

            expected.Add(1);
            expected.Add(2);
            expected.Add(3);
            o.Verify(expected);
        }
Exemplo n.º 28
0
        public void TestBoolListWithNamedParameter()
        {
            ICsClassHierarchy   classH   = TangFactory.GetTang().GetDefaultClassHierarchy();
            INamedParameterNode np       = (INamedParameterNode)classH.GetNode(typeof(BoolListClass.NamedBoolList));
            IList <string>      injected = new List <string>();

            injected.Add("true");
            injected.Add("false");
            injected.Add("true");

            IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder();

            cb.BindList(np, injected);

            IInjector     i = TangFactory.GetTang().NewInjector(cb.Build());
            BoolListClass o = i.GetInstance <BoolListClass>();

            IList <bool> expected = new List <bool>();

            expected.Add(true);
            expected.Add(false);
            expected.Add(true);
            o.Verify(expected);
        }
Exemplo n.º 29
0
 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)
         {
             throw new ClassHierarchyException("Could not parse default value", e);
         }
     }
     if (name.IsSet())
     {
         return new HashSet<object>(ret.ToList<object>());
     }
     else
     {
         if (ret.Length == 0)
         {
             return null;
         }
         else if (ret.Length == 1)
         {
             return ret[0];
         }
         else
         {
             throw new IllegalStateException("Multiple defaults for non-set named parameter! " + name.GetFullName());
         }
     }
 }
Exemplo n.º 30
0
 public object Parse(INamedParameterNode np, string value)
 {
     IClassNode iface;
     try
     {
         iface = (IClassNode)GetNode(np.GetFullArgName());
     }
     catch(NameResolutionException e)
     {
         throw new IllegalStateException("Could not parse validated named parameter argument type.  NamedParameter is " + np.GetFullName() + " argument type is " + np.GetFullArgName());
     }
     Type clazz;
     String fullName;
     try
     {
         clazz = (Type)ClassForName(iface.GetFullName());
         fullName = null;
     }
     catch(TypeLoadException e)
     {
         clazz = null;
         fullName = iface.GetFullName();
     }
     try
     {
         if(clazz != null)
         {
             return parameterParser.Parse(clazz, value);
         }
         else
         {
             return parameterParser.Parse(fullName, value);
         }
     }
     catch (UnsupportedOperationException e)
     {
         try
         {
             INode impl = GetNode(value);
             if (impl is IClassNode)
             {
                 if (IsImplementation(iface, (IClassNode)impl))
                 {
                     return impl;
                 }
             }
             throw new ParseException("Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-subclass " + impl.GetFullName(), e);
         }
         catch(NameResolutionException e2)
         {
             throw new ParseException("Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-class " + value, e);
         }
     }
 }
Exemplo n.º 31
0
        public string ClassPrettyDescriptionString(string fullName)
        {
            INamedParameterNode param = (INamedParameterNode)this.ClassHierarchy.GetNode(fullName);

            return(param.GetDocumentation() + "\n" + param.GetFullName());
        }
Exemplo n.º 32
0
        public string ClassPrettyDefaultString(string longName)
        {
            INamedParameterNode param = (INamedParameterNode)this.ClassHierarchy.GetNode(longName);

            return(param.GetSimpleArgName() + "=" + Join(",", param.GetDefaultInstanceAsStrings()));
        }
Exemplo n.º 33
0
 public ISet<Object> GetBoundSet(INamedParameterNode np)
 {
     return new HashSet<object>();
     //TODO
     //return this.Builder.BoundSetEntries.getValuesForKey(np);
 }
Exemplo n.º 34
0
        private void AddAlias(INamedParameterNode np)
        {
            if (np.GetAlias() != null && !np.GetAlias().Equals(""))
            {
                IDictionary<string, string> mapping = null;
                _aliasLookupTable.TryGetValue(np.GetAliasLanguage(), out mapping);
                if (mapping == null)
                {
                    mapping = new Dictionary<string, string>();
                    _aliasLookupTable.Add(np.GetAliasLanguage(), mapping);
                }

                try
                {
                    mapping.Add(np.GetAlias(), np.GetFullName());
                }
                catch (Exception)
                {
                    var e = new ApplicationException(string.Format(CultureInfo.CurrentCulture, "Duplicated alias {0} on named parameter {1}.", np.GetAlias(), np.GetFullName()));
                    Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
            }
        }
Exemplo n.º 35
0
        public void BindParameter(INamedParameterNode name, String value)
        {
            /* Parse and discard value; this is just for type checking, skip for now*/
            //if(classHierarchy is ICsClassHierarchy)
            //{
            //    ((ICsClassHierarchy)classHierarchy).Parse(name, value);
            //}

            //if(name.IsSet())
            //{
            //    BindSetEntry((INamedParameterNode)name, value);
            //}
            //else
            //{
                NamedParameters.Add(name, value);
            //}
        }
Exemplo n.º 36
0
        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; 
        }
Exemplo n.º 37
0
 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
 }
Exemplo n.º 38
0
 public IList<object> GetBoundList(INamedParameterNode np)
 {
     IList<object> list;
     Builder.BoundLists.TryGetValue(np, out list);
     return list;
 }
Exemplo n.º 39
0
 public void BindSetEntry(INamedParameterNode iface, INode impl)
 {
     BoundSetEntries.Add(iface, impl);
 }
Exemplo n.º 40
0
 public string GetNamedParameter(INamedParameterNode np)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 41
0
        public void BindParameter(INamedParameterNode name, string value)
        {
            /* Parse and discard value; this is just for type checking, skip for now*/
            if (this.ClassHierarchy is ICsClassHierarchy) 
            {
                ((ICsClassHierarchy)ClassHierarchy).Parse(name, value);
            }

            if (name.IsSet()) 
            {
                BindSetEntry((INamedParameterNode)name, value);
            } 
            else 
            {
                try
                {
                    NamedParameters.Add(name, value);
                }
                catch (ArgumentException e)
                {
                    var msg = string.Format(CultureInfo.InvariantCulture, DuplicatedEntryForNamedParamater + "try to bind [{0}] to [{1}], but the configuration has been set for it.", value, name.GetFullName());
                    Utilities.Diagnostics.Exceptions.Throw(new ArgumentException(msg + e.Message), LOGGER);
                }
            }
        }
Exemplo n.º 42
0
 public ISet<object> GetBoundSet(INamedParameterNode np)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 43
0
 public void BindSetEntry(INamedParameterNode iface, INode impl)
 {
     BoundSetEntries.Add(iface, impl);
 }
Exemplo n.º 44
0
        public void BindParameter(INamedParameterNode name, string value)
        {
            /* Parse and discard value; this is just for type checking, skip for now*/
            if (this.ClassHierarchy is ICsClassHierarchy) 
            {
                ((ICsClassHierarchy)ClassHierarchy).Parse(name, value);
            }

            if (name.IsSet()) 
            {
                BindSetEntry((INamedParameterNode)name, value);
            } 
            else 
            {
                NamedParameters.Add(name, value);
            }
        }
Exemplo n.º 45
0
 public void BindList(INamedParameterNode iface, IList<string> impl)
 {
     IList<object> l = new List<object>();
     foreach (var n in impl)
     {
         l.Add((object)n);
     }
     BoundLists.Add(iface, l);
 }
Exemplo n.º 46
0
        private object ParseBoundNamedParameter(INamedParameterNode np)
        {
            object ret;
            ISet<Object> boundSet = this.configuration.GetBoundSet((INamedParameterNode)np);
            if (boundSet.Count != 0)
            {
                ISet<object> ret2 = new MonotonicSet<object>();
                foreach (Object o in boundSet)
                {
                    if(o is string)
                    {
                        try
                        {
                            ret2.Add(this.classHierarchy.Parse(np, (string)o));
                        }
                        catch(ParseException e)
                        {
                            throw new IllegalStateException("Could not parse " + o + " which was passed into " + np + " FIXME: Parsability is not currently checked by bindSetEntry(Node,String)");
                        }
                    }
                    else if(o is INode)
                    {
                        ret2.Add(o);
                    }
                    else
                    {
                        throw new IllegalStateException("Unexpected object " + o + " in bound set.  Should consist of nodes and strings");
                    }
                }

                return ret2;
            }
            else if (namedParameterInstances.ContainsKey(np))
            {
                namedParameterInstances.TryGetValue(np, out ret);
            }
            else
            {
                string value = this.configuration.GetNamedParameter(np);
                if(value == null)
                {
                    ret = null;
                }
                else
                {
                    try
                    {
                        ret = this.classHierarchy.Parse(np, value);
                        namedParameterInstances.Add(np, ret);
                    }
                    catch (BindException e)
                    {
                        throw new IllegalStateException(
                            "Could not parse pre-validated value", e);
                    }
                }
            }
            return ret;
        }
Exemplo n.º 47
0
 public ISet<Object> GetBoundSet(INamedParameterNode np) 
 {
     return Builder.BoundSetEntries.GetValuesForKey(np);
 }