示例#1
0
        private static BeanInstantiator ResolveFactoryMethod(BeanEventType beanEventType,
                                                             EngineImportService engineImportService)
        {
            string factoryMethodName = beanEventType.FactoryMethodName;

            int lastDotIndex = factoryMethodName.LastIndexOf('.');

            if (lastDotIndex == -1)
            {
                try
                {
                    MethodInfo method = engineImportService.ResolveMethod(
                        beanEventType.UnderlyingType, factoryMethodName, new Type[0], new bool[0], new bool[0]);
                    if (beanEventType.FastClass != null)
                    {
                        return(new BeanInstantiatorByFactoryFastClass(beanEventType.FastClass.GetMethod(method)));
                    }
                    else
                    {
                        return(new BeanInstantiatorByFactoryReflection(method));
                    }
                }
                catch (EngineImportException e)
                {
                    string message =
                        string.Format(
                            "Failed to resolve configured factory method '{0}' expected to exist for class '{1}'",
                            factoryMethodName, beanEventType.UnderlyingType);
                    Log.Info(message, e);
                    throw new EventBeanManufactureException(message, e);
                }
            }

            string className  = factoryMethodName.Substring(0, lastDotIndex);
            string methodName = factoryMethodName.Substring(lastDotIndex + 1);

            try
            {
                MethodInfo method = engineImportService.ResolveMethod(className, methodName, new Type[0], new bool[0], new bool[0]);
                if (beanEventType.FastClass != null)
                {
                    FastClass fastClassFactory = FastClass.Create(method.DeclaringType);
                    return(new BeanInstantiatorByFactoryFastClass(fastClassFactory.GetMethod(method)));
                }
                else
                {
                    return(new BeanInstantiatorByFactoryReflection(method));
                }
            }
            catch (EngineImportException e)
            {
                String message = "Failed to resolve configured factory method '" + methodName +
                                 "' expected to exist for class '" + className + "'";
                Log.Info(message, e);
                throw new EventBeanManufactureException(message, e);
            }
        }
示例#2
0
 private FastMethod GetFastMethod(Type clazz)
 {
     try
     {
         MethodInfo method         = _engineImportService.ResolveMethod(clazz, _methodName, _parameterTypes, new bool[_parameterTypes.Length], new bool[_parameterTypes.Length]);
         FastClass  declaringClass = FastClass.Create(method.DeclaringType);
         return(declaringClass.GetMethod(method));
     }
     catch (Exception)
     {
         Log.Debug("Not resolved for class '" + clazz.Name + "' method '" + _methodName + "'");
     }
     return(null);
 }
        private static MethodInfo GetRequiredTypeGetterMethodCanNonStatic(
            string methodName,
            string classNameWhenNoClass,
            Type clazzWhenAvailable,
            EngineImportService engineImportService,
            Type metadataClass)
        {
            MethodInfo typeGetterMethod;
            string     getterMethodName = methodName + "Metadata";

            try
            {
                if (clazzWhenAvailable != null)
                {
                    typeGetterMethod = engineImportService.ResolveMethod(
                        clazzWhenAvailable, getterMethodName, new Type[0], new bool[0], new bool[0]);
                }
                else
                {
                    typeGetterMethod = engineImportService.ResolveMethodOverloadChecked(
                        classNameWhenNoClass, getterMethodName, new Type[0], new bool[0], new bool[0]);
                }
            }
            catch (Exception)
            {
                throw new ExprValidationException(
                          "Could not find getter method for method invocation, expected a method by name '" + getterMethodName +
                          "' accepting no parameters");
            }

            bool fail;

            if (metadataClass.IsInterface)
            {
                fail = !typeGetterMethod.ReturnType.IsImplementsInterface(metadataClass);
            }
            else
            {
                fail = typeGetterMethod.ReturnType != metadataClass;
            }
            if (fail)
            {
                throw new ExprValidationException(
                          "Getter method '" + typeGetterMethod.Name + "' does not return " +
                          metadataClass.GetCleanName());
            }

            return(typeGetterMethod);
        }
 public MethodInfo ResolveMethod(string className, string methodName, Type[] paramTypes, bool[] allowEventBeanType, bool[] allowEventBeanCollType)
 {
     return(_engineImportService.ResolveMethod(className, methodName, paramTypes, allowEventBeanType, allowEventBeanCollType));
 }
示例#5
0
        /// <summary>
        /// Creates a method-invocation polling view for use as a stream that calls a method, or pulls results from cache.
        /// </summary>
        /// <param name="streamNumber">the stream number</param>
        /// <param name="methodStreamSpec">defines the class and method to call</param>
        /// <param name="eventAdapterService">for creating event types and events</param>
        /// <param name="epStatementAgentInstanceHandle">for time-based callbacks</param>
        /// <param name="engineImportService">for resolving configurations</param>
        /// <param name="schedulingService">for scheduling callbacks in expiry-time based caches</param>
        /// <param name="scheduleBucket">for schedules within the statement</param>
        /// <param name="exprEvaluatorContext">expression evaluation context</param>
        /// <param name="variableService">The variable service.</param>
        /// <param name="contextName">Name of the context.</param>
        /// <param name="dataCacheFactory">The data cache factory.</param>
        /// <param name="statementContext">The statement context.</param>
        /// <returns>
        /// pollable view
        /// </returns>
        /// <exception cref="ExprValidationException">
        /// Variable by name ' + variableMetaData.VariableName + ' has been declared for context ' + variableMetaData.ContextPartitionName + ' and can only be used within the same context
        /// or
        /// or
        /// Invalid return type for static method ' + methodFastClass.Name + ' of class ' + methodStreamSpec.ClassName + ', expecting a type
        /// </exception>
        /// <throws>ExprValidationException if the expressions cannot be validated or the method descriptorhas incorrect class and method names, or parameter number and types don't match
        /// </throws>
        public static HistoricalEventViewable CreatePollMethodView(int streamNumber, MethodStreamSpec methodStreamSpec, EventAdapterService eventAdapterService, EPStatementAgentInstanceHandle epStatementAgentInstanceHandle, EngineImportService engineImportService, SchedulingService schedulingService, ScheduleBucket scheduleBucket, ExprEvaluatorContext exprEvaluatorContext, VariableService variableService, string contextName, DataCacheFactory dataCacheFactory, StatementContext statementContext)
        {
            var variableMetaData = variableService.GetVariableMetaData(methodStreamSpec.ClassName);
            MethodPollingExecStrategyEnum strategy;
            VariableReader variableReader;
            string         variableName;

            // Try to resolve the method
            MethodInfo methodReflection;
            FastMethod methodFastClass;
            Type       declaringClass;
            object     invocationTarget;

            try
            {
                if (variableMetaData != null)
                {
                    variableName = variableMetaData.VariableName;
                    if (variableMetaData.ContextPartitionName != null)
                    {
                        if (contextName == null || !contextName.Equals(variableMetaData.ContextPartitionName))
                        {
                            throw new ExprValidationException(
                                      "Variable by name '" + variableMetaData.VariableName + "' has been declared for context '" +
                                      variableMetaData.ContextPartitionName + "' and can only be used within the same context");
                        }
                        strategy         = MethodPollingExecStrategyEnum.TARGET_VAR_CONTEXT;
                        variableReader   = null;
                        invocationTarget = null;
                    }
                    else
                    {
                        variableReader = variableService.GetReader(methodStreamSpec.ClassName, EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID);
                        if (variableMetaData.IsConstant)
                        {
                            invocationTarget = variableReader.Value;
                            if (invocationTarget is EventBean)
                            {
                                invocationTarget = ((EventBean)invocationTarget).Underlying;
                            }
                            strategy = MethodPollingExecStrategyEnum.TARGET_CONST;
                        }
                        else
                        {
                            invocationTarget = null;
                            strategy         = MethodPollingExecStrategyEnum.TARGET_VAR;
                        }
                    }
                    methodReflection = engineImportService.ResolveNonStaticMethod(
                        variableMetaData.VariableType, methodStreamSpec.MethodName);
                }
                else
                {
                    methodReflection = engineImportService.ResolveMethod(
                        methodStreamSpec.ClassName, methodStreamSpec.MethodName);
                    invocationTarget = null;
                    variableReader   = null;
                    variableName     = null;
                    strategy         = MethodPollingExecStrategyEnum.TARGET_CONST;
                }

                declaringClass  = methodReflection.DeclaringType;
                methodFastClass = FastClass.CreateMethod(methodReflection);
            }
            catch (ExprValidationException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new ExprValidationException(e.Message, e);
            }

            // Determine object type returned by method
            var beanClass = methodFastClass.ReturnType;

            if ((beanClass == typeof(void)) || (beanClass.IsBuiltinDataType()))
            {
                throw new ExprValidationException("Invalid return type for static method '" + methodFastClass.Name + "' of class '" + methodStreamSpec.ClassName + "', expecting a class");
            }

            var  isCollection    = false;
            var  isIterator      = false;
            Type collectionClass = null;
            Type iteratorClass   = null;

            if (methodFastClass.ReturnType.IsArray)
            {
                beanClass = methodFastClass.ReturnType.GetElementType();
            }
            else if (!methodFastClass.ReturnType.IsGenericStringDictionary())
            {
                isCollection = methodFastClass.ReturnType.IsGenericCollection();
                if (isCollection)
                {
                    collectionClass = beanClass.GetGenericType(0);
                    beanClass       = collectionClass;
                }

                var beanEnumerable = methodFastClass.ReturnType.FindGenericInterface(typeof(IEnumerable <>));
                if (beanEnumerable != null)
                {
                    isIterator    = true;
                    iteratorClass = beanEnumerable.GetGenericType(0);
                    beanClass     = iteratorClass;
                }
                else
                {
                    var beanEnumerator = methodFastClass.ReturnType.FindGenericInterface(typeof(IEnumerator <>));
                    if (beanEnumerator != null)
                    {
                        isIterator    = true;
                        iteratorClass = beanEnumerator.GetGenericType(0);
                        beanClass     = iteratorClass;
                    }
                }
            }

            // If the method returns a Map, look up the map type
            IDictionary <string, object> mapType = null;
            String mapTypeName = null;

            if ((methodFastClass.ReturnType.IsGenericStringDictionary()) ||
                (methodFastClass.ReturnType.IsArray && methodFastClass.ReturnType.GetElementType().IsGenericStringDictionary()) ||
                (methodFastClass.ReturnType.IsGenericCollection() && methodFastClass.ReturnType.GetGenericType(0).IsGenericStringDictionary()) ||
                (methodFastClass.ReturnType.IsGenericEnumerator() && methodFastClass.ReturnType.GetGenericType(0).IsGenericStringDictionary()) ||
                (methodFastClass.ReturnType.IsGenericEnumerable() && methodFastClass.ReturnType.GetGenericType(0).IsGenericStringDictionary()))
            {
                var metadata =
                    (variableMetaData != null) ?
                    (GetCheckMetadataVariable(methodStreamSpec.MethodName, variableMetaData, variableReader, engineImportService, typeof(IDictionary <string, object>))) :
                    (GetCheckMetadataNonVariable(methodStreamSpec.MethodName, methodStreamSpec.ClassName, engineImportService, typeof(IDictionary <string, object>)));
                mapTypeName = metadata.TypeName;
                mapType     = (IDictionary <string, object>)metadata.TypeMetadata;
            }


            // If the method returns an Object[] or Object[][], look up the type information
            IDictionary <string, object> oaType = null;
            String oaTypeName = null;

            if ((methodFastClass.ReturnType == typeof(object[])) ||
                (methodFastClass.ReturnType == typeof(object[][])) ||
                (methodFastClass.ReturnType.IsGenericCollection() && methodFastClass.ReturnType.GetGenericType(0) == typeof(object[])) ||
                (methodFastClass.ReturnType.IsGenericEnumerator() && methodFastClass.ReturnType.GetGenericType(0) == typeof(object[])) ||
                (methodFastClass.ReturnType.IsGenericEnumerable() && methodFastClass.ReturnType.GetGenericType(0) == typeof(object[])))
            {
                var metadata =
                    (variableMetaData != null) ?
                    (GetCheckMetadataVariable(methodStreamSpec.MethodName, variableMetaData, variableReader, engineImportService, typeof(IDictionary <string, object>))) :
                    (GetCheckMetadataNonVariable(methodStreamSpec.MethodName, methodStreamSpec.ClassName, engineImportService, typeof(IDictionary <string, object>)));
                oaTypeName = metadata.TypeName;
                oaType     = (IDictionary <String, Object>)metadata.TypeMetadata;
            }

            // Determine event type from class and method name
            EventType eventType;

            if (mapType != null)
            {
                eventType = eventAdapterService.AddNestableMapType(mapTypeName, mapType, null, false, true, true, false, false);
            }
            else if (oaType != null)
            {
                eventType = eventAdapterService.AddNestableObjectArrayType(oaTypeName, oaType, null, false, true, true, false, false, false, null);
            }
            else
            {
                eventType = eventAdapterService.AddBeanType(beanClass.FullName, beanClass, false, true, true);
            }

            // Construct polling strategy as a method invocation
            var configCache = engineImportService.GetConfigurationMethodRef(declaringClass.FullName);

            if (configCache == null)
            {
                configCache = engineImportService.GetConfigurationMethodRef(declaringClass.FullName);
            }

            var dataCacheDesc = (configCache != null) ? configCache.DataCacheDesc : null;
            var dataCache     = dataCacheFactory.GetDataCache(dataCacheDesc, statementContext, epStatementAgentInstanceHandle, schedulingService, scheduleBucket, streamNumber);

            PollExecStrategy methodPollStrategy;

            if (mapType != null)
            {
                if (methodFastClass.ReturnType.IsArray)
                {
                    methodPollStrategy = new MethodPollingExecStrategyMapArray(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
                else if (isCollection)
                {
                    methodPollStrategy = new MethodPollingExecStrategyMapCollection(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
                else if (isIterator)
                {
                    methodPollStrategy = new MethodPollingExecStrategyMapIterator(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
                else
                {
                    methodPollStrategy = new MethodPollingExecStrategyMapPlain(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
            }
            else if (oaType != null)
            {
                if (methodFastClass.ReturnType == typeof(object[][]))
                {
                    methodPollStrategy = new MethodPollingExecStrategyOAArray(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
                else if (isCollection)
                {
                    methodPollStrategy = new MethodPollingExecStrategyOACollection(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
                else if (isIterator)
                {
                    methodPollStrategy = new MethodPollingExecStrategyOAIterator(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
                else
                {
                    methodPollStrategy = new MethodPollingExecStrategyOAPlain(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
            }
            else
            {
                if (methodFastClass.ReturnType.IsArray)
                {
                    methodPollStrategy = new MethodPollingExecStrategyPOCOArray(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
                else if (isCollection)
                {
                    methodPollStrategy = new MethodPollingExecStrategyPOCOCollection(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
                else if (isIterator)
                {
                    methodPollStrategy = new MethodPollingExecStrategyPOCOIterator(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
                else
                {
                    methodPollStrategy = new MethodPollingExecStrategyPOCOPlain(eventAdapterService, methodFastClass, eventType, invocationTarget, strategy, variableReader, variableName, variableService);
                }
            }

            return(new MethodPollingViewable(variableMetaData == null, methodReflection.DeclaringType, methodStreamSpec, streamNumber, methodStreamSpec.Expressions, methodPollStrategy, dataCache, eventType, exprEvaluatorContext));
        }