Esempio n. 1
0
        public IList listMethods()
        {
            IList   methods = new ArrayList();
            Boolean considerExposure;

            foreach (DictionaryEntry handlerEntry in this._server)
            {
                considerExposure = XmlRpcExposedAttribute.IsExposed(handlerEntry.Value.GetType());

                foreach (MemberInfo mi in handlerEntry.Value.GetType().GetMembers())
                {
                    if (mi.MemberType != MemberTypes.Method)
                    {
                        continue;
                    }

                    if (!((MethodInfo)mi).IsPublic)
                    {
                        continue;
                    }

                    if (considerExposure && !XmlRpcExposedAttribute.IsExposed(mi))
                    {
                        continue;
                    }

                    methods.Add(string.Format("{0}.{1}", handlerEntry.Key, mi.Name));
                }
            }

            return(methods);
        }
Esempio n. 2
0
        /// <summary>Invoke a method on a given object.</summary>
        /// <remarks>Using reflection, and respecting the <c>XmlRpcExposed</c> attribute,
        /// invoke the <paramref>methodName</paramref> method on the <paramref>target</paramref>
        /// instance with the <paramref>parameters</paramref> provided. All this packages other <c>Invoke</c> methods
        /// end up calling this.</remarks>
        /// <returns><c>Object</c> the value the invoked method returns.</returns>
        /// <exception cref="XmlRpcException">If method does not exist, is not exposed, parameters invalid, or invocation
        /// results in an exception. Note, the <c>XmlRpcException.Code</c> will indicate cause.</exception>
        static public Object Invoke(Object target, String methodName, IList parameters)
        {
            if (target == null)
            {
                throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD,
                                          string.Format("{0}: Invalid target object.", XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG));
            }

            Type       type   = target.GetType();
            MethodInfo method = type.GetMethod(methodName);

            try
            {
                if (!XmlRpcExposedAttribute.ExposedMethod(target, methodName))
                {
                    throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD,
                                              string.Format("{0}: Method {1} is not exposed.", XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG, methodName));
                }
            }
            catch (MissingMethodException me)
            {
                throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_METHOD,
                                          string.Format("{0}: {1}", XmlRpcErrorCodes.SERVER_ERROR_METHOD_MSG, me.Message));
            }

            Object[] args = new Object[parameters.Count];

            int index = 0;

            foreach (Object arg in parameters)
            {
                args[index] = arg;
                index++;
            }

            try
            {
                Object retValue = method.Invoke(target, args);
                if (retValue == null)
                {
                    throw new XmlRpcException(XmlRpcErrorCodes.APPLICATION_ERROR,
                                              string.Format("{0}: Method returned NULL.", XmlRpcErrorCodes.APPLICATION_ERROR_MSG));
                }
                return(retValue);
            }
            catch (XmlRpcException e)
            {
                throw e;
            }
            catch (ArgumentException ae)
            {
                Logger.WriteEntry(string.Format("{0}: {1}", XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG, ae.Message),
                                  LogLevel.Information);
                String call = string.Format("{0}( ", methodName);
                foreach (Object o in args)
                {
                    call += o.GetType().Name;
                    call += " ";
                }
                call += ")";
                throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_PARAMS,
                                          string.Format("{0}: Arguement type mismatch invoking {1}", XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG, call));
            }
            catch (TargetParameterCountException tpce)
            {
                Logger.WriteEntry(string.Format("{0}: {1}", XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG, tpce.Message),
                                  LogLevel.Information);
                throw new XmlRpcException(XmlRpcErrorCodes.SERVER_ERROR_PARAMS,
                                          string.Format("{0}: Arguement count mismatch invoking {1}", XmlRpcErrorCodes.SERVER_ERROR_PARAMS_MSG, methodName));
            }
            catch (TargetInvocationException tie)
            {
                throw new XmlRpcException(XmlRpcErrorCodes.APPLICATION_ERROR,
                                          string.Format("{0} Invoked method {1}: {2}", XmlRpcErrorCodes.APPLICATION_ERROR_MSG, methodName, tie.Message));
            }
        }