Programmatic means of constructing a IObjectDefinition using the builder pattern. Intended primarily for use when implementing custom namespace parsers.
Set methods are used instead of properties, so that chaining of methods can be used to create 'one-liner'definitions that set multiple properties at one.
        protected override void DoParse(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder)
        {

            builder.AddPropertyReference(TxNamespaceUtils.TRANSACTION_MANAGER_PROPERTY,
                GetAttributeValue(element, TxNamespaceUtils.TRANSACTION_MANAGER_ATTRIBUTE));
            XmlNodeList txAttributes = element.SelectNodes("*[local-name()='attributes' and namespace-uri()='" + element.NamespaceURI + "']");
            if (txAttributes.Count > 1 )
            {
                parserContext.ReaderContext.ReportException(element, "tx advice", "Element <attributes> is allowed at most once inside element <advice>");
            }
            else if (txAttributes.Count == 1)
            {
                //using xml defined source
                XmlElement attributeSourceElement = txAttributes[0] as XmlElement;
                AbstractObjectDefinition attributeSourceDefinition =
                    ParseAttributeSource(attributeSourceElement, parserContext);
                builder.AddPropertyValue(TxNamespaceUtils.TRANSACTION_ATTRIBUTE_SOURCE, attributeSourceDefinition);
            }
            else
            {
                //Assume attibutes source
                ObjectDefinitionBuilder txAttributeSourceBuilder = 
                    parserContext.ParserHelper.CreateRootObjectDefinitionBuilder(typeof (AttributesTransactionAttributeSource));

                builder.AddPropertyValue(TxNamespaceUtils.TRANSACTION_ATTRIBUTE_SOURCE,
                                         txAttributeSourceBuilder.ObjectDefinition);

            }
        }
Exemplo n.º 2
0
 public void Configure(ObjectDefinitionBuilder builder)
 {
     foreach (string key in _properties.Keys)
     {
         builder.AddPropertyValue(key, _properties[key]);
     }
 }
 private static void ParseQueueCapacity(ObjectDefinitionBuilder builder, XmlElement queueElement)
 {
     string capacity = queueElement.GetAttribute("capacity");
     if (StringUtils.HasText(capacity)) {
         builder.AddConstructorArg(Convert.ToUInt32(capacity));
     }
 }
 /// <summary>
 /// Creates a new <see cref="ObjectDefinitionBuilder"/> used to construct a <see cref="Spring.Objects.Factory.Support.GenericObjectDefinition"/>.
 /// </summary>
 /// <param name="objectType">the <see cref="Type"/> of the object that the definition is being created for</param>
 public static ObjectDefinitionBuilder GenericObjectDefinition(Type objectType)
 {
    ObjectDefinitionBuilder builder = new ObjectDefinitionBuilder();
     builder.objectDefinition = new GenericObjectDefinition();
     builder.objectDefinition.ObjectType = objectType;
     return builder;
 }
        protected override void DoParse(XmlElement element, ObjectDefinitionBuilder builder)
        {
            base.DoParse(element, builder);
            ParseSimpleProperties(element, builder);

            XmlNodeList subElements = element.ChildNodes;
            ManagedList locators = new ManagedList(subElements.Count);
            ManagedList servers = new ManagedList(subElements.Count);
            for (int i = 0; i < subElements.Count; i++) {
                XmlNode subElement = subElements.Item(i);
                if (subElement != null && subElement.NodeType == XmlNodeType.Element)
                {
                    if ("locator".Equals(subElement.LocalName))
                    {
                        locators.Add(ParseLocator((XmlElement)subElement));
                    }
                    if ("server".Equals(subElement.LocalName))
                    {
                        servers.Add(ParseServer((XmlElement)subElement));
                    }
                }
            }

            if (locators.Count > 0)
            {
                builder.AddPropertyValue("Locators", locators);
            }
            if (servers.Count > 0)
            {
                builder.AddPropertyValue("Servers", servers);
            }
        }
 protected override void DoParse(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder)
 {
     if(!StringUtils.HasText(element.GetAttribute("id"))) {
         parserContext.ReaderContext.ReportException(element, element.Name, "id is required");
     }
     ParseSelectorChain(builder, element, parserContext);
 }
        /// <summary>The do parse.</summary>
        /// <param name="element">The element.</param>
        /// <param name="parserContext">The parser context.</param>
        /// <param name="builder">The builder.</param>
        protected override void DoParse(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder)
        {
            var parser = new ObjectDefinitionParserHelper(parserContext);
            var map = parser.ParseMapElement(element, builder.RawObjectDefinition);

            builder.AddPropertyValue("SourceDictionary", map);
        }
        /// <summary>
        /// Creates a new <see cref="ObjectDefinitionBuilder"/> used to construct a <see cref="Spring.Objects.Factory.Support.GenericObjectDefinition"/>.
        /// </summary>
        public static ObjectDefinitionBuilder GenericObjectDefinition()
        {
            ObjectDefinitionBuilder builder = new ObjectDefinitionBuilder();

            builder.objectDefinition = new GenericObjectDefinition();
            return(builder);
        }
 protected override void DoParse(XmlElement element, ObjectDefinitionBuilder builder)
 {
     base.DoParse(element, builder);
     ParsingUtils.SetPropertyValue(element, builder, "name", "name");
     String attr = element.GetAttribute("cache-ref");
     // add cache reference (fallback to default if nothing is specified)
     builder.AddPropertyReference("cache", (StringUtils.HasText(attr) ? attr : "gemfire-cache"));
 }
        /// <summary>
        /// Creates a new <see cref="ObjectDefinitionBuilder"/> used to construct a <see cref="Spring.Objects.Factory.Support.GenericObjectDefinition"/>.
        /// </summary>
        /// <param name="objectTypeName">the name of the <see cref="Type"/> of the object that the definition is being created for</param>
        public static ObjectDefinitionBuilder GenericObjectDefinition(string objectTypeName)
        {
            ObjectDefinitionBuilder builder = new ObjectDefinitionBuilder();

            builder.objectDefinition = new GenericObjectDefinition();
            builder.objectDefinition.ObjectTypeName = objectTypeName;
            return(builder);
        }
        protected override void DoParse(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder)
        {
            base.DoParse(element, builder);

            //TODO investigate setting of scope on client cache
            //builder.AddPropertyValue("Scope", "some value");

            //TODO check if .NET client has any 'data policy' settings.

            ParsingUtils.SetPropertyValue(element, builder, "name", "name");
            ParsingUtils.SetPropertyValue(element, builder, "pool-name", "poolName");

            String cacheRef = element.GetAttribute("cache-ref");
            // add cache reference (fallback to default if nothing is specified)
            builder.AddPropertyReference("cache", (StringUtils.HasText(cacheRef) ? cacheRef : "gemfire-cache"));

            // client region attributes
            String regionAttributesRef = element.GetAttribute("attributes-ref");
            if (StringUtils.HasText(regionAttributesRef))
            {
                ObjectDefinitionBuilder attrBuilder = ObjectDefinitionBuilder.GenericObjectDefinition(typeof(RegionAttributesFactoryObject));
                builder.AddPropertyReference("attributes", regionAttributesRef);
            }

            ManagedList interests = new ManagedList();
            XmlNodeList subElements = element.ChildNodes;
            for (int i = 0; i < subElements.Count; i++)
            {
                XmlNode subElement = subElements.Item(i);
                if (subElement.NodeType == XmlNodeType.Element)
                {
                    string name = subElement.LocalName;
                    if ("cache-listener".Equals(name))
                    {
                        builder.AddPropertyValue("cacheListeners", ParseCacheListener(parserContext, (XmlElement)subElement, builder));
                    }
                    else if ("regex-interest".Equals(name))
                    {
                        interests.Add(ParseRegexInterest(parserContext, (XmlElement)subElement));
                    }
                    else if ("key-interest".Equals(name))
                    {
                        interests.Add(ParseKeyInterest(parserContext, (XmlElement)subElement));
                    }
                    else if ("all-keys-interest".Equals(name))
                    {
                        interests.Add(ParseAllKeysInterest(parserContext, (XmlElement) subElement));
                    }

                }
            }

            if (subElements.Count > 0)
            {
                builder.AddPropertyValue("interests", interests);
            }
        }
 protected override void DoParse(XmlElement element, ObjectDefinitionBuilder builder)
 {
     base.DoParse(element, builder);
     ParsingUtils.SetPropertyValue(element, builder, "cache-xml-location", "CacheXml");
     ParsingUtils.SetPropertyValue(element, builder, "disconnect-on-close", "DisconnectOnClose");
     ParsingUtils.SetPropertyValue(element, builder, "keepalive-on-close", "KeepAliveOnClose");
     ParsingUtils.SetPropertyValue(element, builder, "distributed-system-name", "DistributedSystemName");
     ParsingUtils.SetPropertyReference(element, builder, "properties-ref", "Properties");
 }
 protected override void DoParse(XmlElement element, ObjectDefinitionBuilder builder)
 {
     string parentFlow = element.GetAttribute("parent");
     if (!string.IsNullOrEmpty(parentFlow))
     {
         builder.AddPropertyReference("ParentFlow", parentFlow);
     }
     DoParseInternal(element,builder);
 }
        public void ObjectType()
        {
            ObjectDefinitionBuilder odb = ObjectDefinitionBuilder.RootObjectDefinition(odf, typeof(TestObject));
            RootObjectDefinition    rod = odb.ObjectDefinition as RootObjectDefinition;

            Assert.IsNotNull(rod);
            Assert.IsTrue(rod.HasObjectType);
            Assert.AreEqual(typeof(TestObject), rod.ObjectType);
        }
        public void ObjectNameChild()
        {
            ObjectDefinitionBuilder odb = ObjectDefinitionBuilder.ChildObjectDefinition(odf, typeof(TestObject).FullName);
            ChildObjectDefinition   rod = odb.ObjectDefinition as ChildObjectDefinition;

            Assert.IsNotNull(rod);
            Assert.IsFalse(rod.HasObjectType);
            Assert.AreEqual(typeof(TestObject).FullName, rod.ParentName);
        }
Exemplo n.º 16
0
        /// <summary>The do parse.</summary>
        /// <param name="element">The element.</param>
        /// <param name="parserContext">The parser context.</param>
        /// <param name="builder">The builder.</param>
        protected override void DoParse(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder)
        {
            var connectionFactoryRef = element.GetAttribute(CONNECTION_FACTORY_ATTRIBUTE);

            if (string.IsNullOrWhiteSpace(connectionFactoryRef))
            {
                parserContext.ReaderContext.ReportFatalException(element, "A '" + CONNECTION_FACTORY_ATTRIBUTE + "' attribute must be set.");
            }

            if (!string.IsNullOrWhiteSpace(connectionFactoryRef))
            {
                // Use constructor with connectionFactory parameter
                builder.AddConstructorArgReference(connectionFactoryRef);
            }

            NamespaceUtils.SetValueIfAttributeDefined(builder, element, CHANNEL_TRANSACTED_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, QUEUE_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, EXCHANGE_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, ROUTING_KEY_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, REPLY_TIMEOUT_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, ENCODING_ATTRIBUTE);
            NamespaceUtils.SetReferenceIfAttributeDefined(builder, element, MESSAGE_CONVERTER_ATTRIBUTE);
            NamespaceUtils.SetReferenceIfAttributeDefined(builder, element, REPLY_QUEUE_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, MANDATORY_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, IMMEDIATE_ATTRIBUTE);
            NamespaceUtils.SetReferenceIfAttributeDefined(builder, element, RETURN_CALLBACK_ATTRIBUTE);
            NamespaceUtils.SetReferenceIfAttributeDefined(builder, element, CONFIRM_CALLBACK_ATTRIBUTE);

            IObjectDefinition replyContainer = null;
            XmlElement childElement = null;
            var childElements = element.SelectChildElementsByTagName(LISTENER_ELEMENT);
            if (childElements.Count > 0)
            {
                childElement = childElements[0] as XmlElement;
            }

            if (childElement != null)
            {
                replyContainer = this.ParseListener(childElement, element, parserContext);
                if (replyContainer != null)
                {
                    replyContainer.PropertyValues.Add("MessageListener", new RuntimeObjectReference(element.GetAttribute(ID_ATTRIBUTE)));
                    var replyContainerName = element.GetAttribute(ID_ATTRIBUTE) + ".ReplyListener";
                    parserContext.Registry.RegisterObjectDefinition(replyContainerName, replyContainer);
                }
            }

            if (replyContainer == null && element.HasAttribute(REPLY_QUEUE_ATTRIBUTE))
            {
                parserContext.ReaderContext.ReportFatalException(element, "For template '" + element.GetAttribute(ID_ATTRIBUTE) + "', when specifying a reply-queue, a <reply-listener/> element is required");
            }
            else if (replyContainer != null && !element.HasAttribute(REPLY_QUEUE_ATTRIBUTE))
            {
                parserContext.ReaderContext.ReportFatalException(element, "For template '" + element.GetAttribute(ID_ATTRIBUTE) + "', a <reply-listener/> element is not allowed if no 'reply-queue' is supplied");
            }
        }
        protected void DoParseInternal(XmlElement element, ObjectDefinitionBuilder builder)
        {
            builder.SetSingleton(false);
            string leftMenu = element.GetAttribute("menu");
            if(!string.IsNullOrEmpty(leftMenu)) builder.AddPropertyReference("Menu", leftMenu);
            string rightScreen = element.GetAttribute("screen");
            if(!string.IsNullOrEmpty(rightScreen)) builder.AddPropertyReference("MainScreen", rightScreen);
            foreach (XmlNode node in element.ChildNodes)
            {
                if (node is XmlComment) continue;

                switch (node.LocalName.ToLower())
                {
                    case "steps":
                        XmlNodeList steps = node.ChildNodes;
                        if (steps.Count > 0) // if flow is configured with a list of steps
                        {
                            IDictionary stepsDic = new System.Collections.Hashtable();
                            int count = 0;
                            foreach (XmlNode step in steps)
                            {
                                if(step is XmlComment) continue;
                                if (!step.LocalName.ToLower().Equals("step"))
                                {
                                    throw new Exception("Do not support node with name " + node.Name + " in config file.");
                                }
                                string key = "Step" + (++count).ToString();
                                stepsDic[key] = step.InnerText;
                            }

                            builder.AddPropertyValue("FlowSteps", stepsDic);
                        }
                        break;
                    case "session":
                        string sessionRefBeanName = node.InnerText;
                        if(!string.IsNullOrEmpty(sessionRefBeanName))
                        {
                            builder.AddDependsOn(sessionRefBeanName);
                            builder.AddPropertyReference("Session", sessionRefBeanName);
                        }

                        break;
                    case "menu":
                        string menuClassName = node.InnerText;
                        builder.AddPropertyValue("MenuClass", Type.GetType(menuClassName));
                        break;
                    case "screen":
                        string screenClassName = node.InnerText;
                        builder.AddPropertyValue("MainScreenClass", Type.GetType(screenClassName));
                        break;
                    default:
                        throw new Exception("Do not support node with name " + node.Name + " in config file.");
                        break;
                }
            }
        }
        /// <summary>The parse bindings.</summary>
        /// <param name="element">The element.</param>
        /// <param name="parserContext">The parser context.</param>
        /// <param name="builder">The builder.</param>
        /// <param name="exchangeName">The exchange name.</param>
        protected override void ParseBindings(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder, string exchangeName)
        {
            var backingType = element.GetAttribute(BACKING_TYPE_ATTRIBUTE);
            var bindings = element.SelectChildElementByTagName(DIRECT_BINDINGS_ELE);
            if (bindings != null && ExchangeTypes.Direct != backingType)
            {
                parserContext.ReaderContext.ReportFatalException(element, "Cannot have direct-bindings if backing-type not 'direct'");
            }

            if (bindings == null)
            {
                bindings = element.SelectChildElementByTagName(TOPIC_BINDINGS_ELE);
                if (bindings != null && !ExchangeTypes.Topic.Equals(backingType))
                {
                    parserContext.ReaderContext.ReportFatalException(element, "Cannot have topic-bindings if backing-type not 'topic'");
                }
            }

            if (bindings == null)
            {
                bindings = element.SelectChildElementByTagName(TOPIC_FANOUT_ELE);
                if (bindings != null && !ExchangeTypes.Fanout.Equals(backingType))
                {
                    parserContext.ReaderContext.ReportFatalException(element, "Cannot have fanout-bindings if backing-type not 'fanout'");
                }
            }

            if (bindings == null)
            {
                bindings = element.SelectChildElementByTagName(TOPIC_HEADERS_ELE);
                if (bindings != null && !ExchangeTypes.Headers.Equals(backingType))
                {
                    parserContext.ReaderContext.ReportFatalException(element, "Cannot have headers-bindings if backing-type not 'headers'");
                }
            }

            if (!string.IsNullOrWhiteSpace(backingType))
            {
                if (ExchangeTypes.Direct.Equals(backingType))
                {
                    this.DoParseBindings(parserContext, exchangeName, bindings, new DirectExchangeParser());
                }
                else if (ExchangeTypes.Topic.Equals(backingType))
                {
                    this.DoParseBindings(parserContext, exchangeName, bindings, new TopicExchangeParser());
                }
                else if (ExchangeTypes.Fanout.Equals(backingType))
                {
                    this.DoParseBindings(parserContext, exchangeName, bindings, new FanoutExchangeParser());
                }
                else if (ExchangeTypes.Headers.Equals(backingType))
                {
                    this.DoParseBindings(parserContext, exchangeName, bindings, new HeadersExchangeParser());
                }
            }
        }
        public void Simple()
        {
            GenericApplicationContext ctx = new GenericApplicationContext();
            IObjectDefinitionFactory  objectDefinitionFactory = new DefaultObjectDefinitionFactory();

            ObjectDefinitionBuilder builder = ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory,
                                                                                           typeof(TestObject));

            builder.AddPropertyValue("Age", 22)
            .AddPropertyValue("Name", "Joe")
            .AddPropertyReference("Spouse", "Spouse")
            .SetSingleton(false);

            ctx.RegisterObjectDefinition("TestObject", builder.ObjectDefinition);

            builder = ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, typeof(TestObject));

            IList friends = new ArrayList();

            friends.Add(new TestObject("Dan", 34));
            friends.Add(new TestObject("Mary", 33));
            builder.AddPropertyValue("Friends", friends)
            .AddConstructorArg("Susan")
            .AddConstructorArg(23)
            .SetSingleton(false);

            ctx.RegisterObjectDefinition("Spouse", builder.ObjectDefinition);



            TestObject to = ctx.GetObject("TestObject") as TestObject;

            Assert.IsNotNull(to);

            Assert.AreEqual("Joe", to.Name);
            Assert.AreEqual(22, to.Age);
            Assert.AreEqual(2, to.Spouse.Friends.Count);
            Assert.AreEqual(23, to.Spouse.Age);

            /*
             * AbstractApplicationContext ctx = ContextRegistry.GetContext() as AbstractApplicationContext;
             *
             *
             *
             * //XmlObjectFactory objectFactory = ctx.ObjectFactory as XmlObjectFactory;
             *
             * IObjectFactory objectFactory = ctx.ObjectFactory;
             *
             * //DefaultListableObjectFactory
             * if (objectFactory != null)
             * {
             *  Console.WriteLine("hi");
             * }
             * //objectFactory.RegisterObjectDefinition("TestObject", builder.ObjectDefinition);
             */
        }
 /*override*/
 protected static void PostProcess(ObjectDefinitionBuilder builder, XmlElement element)
 {
     string policyName = element.GetAttribute(REJECTION_POLICY_ATTRIBUTE);
     builder.AddPropertyValue("rejectedExecutionHandler", CreateRejectedExecutionHandler(policyName));
     builder.AddPropertyValue("corePoolSize", element.GetAttribute(CORE_SIZE_ATTRIBUTE));
     string maxSize = element.GetAttribute(MAX_SIZE_ATTRIBUTE);
     if(StringUtils.HasText(maxSize)) {
         builder.AddPropertyValue("maxPoolSize", maxSize);
     }
 }
        /// <summary>
        /// Parse the supplied XmlElement and populate the supplied ObjectDefinitionBuilder as required.
        /// </summary>
        /// <param name="xml">The obejct representation in XML.</param>
        /// <param name="builder">The builder used to build the object definition in Spring.</param>
        protected override void DoParse(XmlElement xml, ObjectDefinitionBuilder builder)
        {
            // all behaviours with config being parsed have @respondsTo
            string respondsTo = xml.GetAttribute("responds-to");
            builder.AddConstructorArg(respondsTo);

            // all view behaviours have @content-type
            string contentType = xml.GetAttribute("content-type");
            builder.AddConstructorArg(contentType);
        }
Exemplo n.º 22
0
        /// <summary>Adds the constructor arg value if attribute defined.</summary>
        /// <param name="builder">The builder.</param>
        /// <param name="element">The element.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <returns><c>true</c> if [is attribute defined] [the specified element]; otherwise, <c>false</c>.</returns>
        public static bool AddConstructorArgValueIfAttributeDefined(ObjectDefinitionBuilder builder, XmlElement element, string attributeName)
        {
            var value = element.GetAttribute(attributeName);
            if (!string.IsNullOrWhiteSpace(value))
            {
                builder.AddConstructorArg(new TypedStringValue(value));
                return true;
            }

            return false;
        }
        public void ObjectTypeWithFactoryMethod()
        {
            ObjectDefinitionBuilder odb =
                ObjectDefinitionBuilder.RootObjectDefinition(odf, typeof(TestObject), "Create");
            RootObjectDefinition rod = odb.ObjectDefinition as RootObjectDefinition;

            Assert.IsNotNull(rod);
            Assert.IsTrue(rod.HasObjectType);
            Assert.AreEqual(typeof(TestObject), rod.ObjectType);
            Assert.AreEqual("Create", rod.FactoryMethodName);
        }
Exemplo n.º 24
0
        /// <summary>Sets the value if attribute defined.</summary>
        /// <param name="builder">The builder.</param>
        /// <param name="element">The element.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>True if successful, else false.</returns>
        public static bool SetValueIfAttributeDefined(ObjectDefinitionBuilder builder, XmlElement element, string attributeName, string propertyName)
        {
            var attributeValue = element.GetAttribute(attributeName);
            if (!string.IsNullOrWhiteSpace(attributeValue))
            {
                builder.AddPropertyValue(propertyName, new TypedStringValue(attributeValue));
                return true;
            }

            return false;
        }
 protected override void DoParse(XmlElement element, ObjectDefinitionBuilder builder)
 {
     foreach(XmlAttribute attribute in element.Attributes) {
         if(IsEligibleAttribute(attribute.Name)) {
             string propertyName = Conventions.AttributeNameToPropertyName(attribute.LocalName);
             AssertUtils.State(StringUtils.HasText(propertyName), "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty.");
             builder.AddPropertyValue(propertyName, attribute.Value);
         }
     }
     PostProcess(builder, element);
 }
 /*override*/
 protected void PostProcess(ObjectDefinitionBuilder builder, XmlElement element)
 {
     string requestChannelRef = element.GetAttribute("request-channel");
     if(!StringUtils.HasText(requestChannelRef))
         throw new ArgumentException("a 'request-channel' reference is required");
     builder.AddPropertyReference("requestChannel", requestChannelRef);
     string replyChannel = element.GetAttribute("reply-channel");
     if(StringUtils.HasText(replyChannel)) {
         builder.AddPropertyReference("replyChannel", replyChannel);
     }
     DoPostProcess(builder, element);
 }
        /// <summary>
        /// Create a new <code>ObjectDefinitionBuilder</code> used to construct a child object definition..
        /// </summary>
        /// <param name="objectDefinitionFactory">The object definition factory.</param>
        /// <param name="parentObjectName">Name of the parent object.</param>
        /// <returns></returns>
        public static ObjectDefinitionBuilder ChildObjectDefinition(IObjectDefinitionFactory objectDefinitionFactory,
                                                                    string parentObjectName)
        {
            ObjectDefinitionBuilder builder = new ObjectDefinitionBuilder();

            builder.objectDefinitionFactory = objectDefinitionFactory;

            builder.objectDefinition =
                objectDefinitionFactory.CreateObjectDefinition(null, parentObjectName, AppDomain.CurrentDomain);

            return(builder);
        }
 protected override void DoParse(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder)
 {
     String taskExecutorRef = element.GetAttribute("task-executor");
     if(StringUtils.HasText(taskExecutorRef)) {
         builder.AddPropertyReference("taskExecutor", taskExecutorRef);
     }
     else {
         IExecutor taskExecutor = IntegrationContextUtils.CreateThreadPoolTaskExecutor(1, 10, 0, "event-multicaster-");
         builder.AddPropertyValue("taskExecutor", taskExecutor);
     }
     // TODO CopyOnWriteArraySet
     builder.AddPropertyValue("collectionClass", typeof (Set)); //CopyOnWriteArraySet));
 }
        public static object ParseRefOrNestedObjectDeclaration(ParserContext parserContext, XmlElement element,
            ObjectDefinitionBuilder builder, string refAttrName)
        {
            String attr = element.GetAttribute(refAttrName);
            bool hasRef = StringUtils.HasText(attr);

            XmlNodeList childNodes = element.ChildNodes;
            if (hasRef)
            {
                if (childNodes.Count > 0)
                {
                    //"either use the '" + refAttrName + "' attribute or a nested object declaration for '"
                    //+ element.getLocalName() + "' element, but not both", element);
                    parserContext.ReaderContext.ReportException(element, element.LocalName,
                                                                "either use the '" + refAttrName +
                                                                "' attribute or a nested object declaration for '"
                                                                + element.LocalName + "' element, but not both");
                }
                return new RuntimeObjectReference(attr);
            }

            if (childNodes.Count == 0)
            {
                parserContext.ReaderContext.ReportException(element, element.LocalName,
                                                            "specify either '" + refAttrName +
                                                            "' attribute or a nested object declaration for '"
                                                            + element.LocalName + "' element");
            }
            // nested parse nested object definition
            if (childNodes.Count == 1)
            {
                if (childNodes[0].NodeType == XmlNodeType.Element)
                {
                    XmlElement childElement = (XmlElement) childNodes[0];
                    return ParsePropertySubElement(childElement, builder.RawObjectDefinition, parserContext);
                }
            }

            ManagedList list = new ManagedList();

            for (int i = 0; i < childNodes.Count; i++)
            {
                XmlNode childNode = childNodes.Item(i);
                if (childNode != null && childNode.NodeType == XmlNodeType.Element)
                {
                    list.Add(ParsePropertySubElement((XmlElement) childNode, builder.RawObjectDefinition, parserContext));
                }
            }

            return list;
        }
        /// <summary>
        /// Create a new <code>ObjectDefinitionBuilder</code> used to construct a root object definition.
        /// </summary>
        /// <param name="objectDefinitionFactory">The object definition factory.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="factoryMethodName">Name of the factory method.</param>
        /// <returns>A new <code>ObjectDefinitionBuilder</code> instance.</returns>
        public static ObjectDefinitionBuilder RootObjectDefinition(IObjectDefinitionFactory objectDefinitionFactory,
                                                                   Type objectType, string factoryMethodName)
        {
            ObjectDefinitionBuilder builder = new ObjectDefinitionBuilder();

            builder.objectDefinitionFactory = objectDefinitionFactory;

            builder.objectDefinition =
                objectDefinitionFactory.CreateObjectDefinition(objectType.FullName, null, AppDomain.CurrentDomain);

            builder.objectDefinition.ObjectType        = objectType;
            builder.objectDefinition.FactoryMethodName = factoryMethodName;
            return(builder);
        }
        /// <summary>
        /// Create a new <code>ObjectDefinitionBuilder</code> used to construct a root object definition.
        /// </summary>
        /// <param name="objectDefinitionFactory">The object definition factory.</param>
        /// <param name="objectTypeName">Name of the object type.</param>
        /// <param name="factoryMethodName">Name of the factory method.</param>
        /// <returns>A new <code>ObjectDefinitionBuilder</code> instance.</returns>
        public static ObjectDefinitionBuilder RootObjectDefinition(IObjectDefinitionFactory objectDefinitionFactory,
                                                                   string objectTypeName,
                                                                   string factoryMethodName)
        {
            ObjectDefinitionBuilder builder = new ObjectDefinitionBuilder();

            builder.objectDefinitionFactory = objectDefinitionFactory;

            // Pass in null for parent name and also AppDomain to force object definition to be register by name and not type.
            builder.objectDefinition =
                objectDefinitionFactory.CreateObjectDefinition(objectTypeName, null, null);

            builder.objectDefinition.FactoryMethodName = factoryMethodName;

            return(builder);
        }
Exemplo n.º 32
0
        /// <summary>The do parse.</summary>
        /// <param name="element">The element.</param>
        /// <param name="parserContext">The parser context.</param>
        /// <param name="builder">The builder.</param>
        protected override void DoParse(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder)
        {
            if (!NamespaceUtils.IsAttributeDefined(element, "name") && !NamespaceUtils.IsAttributeDefined(element, ID_ATTRIBUTE))
            {
                parserContext.ReaderContext.ReportFatalException(element, "Queue must have either id or name (or both)");
            }

            NamespaceUtils.AddConstructorArgValueIfAttributeDefined(builder, element, "name");

            if (!NamespaceUtils.IsAttributeDefined(element, "name"))
            {
                if (this.AttributeHasIllegalOverride(element, DURABLE_ATTRIBUTE, "false")
                    || this.AttributeHasIllegalOverride(element, EXCLUSIVE_ATTRIBUTE, "true")
                    || this.AttributeHasIllegalOverride(element, AUTO_DELETE_ATTRIBUTE, "true"))
                {
                    parserContext.ReaderContext.ReportFatalException(element, "Anonymous queue cannot specify durable='true', exclusive='false' or auto-delete='false'");
                    return;
                }
            }
            else
            {
                NamespaceUtils.AddConstructorArgBooleanValueIfAttributeDefined(builder, element, DURABLE_ATTRIBUTE, false);
                NamespaceUtils.AddConstructorArgBooleanValueIfAttributeDefined(builder, element, EXCLUSIVE_ATTRIBUTE, false);
                NamespaceUtils.AddConstructorArgBooleanValueIfAttributeDefined(builder, element, AUTO_DELETE_ATTRIBUTE, false);
            }

            var queueArguments = element.GetAttribute(ARGUMENTS);
            var argumentsElement = element.SelectChildElementByTagName(ARGUMENTS);

            if (argumentsElement != null)
            {
                var parser = new ObjectDefinitionParserHelper(parserContext);
                if (!string.IsNullOrWhiteSpace(queueArguments))
                {
                    parserContext.ReaderContext.ReportFatalException(element, "Queue may have either a queue-attributes attribute or element, but not both");
                }

                var map = parser.ParseMapElement(argumentsElement, builder.RawObjectDefinition);
                builder.AddConstructorArg(map);
            }

            if (!string.IsNullOrWhiteSpace(queueArguments))
            {
                builder.AddConstructorArgReference(queueArguments);
            }
        }
 /// <summary>
 /// Parse a "poller" element to provide a reference for the target
 /// ObjectDefinitionBuilder. If the poller element does not contain a "ref"
 /// attribute, this will create and register a PollerMetadata instance and
 /// then add it as a property reference of the target builder.
 /// </summary>
 /// <param name="pollerElement">the "poller" element to parse</param>
 /// <param name="targetBuilder">the builder that expects the "trigger" property</param>
 /// <param name="parserContext">the parserContext for the target builder</param>
 public static void ConfigurePollerMetadata(XmlElement pollerElement, ObjectDefinitionBuilder targetBuilder, ParserContext parserContext)
 {
     string pollerMetadataRef;
     if(pollerElement.HasAttribute("ref")) {
         if(pollerElement.Attributes.Count != 1) {
             parserContext.ReaderContext.ReportException(pollerElement, "poller", "A 'poller' element that provides a 'ref' must have no other attributes.");
         }
         if(pollerElement.ChildNodes.Count != 0) {
             parserContext.ReaderContext.ReportException(pollerElement, "poller", "A 'poller' element that provides a 'ref' must have no child elements.");
         }
         pollerMetadataRef = pollerElement.GetAttribute("ref");
     }
     else {
         IObjectDefinition pollerDef = parserContext.ParserHelper.ParseCustomElement(pollerElement, targetBuilder.ObjectDefinition);
         pollerMetadataRef = parserContext.ReaderContext.RegisterWithGeneratedName(pollerDef);
     }
     targetBuilder.AddPropertyReference("pollerMetadata", pollerMetadataRef);
 }
 private void SetConvention(XmlElement element, ObjectDefinitionBuilder builder, string attribute, string property)
 {
     string value = string.Empty;
     try
     {
         value = element.GetAttribute(attribute);
         if (StringUtils.HasText(value))
         {
             Type type = TypeResolutionUtils.ResolveType(value);
             object instance = Activator.CreateInstance(type);
             builder.AddPropertyValue(property, instance);
         }
     }
     catch (TypeLoadException e)
     {
         throw new ObjectCreationException(string.Format("Convention of type '{0}' could not be loaded: {1}", attribute, value));
     }
 }
        public void ObjectTypeWithSimpleProperty()
        {
            string[] dependsOn          = new string[] { "A", "B", "C" };
            ObjectDefinitionBuilder odb = ObjectDefinitionBuilder.RootObjectDefinition(odf, typeof(TestObject));

            odb.SetSingleton(false).AddPropertyReference("Age", "15");
            foreach (string dep in dependsOn)
            {
                odb.AddDependsOn(dep);
            }

            RootObjectDefinition rod = odb.ObjectDefinition as RootObjectDefinition;

            Assert.IsNotNull(rod);
            Assert.IsFalse(rod.IsSingleton);
            Assert.AreEqual(typeof(TestObject), rod.ObjectType);
            Assert.AreEqual(dependsOn, rod.DependsOn, "DependsOn not as expected");
            Assert.IsTrue(rod.PropertyValues.Contains("Age"));
        }
        /// <summary>The do parse.</summary>
        /// <param name="element">The element.</param>
        /// <param name="parserContext">The parser context.</param>
        /// <param name="builder">The builder.</param>
        protected override void DoParse(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder)
        {
            var exchangeName = element.GetAttribute("name");
            builder.AddConstructorArg(new TypedStringValue(exchangeName));
            this.ParseBindings(element, parserContext, builder, exchangeName);

            NamespaceUtils.AddConstructorArgBooleanValueIfAttributeDefined(builder, element, DURABLE_ATTRIBUTE, true);
            NamespaceUtils.AddConstructorArgBooleanValueIfAttributeDefined(builder, element, AUTO_DELETE_ATTRIBUTE, false);

            var argumentsElement = element.SelectChildElementByTagName(ARGUMENTS_ELEMENT);

            if (argumentsElement != null)
            {
                var parser = new ObjectDefinitionParserHelper(parserContext);
                var map = parser.ParseMapElementToTypedDictionary(argumentsElement, builder.RawObjectDefinition);

                builder.AddConstructorArg(map);
            }
        }
        /// <summary>The do parse.</summary>
        /// <param name="element">The element.</param>
        /// <param name="parserContext">The parser context.</param>
        /// <param name="builder">The builder.</param>
        protected override void DoParse(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder)
        {
            if (element.HasAttribute(ADDRESSES) && (element.HasAttribute(HOST_ATTRIBUTE) || element.HasAttribute(PORT_ATTRIBUTE)))
            {
                parserContext.ReaderContext.ReportFatalException(element, "If the 'addresses' attribute is provided, a connection factory can not have 'host' or 'port' attributes.");
            }

            NamespaceUtils.AddConstructorArgParentRefIfAttributeDefined(builder, element, CONNECTION_FACTORY_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, CHANNEL_CACHE_SIZE_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, HOST_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, PORT_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, USER_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, PASSWORD_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, VIRTUAL_HOST_ATTRIBUTE);

            // TODO: Currently no .NET equivalent to the Java Task Executor that can be used by RabbitMQ.Client
            // NamespaceUtils.SetReferenceIfAttributeDefined(builder, element, EXECUTOR_ATTRIBUTE);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, ADDRESSES);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, PUBLISHER_CONFIRMS);
            NamespaceUtils.SetValueIfAttributeDefined(builder, element, PUBLISHER_RETURNS);
        }
Exemplo n.º 38
0
        /// <summary>The do parse.</summary>
        /// <param name="element">The element.</param>
        /// <param name="parserContext">The parser context.</param>
        /// <param name="builder">The builder.</param>
        protected override void DoParse(XmlElement element, ParserContext parserContext, ObjectDefinitionBuilder builder)
        {
            var connectionFactoryRef = element.GetAttribute(CONNECTION_FACTORY_ATTRIBUTE);

            // At least one of 'templateRef' or 'connectionFactoryRef' attribute must be set.
            if (string.IsNullOrWhiteSpace(connectionFactoryRef))
            {
                parserContext.ReaderContext.ReportFatalException(element, "A '" + CONNECTION_FACTORY_ATTRIBUTE + "' attribute must be set.");
            }

            if (!string.IsNullOrWhiteSpace(connectionFactoryRef))
            {
                // Use constructor with connectionFactory parameter
                builder.AddConstructorArgReference(connectionFactoryRef);
            }

            var attributeValue = element.GetAttribute(AUTO_STARTUP_ATTRIBUTE);
            if (!string.IsNullOrWhiteSpace(attributeValue))
            {
                builder.AddPropertyValue("AutoStartup", attributeValue);
            }
        }
 private void ParseSelectorChain(ObjectDefinitionBuilder builder, XmlElement element, ParserContext parserContext)
 {
     IntegrationNamespaceUtils.SetValueIfAttributeDefined(builder, element, "voting-strategy");
     ManagedList selectors = new ManagedList();
     selectors.ElementTypeName = typeof (IMessageSelector).FullName;
     XmlNodeList childNodes = element.ChildNodes;
     for(int i = 0; i < childNodes.Count; i++) {
         XmlNode child = childNodes.Item(i);
         if(child.NodeType == XmlNodeType.Element) {
             string nodeName = child.LocalName;
             if("selector".Equals(nodeName)) {
                 string refatr = ((XmlElement)child).GetAttribute("ref");
                 selectors.Add(new RuntimeObjectReference(refatr));
             }
             else if("selector-chain".Equals(nodeName)) {
                 ObjectDefinitionBuilder nestedBuilder = ObjectDefinitionBuilder.GenericObjectDefinition(GetObjectTypeName(null));
                 ParseSelectorChain(nestedBuilder, (XmlElement)child, parserContext);
                 string nestedBeanName = parserContext.ReaderContext.RegisterWithGeneratedName(nestedBuilder.ObjectDefinition);
                 selectors.Add(new RuntimeObjectReference(nestedBeanName));
             }
         }
     }
     builder.AddPropertyValue("selectors", selectors);
 }
 /// <summary>
 /// Populates the object definition property corresponding to the specified
 /// attributeName with the value of that attribute if it is defined in the
 /// given element.
 /// 
 /// <p>The property name will be the camel-case equivalent of the lower
 /// case hyphen separated attribute (e.g. the "foo-bar" attribute would
 /// match the "fooBar" property).
 /// <see cref="Conventions.AttributeNameToPropertyName"/>
 /// </summary>
 /// <param name="builder">the builder</param>
 /// <param name="element">the XML element where the attribute should be defined</param>
 /// <param name="attributeName">the name of the attribute whose value will be set</param>
 public static void SetValueIfAttributeDefined(ObjectDefinitionBuilder builder, XmlElement element, string attributeName)
 {
     SetValueIfAttributeDefined(builder, element, attributeName, Conventions.AttributeNameToPropertyName(attributeName));
 }