コード例 #1
0
 /// <summary>
 /// Creates a new instance of the <see cref="Spring.Objects.MutablePropertyValues"/>
 /// class.
 /// </summary>
 /// <remarks>
 /// <p>
 /// Deep copy constructor. Guarantees <see cref="Spring.Objects.PropertyValue"/>
 /// references are independent, although it can't deep copy objects currently
 /// referenced by individual <see cref="Spring.Objects.PropertyValue"/> objects.
 /// </p>
 /// </remarks>
 public MutablePropertyValues(IPropertyValues other)
 {
     if (other != null)
     {
         AddAll(other.PropertyValues);
     }
 }
コード例 #2
0
        public void ChangesSince()
        {
            IDictionary <string, object> map = new Dictionary <string, object>();
            PropertyValue propName           = new PropertyValue("Name", "Fiona Apple");

            map.Add(propName.Name, propName.Value);
            map.Add("Age", 24);
            MutablePropertyValues props    = new MutablePropertyValues(map);
            MutablePropertyValues newProps = new MutablePropertyValues(map);

            // change the name... this is the change we'll be looking for
            newProps.SetPropertyValueAt(new PropertyValue(propName.Name, "Naomi Woolf"), 0);
            IPropertyValues changes = newProps.ChangesSince(props);

            Assert.AreEqual(1, changes.PropertyValues.Count);
            // the name was changed, so its the name property that should be in the changed list
            Assert.IsTrue(changes.Contains("name"));

            newProps.Add(new PropertyValue("Commentator", "Naomi Woolf"));
            changes = newProps.ChangesSince(props);
            Assert.AreEqual(2, changes.PropertyValues.Count);
            // the Commentator was added, so its the Commentator property that should be in the changed list
            Assert.IsTrue(changes.Contains("commentator"));
            // the name was changed, so its the name property that should be in the changed list
            Assert.IsTrue(changes.Contains("name"));
        }
コード例 #3
0
        /// <summary>
        /// Return the difference (changes, additions, but not removals) of
        /// property values between the supplied argument and the values
        /// contained in the collection.
        /// </summary>
        /// <param name="old">Another property values collection.</param>
        /// <returns>
        /// The collection of property values that are different than the supplied one.
        /// </returns>
        public IPropertyValues ChangesSince(IPropertyValues old)
        {
            MutablePropertyValues changes = new MutablePropertyValues();

            if (old == this)
            {
                return(changes);
            }
            // for each property value in this (the newer set)
            foreach (PropertyValue newProperty in propertyValuesList)
            {
                PropertyValue oldProperty = old.GetPropertyValue(newProperty.Name);
                if (oldProperty == null)
                {
                    // if there wasn't an old one, add it
                    changes.Add(newProperty);
                }
                else if (!oldProperty.Equals(newProperty))
                {
                    // it's changed
                    changes.Add(newProperty);
                }
            }
            return(changes);
        }
コード例 #4
0
 /// <summary>
 /// Inject values for members into object instance
 /// </summary>
 /// <param name="instance"></param>
 /// <param name="objectName"></param>
 /// <param name="pvs"></param>
 public void Inject(object instance, string objectName, IPropertyValues pvs)
 {
     for (var i = 0; i < _injectedElements.Count; i++)
     {
         var element = _injectedElements[i];
         Logger.Debug(m => m("Processing injected method of bean '{0}': {1}", objectName, element));
         element.Inject(instance, objectName, pvs);
     }
 }
コード例 #5
0
            public override void Inject(Object instance, String objectName, IPropertyValues pvs)
            {
                var field = (FieldInfo)_member;

                try
                {
                    Object value;
                    if (_cached)
                    {
                        value = ResolvedCachedArgument(objectName, _cachedFieldValue);
                    }
                    else
                    {
                        var   descriptor           = new DependencyDescriptor(field, _required);
                        IList autowiredObjectNames = new ArrayList();
                        value = _objectFactory.ResolveDependency(descriptor, objectName, autowiredObjectNames);
                        lock (this)
                        {
                            if (!_cached)
                            {
                                if (value != null || _required)
                                {
                                    _cachedFieldValue = descriptor;
                                    RegisterDependentObjects(objectName, autowiredObjectNames);
                                    if (autowiredObjectNames.Count == 1)
                                    {
                                        var autowiredBeanName = autowiredObjectNames[0] as string;
                                        if (_objectFactory.ContainsObject(autowiredBeanName))
                                        {
                                            if (_objectFactory.IsTypeMatch(autowiredBeanName, field.GetType()))
                                            {
                                                _cachedFieldValue = new RuntimeObjectReference(autowiredBeanName);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    _cachedFieldValue = null;
                                }
                                _cached = true;
                            }
                        }
                    }
                    if (value != null)
                    {
                        field.SetValue(instance, value);
                    }
                }
                catch (Exception ex)
                {
                    throw new ObjectCreationException("Could not autowire field: " + field, ex);
                }
            }
コード例 #6
0
	    /// <summary>
	    /// Inject values for members into object instance
	    /// </summary>
	    /// <param name="instance"></param>
	    /// <param name="objectName"></param>
	    /// <param name="pvs"></param>
	    public void Inject(Object instance, string objectName, IPropertyValues pvs)
	    {
	        if (_injectedElements.Count == 0)
                return;

	        foreach(var element in _injectedElements)
	        {
	            Logger.Debug(m => m("Processing injected method of bean '{0}': {1}", objectName, element));
	            element.Inject(instance, objectName, pvs);
	        }
	    }
コード例 #7
0
        /// <summary>
        /// Inject values for members into object instance
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="objectName"></param>
        /// <param name="pvs"></param>
        public void Inject(Object instance, string objectName, IPropertyValues pvs)
        {
            if (_injectedElements.Count == 0)
            {
                return;
            }

            foreach (var element in _injectedElements)
            {
                Logger.Debug(m => m("Processing injected method of bean '{0}': {1}", objectName, element));
                element.Inject(instance, objectName, pvs);
            }
        }
コード例 #8
0
        public void ChangesSinceWithSelf()
        {
            IDictionary <string, object> map = new Dictionary <string, object>();

            map.Add("Name", "Fiona Apple");
            map.Add("Age", 24);
            MutablePropertyValues props = new MutablePropertyValues(map);

            props.Remove("name");
            // get all of the changes between self and self again (there should be none);
            IPropertyValues changes = props.ChangesSince(props);

            Assert.AreEqual(0, changes.PropertyValues.Count);
        }
コード例 #9
0
        /// <summary>
        /// Perform a bulk update with full control over behavior.
        /// </summary>
        /// <remarks>
        /// <p>
        /// This method may throw a reflection-based exception, if there is a critical
        /// failure such as no matching field... less serious exceptions will be accumulated
        /// and thrown as a single <see cref="Spring.Objects.PropertyAccessExceptionsException"/>.
        /// </p>
        /// </remarks>
        /// <param name="propertyValues">
        /// The <see cref="Spring.Objects.PropertyValue"/>s to set on the target object.
        /// </param>
        /// <param name="ignoreUnknown">
        /// Should we ignore unknown values (not found in the object!?).
        /// </param>
        /// <exception cref="NotWritablePropertyException">
        /// If an error is encountered while setting a property (only thrown if the
        /// <paramref name="ignoreUnknown"/> parameter is set to <see langword="false"/>).
        /// </exception>
        /// <exception cref="Spring.Objects.PropertyAccessExceptionsException">
        /// On a <see cref="System.Type"/> mismatch while setting a property, insufficient permissions, etc.
        /// </exception>
        /// <seealso cref="Spring.Objects.IObjectWrapper.SetPropertyValues(IPropertyValues, bool)"/>
        public virtual void SetPropertyValues(IPropertyValues propertyValues, bool ignoreUnknown)
        {
            ArrayList propertyAccessExceptions = new ArrayList();

            foreach (PropertyValue pv in propertyValues)
            {
                try
                {
                    SetPropertyValue(pv);
                }
                catch (NotWritablePropertyException ex)
                {
                    if (!ignoreUnknown)
                    {
                        Log.Error(string.Format("Failed setting property '{0}'", pv.Name), ex);
                        throw;
                    }
                }
                catch (InvalidPropertyException ex)
                {
                    if (!ignoreUnknown)
                    {
                        Log.Error(string.Format("Failed setting property '{0}'", pv.Name), ex);
                        throw;
                    }
                }
                catch (TypeMismatchException ex) // otherwise, just ignore it and continue...
                {
                    Log.Error(string.Format("Failed setting property '{0}'", pv.Name), ex);
                    propertyAccessExceptions.Add(ex);
                }
                catch (MethodInvocationException ex)
                {
                    Log.Error(string.Format("Failed setting property '{0}'", pv.Name), ex);
                    propertyAccessExceptions.Add(ex);
                }
                catch (Exception ex)
                {
                    Log.Error(string.Format("Failed setting property '{0}' on instance of type '{1}'", pv.Name, this.WrappedType.FullName), ex);
                    throw;
                }
            }

            // if we encountered individual exceptions, throw the composite exception...
            if (propertyAccessExceptions.Count > 0)
            {
                throw new PropertyAccessExceptionsException(this,
                                                            (PropertyAccessException[])propertyAccessExceptions.ToArray(typeof(PropertyAccessException)));
            }
        }
コード例 #10
0
        /// <summary>
        /// Injects autoried annotated properties, fields, methods into objectInstance
        /// </summary>
        /// <param name="pvs"></param>
        /// <param name="pis"></param>
        /// <param name="objectInstance"></param>
        /// <param name="objectName">Name of the object.</param>
        /// <returns>The actual property values to apply to the given object (can be the
        /// passed-in PropertyValues instances0 or null to skip property population.</returns>
        public override IPropertyValues PostProcessPropertyValues(IPropertyValues pvs, IList <PropertyInfo> pis,
                                                                  object objectInstance, string objectName)
        {
            var metadata = FindAutowiringMetadata(objectInstance.GetType());

            try
            {
                metadata.Inject(objectInstance, objectName, pvs);
            }
            catch (Exception ex)
            {
                throw new ObjectCreationException(objectName, "Injection of autowired dependencies failed", ex);
            }
            return(pvs);
        }
コード例 #11
0
        public override IPropertyValues PostProcessPropertyValues(IPropertyValues propertyValues, PropertyInfo[] propertyInfos, object objectInstance, string objectName)
        {
            MutablePropertyValues mpv = new MutablePropertyValues(propertyValues);

            IConfigurableObjectDefinition objectDefinition = (IConfigurableObjectDefinition)_objectFactory.GetObjectDefinition(objectName);
            DependencyCheckingMode checkMode = objectDefinition.DependencyCheck;
            PropertyInfo[] unresolvedProperties = AutowireUtils.GetUnsatisfiedDependencies(propertyInfos, propertyValues, checkMode);
            foreach(PropertyInfo propertyInfo in unresolvedProperties)
            {
                object value = ResolveDependency(objectName, objectDefinition, new ObjectWrapper(objectInstance), propertyInfo);
                if (value != null)
                {
                    mpv.Add(propertyInfo.Name, value);
                }
            }

            return base.PostProcessPropertyValues(mpv, propertyInfos, objectInstance, objectName);
        }
コード例 #12
0
        /// <summary>
        /// Post-process the given property values before the factory applies them
        /// to the given object.  Checks for the attribute specified by this PostProcessor's RequiredAttributeType.
        /// </summary>
        /// <param name="pvs">The property values that the factory is about to apply (never <code>null</code>).</param>
        /// <param name="pis">The relevant property infos for the target object (with ignored
        /// dependency types - which the factory handles specifically - already filtered out)</param>
        /// <param name="objectInstance">The object instance created, but whose properties have not yet
        /// been set.</param>
        /// <param name="objectName">Name of the object.</param>
        /// <returns>
        /// The actual property values to apply to the given object (can be the
        /// passed-in PropertyValues instances or null to skip property population.
        /// </returns>
        /// <exception cref="ObjectInitializationException">If a required property value has not been specified
        /// in the configuration metadata.</exception>
        public override IPropertyValues PostProcessPropertyValues(IPropertyValues pvs, IList <PropertyInfo> pis, object objectInstance, string objectName)
        {
            if (!validatedObjectNames.Contains(objectName))
            {
                List <string> invalidProperties = new List <string>();

                foreach (PropertyInfo pi in pis)
                {
                    if (IsRequiredProperty(pi) && !pvs.Contains(pi.Name))
                    {
                        invalidProperties.Add(pi.Name);
                    }
                }
                if (invalidProperties.Count != 0)
                {
                    throw new ObjectInitializationException(
                              BuildExceptionMessage(invalidProperties, objectName));
                }
                validatedObjectNames.Add(objectName);
            }
            return(pvs);
        }
コード例 #13
0
        /// <summary>
        /// Post-process the given property values before the factory applies them
        /// to the given object.  Checks for the attribute specified by this PostProcessor's RequiredAttributeType.
        /// </summary>
        /// <param name="pvs">The property values that the factory is about to apply (never <code>null</code>).</param>
        /// <param name="pis">The relevant property infos for the target object (with ignored
        /// dependency types - which the factory handles specifically - already filtered out)</param>
        /// <param name="objectInstance">The object instance created, but whose properties have not yet
        /// been set.</param>
        /// <param name="objectName">Name of the object.</param>
        /// <returns>
        /// The actual property values to apply to the given object (can be the
        /// passed-in PropertyValues instances or null to skip property population.
        /// </returns>
        /// <exception cref="ObjectInitializationException">If a required property value has not been specified
        /// in the configuration metadata.</exception>
        public override IPropertyValues PostProcessPropertyValues(IPropertyValues pvs, PropertyInfo[] pis, object objectInstance,
                                                                  string objectName)
        {
            if (!validatedObjectNames.Contains(objectName))
            {
                ArrayList invalidProperties = new ArrayList();

                foreach (PropertyInfo pi in pis)
                {
                    if (IsRequiredProperty(pi) && !pvs.Contains(pi.Name))
                    {
                        invalidProperties.Add(pi.Name);
                    }
                }
                if (invalidProperties.Count != 0)
                {
                    throw new ObjectInitializationException(
                              BuildExceptionMessage((string[])invalidProperties.ToArray(typeof(string)), objectName));
                }
                validatedObjectNames.Add(objectName);
            }
            return(pvs);
        }
コード例 #14
0
 public EntityEventArgs(EntityState state, IPropertyValues originalValues, IPropertyValues currentValues)
 {
     State          = state;
     CurrentValues  = currentValues;
     OriginalValues = originalValues;
 }
コード例 #15
0
 /// <summary>
 /// Default behavior, return passed in PropertyValues
 /// </summary>
 /// <param name="pvs">The property values that the factory is about to apply (never <code>null</code>).</param>
 /// <param name="pis">he relevant property infos for the target object (with ignored
 /// dependency types - which the factory handles specifically - already filtered out)</param>
 /// <param name="objectInstance">The object instance created, but whose properties have not yet
 /// been set.</param>
 /// <param name="objectName">Name of the object.</param>
 /// <returns>The passed in PropertyValues</returns>
 public IPropertyValues PostProcessPropertyValues(IPropertyValues pvs, PropertyInfo[] pis, object objectInstance,
                                                  string objectName)
 {
     return(pvs);
 }
コード例 #16
0
ファイル: ObjectWrapper.cs プロジェクト: fgq841103/spring-net
        /// <summary>
        /// Perform a bulk update with full control over behavior.
        /// </summary>
        /// <remarks>
        /// <p>
        /// This method may throw a reflection-based exception, if there is a critical
        /// failure such as no matching field... less serious exceptions will be accumulated
        /// and thrown as a single <see cref="Spring.Objects.PropertyAccessExceptionsException"/>.
        /// </p>
        /// </remarks>
        /// <param name="propertyValues">
        /// The <see cref="Spring.Objects.PropertyValue"/>s to set on the target object.
        /// </param>
        /// <param name="ignoreUnknown">
        /// Should we ignore unknown values (not found in the object!?).
        /// </param>
        /// <exception cref="NotWritablePropertyException">
        /// If an error is encountered while setting a property (only thrown if the
        /// <paramref name="ignoreUnknown"/> parameter is set to <see langword="false"/>).
        /// </exception>
        /// <exception cref="Spring.Objects.PropertyAccessExceptionsException">
        /// On a <see cref="System.Type"/> mismatch while setting a property, insufficient permissions, etc.
        /// </exception>
        /// <seealso cref="Spring.Objects.IObjectWrapper.SetPropertyValues(IPropertyValues, bool)"/>
        public virtual void SetPropertyValues(IPropertyValues propertyValues, bool ignoreUnknown)
        {
            List<PropertyAccessException> propertyAccessExceptions = new List<PropertyAccessException>();
            foreach (PropertyValue pv in propertyValues)
            {
                try
                {
                    SetPropertyValue(pv);
                }
                catch (NotWritablePropertyException ex)
                {
                    if (!ignoreUnknown)
                    {
                        Log.Error(string.Format("Failed setting property '{0}'", pv.Name), ex);
                        throw;
                    }
                }
                catch (InvalidPropertyException ex)
                {
                    if (!ignoreUnknown)
                    {
                        Log.Error(string.Format("Failed setting property '{0}'", pv.Name), ex);
                        throw;
                    }
                }
                catch (TypeMismatchException ex) // otherwise, just ignore it and continue...
                {
                    Log.Error(string.Format("Failed setting property '{0}'", pv.Name), ex);
                    propertyAccessExceptions.Add(ex);
                }
                catch (MethodInvocationException ex)
                {
                    Log.Error(string.Format("Failed setting property '{0}'", pv.Name), ex);
                    propertyAccessExceptions.Add(ex);
                }
                catch (Exception ex)
                {
                    Log.Error(string.Format("Failed setting property '{0}' on instance of type '{1}'", pv.Name, this.WrappedType.FullName), ex);
                    throw;
                }
            }

            // if we encountered individual exceptions, throw the composite exception...
            if (propertyAccessExceptions.Count > 0)
            {
                throw new PropertyAccessExceptionsException(this, propertyAccessExceptions.ToArray());
            }
        }
コード例 #17
0
 /// <summary>
 /// Post-process the given property values before the factory applies them
 /// to the given object.
 /// </summary>
 /// <remarks>Allows for checking whether all dependencies have been
 /// satisfied, for example based on a "Required" annotation on bean property setters.
 /// <para>Also allows for replacing the property values to apply, typically through
 /// creating a new MutablePropertyValues instance based on the original PropertyValues,
 /// adding or removing specific values.
 /// </para>
 /// </remarks>
 /// <param name="pvs">The property values that the factory is about to apply (never <code>null</code>).</param>
 /// <param name="pis">he relevant property infos for the target object (with ignored
 /// dependency types - which the factory handles specifically - already filtered out)</param>
 /// <param name="objectInstance">The object instance created, but whose properties have not yet
 /// been set.</param>
 /// <param name="objectName">Name of the object.</param>
 /// <returns>The actual property values to apply to the given object (can be the
 /// passed-in PropertyValues instances0 or null to skip property population.</returns>
 public virtual IPropertyValues PostProcessPropertyValues(IPropertyValues pvs, IList <PropertyInfo> pis, object objectInstance, string objectName)
 {
     return(pvs);
 }
コード例 #18
0
 public IPropertyValues PostProcessPropertyValues(IPropertyValues pvs, IList<PropertyInfo> pis, object objectInstance, string objectName)
 {
     return pvs;
 }
コード例 #19
0
 public EntityEventArgs(EntityState state, IPropertyValues originalValues, IPropertyValues currentValues)
 {
     State = state;
     CurrentValues = currentValues;
     OriginalValues = originalValues;
 }
コード例 #20
0
        public override IPropertyValues PostProcessPropertyValues(IPropertyValues pvs, PropertyInfo[] pis, object objectInstance, string objectName)
        {
            foreach(var pi in pis)
            {
                if(IsAutowiredProperty(pi))
                {
                    var att = (AutowiredAttribute)pi.GetCustomAttributes(typeof(AutowiredAttribute), true)[0];
                    IDictionary dic = ApplicationContext.GetObjectsOfType(pi.PropertyType);
                    if(dic.Keys.Count == 0)
                    {

                        if(att.Required == false)
                        {
                            continue;
                        }
                    }
                    IDictionaryEnumerator enumerator = dic.GetEnumerator();
                    enumerator.MoveNext();
                    object autowireObject = enumerator.Value;
                    MutablePropertyValues mutablePvs = (MutablePropertyValues) pvs;
                    if (mutablePvs.GetPropertyValue(pi.Name) == null)
                        mutablePvs.Add(pi.Name, autowireObject);
                }
            }
            return pvs;
        }
コード例 #21
0
 public ProjectFilePropertyExpressionParser(IPropertyValues props)
 {
     Props = props;
 }
 /// <summary>
 /// Injects autoried annotated properties, fields, methods into objectInstance
 /// </summary>
 /// <param name="pvs"></param>
 /// <param name="pis"></param>
 /// <param name="objectInstance"></param>
 /// <param name="objectName">Name of the object.</param>
 /// <returns>The actual property values to apply to the given object (can be the 
 /// passed-in PropertyValues instances0 or null to skip property population.</returns>
 public override IPropertyValues PostProcessPropertyValues(IPropertyValues pvs, IList<PropertyInfo> pis,
                                                           object objectInstance, string objectName)
 {
     var metadata = FindAutowiringMetadata(objectInstance.GetType());
     try
     {
         metadata.Inject(objectInstance, objectName, pvs);
     }
     catch (Exception ex)
     {
         throw new ObjectCreationException(objectName, "Injection of autowired dependencies failed", ex);
     }
     return pvs;
 }
 /// <summary>
 /// Post-process the given property values before the factory applies them
 /// to the given object.
 /// </summary>
 /// <remarks>Allows for checking whether all dependencies have been
 /// satisfied, for example based on a "Required" annotation on bean property setters.
 /// <para>Also allows for replacing the property values to apply, typically through
 /// creating a new MutablePropertyValues instance based on the original PropertyValues,
 /// adding or removing specific values.
 /// </para>
 /// </remarks>
 /// <param name="pvs">The property values that the factory is about to apply (never <code>null</code>).</param>
 /// <param name="pis">he relevant property infos for the target object (with ignored
 /// dependency types - which the factory handles specifically - already filtered out)</param>
 /// <param name="objectInstance">The object instance created, but whose properties have not yet 
 /// been set.</param>
 /// <param name="objectName">Name of the object.</param>
 /// <returns>The actual property values to apply to the given object (can be the 
 /// passed-in PropertyValues instances0 or null to skip property population.</returns>
 public virtual IPropertyValues PostProcessPropertyValues(IPropertyValues pvs, PropertyInfo[] pis, object objectInstance,
                                                  string objectName)
 {
     return pvs;
 }
コード例 #24
0
 /// <summary>
 /// Ececuted to inject value to associated memeber info
 /// </summary>
 /// <param name="target"></param>
 /// <param name="requestingObjectName"></param>
 /// <param name="pvs"></param>
 public abstract void Inject(object target, string requestingObjectName, IPropertyValues pvs);
        /// <summary>
        /// Post-process the given property values before the factory applies them
        /// to the given object.  Checks for the attribute specified by this PostProcessor's RequiredAttributeType.
        /// </summary>
        /// <param name="pvs">The property values that the factory is about to apply (never <code>null</code>).</param>
        /// <param name="pis">The relevant property infos for the target object (with ignored
        /// dependency types - which the factory handles specifically - already filtered out)</param>
        /// <param name="objectInstance">The object instance created, but whose properties have not yet
        /// been set.</param>
        /// <param name="objectName">Name of the object.</param>
        /// <returns>
        /// The actual property values to apply to the given object (can be the
        /// passed-in PropertyValues instances or null to skip property population.
        /// </returns>
        /// <exception cref="ObjectInitializationException">If a required property value has not been specified
        /// in the configuration metadata.</exception>
        public override IPropertyValues PostProcessPropertyValues(IPropertyValues pvs, PropertyInfo[] pis, object objectInstance,
                                                         string objectName)
        {
            if (!validatedObjectNames.Contains(objectName))
            {
                ArrayList invalidProperties = new ArrayList();

                foreach (PropertyInfo pi in pis)
                {
                    if (IsRequiredProperty(pi) && !pvs.Contains(pi.Name))
                    {
                        invalidProperties.Add(pi.Name);
                    }
                }
                if (invalidProperties.Count != 0)
                {
                    throw new ObjectInitializationException(
                        BuildExceptionMessage((string[]) invalidProperties.ToArray(typeof (string)), objectName));
                }
                validatedObjectNames.Add(objectName);
            }
            return pvs;
        }
コード例 #26
0
            public override void Inject(Object target, string objectName, IPropertyValues pvs)
            {
                MethodInfo method = _member as MethodInfo;

                try
                {
                    Object[] arguments;
                    if (_cached)
                    {
                        arguments = ResolveCachedArguments(objectName);
                    }
                    else
                    {
                        Type[] paramTypes = method.GetParameters().Select(p => p.ParameterType).ToArray();
                        arguments = new Object[paramTypes.Length];
                        var   descriptors        = new DependencyDescriptor[paramTypes.Length];
                        IList autowiredBeanNames = new ArrayList();
                        for (int i = 0; i < arguments.Length; i++)
                        {
                            MethodParameter methodParam = new MethodParameter(method, i);
                            descriptors[i] = new DependencyDescriptor(methodParam, _required);
                            arguments[i]   = _objectFactory.ResolveDependency(descriptors[i], objectName,
                                                                              autowiredBeanNames);
                            if (arguments[i] == null && !_required)
                            {
                                arguments = null;
                                break;
                            }
                        }
                        lock (this)
                        {
                            if (!_cached)
                            {
                                if (arguments != null)
                                {
                                    _cachedMethodArguments = new Object[arguments.Length];
                                    for (int i = 0; i < arguments.Length; i++)
                                    {
                                        _cachedMethodArguments[i] = descriptors[i];
                                    }
                                    RegisterDependentObjects(objectName, autowiredBeanNames);
                                    if (autowiredBeanNames.Count == paramTypes.Length)
                                    {
                                        for (int i = 0; i < paramTypes.Length; i++)
                                        {
                                            string autowiredBeanName = autowiredBeanNames[i] as string;
                                            if (_objectFactory.ContainsObject(autowiredBeanName))
                                            {
                                                if (_objectFactory.IsTypeMatch(autowiredBeanName, paramTypes[i]))
                                                {
                                                    _cachedMethodArguments[i] =
                                                        new RuntimeObjectReference(autowiredBeanName);
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    _cachedMethodArguments = null;
                                }
                                _cached = true;
                            }
                        }
                    }
                    if (arguments != null)
                    {
                        method.Invoke(target, arguments);
                    }
                }
                catch (Exception ex)
                {
                    throw new ObjectCreationException("Could not autowire method: " + method, ex);
                }
            }
コード例 #27
0
 /// <summary>
 /// Ececuted to inject value to associated memeber info
 /// </summary>
 /// <param name="target"></param>
 /// <param name="requestingObjectName"></param>
 /// <param name="pvs"></param>
 public abstract void Inject(object target, string requestingObjectName, IPropertyValues pvs);
コード例 #28
0
 /// <summary>Set a number of property values in bulk.</summary>
 /// <remarks>
 /// <p>
 /// Does not allow unknown fields. Equivalent to
 /// <see cref="Spring.Objects.ObjectWrapper.SetPropertyValues(IPropertyValues, bool)"/>
 /// with <see langword="null"/> and <cref lang="false"/> for
 /// arguments.
 /// </p>
 /// </remarks>
 /// <param name="pvs">
 /// The <see cref="Spring.Objects.IPropertyValues"/> to set on the target
 /// object.
 /// </param>
 /// <exception cref="Spring.Core.NotWritablePropertyException">
 /// If an error is encountered while setting a property.
 /// </exception>
 /// <exception cref="Spring.Objects.PropertyAccessExceptionsException">
 /// On a <see cref="System.Type"/> mismatch while setting a property, insufficient permissions, etc.
 /// </exception>
 /// <seealso cref="Spring.Objects.IObjectWrapper.SetPropertyValues(IPropertyValues, bool)"/>
 public virtual void SetPropertyValues(IPropertyValues pvs)
 {
     SetPropertyValues(pvs, false);
 }
コード例 #29
0
        /// <summary>
        /// Returns the list of <paramref name="propertyInfos"/> that are not satisfied by <paramref name="properties"/>.
        /// </summary>
        /// <returns>the filtered list. Is never <c>null</c></returns>
        public static PropertyInfo[] GetUnsatisfiedDependencies(PropertyInfo[] propertyInfos, IPropertyValues properties, DependencyCheckingMode dependencyCheck)
        {
            ArrayList unsatisfiedDependenciesList = new ArrayList();

            foreach (PropertyInfo property in propertyInfos)
            {
                if (property.CanWrite && properties.GetPropertyValue(property.Name) == null)
                {
                    bool isSimple    = ObjectUtils.IsSimpleProperty(property.PropertyType);
                    bool unsatisfied = (dependencyCheck == DependencyCheckingMode.All) || (isSimple && dependencyCheck == DependencyCheckingMode.Simple) ||
                                       (!isSimple && dependencyCheck == DependencyCheckingMode.Objects);
                    if (unsatisfied)
                    {
                        unsatisfiedDependenciesList.Add(property);
                    }
                }
            }
            return((PropertyInfo[])unsatisfiedDependenciesList.ToArray(typeof(PropertyInfo)));
        }
コード例 #30
0
 /// <summary>
 /// Returns the list of <paramref name="propertyInfos"/> that are not satisfied by <paramref name="properties"/>.
 /// </summary>
 /// <returns>the filtered list. Is never <c>null</c></returns>
 public static PropertyInfo[] GetUnsatisfiedDependencies(PropertyInfo[] propertyInfos, IPropertyValues properties, DependencyCheckingMode dependencyCheck)
 {
     ArrayList unsatisfiedDependenciesList = new ArrayList();
     foreach (PropertyInfo property in propertyInfos)
     {
         if (property.CanWrite && properties.GetPropertyValue(property.Name) == null)
         {
             bool isSimple = ObjectUtils.IsSimpleProperty(property.PropertyType);
             bool unsatisfied = (dependencyCheck == DependencyCheckingMode.All) || (isSimple && dependencyCheck == DependencyCheckingMode.Simple)
                                || (!isSimple && dependencyCheck == DependencyCheckingMode.Objects);
             if (unsatisfied)
             {
                 unsatisfiedDependenciesList.Add(property);
             }
         }
     }
     return (PropertyInfo[])unsatisfiedDependenciesList.ToArray(typeof(PropertyInfo));
 }
コード例 #31
0
 /// <summary>
 /// Return the difference (changes, additions, but not removals) of
 /// property values between the supplied argument and the values
 /// contained in the collection.
 /// </summary>
 /// <param name="old">Another property values collection.</param>
 /// <returns>
 /// The collection of property values that are different than the supplied one.
 /// </returns>
 public IPropertyValues ChangesSince(IPropertyValues old)
 {
     MutablePropertyValues changes = new MutablePropertyValues ();
     if (old == this)
     {
         return changes;
     }
     // for each property value in this (the newer set)
     foreach (PropertyValue newProperty in propertyValuesList)
     {
         PropertyValue oldProperty = old.GetPropertyValue (newProperty.Name);
         if (oldProperty == null)
         {
             // if there wasn't an old one, add it
             changes.Add (newProperty);
         }
         else if (!oldProperty.Equals (newProperty))
         {
             // it's changed
             changes.Add (newProperty);
         }
     }
     return changes;
 }
コード例 #32
0
 /// <summary>
 /// Returns the list of <paramref name="propertyInfos"/> that are not satisfied by <paramref name="properties"/>.
 /// </summary>
 /// <returns>the filtered list. Is never <c>null</c></returns>
 public static IList<PropertyInfo> GetUnsatisfiedDependencies(IList<PropertyInfo> propertyInfos, IPropertyValues properties, DependencyCheckingMode dependencyCheck)
 {
     List<PropertyInfo> unsatisfiedDependenciesList = new List<PropertyInfo>();
     foreach (PropertyInfo property in propertyInfos)
     {
         if (property.CanWrite && properties.GetPropertyValue(property.Name) == null)
         {
             bool isSimple = ObjectUtils.IsSimpleProperty(property.PropertyType);
             bool unsatisfied = (dependencyCheck == DependencyCheckingMode.All) || (isSimple && dependencyCheck == DependencyCheckingMode.Simple)
                                || (!isSimple && dependencyCheck == DependencyCheckingMode.Objects);
             if (unsatisfied)
             {
                 unsatisfiedDependenciesList.Add(property);
             }
         }
     }
     return unsatisfiedDependenciesList;
 }
コード例 #33
0
 /// <summary>
 /// Creates a new instance of the <see cref="Spring.Objects.MutablePropertyValues"/>
 /// class.
 /// </summary>
 /// <remarks>
 /// <p>
 /// Deep copy constructor. Guarantees <see cref="Spring.Objects.PropertyValue"/>
 /// references are independent, although it can't deep copy objects currently
 /// referenced by individual <see cref="Spring.Objects.PropertyValue"/> objects.
 /// </p>
 /// </remarks>
 public MutablePropertyValues(IPropertyValues other)
 {
     if (other != null)
     {
         AddAll (other.PropertyValues);
     }
 }
 public override void Inject(Object target, string objectName, IPropertyValues pvs)
 {
     MethodInfo method = _member as MethodInfo;
     try
     {
         Object[] arguments;
         if (_cached)
         {
             arguments = ResolveCachedArguments(objectName);
         }
         else
         {
             Type[] paramTypes = method.GetParameters().Select(p => p.ParameterType).ToArray();
             arguments = new Object[paramTypes.Length];
             var descriptors = new DependencyDescriptor[paramTypes.Length];
             IList autowiredBeanNames = new ArrayList();
             for (int i = 0; i < arguments.Length; i++)
             {
                 MethodParameter methodParam = new MethodParameter(method, i);
                 descriptors[i] = new DependencyDescriptor(methodParam, _required);
                 arguments[i] = _objectFactory.ResolveDependency(descriptors[i], objectName,
                                                                 autowiredBeanNames);
                 if (arguments[i] == null && !_required)
                 {
                     arguments = null;
                     break;
                 }
             }
             lock (this)
             {
                 if (!_cached)
                 {
                     if (arguments != null)
                     {
                         _cachedMethodArguments = new Object[arguments.Length];
                         for (int i = 0; i < arguments.Length; i++)
                         {
                             _cachedMethodArguments[i] = descriptors[i];
                         }
                         RegisterDependentObjects(objectName, autowiredBeanNames);
                         if (autowiredBeanNames.Count == paramTypes.Length)
                         {
                             for (int i = 0; i < paramTypes.Length; i++)
                             {
                                 string autowiredBeanName = autowiredBeanNames[i] as string;
                                 if (_objectFactory.ContainsObject(autowiredBeanName))
                                 {
                                     if (_objectFactory.IsTypeMatch(autowiredBeanName, paramTypes[i]))
                                     {
                                         _cachedMethodArguments[i] =
                                             new RuntimeObjectReference(autowiredBeanName);
                                     }
                                 }
                             }
                         }
                     }
                     else
                     {
                         _cachedMethodArguments = null;
                     }
                     _cached = true;
                 }
             }
         }
         if (arguments != null)
         {
             method.Invoke(target, arguments);
         }
     }
     catch (Exception ex)
     {
         throw new ObjectCreationException("Could not autowire method: " + method, ex);
     }
 }
コード例 #35
0
ファイル: ObjectWrapper.cs プロジェクト: fgq841103/spring-net
 /// <summary>Set a number of property values in bulk.</summary>
 /// <remarks>
 /// <p>
 /// Does not allow unknown fields. Equivalent to
 /// <see cref="Spring.Objects.ObjectWrapper.SetPropertyValues(IPropertyValues, bool)"/>
 /// with <see langword="null"/> and <cref lang="false"/> for
 /// arguments.
 /// </p>
 /// </remarks>
 /// <param name="pvs">
 /// The <see cref="Spring.Objects.IPropertyValues"/> to set on the target
 /// object.
 /// </param>
 /// <exception cref="Spring.Core.NotWritablePropertyException">
 /// If an error is encountered while setting a property.
 /// </exception>
 /// <exception cref="Spring.Objects.PropertyAccessExceptionsException">
 /// On a <see cref="System.Type"/> mismatch while setting a property, insufficient permissions, etc.
 /// </exception>
 /// <seealso cref="Spring.Objects.IObjectWrapper.SetPropertyValues(IPropertyValues, bool)"/>
 public virtual void SetPropertyValues(IPropertyValues pvs)
 {
     SetPropertyValues(pvs, false);
 }
 public override void Inject(Object instance, String objectName, IPropertyValues pvs)
 {
     var field = (FieldInfo) _member;
     try
     {
         Object value;
         if (_cached)
         {
             value = ResolvedCachedArgument(objectName, _cachedFieldValue);
         }
         else
         {
             var descriptor = new DependencyDescriptor(field, _required);
             IList autowiredObjectNames = new ArrayList();
             value = _objectFactory.ResolveDependency(descriptor, objectName, autowiredObjectNames);
             lock (this)
             {
                 if (!_cached)
                 {
                     if (value != null || _required)
                     {
                         _cachedFieldValue = descriptor;
                         RegisterDependentObjects(objectName, autowiredObjectNames);
                         if (autowiredObjectNames.Count == 1)
                         {
                             var autowiredBeanName = autowiredObjectNames[0] as string;
                             if (_objectFactory.ContainsObject(autowiredBeanName))
                             {
                                 if (_objectFactory.IsTypeMatch(autowiredBeanName, field.GetType()))
                                 {
                                     _cachedFieldValue = new RuntimeObjectReference(autowiredBeanName);
                                 }
                             }
                         }
                     }
                     else
                     {
                         _cachedFieldValue = null;
                     }
                     _cached = true;
                 }
             }
         }
         if (value != null)
         {
             field.SetValue(instance, value);
         }
     }
     catch (Exception ex)
     {
         throw new ObjectCreationException("Could not autowire field: " + field, ex);
     }
 }
        /// <summary>
        /// Perform a dependency check that all properties exposed have been set, if desired.
        /// </summary>
        /// <remarks>
        /// <p>
        /// Dependency checks can be objects (collaborating objects), simple (primitives
        /// and <see cref="System.String"/>), or all (both).
        /// </p>
        /// </remarks>
        /// <param name="name">
        /// The name of the object.
        /// </param>
        /// <param name="definition">
        /// The definition of the named object.
        /// </param>
        /// <param name="wrapper">
        /// The <see cref="Spring.Objects.IObjectWrapper"/> wrapping the target object.
        /// </param>
        /// <param name="properties">
        /// The property values to be checked.
        /// </param>
        /// <exception cref="Spring.Objects.Factory.UnsatisfiedDependencyException">
        /// If all of the checked dependencies were not satisfied.
        /// </exception>
        protected void DependencyCheck(string name, IConfigurableObjectDefinition definition, IObjectWrapper wrapper, IPropertyValues properties)
        {
            DependencyCheckingMode dependencyCheck = definition.DependencyCheck;
            if (dependencyCheck == DependencyCheckingMode.None)
            {
                return;
            }

            PropertyInfo[] filteredPropInfo = FilterPropertyInfoForDependencyCheck(wrapper);
            if (HasInstantiationAwareBeanPostProcessors)
            {
                foreach (IObjectPostProcessor processor in ObjectPostProcessors)
                {
                    IInstantiationAwareObjectPostProcessor inProc = processor as IInstantiationAwareObjectPostProcessor;
                    if (inProc != null)
                    {
                        properties =
                            inProc.PostProcessPropertyValues(properties, filteredPropInfo, wrapper.WrappedInstance, name);
                        if (properties == null)
                        {
                            return;
                        }
                    }
                }
            }


            CheckDependencies(name, definition, filteredPropInfo, properties);
        }
        private void CheckDependencies(string name, IConfigurableObjectDefinition definition, PropertyInfo[] filteredPropInfo, IPropertyValues properties)
        {
            DependencyCheckingMode dependencyCheck = definition.DependencyCheck;
            PropertyInfo[] unsatisfiedDependencies = AutowireUtils.GetUnsatisfiedDependencies(filteredPropInfo, properties, dependencyCheck);

            if (unsatisfiedDependencies.Length > 0)
            {
                throw new UnsatisfiedDependencyException(definition.ResourceDescription, name, unsatisfiedDependencies[0].Name,
                    "Set this property value or disable dependency checking for this object.");                
            }
        }
        /// <summary>
        /// Apply the given property values, resolving any runtime references
        /// to other objects in this object factory.
        /// </summary>
        /// <param name="name">
        /// The object name passed for better exception information.
        /// </param>
        /// <param name="definition">
        /// The definition of the named object.
        /// </param>
        /// <param name="wrapper">
        /// The <see cref="Spring.Objects.IObjectWrapper"/> wrapping the target object.
        /// </param>
        /// <param name="properties">
        /// The new property values.
        /// </param>
        /// <remarks>
        /// <p>
        /// Must use deep copy, so that we don't permanently modify this property.
        /// </p>
        /// </remarks>
        protected void ApplyPropertyValues(string name, RootObjectDefinition definition, IObjectWrapper wrapper, IPropertyValues properties)
        {
            if (properties == null || properties.PropertyValues.Length == 0)
            {
                return;
            }
            ObjectDefinitionValueResolver valueResolver = CreateValueResolver();

            MutablePropertyValues deepCopy = new MutablePropertyValues(properties);
            PropertyValue[] copiedProperties = deepCopy.PropertyValues;
            for (int i = 0; i < copiedProperties.Length; ++i)
            {
                PropertyValue copiedProperty = copiedProperties[i];
                //(string name, RootObjectDefinition definition, string argumentName, object argumentValue)
                object value = valueResolver.ResolveValueIfNecessary(name, definition, copiedProperty.Name, copiedProperty.Value);
                // object value = ResolveValueIfNecessary(name, definition, copiedProperty.Name, copiedProperty.Value);
                PropertyValue propertyValue = new PropertyValue(copiedProperty.Name, value, copiedProperty.Expression);
                // update mutable copy...
                deepCopy.SetPropertyValueAt(propertyValue, i);
            }
            // set the (possibly resolved) deep copy properties...
            try
            {
                wrapper.SetPropertyValues(deepCopy);
            }
            catch (ObjectsException ex)
            {
                // improve the message by showing the context...
                throw new ObjectCreationException(definition.ResourceDescription, name, "Error setting property values: " + ex.Message, ex);
            }
        }
コード例 #40
0
ファイル: AutowireUtils.cs プロジェクト: Oragon/Oragon.Spring
        /// <summary>
        /// Returns the list of <paramref name="propertyInfos"/> that are not satisfied by <paramref name="properties"/>.
        /// </summary>
        /// <returns>the filtered list. Is never <c>null</c></returns>
        public static IList <PropertyInfo> GetUnsatisfiedDependencies(IList <PropertyInfo> propertyInfos, IPropertyValues properties, DependencyCheckingMode dependencyCheck)
        {
            List <PropertyInfo> unsatisfiedDependenciesList = new List <PropertyInfo>();

            foreach (PropertyInfo property in propertyInfos)
            {
                if (property.CanWrite && properties.GetPropertyValue(property.Name) == null)
                {
                    bool isSimple    = ObjectUtils.IsSimpleProperty(property.PropertyType);
                    bool unsatisfied = (dependencyCheck == DependencyCheckingMode.All) || (isSimple && dependencyCheck == DependencyCheckingMode.Simple) ||
                                       (!isSimple && dependencyCheck == DependencyCheckingMode.Objects);
                    if (unsatisfied)
                    {
                        unsatisfiedDependenciesList.Add(property);
                    }
                }
            }
            return(unsatisfiedDependenciesList);
        }
コード例 #41
0
        /// <summary>
        /// Post-process the given property values before the factory applies them
        /// to the given object.  Checks for the attribute specified by this PostProcessor's RequiredAttributeType.
        /// </summary>
        /// <param name="pvs">The property values that the factory is about to apply (never <code>null</code>).</param>
        /// <param name="pis">The relevant property infos for the target object (with ignored
        /// dependency types - which the factory handles specifically - already filtered out)</param>
        /// <param name="objectInstance">The object instance created, but whose properties have not yet
        /// been set.</param>
        /// <param name="objectName">Name of the object.</param>
        /// <returns>
        /// The actual property values to apply to the given object (can be the
        /// passed-in PropertyValues instances or null to skip property population.
        /// </returns>
        /// <exception cref="ObjectInitializationException">If a required property value has not been specified
        /// in the configuration metadata.</exception>
        public override IPropertyValues PostProcessPropertyValues(IPropertyValues pvs, IList<PropertyInfo> pis, object objectInstance, string objectName)
        {
            if (!validatedObjectNames.Contains(objectName))
            {
                List<string> invalidProperties = new List<string>();

                foreach (PropertyInfo pi in pis)
                {
                    if (IsRequiredProperty(pi) && !pvs.Contains(pi.Name))
                    {
                        invalidProperties.Add(pi.Name);
                    }
                }
                if (invalidProperties.Count != 0)
                {
                    throw new ObjectInitializationException(
                        BuildExceptionMessage(invalidProperties, objectName));
                }
                validatedObjectNames.Add(objectName);
            }
            return pvs;
        }