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.core.xml;
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.XmlTransient;
023    
024    import org.apache.camel.CamelContext;
025    import org.apache.camel.Endpoint;
026    import org.apache.camel.ProducerTemplate;
027    import org.apache.camel.impl.DefaultProducerTemplate;
028    import org.apache.camel.util.ServiceHelper;
029    
030    /**
031     * A factory for creating a new {@link org.apache.camel.ProducerTemplate}
032     * instance with a minimum of XML
033     *
034     * @version 
035     */
036    @XmlAccessorType(XmlAccessType.FIELD)
037    public abstract class AbstractCamelProducerTemplateFactoryBean extends AbstractCamelFactoryBean<ProducerTemplate> {
038        @XmlTransient
039        private ProducerTemplate template;
040        @XmlAttribute(required = false)
041        private String defaultEndpoint;
042        @XmlAttribute
043        private Integer maximumCacheSize;
044    
045        public ProducerTemplate getObject() throws Exception {
046            CamelContext context = getCamelContext();
047            if (defaultEndpoint != null) {
048                Endpoint endpoint = context.getEndpoint(defaultEndpoint);
049                if (endpoint == null) {
050                    throw new IllegalArgumentException("No endpoint found for URI: " + defaultEndpoint);
051                } else {
052                    template = new DefaultProducerTemplate(context, endpoint);
053                }
054            } else {
055                template = new DefaultProducerTemplate(context);
056            }
057    
058            // set custom cache size if provided
059            if (maximumCacheSize != null) {
060                template.setMaximumCacheSize(maximumCacheSize);
061            }
062    
063            // must start it so its ready to use
064            ServiceHelper.startService(template);
065            return template;
066        }
067    
068        public Class<DefaultProducerTemplate> getObjectType() {
069            return DefaultProducerTemplate.class;
070        }
071    
072        public void destroy() throws Exception {
073            ServiceHelper.stopService(template);
074        }
075    
076        // Properties
077        // -------------------------------------------------------------------------
078    
079        public String getDefaultEndpoint() {
080            return defaultEndpoint;
081        }
082    
083        /**
084         * Sets the default endpoint URI used by default for sending message exchanges
085         */
086        public void setDefaultEndpoint(String defaultEndpoint) {
087            this.defaultEndpoint = defaultEndpoint;
088        }
089    
090        public Integer getMaximumCacheSize() {
091            return maximumCacheSize;
092        }
093    
094        public void setMaximumCacheSize(Integer maximumCacheSize) {
095            this.maximumCacheSize = maximumCacheSize;
096        }
097    
098    }