Пример #1
0
        public static void RegisterHandler(Type handlerType)
        {
            ExTraceGlobals.OehCallTracer.TraceDebug(0L, "OwaEventRegistry.RegisterHandler");
            if (handlerType == null)
            {
                throw new ArgumentNullException("handlerType");
            }
            object[] customAttributes = handlerType.GetCustomAttributes(typeof(OwaEventNamespaceAttribute), false);
            if (customAttributes == null || customAttributes.Length == 0)
            {
                throw new OwaNotSupportedException("Internal error: Handler is missing OwaEventNamespaceAttribute attribute");
            }
            OwaEventNamespaceAttribute owaEventNamespaceAttribute = (OwaEventNamespaceAttribute)customAttributes[0];

            owaEventNamespaceAttribute.HandlerType = handlerType;
            customAttributes = handlerType.GetCustomAttributes(typeof(OwaEventSegmentationAttribute), false);
            if (customAttributes != null && customAttributes.Length > 0)
            {
                OwaEventSegmentationAttribute owaEventSegmentationAttribute = (OwaEventSegmentationAttribute)customAttributes[0];
                owaEventNamespaceAttribute.SegmentationFlags = owaEventSegmentationAttribute.SegmentationFlags;
            }
            else
            {
                owaEventNamespaceAttribute.SegmentationFlags = 0UL;
            }
            Type objectIdType = null;

            customAttributes = handlerType.GetCustomAttributes(typeof(OwaEventObjectIdAttribute), false);
            if (customAttributes != null && customAttributes.Length > 0)
            {
                objectIdType = ((OwaEventObjectIdAttribute)customAttributes[0]).ObjectIdType;
            }
            ExTraceGlobals.OehDataTracer.TraceDebug <string>(0L, "Handler type: '{0}'", handlerType.ToString());
            foreach (MethodInfo methodInfo in handlerType.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                customAttributes = methodInfo.GetCustomAttributes(typeof(OwaEventAttribute), false);
                if (customAttributes != null && customAttributes.Length > 0)
                {
                    OwaEventAttribute owaEventAttribute = (OwaEventAttribute)customAttributes[0];
                    ParameterInfo[]   parameters        = methodInfo.GetParameters();
                    if (methodInfo.ReturnType == typeof(IAsyncResult))
                    {
                        if (parameters.Length != 2 || parameters[0].ParameterType != typeof(AsyncCallback) || parameters[1].ParameterType != typeof(object))
                        {
                            throw new OwaNotSupportedException("Wrong signature for async event handler method.");
                        }
                        owaEventAttribute.IsAsync         = true;
                        owaEventAttribute.BeginMethodInfo = methodInfo;
                    }
                    else if (methodInfo.ReturnType == typeof(void))
                    {
                        if (parameters.Length == 1 && parameters[0].ParameterType == typeof(IAsyncResult))
                        {
                            owaEventAttribute.EndMethodInfo = methodInfo;
                            owaEventAttribute.IsAsync       = true;
                        }
                        else
                        {
                            if (parameters.Length != 0)
                            {
                                throw new OwaNotSupportedException("Wrong signature for event handler method.");
                            }
                            owaEventAttribute.MethodInfo = methodInfo;
                            owaEventAttribute.IsAsync    = false;
                        }
                    }
                    if (!owaEventAttribute.IsAsync || !(null != owaEventAttribute.EndMethodInfo))
                    {
                        OwaEventRegistry.ScanHandlerAttributes(methodInfo, owaEventAttribute, objectIdType);
                    }
                    owaEventNamespaceAttribute.AddEventInfo(owaEventAttribute);
                }
            }
            foreach (object obj in owaEventNamespaceAttribute.EventInfoTable.Values)
            {
                OwaEventAttribute owaEventAttribute2 = obj as OwaEventAttribute;
                if (owaEventAttribute2.MethodInfo != null)
                {
                    if (owaEventAttribute2.BeginMethodInfo != null || owaEventAttribute2.EndMethodInfo != null)
                    {
                        throw new OwaNotSupportedException("Namespace defines the same event both sync and async");
                    }
                }
                else if (owaEventAttribute2.BeginMethodInfo != null)
                {
                    if (owaEventAttribute2.EndMethodInfo == null)
                    {
                        throw new OwaNotSupportedException(string.Format("Begin async method {0} for event {1} is missing its corresponding End method", owaEventAttribute2.BeginMethodInfo.Name, owaEventAttribute2.Name));
                    }
                }
                else if (owaEventAttribute2.EndMethodInfo != null && owaEventAttribute2.BeginMethodInfo == null)
                {
                    throw new OwaNotSupportedException(string.Format("End async method {0} for event {1} is missing its corresponding Begin method", owaEventAttribute2.EndMethodInfo.Name, owaEventAttribute2.Name));
                }
            }
            OwaEventRegistry.handlerTable.Add(owaEventNamespaceAttribute.Name, owaEventNamespaceAttribute);
        }
Пример #2
0
        private static void ScanHandlerAttributes(MethodInfo method, OwaEventAttribute eventInfo, Type objectIdType)
        {
            object[] customAttributes = method.GetCustomAttributes(typeof(OwaEventVerbAttribute), false);
            if (customAttributes != null && customAttributes.Length > 0)
            {
                OwaEventVerbAttribute owaEventVerbAttribute = (OwaEventVerbAttribute)customAttributes[0];
                eventInfo.AllowedVerbs = owaEventVerbAttribute.Verb;
            }
            else
            {
                eventInfo.AllowedVerbs = OwaEventVerb.Post;
            }
            customAttributes = method.GetCustomAttributes(typeof(OwaEventSegmentationAttribute), false);
            if (customAttributes != null && customAttributes.Length > 0)
            {
                OwaEventSegmentationAttribute owaEventSegmentationAttribute = (OwaEventSegmentationAttribute)customAttributes[0];
                eventInfo.SegmentationFlags = owaEventSegmentationAttribute.SegmentationFlags;
            }
            else
            {
                eventInfo.SegmentationFlags = 0UL;
            }
            ExTraceGlobals.OehDataTracer.TraceDebug <string, OwaEventVerb>(0L, "Event handler found. Name: '{0}'. Allowed verbs: '{1}'.", eventInfo.Name, eventInfo.AllowedVerbs);
            ulong num  = 0UL;
            int   num2 = 0;

            if (Globals.CanaryProtectionRequired)
            {
                eventInfo.AddParameterInfo(new OwaEventParameterAttribute("canary", typeof(string)));
            }
            customAttributes = method.GetCustomAttributes(typeof(OwaEventParameterAttribute), false);
            if (customAttributes != null && customAttributes.Length > 0)
            {
                if (customAttributes.Length >= 64)
                {
                    throw new OwaNotSupportedException("Event handler declares more parameters than allowed");
                }
                if ((eventInfo.AllowedVerbs & OwaEventVerb.Get) != OwaEventVerb.Unsupported && customAttributes.Length > 16)
                {
                    throw new OwaNotSupportedException("Event handler declares more parameters than allowed for a GET request");
                }
                foreach (OwaEventParameterAttribute owaEventParameterAttribute in customAttributes)
                {
                    if (objectIdType != null && owaEventParameterAttribute.Type == typeof(ObjectId))
                    {
                        owaEventParameterAttribute = new OwaEventParameterAttribute(owaEventParameterAttribute.Name, objectIdType, owaEventParameterAttribute.IsArray, owaEventParameterAttribute.IsOptional);
                    }
                    if (!eventInfo.IsInternal && !OwaEventRegistry.IsAllowedType(owaEventParameterAttribute.Type))
                    {
                        string message = string.Format("Event handler is using a type that is not supported method: '{0}' param type '{1}'", method.Name, owaEventParameterAttribute.Type);
                        throw new OwaNotSupportedException(message);
                    }
                    if (string.Equals(owaEventParameterAttribute.Name, "ns", StringComparison.Ordinal) || string.Equals(owaEventParameterAttribute.Name, "ev", StringComparison.Ordinal))
                    {
                        throw new OwaNotSupportedException("Handler is trying to use a reserve name for a parameter");
                    }
                    if (OwaEventRegistry.structTypeTable[owaEventParameterAttribute.Type] != null)
                    {
                        owaEventParameterAttribute.IsStruct = true;
                    }
                    owaEventParameterAttribute.ParameterMask = 1UL << num2;
                    if (!owaEventParameterAttribute.IsOptional)
                    {
                        num |= owaEventParameterAttribute.ParameterMask;
                    }
                    eventInfo.AddParameterInfo(owaEventParameterAttribute);
                    num2++;
                    ExTraceGlobals.OehDataTracer.TraceDebug(0L, "Event handler parameter found, name: '{0}', type: '{1}', isArray: '{2}', isOptional: '{3}', isStruct: '{4}'", new object[]
                    {
                        owaEventParameterAttribute.Name,
                        owaEventParameterAttribute.Type,
                        owaEventParameterAttribute.IsArray,
                        owaEventParameterAttribute.IsOptional,
                        owaEventParameterAttribute.IsStruct
                    });
                }
            }
            eventInfo.RequiredMask = num;
        }