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    package org.apache.camel.spring;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlAttribute;
022    import javax.xml.bind.annotation.XmlRootElement;
023    import javax.xml.bind.annotation.XmlTransient;
024    
025    import org.apache.camel.CamelContext;
026    import org.apache.camel.CamelContextAware;
027    import org.apache.camel.Endpoint;
028    import org.apache.camel.impl.DefaultProducerTemplate;
029    import org.apache.camel.model.IdentifiedType;
030    import org.springframework.beans.factory.FactoryBean;
031    import org.springframework.beans.factory.InitializingBean;
032    
033    /**
034     * A Spring {@link FactoryBean} for creating a new {@link org.apache.camel.ProducerTemplate}
035     * instance with a minimum of XML
036     * 
037     * @version $Revision: 679972 $
038     */
039    @XmlRootElement(name = "template")
040    @XmlAccessorType(XmlAccessType.FIELD)
041    public class CamelTemplateFactoryBean extends IdentifiedType implements FactoryBean, InitializingBean, CamelContextAware {
042        @XmlAttribute(required = false)
043        private String defaultEndpoint;
044        @XmlTransient
045        private CamelContext camelContext;
046    
047        public void afterPropertiesSet() throws Exception {
048            if (camelContext == null) {
049                throw new IllegalArgumentException("A CamelContext must be injected!");
050            }
051        }
052    
053        public Object getObject() throws Exception {
054            CamelContext context = getCamelContext();
055            if (defaultEndpoint != null) {
056                Endpoint endpoint = context.getEndpoint(defaultEndpoint);
057                if (endpoint == null) {
058                    throw new IllegalArgumentException("No endpoint found for URI: " + defaultEndpoint);
059                } else {
060                    return new DefaultProducerTemplate(context, endpoint);
061                }
062            }
063            return new DefaultProducerTemplate(context);
064        }
065    
066        public Class getObjectType() {
067            return DefaultProducerTemplate.class;
068        }
069    
070        public boolean isSingleton() {
071            return true;
072        }
073    
074        // Properties
075        // -------------------------------------------------------------------------
076        public CamelContext getCamelContext() {
077            return camelContext;
078        }
079    
080        public void setCamelContext(CamelContext camelContext) {
081            this.camelContext = camelContext;
082        }
083    
084        public String getDefaultEndpoint() {
085            return defaultEndpoint;
086        }
087    
088        /**
089         * Sets the default endpoint URI used by default for sending message
090         * exchanges
091         */
092        public void setDefaultEndpoint(String defaultEndpoint) {
093            this.defaultEndpoint = defaultEndpoint;
094        }
095    }