001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.camel.component.spring.integration.adapter.config;
019
020 import org.w3c.dom.Attr;
021 import org.w3c.dom.Element;
022 import org.w3c.dom.NamedNodeMap;
023 import org.w3c.dom.Node;
024 import org.w3c.dom.NodeList;
025
026 import org.apache.camel.util.ObjectHelper;
027 import org.springframework.beans.factory.config.BeanDefinition;
028 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
029 import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
030 import org.springframework.beans.factory.xml.ParserContext;
031 import org.springframework.util.StringUtils;
032
033 /**
034 * This BeanDefinition paraser help to inject the camel context into the beans
035 *
036 * @version $Revision: 816070 $
037 */
038 public class AbstractCamelContextBeanDefinitionParaser extends AbstractSingleBeanDefinitionParser {
039 private static final String DEFAULT_CAMEL_CONTEXT_NAME = "camelContext";
040
041 private String getContextId(String contextId) {
042 if (ObjectHelper.isEmpty(contextId)) {
043 //Set the contextId default value here
044 return DEFAULT_CAMEL_CONTEXT_NAME;
045 } else {
046 return contextId;
047 }
048 }
049
050 protected void mapToProperty(BeanDefinitionBuilder bean, String propertyName, String val) {
051 if (ID_ATTRIBUTE.equals(propertyName)) {
052 return;
053 }
054
055 if (StringUtils.hasText(val)) {
056 if (val.startsWith("#")) {
057 bean.addPropertyReference(propertyName, val.substring(1));
058 } else {
059 bean.addPropertyValue(propertyName, val);
060 }
061 }
062 }
063
064 protected void wireCamelContext(BeanDefinitionBuilder bean, String camelContextId) {
065 bean.addPropertyReference("camelContext", camelContextId);
066 }
067
068 protected void parseAttributes(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
069 NamedNodeMap atts = element.getAttributes();
070
071 for (int i = 0; i < atts.getLength(); i++) {
072 Attr node = (Attr) atts.item(i);
073 String val = node.getValue();
074 String name = node.getLocalName();
075
076 if (name.equals("requestChannel") || name.equals("replyChannel")) {
077 bean.addPropertyReference(name, val);
078 } else {
079 mapToProperty(bean, name, val);
080 }
081 }
082 }
083
084 protected void parseCamelContext(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
085 NodeList children = element.getChildNodes();
086 for (int i = 0; i < children.getLength(); i++) {
087 Node n = children.item(i);
088 if (n.getNodeType() == Node.ELEMENT_NODE) {
089 String name = n.getLocalName();
090 if ("camelContext".equals(name)) {
091 // Parser the camel context
092 BeanDefinition bd = ctx.getDelegate().parseCustomElement((Element)n);
093 // Get the inner camel context id
094 String contextId = (String)bd.getPropertyValues().getPropertyValue("id").getValue();
095 wireCamelContext(bean, getContextId(contextId));
096 } else if ("camelContextRef".equals(name)) {
097 String contextId = n.getTextContent();
098 wireCamelContext(bean, getContextId(contextId));
099 }
100 }
101 }
102 }
103
104 protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
105 parseAttributes(element, ctx, bean);
106 parseCamelContext(element, ctx, bean);
107
108 }
109
110 }