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.cxf.spring;
019    
020    import java.util.HashMap;
021    import java.util.Map;
022    import org.w3c.dom.Element;
023    
024    import org.apache.cxf.common.util.StringUtils;
025    import org.apache.cxf.configuration.spring.AbstractBeanDefinitionParser;
026    import org.springframework.beans.PropertyValue;
027    import org.springframework.beans.factory.BeanDefinitionStoreException;
028    import org.springframework.beans.factory.support.AbstractBeanDefinition;
029    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
030    import org.springframework.beans.factory.xml.ParserContext;
031    
032    
033    
034    /**
035     * 
036     */
037    public abstract class AbstractCxfBeanDefinitionParser extends AbstractBeanDefinitionParser {
038        
039        /**
040         * Override mapToProperty() to handle the '#' reference notation ourselves.  We put those 
041         * properties with '#' in property map and let component to invoke setProperties() on the
042         * endpoint. 
043         */
044        @Override
045        protected void mapToProperty(BeanDefinitionBuilder bean, String propertyName, String val) {
046            if (ID_ATTRIBUTE.equals(propertyName)) {
047                return;
048            }
049    
050            if (org.springframework.util.StringUtils.hasText(val)) {
051                if (val.startsWith("#")) {
052                    Map<String, Object> map = getPropertyMap(bean, true);
053                    map.put(propertyName, val);
054                } else {
055                    bean.addPropertyValue(propertyName, val);
056                }
057            }
058            
059        }
060    
061        @Override
062        protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
063            super.doParse(element, ctx, bean);
064            bean.setLazyInit(false);
065            // put the bean id into the property map
066            Map<String, Object> map = getPropertyMap(bean, true);
067            map.put("beanId", resolveId(element, bean.getBeanDefinition(), ctx));        
068        }
069    
070        @Override
071        protected String resolveId(Element elem,
072                                   AbstractBeanDefinition definition,
073                                   ParserContext ctx)
074            throws BeanDefinitionStoreException {
075            String id = super.resolveId(elem, definition, ctx);        
076            
077            if (StringUtils.isEmpty(id)) {
078                throw new BeanDefinitionStoreException("The bean id is needed.");
079            }       
080            return id;
081        }
082    
083        @Override
084        protected boolean hasBusProperty() {
085            return true;
086        }
087    
088        @SuppressWarnings("unchecked")
089        protected Map<String, Object> getPropertyMap(BeanDefinitionBuilder bean, boolean lazyInstantiation) {
090            PropertyValue propertyValue = (PropertyValue)bean.getBeanDefinition().getPropertyValues()
091                .getPropertyValue("properties");
092            
093            Map<String, Object> map = null;
094            if (propertyValue == null) {
095                if (lazyInstantiation) {
096                    map = new HashMap<String, Object>();
097                    bean.addPropertyValue("properties", map);
098                }
099            } else {
100                map = (Map<String, Object>)propertyValue.getValue();
101            }
102            return map;
103        }
104    
105    }