001: /*
002: * $Id: AbstractMuleNamespaceHandler.java 11118 2008-02-28 15:15:13Z acooke $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010: package org.mule.config.spring.handlers;
011:
012: import org.mule.config.spring.factories.InboundEndpointFactoryBean;
013: import org.mule.config.spring.factories.OutboundEndpointFactoryBean;
014: import org.mule.config.spring.parsers.MuleDefinitionParser;
015: import org.mule.config.spring.parsers.MuleDefinitionParserConfiguration;
016: import org.mule.config.spring.parsers.PostProcessor;
017: import org.mule.config.spring.parsers.PreProcessor;
018: import org.mule.config.spring.parsers.assembly.configuration.ValueMap;
019: import org.mule.config.spring.parsers.generic.MuleOrphanDefinitionParser;
020: import org.mule.config.spring.parsers.specific.endpoint.TransportEndpointDefinitionParser;
021: import org.mule.config.spring.parsers.specific.endpoint.TransportGlobalEndpointDefinitionParser;
022: import org.mule.config.spring.parsers.specific.endpoint.support.AddressedEndpointDefinitionParser;
023:
024: import java.util.HashSet;
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.Set;
028:
029: import org.springframework.beans.factory.config.BeanDefinition;
030: import org.springframework.beans.factory.xml.BeanDefinitionParser;
031: import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
032: import org.springframework.beans.factory.xml.ParserContext;
033: import org.w3c.dom.Element;
034:
035: /**
036: * This Namespace handler extends the default Spring {@link org.springframework.beans.factory.xml.NamespaceHandlerSupport}
037: * to allow certain elements in document to be ignored by the handler.
038: */
039: public abstract class AbstractMuleNamespaceHandler extends
040: NamespaceHandlerSupport {
041:
042: public static final String GLOBAL_ENDPOINT = "endpoint";
043: public static final String INBOUND_ENDPOINT = "inbound-endpoint";
044: public static final String OUTBOUND_ENDPOINT = "outbound-endpoint";
045:
046: /**
047: * @param name The name of the element to be ignored.
048: */
049: protected final void registerIgnoredElement(String name) {
050: registerBeanDefinitionParser(name,
051: new IgnoredDefinitionParser());
052: }
053:
054: protected MuleDefinitionParserConfiguration registerConnectorDefinitionParser(
055: Class connectorClass) {
056: return registerConnectorDefinitionParser(new MuleOrphanDefinitionParser(
057: connectorClass, true));
058: }
059:
060: protected MuleDefinitionParserConfiguration registerConnectorDefinitionParser(
061: MuleDefinitionParser parser) {
062: registerBeanDefinitionParser("connector", parser);
063: return parser;
064: }
065:
066: protected MuleDefinitionParserConfiguration registerMuleBeanDefinitionParser(
067: String name, MuleDefinitionParser parser) {
068: registerBeanDefinitionParser(name, parser);
069: return parser;
070: }
071:
072: protected MuleDefinitionParserConfiguration registerStandardTransportEndpoints(
073: String protocol, String[] requiredAttributes) {
074: return new RegisteredMdps(protocol,
075: AddressedEndpointDefinitionParser.PROTOCOL,
076: requiredAttributes);
077: }
078:
079: protected MuleDefinitionParserConfiguration registerMetaTransportEndpoints(
080: String protocol) {
081: return new RegisteredMdps(protocol,
082: AddressedEndpointDefinitionParser.META, new String[] {});
083: }
084:
085: private class IgnoredDefinitionParser implements
086: BeanDefinitionParser {
087: public BeanDefinition parse(Element element,
088: ParserContext parserContext) {
089: return null;
090: }
091: }
092:
093: private class RegisteredMdps implements
094: MuleDefinitionParserConfiguration {
095:
096: private Set bdps = new HashSet();
097:
098: private RegisteredMdps(String protocol, boolean isMeta,
099: String[] requiredAttributes) {
100: registerBeanDefinitionParser("endpoint",
101: add(new TransportGlobalEndpointDefinitionParser(
102: protocol, isMeta, requiredAttributes,
103: new String[] {})));
104: registerBeanDefinitionParser("inbound-endpoint",
105: add(new TransportEndpointDefinitionParser(protocol,
106: isMeta, InboundEndpointFactoryBean.class,
107: requiredAttributes, new String[] {})));
108: registerBeanDefinitionParser("outbound-endpoint",
109: add(new TransportEndpointDefinitionParser(protocol,
110: isMeta, OutboundEndpointFactoryBean.class,
111: requiredAttributes, new String[] {})));
112: }
113:
114: private MuleDefinitionParser add(MuleDefinitionParser bdp) {
115: bdps.add(bdp);
116: return bdp;
117: }
118:
119: public MuleDefinitionParserConfiguration registerPreProcessor(
120: PreProcessor preProcessor) {
121: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
122: ((MuleDefinitionParserConfiguration) bdp.next())
123: .registerPreProcessor(preProcessor);
124: }
125: return this ;
126: }
127:
128: public MuleDefinitionParserConfiguration registerPostProcessor(
129: PostProcessor postProcessor) {
130: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
131: ((MuleDefinitionParserConfiguration) bdp.next())
132: .registerPostProcessor(postProcessor);
133: }
134: return this ;
135: }
136:
137: public MuleDefinitionParserConfiguration addReference(
138: String propertyName) {
139: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
140: ((MuleDefinitionParserConfiguration) bdp.next())
141: .addReference(propertyName);
142: }
143: return this ;
144: }
145:
146: public MuleDefinitionParserConfiguration addMapping(
147: String propertyName, Map mappings) {
148: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
149: ((MuleDefinitionParserConfiguration) bdp.next())
150: .addMapping(propertyName, mappings);
151: }
152: return this ;
153: }
154:
155: public MuleDefinitionParserConfiguration addMapping(
156: String propertyName, String mappings) {
157: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
158: ((MuleDefinitionParserConfiguration) bdp.next())
159: .addMapping(propertyName, mappings);
160: }
161: return this ;
162: }
163:
164: public MuleDefinitionParserConfiguration addMapping(
165: String propertyName, ValueMap mappings) {
166: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
167: ((MuleDefinitionParserConfiguration) bdp.next())
168: .addMapping(propertyName, mappings);
169: }
170: return this ;
171: }
172:
173: public MuleDefinitionParserConfiguration addAlias(String alias,
174: String propertyName) {
175: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
176: ((MuleDefinitionParserConfiguration) bdp.next())
177: .addAlias(alias, propertyName);
178: }
179: return this ;
180: }
181:
182: public MuleDefinitionParserConfiguration addCollection(
183: String propertyName) {
184: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
185: ((MuleDefinitionParserConfiguration) bdp.next())
186: .addCollection(propertyName);
187: }
188: return this ;
189: }
190:
191: public MuleDefinitionParserConfiguration addIgnored(
192: String propertyName) {
193: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
194: ((MuleDefinitionParserConfiguration) bdp.next())
195: .addIgnored(propertyName);
196: }
197: return this ;
198: }
199:
200: public MuleDefinitionParserConfiguration removeIgnored(
201: String propertyName) {
202: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
203: ((MuleDefinitionParserConfiguration) bdp.next())
204: .removeIgnored(propertyName);
205: }
206: return this ;
207: }
208:
209: public MuleDefinitionParserConfiguration setIgnoredDefault(
210: boolean ignoreAll) {
211: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
212: ((MuleDefinitionParserConfiguration) bdp.next())
213: .setIgnoredDefault(ignoreAll);
214: }
215: return this ;
216: }
217:
218: public MuleDefinitionParserConfiguration addBeanFlag(String flag) {
219: for (Iterator bdp = bdps.iterator(); bdp.hasNext();) {
220: ((MuleDefinitionParserConfiguration) bdp.next())
221: .addBeanFlag(flag);
222: }
223: return this;
224: }
225:
226: }
227:
228: }
|