Beispiel #1
0
 public DataFlowOpInitializeContext(
     IContainer container,
     string dataFlowName,
     string operatorName,
     int operatorNumber,
     AgentInstanceContext agentInstanceContext,
     IDictionary<string, object> additionalParameters,
     string dataFlowInstanceId,
     EPDataFlowOperatorParameterProvider parameterProvider,
     DataFlowOperatorFactory dataFlowOperatorFactory,
     object dataflowInstanceUserObject)
 {
     Container = container;
     DataFlowName = dataFlowName;
     OperatorName = operatorName;
     OperatorNumber = operatorNumber;
     AgentInstanceContext = agentInstanceContext;
     AdditionalParameters = additionalParameters;
     DataFlowInstanceId = dataFlowInstanceId;
     ParameterProvider = parameterProvider;
     DataFlowOperatorFactory = dataFlowOperatorFactory;
     DataflowInstanceUserObject = dataflowInstanceUserObject;
 }
Beispiel #2
0
        public static void PopulateObject(String operatorName,
                                          int operatorNum,
                                          String dataFlowName,
                                          IDictionary <String, Object> objectProperties,
                                          Object top,
                                          EngineImportService engineImportService,
                                          EPDataFlowOperatorParameterProvider optionalParameterProvider,
                                          IDictionary <String, Object> optionalParameterURIs)
        {
            var applicableClass  = top.GetType();
            var writables        = PropertyHelper.GetWritableProperties(applicableClass);
            var annotatedFields  = TypeHelper.FindAnnotatedFields(applicableClass, typeof(DataFlowOpParameterAttribute));
            var annotatedMethods = TypeHelper.FindAnnotatedMethods(applicableClass, typeof(DataFlowOpParameterAttribute));

            // find catch-all methods
            var catchAllMethods = new LinkedHashSet <MethodInfo>();

            if (annotatedMethods != null)
            {
                foreach (var method in annotatedMethods)
                {
                    var anno = (DataFlowOpParameterAttribute)TypeHelper.GetAnnotations(
                        typeof(DataFlowOpParameterAttribute),
                        method.GetCustomAttributes(true).Cast <Attribute>().ToArray())[0];
                    if (anno.All)
                    {
                        var parameterTypes = method.GetParameterTypes();
                        if ((parameterTypes.Length == 2) &&
                            (parameterTypes[0] == typeof(String)) &&
                            (parameterTypes[1] == typeof(Object)))
                        {
                            catchAllMethods.Add(method);
                            continue;
                        }
                        throw new ExprValidationException("Invalid annotation for catch-call");
                    }
                }
            }

            // map provided values
            foreach (var property in objectProperties)
            {
                var found        = false;
                var propertyName = property.Key;

                // invoke catch-all setters
                foreach (var method in catchAllMethods)
                {
                    try {
                        method.Invoke(top, new Object[] { propertyName, property.Value });
                    }
                    catch (MemberAccessException e)
                    {
                        throw new ExprValidationException("Illegal access invoking method for property '" + propertyName + "' for class " + applicableClass.Name + " method " + method.Name, e);
                    }
                    catch (TargetInvocationException e)
                    {
                        throw new ExprValidationException("Exception invoking method for property '" + propertyName + "' for class " + applicableClass.Name + " method " + method.Name + ": " + e.InnerException.Message, e);
                    }
                    found = true;
                }

                if (propertyName.ToLower() == CLASS_PROPERTY_NAME)
                {
                    continue;
                }

                // use the writeable property descriptor (appropriate setter method) from writing the property
                var descriptor = FindDescriptor(applicableClass, propertyName, writables);
                if (descriptor != null)
                {
                    var coerceProperty = CoerceProperty(propertyName, applicableClass, property.Value, descriptor.PropertyType, engineImportService, false, true);

                    try {
                        descriptor.WriteMethod.Invoke(top, new Object[] { coerceProperty });
                    }
                    catch (ArgumentException e) {
                        throw new ExprValidationException("Illegal argument invoking setter method for property '" + propertyName + "' for class " + applicableClass.Name + " method " + descriptor.WriteMethod.Name + " provided value " + coerceProperty, e);
                    }
                    catch (MemberAccessException e) {
                        throw new ExprValidationException("Illegal access invoking setter method for property '" + propertyName + "' for class " + applicableClass.Name + " method " + descriptor.WriteMethod.Name, e);
                    }
                    catch (TargetInvocationException e) {
                        throw new ExprValidationException("Exception invoking setter method for property '" + propertyName + "' for class " + applicableClass.Name + " method " + descriptor.WriteMethod.Name + ": " + e.InnerException.Message, e);
                    }
                    continue;
                }

                // in .NET, it's common to name fields with an underscore prefix, this modified
                // notation is preserved in the modPropertyName
                var modPropertyName = "_" + propertyName;
                // find the field annotated with <seealso cref="GraphOpProperty" />
                foreach (var annotatedField in annotatedFields)
                {
                    var anno = (DataFlowOpParameterAttribute)TypeHelper.GetAnnotations(
                        typeof(DataFlowOpParameterAttribute),
                        annotatedField.GetCustomAttributes(true).Cast <Attribute>().ToArray())[0];
                    if ((anno.Name == propertyName) || (annotatedField.Name == propertyName) || (annotatedField.Name == modPropertyName))
                    {
                        var coerceProperty = CoerceProperty(
                            propertyName, applicableClass, property.Value, annotatedField.FieldType, engineImportService,
                            true, true);
                        try
                        {
                            annotatedField.SetValue(top, coerceProperty);
                        }
                        catch (Exception e)
                        {
                            throw new ExprValidationException(
                                      "Failed to set field '" + annotatedField.Name + "': " + e.Message, e);
                        }
                        found = true;
                        break;
                    }
                }

                if (found)
                {
                    continue;
                }

                throw new ExprValidationException("Failed to find writable property '" + propertyName + "' for class " + applicableClass);
            }

            // second pass: if a parameter URI - value pairs were provided, check that
            if (optionalParameterURIs != null)
            {
                foreach (var annotatedField in annotatedFields)
                {
                    try {
                        var uri = operatorName + "/" + annotatedField.Name;
                        if (optionalParameterURIs.ContainsKey(uri))
                        {
                            var value = optionalParameterURIs.Get(uri);
                            annotatedField.SetValue(top, value);
                            if (Log.IsDebugEnabled)
                            {
                                Log.Debug("Found parameter '" + uri + "' for data flow " + dataFlowName + " setting " + value);
                            }
                        }
                        else
                        {
                            if (Log.IsDebugEnabled)
                            {
                                Log.Debug("Not found parameter '" + uri + "' for data flow " + dataFlowName);
                            }
                        }
                    }
                    catch (Exception e) {
                        throw new ExprValidationException("Failed to set field '" + annotatedField.Name + "': " + e.Message, e);
                    }
                }

                foreach (var method in annotatedMethods)
                {
                    //var anno = (DataFlowOpParameterAttribute) TypeHelper.GetAnnotations<DataFlowOpParameterAttribute>(method.GetCustomAttributes(false))[0];

                    var anno = method.GetCustomAttributes(typeof(DataFlowOpParameterAttribute), false)
                               .Cast <DataFlowOpParameterAttribute>()
                               .First();

                    if (anno.All)
                    {
                        var parameterTypes = method.GetParameterTypes();
                        if (parameterTypes.Length == 2 && parameterTypes[0] == typeof(string) && parameterTypes[1] == typeof(object))
                        {
                            foreach (var entry in optionalParameterURIs)
                            {
                                var uri      = new Uri(entry.Key, UriKind.RelativeOrAbsolute);
                                var elements = URIUtil.ParsePathElements(uri);
                                if (elements.Length < 2)
                                {
                                    throw new ExprValidationException(string.Format("Failed to parse URI '{0}', expected 'operator_name/property_name' format", entry.Key));
                                }
                                if (elements[0] == operatorName)
                                {
                                    try
                                    {
                                        method.Invoke(top, new Object[] { elements[1], entry.Value });
                                    }
                                    catch (ArgumentException e)
                                    {
                                        throw new ExprValidationException("Illegal argument invoking setter method for property '" + entry.Key + "' for class " + applicableClass.Name + " method " + method.Name, e);
                                    }
                                    catch (MemberAccessException e)
                                    {
                                        throw new ExprValidationException("Illegal access invoking setter method for property '" + entry.Key + "' for class " + applicableClass.Name + " method " + method.Name, e);
                                    }
                                    catch (TargetInvocationException e)
                                    {
                                        throw new ExprValidationException("Exception invoking setter method for property '" + entry.Key + "' for class " + applicableClass.Name + " method " + method.Name + ": " + e.InnerException.Message, e);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // third pass: if a parameter provider is provided, use that
            if (optionalParameterProvider != null)
            {
                foreach (var annotatedField in annotatedFields)
                {
                    try {
                        var provided = annotatedField.GetValue(top);
                        var value    = optionalParameterProvider.Provide(new EPDataFlowOperatorParameterProviderContext(operatorName, annotatedField.Name, top, operatorNum, provided, dataFlowName));
                        if ((value == null) && (annotatedField.Name.StartsWith("_")))
                        {
                            value = optionalParameterProvider.Provide(new EPDataFlowOperatorParameterProviderContext(operatorName, annotatedField.Name.Substring(1), top, operatorNum, provided, dataFlowName));
                        }

                        if (value != null)
                        {
                            annotatedField.SetValue(top, value);
                        }
                    }
                    catch (Exception e) {
                        throw new ExprValidationException("Failed to set field '" + annotatedField.Name + "': " + e.Message, e);
                    }
                }
            }
        }
Beispiel #3
0
        public static void PopulateObject(
            string operatorName,
            int operatorNum,
            string dataFlowName,
            IDictionary<string, object> objectProperties,
            object top,
            ExprNodeOrigin exprNodeOrigin,
            ExprValidationContext exprValidationContext,
            EPDataFlowOperatorParameterProvider optionalParameterProvider,
            IDictionary<string, object> optionalParameterURIs)
        {
            var applicableClass = top.GetType();
            var writables = PropertyHelper.GetWritableProperties(applicableClass);
            var annotatedFields =
                TypeHelper.FindAnnotatedFields(top.GetType(), typeof(DataFlowOpParameterAttribute));
            var annotatedMethods =
                TypeHelper.FindAnnotatedMethods(top.GetType(), typeof(DataFlowOpParameterAttribute));

            // find catch-all methods
            ISet<MethodInfo> catchAllMethods = new LinkedHashSet<MethodInfo>();
            if (annotatedMethods != null) {
                foreach (var method in annotatedMethods) {
                    var anno = (DataFlowOpParameterAttribute) TypeHelper.GetAnnotations(
                        typeof(DataFlowOpParameterAttribute),
                        method.UnwrapAttributes())[0];
                    if (anno.IsAll) {
                        var parameterTypes = method.GetParameterTypes();
                        if (parameterTypes.Length == 2 &&
                            parameterTypes[0] == typeof(string) &&
                            parameterTypes[1] == typeof(ExprNode)) {
                            catchAllMethods.Add(method);
                            continue;
                        }

                        throw new ExprValidationException("Invalid annotation for catch-call");
                    }
                }
            }

            // map provided values
            foreach (var property in objectProperties) {
                var found = false;
                var propertyName = property.Key;

                // invoke catch-all setters
                foreach (var method in catchAllMethods) {
                    try {
                        method.Invoke(top, new[] {propertyName, property.Value});
                    }
                    catch (MemberAccessException e) {
                        throw new ExprValidationException(
                            "Illegal access invoking method for property '" +
                            propertyName +
                            "' for class " +
                            applicableClass.Name +
                            " method " +
                            method.Name,
                            e);
                    }
                    catch (TargetException e) {
                        throw new ExprValidationException(
                            "Exception invoking method for property '" +
                            propertyName +
                            "' for class " +
                            applicableClass.Name +
                            " method " +
                            method.Name +
                            ": " +
                            e.InnerException.Message,
                            e);
                    }

                    found = true;
                }

                if (propertyName.ToLowerInvariant().Equals(CLASS_PROPERTY_NAME)) {
                    continue;
                }

                // use the writeable property descriptor (appropriate setter method) from writing the property
                var descriptor = FindDescriptor(applicableClass, propertyName, writables);
                if (descriptor != null) {
                    var coerceProperty = CoerceProperty(
                        propertyName,
                        applicableClass,
                        property.Value,
                        descriptor.PropertyType,
                        exprNodeOrigin,
                        exprValidationContext,
                        false,
                        true);

                    try {
                        var writeMember = descriptor.WriteMember;
                        if (writeMember is MethodInfo writeMethod) {
                            writeMethod.Invoke(top, new[] {coerceProperty});
                        }
                        else if (writeMember is PropertyInfo writeProperty) {
                            writeProperty.SetValue(top, coerceProperty);
                        }
                        else {
                            throw new IllegalStateException("writeMember of invalid type");
                        }
                    }
                    catch (ArgumentException e) {
                        throw new ExprValidationException(
                            "Illegal argument invoking setter method for property '" +
                            propertyName +
                            "' for class " +
                            applicableClass.Name +
                            " method " +
                            descriptor.WriteMember.Name +
                            " provided value " +
                            coerceProperty,
                            e);
                    }
                    catch (MemberAccessException e) {
                        throw new ExprValidationException(
                            "Illegal access invoking setter method for property '" +
                            propertyName +
                            "' for class " +
                            applicableClass.Name +
                            " method " +
                            descriptor.WriteMember.Name,
                            e);
                    }
                    catch (TargetException e) {
                        throw new ExprValidationException(
                            "Exception invoking setter method for property '" +
                            propertyName +
                            "' for class " +
                            applicableClass.Name +
                            " method " +
                            descriptor.WriteMember.Name +
                            ": " +
                            e.InnerException.Message,
                            e);
                    }

                    continue;
                }

                // find the field annotated with {@link @GraphOpProperty}
                foreach (var annotatedField in annotatedFields) {
                    var anno = (DataFlowOpParameterAttribute) TypeHelper.GetAnnotations<DataFlowOpParameterAttribute>(
                        annotatedField.UnwrapAttributes())[0];
                    if (anno.Name.Equals(propertyName) || annotatedField.Name.Equals(propertyName)) {
                        var coerceProperty = CoerceProperty(
                            propertyName,
                            applicableClass,
                            property.Value,
                            annotatedField.FieldType,
                            exprNodeOrigin,
                            exprValidationContext,
                            true,
                            true);
                        try {
                            annotatedField.SetValue(top, coerceProperty);
                        }
                        catch (Exception e) {
                            throw new ExprValidationException(
                                "Failed to set field '" + annotatedField.Name + "': " + e.Message,
                                e);
                        }

                        found = true;
                        break;
                    }
                }

                if (found) {
                    continue;
                }

                throw new ExprValidationException(
                    "Failed to find writable property '" + propertyName + "' for class " + applicableClass.Name);
            }

            // second pass: if a parameter URI - value pairs were provided, check that
            if (optionalParameterURIs != null) {
                foreach (var annotatedField in annotatedFields) {
                    try {
                        var uri = operatorName + "/" + annotatedField.Name;
                        if (optionalParameterURIs.ContainsKey(uri)) {
                            var value = optionalParameterURIs.Get(uri);
                            annotatedField.SetValue(top, value);
                            if (Log.IsDebugEnabled) {
                                Log.Debug(
                                    "Found parameter '" +
                                    uri +
                                    "' for data flow " +
                                    dataFlowName +
                                    " setting " +
                                    value);
                            }
                        }
                        else {
                            if (Log.IsDebugEnabled) {
                                Log.Debug("Not found parameter '" + uri + "' for data flow " + dataFlowName);
                            }
                        }
                    }
                    catch (Exception e) {
                        throw new ExprValidationException(
                            "Failed to set field '" + annotatedField.Name + "': " + e.Message,
                            e);
                    }
                }

                foreach (var method in annotatedMethods) {
                    var anno = (DataFlowOpParameterAttribute) TypeHelper.GetAnnotations(
                        typeof(DataFlowOpParameterAttribute),
                        method.UnwrapAttributes())[0];
                    if (anno.IsAll) {
                        var parameters = method.GetParameters();

                        var parameterTypes = method.GetParameterTypes();
                        if (parameterTypes.Length == 2 &&
                            parameterTypes[0] == typeof(string) &&
                            parameterTypes[1] == typeof(object)) {
                            foreach (var entry in optionalParameterURIs) {
                                var elements = URIUtil.ParsePathElements(new Uri(entry.Key));
                                if (elements.Length < 2) {
                                    throw new ExprValidationException(
                                        "Failed to parse URI '" +
                                        entry.Key +
                                        "', expected " +
                                        "'operator_name/property_name' format");
                                }

                                if (elements[0].Equals(operatorName)) {
                                    try {
                                        method.Invoke(top, new[] {elements[1], entry.Value});
                                    }
                                    catch (MemberAccessException e) {
                                        throw new ExprValidationException(
                                            "Illegal access invoking method for property '" +
                                            entry.Key +
                                            "' for class " +
                                            applicableClass.Name +
                                            " method " +
                                            method.Name,
                                            e);
                                    }
                                    catch (TargetException e) {
                                        throw new ExprValidationException(
                                            "Exception invoking method for property '" +
                                            entry.Key +
                                            "' for class " +
                                            applicableClass.Name +
                                            " method " +
                                            method.Name +
                                            ": " +
                                            e.InnerException.Message,
                                            e);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #4
0
 /// <summary>Sets the parameter provider. </summary>
 /// <param name="parameterProvider">parameter provider</param>
 public void SetParameterProvider(EPDataFlowOperatorParameterProvider parameterProvider)
 {
     _parameterProvider = parameterProvider;
 }
Beispiel #5
0
 /// <summary>Sets the parameter provider. </summary>
 /// <param name="parameterProvider">parameter provider</param>
 /// <returns>this options object</returns>
 public EPDataFlowInstantiationOptions ParameterProvider(EPDataFlowOperatorParameterProvider parameterProvider)
 {
     _parameterProvider = parameterProvider;
     return(this);
 }