Пример #1
0
        //// public <T> void bindSetEntry(Class<? extends Name<Set<T>>> iface, Class<? extends T> impl) throws BindException;

        /// <summary>
        /// Binds an implementation to a named parameter of ISet entry.
        /// </summary>
        /// <param name="iface">The iface.</param>
        /// <param name="impl">The impl.</param>
        /// <returns></returns>
        /// <exception cref="BindException">BindSetEntry got an interface that resolved to  + n + ; expected a NamedParameter</exception>
        ICsInternalConfigurationBuilder ICsInternalConfigurationBuilder.BindSetEntry(Type iface, Type impl)
        {
            INode n = GetNode(iface);
            INode m = GetNode(impl);

            if (!(n is INamedParameterNode))
            {
                var ex = new BindException("BindSetEntry got an interface that resolved to " + n + "; expected a NamedParameter");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }
            Type setType = ReflectionUtilities.GetInterfaceTarget(typeof(Name <>), iface);

            // if (!ReflectionUtilities.GetRawClass(setType).Equals(typeof(ISet<>)))
            if (!ReflectionUtilities.IsGenericTypeof(typeof(ISet <>), setType))
            {
                var ex = new BindException("BindSetEntry got a NamedParameter that takes a " + setType + "; expected Set<...>");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }

            Type valType = ReflectionUtilities.GetInterfaceTarget(typeof(ISet <>), setType);

            //// if (!ReflectionUtilities.GetRawClass(valType).IsAssignableFrom(impl))
            if (!valType.IsAssignableFrom(impl))
            {
                var ex = new BindException("BindSetEntry got implementation " + impl + " that is incompatible with expected type " + valType);
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }

            BindSetEntry((INamedParameterNode)n, m);
            return(this);
        }
Пример #2
0
        public void ResolveMethodExceptionSubType()
        {
            IOException ioException = new FileNotFoundException();

            Assert.Equal("HandleIOException", resolver.ResolveMethod(ioException).Name);
            SocketException bindException = new BindException();

            Assert.Equal("HandleSocketException", resolver.ResolveMethod(bindException).Name);
        }
Пример #3
0
        public virtual void TestWrapBindException()
        {
            IOException e       = new BindException("failed");
            IOException wrapped = VerifyExceptionClass(e, typeof(BindException));

            AssertInException(wrapped, "failed");
            AssertLocalDetailsIncluded(wrapped);
            AssertNotInException(wrapped, DestPortName);
            AssertInException(wrapped, "/BindException");
        }
Пример #4
0
        //TODO
        //public  <T, U extends T> void AddParser(Class<U> clazz, Class<? extends ExternalConstructor<T>> ec) throws BindException {
        //public void AddParser<T, U, V>(GenericType<U> clazz, GenericType<V> ec)
        //    where U : T
        //    where V:  IExternalConstructor<T>
        //{

        //clazz: A, ec: ACons
        private void AddParser(Type clazz, Type ec)
        {
            ConstructorInfo c = ec.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string) }, null);

            if (c == null)
            {
                var e = new BindException("Constructor " + ReflectionUtilities.GetAssemblyQualifiedName(ec) + "(String) does not exist!");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
            }

            //c.setAccessible(true); //set as public
            parsers.Add(ReflectionUtilities.GetAssemblyQualifiedName(clazz), c);
        }
Пример #5
0
        /// <summary>
        /// Binds an implementation for a named parameter.
        /// </summary>
        /// <param name="iface">The iface.</param>
        /// <param name="impl">The impl.</param>
        /// <returns></returns>
        /// <exception cref="BindException">Type mismatch when setting named parameter  + ifaceN
        /// +  Expected NamedParameterNode</exception>
        ICsInternalConfigurationBuilder ICsInternalConfigurationBuilder.BindNamedParameter(Type iface, Type impl)
        {
            INode ifaceN = GetNode(iface);
            INode implN  = GetNode(impl);

            if (!(ifaceN is INamedParameterNode))
            {
                var ex = new BindException("Type mismatch when setting named parameter " + ifaceN
                                           + " Expected NamedParameterNode");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }
            Bind(ifaceN, implN);
            return(this);
        }
Пример #6
0
        //// public <T> void bindConstructor(Class<T> c, Class<? extends ExternalConstructor<? extends T>> v) throws BindException;

        /// <summary>
        /// Binds an external constructor.
        /// </summary>
        /// <param name="c">The c.</param>
        /// <param name="v">The v.</param>
        /// <returns></returns>
        /// <exception cref="BindException">BindConstructor got class that resolved to  + n + ; expected ClassNode</exception>
        ICsInternalConfigurationBuilder ICsInternalConfigurationBuilder.BindConstructor(Type c, Type v)
        {
            INode n = GetNode(c);
            INode m = GetNode(v);

            if (!(n is IClassNode))
            {
                var ex = new BindException("BindConstructor got class that resolved to " + n + "; expected ClassNode");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }
            if (!(m is IClassNode))
            {
                var ex = new BindException("BindConstructor got class that resolved to " + m + "; expected ClassNode");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }
            BindConstructor((IClassNode)n, (IClassNode)m);
            return(this);
        }
Пример #7
0
 public IConstructorDef GetConstructorDef(IList <IClassNode> paramTypes)
 {
     if (!IsInjectionCandidate())
     {
         var e = new BindException("Cannot @Inject non-static member/local class: "
                                   + GetFullName());
         Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
     }
     foreach (IConstructorDef c in GetAllConstructors())
     {
         if (c.TakesParameters(paramTypes))
         {
             return(c);
         }
     }
     Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new BindException("Could not find requested constructor for class " + GetFullName()), LOGGER);
     return(null); // this line would not be reached as Thrwo throws an exception
 }
Пример #8
0
        ICsInternalConfigurationBuilder ICsInternalConfigurationBuilder.BindList(Type iface, IList <string> implList)
        {
            INode n = GetNode(iface);

            if (!(n is INamedParameterNode))
            {
                var ex = new BindException("BindList got an interface that resolved to " + n + "; expected a NamedParameter");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }

            Type listType = ReflectionUtilities.GetInterfaceTarget(typeof(Name <>), iface);

            if (!ReflectionUtilities.IsGenericTypeof(typeof(IList <>), listType))
            {
                var ex = new BindException("BindList got a NamedParameter that takes a " + listType + "; expected List<...>");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }

            BindList((INamedParameterNode)n, implList);
            return(this);
        }
Пример #9
0
        //// public <T> void bindSetEntry(Class<? extends Name<Set<T>>> iface, String value) throws BindException;

        /// <summary>
        /// Binds a string value to to a named parameter of ISet entry
        /// </summary>
        /// <param name="iface">The iface.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        /// <exception cref="BindException">BindSetEntry got an interface that resolved to  + n + ; expected a NamedParameter</exception>
        ICsInternalConfigurationBuilder ICsInternalConfigurationBuilder.BindSetEntry(Type iface, string value)
        {
            INode n = GetNode(iface);

            if (!(n is INamedParameterNode))
            {
                var ex = new BindException("BindSetEntry got an interface that resolved to " + n + "; expected a NamedParameter");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }
            Type setType = ReflectionUtilities.GetInterfaceTarget(typeof(Name <>), iface);

            // check if setType is ISet
            if (ReflectionUtilities.GetInterfaceTarget(typeof(ISet <>), setType) == null)
            {
                var ex = new BindException("BindSetEntry got a NamedParameter that takes a " + setType + "; expected Set<...>");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }
            //// Type valType = ReflectionUtilities.getInterfaceTarget(Set.class, setType);
            BindSetEntry((INamedParameterNode)n, value);
            return(this);
        }
Пример #10
0
        /// <summary>
        /// Binds a string to a named parameter.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        /// <exception cref="BindException">Detected type mismatch when setting named parameter  + name
        /// + Expected NamedParameterNode, but namespace contains a  + np</exception>
        public ICsConfigurationBuilder BindNamedParameter(Type name, string value)
        {
            if (value == null)
            {
                var ex = new IllegalStateException(string.Format(CultureInfo.CurrentCulture, "The value null set to the named parameter {0} is illegal.", name));
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }
            INode np = GetNode(name);

            if (np is INamedParameterNode)
            {
                BindParameter((INamedParameterNode)np, value);
            }
            else
            {
                var ex = new BindException(
                    "Detected type mismatch when setting named parameter " + name
                    + "  Expected NamedParameterNode, but namespace contains a " + np);
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }
            return(this);
        }
Пример #11
0
        public ICsConfigurationBuilder BindList(Type iface, IList <Type> implList)
        {
            INode         n      = GetNode(iface);
            IList <INode> result = new List <INode>();

            if (!(n is INamedParameterNode))
            {
                var ex = new BindException("BindList got an interface that resolved to " + n + "; expected a NamedParameter");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }

            Type listType = ReflectionUtilities.GetInterfaceTarget(typeof(Name <>), iface);

            if (!ReflectionUtilities.IsGenericTypeof(typeof(IList <>), listType))
            {
                var ex = new BindException("BindList got a NamedParameter that takes a " + listType + "; expected List<...>");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }

            if (implList != null && implList.Count > 0)
            {
                Type valType = ReflectionUtilities.GetInterfaceTarget(typeof(IList <>), listType);

                foreach (var item in implList)
                {
                    if (!valType.IsAssignableFrom((Type)item))
                    {
                        var ex =
                            new BindException("BindList got implementation " + item +
                                              " that is incompatible with expected type " + valType);
                        Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                    }
                    result.Add(GetNode(item));
                }
            }

            BindList((INamedParameterNode)n, result);
            return(this);
        }
Пример #12
0
        /// <summary>
        /// Binds the class impl as the implementation of the interface iface
        /// </summary>
        /// <param name="iface">The iface.</param>
        /// <param name="impl">The impl.</param>
        /// <returns></returns>
        public ICsConfigurationBuilder BindImplementation(Type iface, Type impl)
        {
            INode cn = GetNode(iface);
            INode dn = GetNode(impl);

            if (!(cn is IClassNode))
            {
                var ex = new BindException(
                    "bindImplementation passed interface that resolved to " + cn
                    + " expected a ClassNode<?>");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }
            if (!(dn is IClassNode))
            {
                var ex = new BindException(
                    "bindImplementation passed implementation that resolved to " + dn
                    + " expected a ClassNode<?>");
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
            }
            BindImplementation((IClassNode)cn, (IClassNode)dn);
            return(this);
        }
Пример #13
0
 internal static void ProcessConfigData(IConfigurationBuilder conf, IList <KeyValuePair <string, string> > settings)
 {
     foreach (KeyValuePair <string, string> kv in settings)
     {
         try
         {
             conf.Bind(kv.Key, kv.Value);
         }
         catch (BindException ex)
         {
             Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER);
             var e = new BindException("Failed to process configuration tuple: [" + kv.Key + "=" + kv.Value + "]", ex);
             Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
         }
         catch (ClassHierarchyException ex)
         {
             Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER);
             var e = new ClassHierarchyException("Failed to process configuration tuple: [" + kv.Key + "=" + kv.Value + "]", ex);
             Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
         }
     }
 }
Пример #14
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);
            }
        }
Пример #15
0
        public IConfiguration Build()
        {
            ConfigurationModule c = DeepCopy();

            if (!c.reqSet.ContainsAll(c.Builder.ReqDecl))
            {
                ISet <FieldInfo> missingSet = new MonotonicHashSet <FieldInfo>();
                foreach (FieldInfo f in c.Builder.ReqDecl)
                {
                    if (!c.reqSet.Contains(f))
                    {
                        missingSet.Add(f);
                    }
                }
                var e = new BindException(
                    "Attempt to build configuration before setting required option(s): "
                    + Builder.ToString(missingSet));
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
            }

            foreach (Type clazz in c.Builder.FreeImpls.Keys)
            {
                object i = c.Builder.FreeImpls.Get(clazz);
                if (c.setImpls.ContainsKey(i))
                {
                    var cb = (ICsInternalConfigurationBuilder)c.Builder.B;
                    cb.Bind(clazz, c.setImpls.Get(i));
                }
                else if (c.setLateImpls.ContainsKey(i))
                {
                    c.Builder.B.Bind(ReflectionUtilities.GetAssemblyQualifiedName(clazz), c.setLateImpls.Get(i));
                }
                else if (c.setImplSets.ContainsKey(i) || c.setLateImplSets.ContainsKey(i))
                {
                    foreach (Type clz in c.setImplSets.GetValuesForKey(i))
                    {
                        ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B;
                        b.BindSetEntry(clazz, clz);
                    }
                    foreach (string s in c.setLateImplSets.GetValuesForKey(i))
                    {
                        ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B;
                        b.BindSetEntry(clazz, s);
                    }
                }
                else if (c.setImplLists.ContainsKey(i))
                {
                    ICsConfigurationBuilder b = (ICsConfigurationBuilder)c.Builder.B;
                    b.BindList(clazz, setImplLists.Get(i));
                }
                else if (c.setLateImplLists.ContainsKey(i))
                {
                    ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B;
                    b.BindList(clazz, setLateImplLists.Get(i));
                }
            }

            foreach (Type clazz in c.Builder.FreeParams.Keys)
            {
                object p        = c.Builder.FreeParams.Get(clazz);
                string s        = c.setParams.Get(p);
                bool   foundOne = false;
                if (s != null)
                {
                    ICsConfigurationBuilder cb = c.Builder.B;
                    cb.BindNamedParameter(clazz, s);
                    foundOne = true;
                }

                IList <string> paramListStr = c.setParamLists.Get(p);
                if (paramListStr != null)
                {
                    ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B;
                    b.BindList(clazz, paramListStr);
                    foundOne = true;
                }

                foreach (string paramStr in c.setParamSets.GetValuesForKey(p))
                {
                    ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B;
                    b.BindSetEntry(clazz, paramStr);
                    foundOne = true;
                }

                if (!foundOne)
                {
                    if (!ReflectionUtilities.IsInstanceOfGeneric(p, typeof(OptionalParameter <>)))
                    {
                        Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException(), LOGGER);
                    }
                }
            }
            return(c.Builder.B.Build());
        }