/** * Register an error channel in the given ObjectDefinitionRegistry if not yet present. * The bean name for which this is checking is defined by the constant * {@link IntegrationContextUtils#ERROR_CHANNEL_BEAN_NAME}. */ private void RegisterErrorChannelIfNecessary(IObjectDefinitionRegistry registry) { if (!registry.ContainsObjectDefinition(IntegrationContextUtils.ErrorChannelObjectName)) { if (logger.IsInfoEnabled) { logger.Info("No bean named '" + IntegrationContextUtils.ErrorChannelObjectName + "' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created."); } RootObjectDefinition errorChannelDef = new RootObjectDefinition(); errorChannelDef.ObjectTypeName = IntegrationNamespaceUtils.BASE_PACKAGE + ".Channel.PublishSubscribeChannel"; ObjectDefinitionHolder errorChannelHolder = new ObjectDefinitionHolder(errorChannelDef, IntegrationContextUtils.ErrorChannelObjectName); ObjectDefinitionReaderUtils.RegisterObjectDefinition(errorChannelHolder, registry); ObjectDefinitionBuilder loggingHandlerBuilder = ObjectDefinitionBuilder.GenericObjectDefinition(IntegrationNamespaceUtils.HANDLER_PACKAGE + ".LoggingHandler"); string loggingHandlerObjectName = ObjectDefinitionReaderUtils.GenerateObjectName(loggingHandlerBuilder.ObjectDefinition, registry); loggingHandlerBuilder.AddConstructorArg("ERROR"); ObjectDefinitionHolder loggingHandlerHolder = new ObjectDefinitionHolder(loggingHandlerBuilder.ObjectDefinition, loggingHandlerObjectName); ObjectDefinitionReaderUtils.RegisterObjectDefinition(loggingHandlerHolder, registry); ObjectDefinitionBuilder loggingEndpointBuilder = ObjectDefinitionBuilder.GenericObjectDefinition(IntegrationNamespaceUtils.ENDPOINT_PACKAGE + ".EventDrivenConsumer"); loggingEndpointBuilder.AddConstructorArgReference(IntegrationContextUtils.ErrorChannelObjectName); loggingEndpointBuilder.AddConstructorArgReference(loggingHandlerObjectName); string loggingEndpointObjectName = ObjectDefinitionReaderUtils.GenerateObjectName(loggingEndpointBuilder.ObjectDefinition, registry); ObjectDefinitionHolder loggingEndpointHolder = new ObjectDefinitionHolder(loggingEndpointBuilder.ObjectDefinition, loggingEndpointObjectName); ObjectDefinitionReaderUtils.RegisterObjectDefinition(loggingEndpointHolder, registry); } }
/// <summary> /// Process the object element /// </summary> protected virtual void ProcessObjectDefinition(XmlElement element, ObjectDefinitionParserHelper helper) { // TODO: add event handling try { ObjectDefinitionHolder bdHolder = helper.ParseObjectDefinitionElement(element); if (bdHolder == null) { return; } bdHolder = helper.DecorateObjectDefinitionIfRequired(element, bdHolder); if (log.IsEnabled(LogLevel.Debug)) { log.LogDebug(string.Format(CultureInfo.InvariantCulture, "Registering object definition with id '{0}'.", bdHolder.ObjectName)); } ObjectDefinitionReaderUtils.RegisterObjectDefinition(bdHolder, ReaderContext.Registry); // TODO: Send registration event. // ReaderContext.FireComponentRegistered(new BeanComponentDefinition(bdHolder)); } catch (ObjectDefinitionStoreException) { throw; } catch (Exception ex) { throw new ObjectDefinitionStoreException( $"Failed parsing object definition '{element.OuterXml}'", ex); } }
protected override ObjectDefinitionHolder CreateObjectDefinitionHolder(XmlElement element, IConfigurableObjectDefinition definition, string objectName, string[] aliasesArray) { IWebObjectDefinition webDefinition = definition as IWebObjectDefinition; if (webDefinition != null) { if (definition.IsSingleton && element.HasAttribute(ObjectDefinitionConstants.ScopeAttribute)) { webDefinition.Scope = GetScope(element.GetAttribute(ObjectDefinitionConstants.ScopeAttribute)); } // force request and session scoped objects to be lazily initialized... if (webDefinition.Scope == ObjectScope.Request || webDefinition.Scope == ObjectScope.Session) { definition.IsLazyInit = true; } // string typeName = element.GetAttribute(ObjectDefinitionConstants.TypeAttribute); string typeName = definition.ObjectTypeName; if (typeName != null && (typeName.EndsWith(".ascx") || typeName.EndsWith(".master"))) { definition.IsAbstract = true; } } ObjectDefinitionHolder holder = base.CreateObjectDefinitionHolder(element, definition, objectName, aliasesArray); return(holder); }
/// <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); }
/// <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); }
public void RegisterObjectDefinitionSunnyDay() { ObjectDefinitionHolder holder = new ObjectDefinitionHolder(definition, "foo"); ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, registry); A.CallTo(() => registry.RegisterObjectDefinition(null, null)).WithAnyArguments().MustHaveHappened(); }
/// <summary> /// Parse the specified element and register any resulting /// IObjectDefinitions with the IObjectDefinitionRegistry that is /// embedded in the supplied ParserContext. /// </summary> /// <param name="element">The element to be parsed into one or more IObjectDefinitions</param> /// <param name="parserContext">The object encapsulating the current state of the parsing /// process.</param> /// <returns> /// The primary IObjectDefinition (can be null as explained above) /// </returns> /// <remarks> /// Implementations should return the primary IObjectDefinition /// that results from the parse phase if they wish to used nested /// inside (for example) a <code><property></code> tag. /// <para>Implementations may return null if they will not /// be used in a nested scenario. /// </para> /// </remarks> public override IObjectDefinition ParseElement(XmlElement element, ParserContext parserContext) { ParseElementCalled = true; ObjectDefinitionHolder holder = ParseTestObjectDefinition(element, parserContext); ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, parserContext.Registry); return(null); }
public void RegisterObjectDefinitionSunnyDayWithAliases() { ObjectDefinitionHolder holder = new ObjectDefinitionHolder(definition, "foo", new string[] { "bar", "baz" }); ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, registry); A.CallTo(() => registry.RegisterObjectDefinition("foo", definition)).MustHaveHappened(); A.CallTo(() => registry.RegisterAlias("foo", "bar")).MustHaveHappened(); A.CallTo(() => registry.RegisterAlias("foo", "baz")).MustHaveHappened(); }
public void RegisterObjectDefinitionSunnyDay() { registry.RegisterObjectDefinition(null, null); LastCall.IgnoreArguments(); mocks.ReplayAll(); ObjectDefinitionHolder holder = new ObjectDefinitionHolder(definition, "foo"); ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, registry); mocks.VerifyAll(); }
public void RegisterObjectDefinitionWithDuplicateAlias() { registry.RegisterObjectDefinition("foo", definition); // we assume that some other object defition has already been associated with this alias... A.CallTo(() => registry.RegisterAlias(null, null)).WithAnyArguments().Throws <ObjectDefinitionStoreException>(); ObjectDefinitionHolder holder = new ObjectDefinitionHolder(definition, "foo", new string[] { "bing" }); Assert.Throws <ObjectDefinitionStoreException>(() => ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, registry)); }
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 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(); }
private void RegisterNullChannel(IObjectDefinitionRegistry registry) { if (registry.IsObjectNameInUse(IntegrationContextUtils.NullChannelObjectName)) { throw new IllegalStateException("The object name '" + IntegrationContextUtils.NullChannelObjectName + "' is reserved."); } RootObjectDefinition nullChannelDef = new RootObjectDefinition(); nullChannelDef.ObjectTypeName = IntegrationNamespaceUtils.BASE_PACKAGE + ".Channel.NullChannel"; ObjectDefinitionHolder nullChannelHolder = new ObjectDefinitionHolder(nullChannelDef, IntegrationContextUtils.NullChannelObjectName); ObjectDefinitionReaderUtils.RegisterObjectDefinition(nullChannelHolder, registry); }
/// <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); string[] aliases = objectDefinition.Aliases; for (int i = 0; i < aliases.Length; ++i) { string alias = aliases[i]; registry.RegisterAlias(objectDefinition.ObjectName, alias); } }
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 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); }
/// <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); }
private static object ParsePropertySubElement(XmlElement childElement, AbstractObjectDefinition rawObjectDefinition, ParserContext parserContext) { string localName = childElement.LocalName; if ("object".Equals(localName)) { ObjectDefinitionHolder holder = parserContext.ParserHelper.ParseObjectDefinitionElement(childElement); return(holder); } if ("ref".Equals(localName)) { string reference = childElement.GetAttribute("object"); return(new RuntimeObjectReference(reference)); } parserContext.ReaderContext.ReportException(childElement, localName, "unsupported element"); return(null); }
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); }
public ManagedList ParseInterceptors(XmlElement element, ParserContext parserContext) { ManagedList interceptors = new ManagedList(); foreach (XmlNode child in element.ChildNodes) { /* TODO: * check full elementname (incl. NamespaceUri) */ if (child.NodeType == XmlNodeType.Element) { XmlElement childElement = (XmlElement)child; string localName = child.LocalName; if ("object".Equals(localName)) { ObjectDefinitionHolder holder = parserContext.ParserHelper.ParseObjectDefinitionElement(childElement); interceptors.Add(holder); } else if ("ref".Equals(localName)) { string reference = childElement.GetAttribute("object"); interceptors.Add(new RuntimeObjectReference(reference)); } else { if (!parsers.ContainsKey(localName)) { parserContext.ReaderContext.ReportException(childElement, localName, "unsupported interceptor element"); } IObjectDefinitionRegisteringParser parser = parsers[localName]; string interceptorObjectName = parser.Parse(childElement, parserContext); interceptors.Add(new RuntimeObjectReference(interceptorObjectName)); } } } return(interceptors); }
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(); }
/// <summary> /// Register a TaskScheduler in the given <see cref="IObjectDefinitionFactory"/> if not yet present. /// The object name for which this is checking is defined by the constant <see cref="IntegrationContextUtils.TaskSchedulerObjectName"/> /// </summary> /// <param name="registry">the <see cref="IObjectDefinitionFactory"/></param> private void RegisterTaskSchedulerIfNecessary(IObjectDefinitionRegistry registry) { if (!registry.ContainsObjectDefinition(IntegrationContextUtils.TaskSchedulerObjectName)) { if (logger.IsInfoEnabled) { logger.Info("No object named '" + IntegrationContextUtils.TaskSchedulerObjectName + "' has been explicitly defined. Therefore, a default SimpleTaskScheduler will be created."); } IExecutor taskExecutor = IntegrationContextUtils.CreateThreadPoolTaskExecutor(2, 100, 0, "task-scheduler-"); ObjectDefinitionBuilder schedulerBuilder = ObjectDefinitionBuilder.GenericObjectDefinition(IntegrationNamespaceUtils.SCHEDULING_PACKAGE + ".SimpleTaskScheduler"); schedulerBuilder.AddConstructorArg(taskExecutor); ObjectDefinitionBuilder errorHandlerBuilder = ObjectDefinitionBuilder.GenericObjectDefinition(IntegrationNamespaceUtils.CHANNEL_PACKAGE + ".MessagePublishingErrorHandler"); errorHandlerBuilder.AddPropertyReference("defaultErrorChannel", IntegrationContextUtils.ErrorChannelObjectName); string errorHandlerBeanName = ObjectDefinitionReaderUtils.GenerateObjectName(errorHandlerBuilder.ObjectDefinition, registry); ObjectDefinitionHolder errorHandlerHolder = new ObjectDefinitionHolder(errorHandlerBuilder.ObjectDefinition, errorHandlerBeanName); ObjectDefinitionReaderUtils.RegisterObjectDefinition(errorHandlerHolder, registry); schedulerBuilder.AddPropertyReference("errorHandler", errorHandlerBeanName); ObjectDefinitionHolder schedulerHolder = new ObjectDefinitionHolder(schedulerBuilder.ObjectDefinition, IntegrationContextUtils.TaskSchedulerObjectName); ObjectDefinitionReaderUtils.RegisterObjectDefinition(schedulerHolder, registry); } }
/// <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); }
public void RegisterObjectDefinitionWithNullRegistry() { ObjectDefinitionHolder holder = new ObjectDefinitionHolder(definition, "foo"); Assert.Throws <ArgumentNullException>(() => ObjectDefinitionReaderUtils.RegisterObjectDefinition(holder, null)); }
public ObjectDefinitionHolder Decorate(XmlNode node, ObjectDefinitionHolder definition, ParserContext parserContext) { throw new NotImplementedException(); }
/// <summary> /// TODO /// </summary> /// <param name="name"> /// The name of the object that is having the value of one of its properties resolved. /// </param> /// <param name="definition"> /// The definition of the named object. /// </param> /// <param name="argumentName"> /// The name of the property the value of which is being resolved. /// </param> /// <param name="argumentValue"> /// The value of the property that is being resolved. /// </param> private object ResolvePropertyValue(string name, IObjectDefinition definition, string argumentName, object argumentValue) { object resolvedValue = null; // we must check the argument value to see whether it requires a runtime // reference to another object to be resolved. // if it does, we'll attempt to instantiate the object and set the reference. if (RemotingServices.IsTransparentProxy(argumentValue)) { resolvedValue = argumentValue; } else if (argumentValue is ICustomValueReferenceHolder) { resolvedValue = ((ICustomValueReferenceHolder)argumentValue).Resolve(objectFactory, name, definition, argumentName, argumentValue); } else if (argumentValue is ObjectDefinitionHolder) { // contains an IObjectDefinition with name and aliases... ObjectDefinitionHolder holder = (ObjectDefinitionHolder)argumentValue; resolvedValue = ResolveInnerObjectDefinition(name, holder.ObjectName, argumentName, holder.ObjectDefinition, definition.IsSingleton); } else if (argumentValue is IObjectDefinition) { // resolve plain IObjectDefinition, without contained name: use dummy name... IObjectDefinition def = (IObjectDefinition)argumentValue; resolvedValue = ResolveInnerObjectDefinition(name, "(inner object)", argumentName, def, definition.IsSingleton); } else if (argumentValue is RuntimeObjectReference) { RuntimeObjectReference roref = (RuntimeObjectReference)argumentValue; resolvedValue = ResolveReference(definition, name, argumentName, roref); } else if (argumentValue is ExpressionHolder) { ExpressionHolder expHolder = (ExpressionHolder)argumentValue; object context = null; IDictionary <string, object> variables = null; if (expHolder.Properties != null) { PropertyValue contextProperty = expHolder.Properties.GetPropertyValue("Context"); context = contextProperty == null ? null : ResolveValueIfNecessary(name, definition, "Context", contextProperty.Value); PropertyValue variablesProperty = expHolder.Properties.GetPropertyValue("Variables"); object vars = (variablesProperty == null ? null : ResolveValueIfNecessary(name, definition, "Variables", variablesProperty.Value)); if (vars is IDictionary <string, object> ) { variables = (IDictionary <string, object>)vars; } if (vars is IDictionary) { IDictionary temp = (IDictionary)vars; variables = new Dictionary <string, object>(temp.Count); foreach (DictionaryEntry entry in temp) { variables.Add((string)entry.Key, entry.Value); } } else { if (vars != null) { throw new ArgumentException("'Variables' must resolve to an IDictionary"); } } } if (variables == null) { variables = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); } // add 'this' objectfactory reference to variables variables.Add(Expression.ReservedVariableNames.CurrentObjectFactory, objectFactory); resolvedValue = expHolder.Expression.GetValue(context, variables); } else if (argumentValue is IManagedCollection) { resolvedValue = ((IManagedCollection)argumentValue).Resolve(name, definition, argumentName, ResolveValueIfNecessary); } else if (argumentValue is TypedStringValue) { TypedStringValue tsv = (TypedStringValue)argumentValue; try { Type resolvedTargetType = ResolveTargetType(tsv); if (resolvedTargetType != null) { resolvedValue = TypeConversionUtils.ConvertValueIfNecessary(tsv.TargetType, tsv.Value, null); } else { resolvedValue = tsv.Value; } } catch (Exception ex) { throw new ObjectCreationException(definition.ResourceDescription, name, "Error converted typed String value for " + argumentName, ex); } } else { // no need to resolve value... resolvedValue = argumentValue; } return(resolvedValue); }
/// <summary> /// Parse the specified XmlNode and decorate the supplied ObjectDefinitionHolder, /// returning the decorated definition. /// </summary> /// <remarks>The XmlNode may either be an XmlAttribute or an XmlElement, depending on /// whether a custom attribute or element is being parsed. /// <para>Implementations may choose to return a completely new definition, /// which will replace the original definition in the resulting IApplicationContext/IObjectFactory. /// </para> /// <para>The supplied ParserContext can be used to register any additional objects needed to support /// the main definition.</para> /// </remarks> /// <param name="node">The source element or attribute that is to be parsed.</param> /// <param name="definition">The current object definition.</param> /// <param name="parserContext">The object encapsulating the current state of the parsing /// process.</param> /// <returns>The decorated definition (to be registered in the IApplicationContext/IObjectFactory), /// or simply the original object definition if no decoration is required. A null value is strickly /// speaking invalid, but will leniently treated like the case where the original object definition /// gets returned.</returns> public ObjectDefinitionHolder Decorate(XmlNode node, ObjectDefinitionHolder definition, ParserContext parserContext) { return(null); }
/// <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); }
/// <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> /// 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); }