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    import javax.xml.bind.annotation.XmlType;
024    
025    import org.apache.camel.CamelContext;
026    import org.apache.camel.CamelContextAware;
027    import org.apache.camel.Endpoint;
028    import org.apache.camel.ProducerTemplate;
029    import org.apache.camel.impl.DefaultProducerTemplate;
030    import org.apache.camel.model.IdentifiedType;
031    import org.apache.camel.util.ServiceHelper;
032    
033    /**
034     * A factory for creating a new {@link org.apache.camel.ProducerTemplate}
035     * instance with a minimum of XML
036     *
037     * @version $Revision: 934375 $
038     */
039    @XmlAccessorType(XmlAccessType.FIELD)
040    public abstract class AbstractCamelProducerTemplateFactoryBean extends IdentifiedType implements CamelContextAware {
041        @XmlTransient
042        private ProducerTemplate template;
043        @XmlAttribute(required = false)
044        private String defaultEndpoint;
045        @XmlAttribute
046        private String camelContextId;
047        @XmlTransient
048        private CamelContext camelContext;
049        @XmlAttribute
050        private Integer maximumCacheSize;
051    
052        public void afterPropertiesSet() throws Exception {
053            if (camelContext == null && camelContextId != null) {
054                camelContext = getCamelContextWithId(camelContextId);
055            }
056            if (camelContext == null) {
057                throw new IllegalArgumentException("A CamelContext or a CamelContextId must be injected!");
058            }
059        }
060    
061        protected abstract CamelContext getCamelContextWithId(String camelContextId);
062    
063        public Object getObject() throws Exception {
064            CamelContext context = getCamelContext();
065            if (defaultEndpoint != null) {
066                Endpoint endpoint = context.getEndpoint(defaultEndpoint);
067                if (endpoint == null) {
068                    throw new IllegalArgumentException("No endpoint found for URI: " + defaultEndpoint);
069                } else {
070                    template = new DefaultProducerTemplate(context, endpoint);
071                }
072            } else {
073                template = new DefaultProducerTemplate(context);
074            }
075    
076            // set custom cache size if provided
077            if (maximumCacheSize != null) {
078                template.setMaximumCacheSize(maximumCacheSize);
079            }
080    
081            // must start it so its ready to use
082            ServiceHelper.startService(template);
083            return template;
084        }
085    
086        public Class getObjectType() {
087            return DefaultProducerTemplate.class;
088        }
089    
090        public boolean isSingleton() {
091            return true;
092        }
093    
094        public void destroy() throws Exception {
095            ServiceHelper.stopService(template);
096        }
097    
098        // Properties
099        // -------------------------------------------------------------------------
100        public CamelContext getCamelContext() {
101            return camelContext;
102        }
103    
104        public void setCamelContext(CamelContext camelContext) {
105            this.camelContext = camelContext;
106        }
107    
108        public String getDefaultEndpoint() {
109            return defaultEndpoint;
110        }
111    
112        /**
113         * Sets the default endpoint URI used by default for sending message exchanges
114         */
115        public void setDefaultEndpoint(String defaultEndpoint) {
116            this.defaultEndpoint = defaultEndpoint;
117        }
118    
119        public String getCamelContextId() {
120            return camelContextId;
121        }
122    
123        public void setCamelContextId(String camelContextId) {
124            this.camelContextId = camelContextId;
125        }
126    
127        public Integer getMaximumCacheSize() {
128            return maximumCacheSize;
129        }
130    
131        public void setMaximumCacheSize(Integer maximumCacheSize) {
132            this.maximumCacheSize = maximumCacheSize;
133        }
134    
135    
136    }