public void RegisterObjectDefinitionSunnyDay()
        {
            registry.RegisterObjectDefinition(null, null);
            LastCall.IgnoreArguments();
            mocks.ReplayAll();

            ObjectDefinitionHolder holder = new ObjectDefinitionHolder(definition, "foo");

            ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, registry);
            mocks.VerifyAll();
        }
 private static string CreateDirectChannel(XmlElement element, ParserContext parserContext)
 {
     string channelId = element.GetAttribute("id");
     if(!StringUtils.HasText(channelId)) {
         parserContext.ReaderContext.ReportException(element, "channel", "The channel-adapter's 'id' attribute is required when no 'channel' "
                 + "reference has been provided, because that 'id' would be used for the created channel.");
     }
     ObjectDefinitionBuilder channelBuilder = ObjectDefinitionBuilder.GenericObjectDefinition(IntegrationNamespaceUtils.CHANNEL_PACKAGE + ".DirectChannel");
     ObjectDefinitionHolder holder = new ObjectDefinitionHolder(channelBuilder.ObjectDefinition, channelId);
     ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, parserContext.Registry);
     return channelId;
 }
        public void RegisterObjectDefinitionSunnyDayWithAliases()
        {
            registry.RegisterObjectDefinition("foo", definition);
            registry.RegisterAlias("foo", "bar");
            registry.RegisterAlias("foo", "baz");
            mocks.ReplayAll();

            ObjectDefinitionHolder holder = new ObjectDefinitionHolder(definition, "foo", new string[] {"bar", "baz"});

            ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, registry);
            
            mocks.VerifyAll();
        }
        public override IObjectDefinition ParseElement(XmlElement element, ParserContext parserContext)
        {
            AbstractObjectDefinition definition = this.ParseInternal(element, parserContext);

            if (!parserContext.IsNested)
            {
                string id = null;
                try
                {
                    id = this.ResolveId(element, definition, parserContext);
                    if (!StringUtils.HasText(id))
                    {
                        parserContext.ReaderContext.ReportException(element, "null",
                                                                    "Id is required for element '" + element.LocalName + "' when used as a top-level tag", null);
                    }

                    string[] name = new string[0];

                    if (NamespaceUtils.IsAttributeDefined(element, "name"))
                    {
                        name = new[] { GetAttributeValue(element, "name") };
                    }

                    ObjectDefinitionHolder holder;

                    if (name.Length == 0)
                    {
                        holder = new ObjectDefinitionHolder(definition, id);
                    }
                    else
                    {
                        holder = new ObjectDefinitionHolder(definition, id, name);
                    }


                    this.RegisterObjectDefinition(holder, parserContext.Registry);
                }
                catch (ObjectDefinitionStoreException ex)
                {
                    parserContext.ReaderContext.ReportException(element, id, ex.Message);
                    return null;
                }
            }
            return definition;
        }
 public void RegisterObjectDefinitionWithNullRegistry()
 {
     ObjectDefinitionHolder holder = new ObjectDefinitionHolder(definition, "foo");
     ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, null);
 }
        public void RegisterObjectDefinitionWithDuplicateAlias()
        {
            registry.RegisterObjectDefinition("foo", definition);

            // we assume that some other object defition has already been associated with this alias...
            registry.RegisterAlias(null, null);
            LastCall.IgnoreArguments().Throw(new ObjectDefinitionStoreException());
            mocks.ReplayAll();

            ObjectDefinitionHolder holder
                = new ObjectDefinitionHolder(definition, "foo", new string[] { "bing" });

            try
            {
                ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, registry);
                Assert.Fail("Must have thrown an ObjectDefinitionStoreException store by this point.");
            }
            catch (ObjectDefinitionStoreException)
            {
                // expected...
            }

            mocks.VerifyAll();
        }
 public ObjectDefinitionHolder Decorate(XmlNode node, ObjectDefinitionHolder definition, ParserContext parserContext)
 {
     throw new System.NotImplementedException();
 }
 public void RegisterObjectDefinitionWithNullRegistry()
 {
     ObjectDefinitionHolder holder = new ObjectDefinitionHolder(definition, "foo");
     Assert.Throws<ArgumentNullException>(() => ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, null));
 }
 /// <summary>
 /// Decorates the object definition if required.
 /// </summary>
 /// <param name="element">The element.</param>
 /// <param name="holder">The holder.</param>
 /// <returns></returns>
 public ObjectDefinitionHolder DecorateObjectDefinitionIfRequired(XmlElement element, ObjectDefinitionHolder holder)
 {
     //TODO decoration processing.
     return holder;
 }
        /// <summary>
        /// Determine whether the provided object definition is an autowire candidate.
        /// <p>To be considered a candidate the object's <em>autowire-candidate</em>
        /// attribute must not have been set to 'false'. Also, if an attribute on
        /// the field or parameter to be autowired is recognized by this bean factory
        /// as a <em>qualifier</em>, the object must 'match' against the attribute as
        /// well as any attributes it may contain. The bean definition must contain
        /// the same qualifier or match by meta attributes. A "value" attribute will
        /// fallback to match against the bean name or an alias if a qualifier or
        /// attribute does not match.</p>
        /// </summary>
        public bool IsAutowireCandidate(ObjectDefinitionHolder odHolder, DependencyDescriptor descriptor)
        {
		    if (!odHolder.ObjectDefinition.IsAutowireCandidate)
            {
			    // if explicitly false, do not proceed with qualifier check
			    return false;
		    }
		    if (descriptor == null) {
                // no qualification necessaryodHolder
			    return true;
		    }
		    bool match = CheckQualifiers(odHolder, descriptor.Attributes);
		    if (match)
            {
			    MethodParameter methodParam = descriptor.MethodParameter;
			    if (methodParam != null)
                {
				    var method = methodParam.MethodInfo;
				    if (method == null || method.ReturnType == typeof(void)) {
					    match = CheckQualifiers(odHolder, methodParam.MethodAttributes);
				    }
			    }
		    }
		    return match;
        }
 /// <summary>
 /// Registers the supplied <see cref="ObjectDefinitionHolder"/> with the supplied
 /// <see cref="IObjectDefinitionRegistry"/>.
 /// </summary>
 /// <remarks>Subclasses can override this method to control whether or not the supplied
 /// <see cref="ObjectDefinitionHolder"/> is actually even registered, or to
 /// register even more objects.
 /// <para>
 /// The default implementation registers the supplied <see cref="ObjectDefinitionHolder"/>
 /// with the supplied <see cref="ObjectDefinitionHolder"/> only if the <code>IsNested</code>
 /// parameter is <code>false</code>, because one typically does not want inner objects
 /// to be registered as top level objects.
 /// </para>
 /// </remarks>
 /// 
 /// <param name="definition">The object definition to be registered.</param>
 /// <param name="registry">The registry that the bean is to be registered with.</param>
 protected virtual void RegisterObjectDefinition(ObjectDefinitionHolder definition, IObjectDefinitionRegistry registry)
 {
     ObjectDefinitionReaderUtils.RegisterObjectDefinition(definition, registry);
 }
        /// <summary>
        /// Parse the specified XmlElement and register the resulting
        /// ObjectDefinitions with the <see cref="ParserContext.Registry"/> IObjectDefinitionRegistry
        /// embedded in the supplied <see cref="ParserContext"/>
        /// </summary>
        /// <param name="element">The element to be parsed.</param>
        /// <param name="parserContext">TThe object encapsulating the current state of the parsing process.
        /// Provides access to a IObjectDefinitionRegistry</param>
        /// <returns>The primary object definition.</returns>
        /// <remarks>
        /// 	<p>
        /// This method is never invoked if the parser is namespace aware
        /// and was called to process the root node.
        /// </p>
        /// </remarks>
        public virtual IObjectDefinition ParseElement(XmlElement element, ParserContext parserContext)
        {
            AbstractObjectDefinition definition = ParseInternal(element, parserContext);

            if (!parserContext.IsNested)
            {
                string id = null;
                try
                {
                    id = ResolveId(element, definition, parserContext);
                    if (!StringUtils.HasText(id))
                    {
                        parserContext.ReaderContext.ReportException(element, "null",
                                "Id is required for element '" + element.LocalName + "' when used as a top-level tag", null);
                    }
                    ObjectDefinitionHolder holder = new ObjectDefinitionHolder(definition, id);
                    RegisterObjectDefinition(holder, parserContext.Registry);
                }
                catch (ObjectDefinitionStoreException ex)
                {
                    parserContext.ReaderContext.ReportException(element, id, ex.Message);
                    return null;
                }
            }
            return definition;


        }
 protected override AbstractObjectDefinition ParseInternal(XmlElement element, ParserContext parserContext)
 {
     ObjectDefinitionBuilder handlerBuilder = ParseHandler(element, parserContext);
     IntegrationNamespaceUtils.SetReferenceIfAttributeDefined(handlerBuilder, element, "output-channel");
     IntegrationNamespaceUtils.SetValueIfAttributeDefined(handlerBuilder, element, "order");
     AbstractObjectDefinition handlerBeanDefinition = handlerBuilder.ObjectDefinition;
     string inputChannelAttributeName = InputChannelAttributeName;
     if(!element.HasAttribute(inputChannelAttributeName)) {
         if(!parserContext.IsNested) {
             parserContext.ReaderContext.ReportException(element, element.Name, "The '" + inputChannelAttributeName
                     + "' attribute is required for top-level endpoint elements.");
         }
         return handlerBeanDefinition;
     }
     ObjectDefinitionBuilder builder = ObjectDefinitionBuilder.GenericObjectDefinition(IntegrationNamespaceUtils.CONFIG_PACKAGE + ".ConsumerEndpointFactoryObject");
     string handlerBeanName = parserContext.ReaderContext.RegisterWithGeneratedName(handlerBeanDefinition);
     builder.AddConstructorArgReference(handlerBeanName);
     string inputChannelName = element.GetAttribute(inputChannelAttributeName);
     if(!parserContext.Registry.ContainsObjectDefinition(inputChannelName)) {
         ObjectDefinitionBuilder channelDef = ObjectDefinitionBuilder.GenericObjectDefinition(IntegrationNamespaceUtils.CHANNEL_PACKAGE + ".DirectChannel");
         ObjectDefinitionHolder holder = new ObjectDefinitionHolder(channelDef.ObjectDefinition, inputChannelName);
         ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, parserContext.Registry);
     }
     builder.AddPropertyValue("inputChannelName", inputChannelName);
     XmlElement pollerElement = DomUtils.GetChildElementByTagName(element, "poller");
     if(pollerElement != null) {
         IntegrationNamespaceUtils.ConfigurePollerMetadata(pollerElement, builder, parserContext);
     }
     IntegrationNamespaceUtils.SetValueIfAttributeDefined(builder, element, "auto-startup");
     return builder.ObjectDefinition;
 }
 private void RegisterDefaultConfiguringObjectFactoryPostProcessorIfNecessary(ParserContext parserContext)
 {
     if (!parserContext.Registry.IsObjectNameInUse(DEFAULT_CONFIGURING_POSTPROCESSOR_OBJECT_NAME))
     {
         ObjectDefinitionBuilder builder =
             ObjectDefinitionBuilder.GenericObjectDefinition(IntegrationNamespaceUtils.CONFIG_XML_PACKAGE + "." +
                                                             DEFAULT_CONFIGURING_POSTPROCESSOR_SIMPLE_CLASS_NAME);
         ObjectDefinitionHolder holder = new ObjectDefinitionHolder(builder.ObjectDefinition,
                                                                    DEFAULT_CONFIGURING_POSTPROCESSOR_OBJECT_NAME);
         ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, parserContext.Registry);
     }
 }
 public ObjectDefinitionHolder Decorate(XmlNode node, ObjectDefinitionHolder definition,
                                        ParserContext parserContext)
 {
     return namespaceHandlerDelegate.Decorate(node, definition, parserContext);
 }
 /// <summary>
 /// Determines whether the given object definition qualifies as an
 /// autowire candidate for the given dependency.
 /// </summary>
 /// <param name="odHolder">The object definition including object name and aliases.</param>
 /// <param name="descriptor">The descriptor for the target method parameter or field.</param>
 /// <returns>
 /// 	<c>true</c> if the object definition qualifies as autowire candidate; otherwise, <c>false</c>.
 /// </returns>
 public bool IsAutowireCandidate(ObjectDefinitionHolder odHolder, DependencyDescriptor descriptor)
 {
     return odHolder.ObjectDefinition.IsAutowireCandidate;
 }
 public ObjectDefinitionHolder Decorate(XmlNode node, ObjectDefinitionHolder definition,
                                        ParserContext parserContext)
 {
     return null;
 }
        /// <summary>
        /// Match the given qualifier annotations against the candidate bean definition.
        /// </summary>
        protected bool CheckQualifiers(ObjectDefinitionHolder odHolder, Attribute[] annotationsToSearch)
        {
		    if (annotationsToSearch == null || annotationsToSearch.Length == 0) {
			    return true;
		    }
		    foreach (var attribute in annotationsToSearch)
            {
                if (IsQualifier(attribute.GetType()))
                {
                    if (!CheckQualifier(odHolder, attribute))
                    {
                        return false;
                    }
			    }
		    }
		    return true;
	    }
        /// <summary>
        /// Registers the supplied <paramref name="objectDefinition"/> with the
        /// supplied <paramref name="registry"/>.
        /// </summary>
        /// <remarks>
        /// <p>
        /// This is a convenience method that registers the
        /// <see cref="Spring.Objects.Factory.Config.ObjectDefinitionHolder.ObjectDefinition"/>
        /// of the supplied <paramref name="objectDefinition"/> under the 
        /// <see cref="Spring.Objects.Factory.Config.ObjectDefinitionHolder.ObjectName"/>
        /// property value of said <paramref name="objectDefinition"/>. If the
        /// supplied <paramref name="objectDefinition"/> has any
        /// <see cref="Spring.Objects.Factory.Config.ObjectDefinitionHolder.Aliases"/>,
        /// then those aliases will also be registered with the supplied
        /// <paramref name="registry"/>.
        /// </p>
        /// </remarks>
        /// <param name="objectDefinition">
        /// The object definition holder containing the
        /// <see cref="Spring.Objects.Factory.Config.IObjectDefinition"/> that
        /// is to be registered.
        /// </param>
        /// <param name="registry">
        /// The registry that the supplied <paramref name="objectDefinition"/>
        /// is to be registered with.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        /// If either of the supplied arguments is <see langword="null"/>.
        /// </exception>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// If the <paramref name="objectDefinition"/> could not be registered
        /// with the <paramref name="registry"/>.
        /// </exception>
        public static void RegisterObjectDefinition(
            ObjectDefinitionHolder objectDefinition, IObjectDefinitionRegistry registry)
        {
            AssertUtils.ArgumentNotNull(objectDefinition, "objectDefinition");
            AssertUtils.ArgumentNotNull(registry, "registry");

            registry.RegisterObjectDefinition(objectDefinition.ObjectName, objectDefinition.ObjectDefinition);
            IList<string> aliases = objectDefinition.Aliases;
            for (int i = 0; i < aliases.Count; ++i)
            {
                string alias = aliases[i];
                registry.RegisterAlias(objectDefinition.ObjectName, alias);
            }
        }
        /// <summary>
        /// Match the given qualifier attribute against the candidate bean definition.
        /// </summary>
        protected bool CheckQualifier(ObjectDefinitionHolder odHolder, Attribute attribute)
        {
		    Type type = attribute.GetType();
		    RootObjectDefinition od = (RootObjectDefinition) odHolder.ObjectDefinition;
		    AutowireCandidateQualifier qualifier = od.GetQualifier(type.FullName);
		    if (qualifier == null) {
			    qualifier = od.GetQualifier(type.Name);
		    }
		    if (qualifier == null) {
			    Attribute targetAttribute = null;
                // TODO: Get the resolved factory method
			    //if (od.GetResolvedFactoryMethod() != null) {
                //    targetAttribute = Attribute.GetCustomAttribute(od.GetResolvedFactoryMethod(), type);
			    //}
			    if (targetAttribute == null) {
				    // look for matching attribute on the target class
				    if (_objectFactory != null) {
					    Type objectType = od.ObjectType;
					    if (objectType != null)
					    {
					        targetAttribute = Attribute.GetCustomAttribute(objectType, type);
					    }
				    }
				    if (targetAttribute == null && od.ObjectType != null) {
                        targetAttribute = Attribute.GetCustomAttribute(od.ObjectType, type);
				    }
			    }
			    if (targetAttribute != null && targetAttribute.Equals(attribute)) {
				    return true;
			    }
		    }

		    IDictionary<string, object> attributes = AttributeUtils.GetAttributeProperties(attribute);
		    if (attributes.Count == 0 && qualifier == null) {
			    // if no attributes, the qualifier must be present
			    return false;
		    }
		    foreach(var entry in attributes)
            {
			    string propertyName = entry.Key;
                object expectedValue = entry.Value;
			    object actualValue = null;
			    // check qualifier first
			    if (qualifier != null)
                {
				    actualValue = qualifier.GetAttribute(propertyName);
			    }
			    if (actualValue == null)                
                {
				    // fall back on bean definition attribute
				    actualValue = od.GetAttribute(propertyName);
			    }
			    if (actualValue == null && propertyName.Equals(AutowireCandidateQualifier.VALUE_KEY) &&
					    expectedValue is string && odHolder.MatchesName((string) expectedValue))
                {
				    // fall back on bean name (or alias) match
				    continue;
			    }
			    if (actualValue == null && qualifier != null)
                {
				    // fall back on default, but only if the qualifier is present
                    actualValue = AttributeUtils.GetDefaultValue(attribute, propertyName);
			    }
			    if (actualValue != null)
                {
                    actualValue = TypeConversionUtils.ConvertValueIfNecessary(expectedValue.GetType(), actualValue, null);
			    }
			    if (!expectedValue.Equals(actualValue)) {
				    return false;
			    }
		    }
		    return true;
	    }