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.XmlRootElement;
023 import javax.xml.bind.annotation.XmlTransient;
024 import javax.xml.bind.annotation.XmlType;
025
026 import org.apache.camel.CamelContext;
027 import org.apache.camel.CamelContextAware;
028 import org.apache.camel.ConsumerTemplate;
029 import org.apache.camel.impl.DefaultConsumerTemplate;
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.ConsumerTemplate}
035 * instance with a minimum of XML
036 *
037 * @version $Revision: 934375 $
038 */
039 @XmlAccessorType(XmlAccessType.FIELD)
040 public abstract class AbstractCamelConsumerTemplateFactoryBean extends IdentifiedType implements CamelContextAware {
041 @XmlTransient
042 private ConsumerTemplate template;
043 @XmlAttribute
044 private String camelContextId;
045 @XmlTransient
046 private CamelContext camelContext;
047 @XmlAttribute
048 private Integer maximumCacheSize;
049
050 public void afterPropertiesSet() throws Exception {
051 if (camelContext == null && camelContextId != null) {
052 camelContext = getCamelContextWithId(camelContextId);
053 }
054 if (camelContext == null) {
055 throw new IllegalArgumentException("A CamelContext or a CamelContextId must be injected!");
056 }
057 }
058
059 protected abstract CamelContext getCamelContextWithId(String camelContextId);
060
061 public Object getObject() throws Exception {
062 template = new DefaultConsumerTemplate(camelContext);
063
064 // set custom cache size if provided
065 if (maximumCacheSize != null) {
066 template.setMaximumCacheSize(maximumCacheSize);
067 }
068
069 // must start it so its ready to use
070 ServiceHelper.startService(template);
071 return template;
072 }
073
074 public Class getObjectType() {
075 return DefaultConsumerTemplate.class;
076 }
077
078 public boolean isSingleton() {
079 return true;
080 }
081
082 public void destroy() throws Exception {
083 ServiceHelper.stopService(template);
084 }
085
086 // Properties
087 // -------------------------------------------------------------------------
088
089 public CamelContext getCamelContext() {
090 return camelContext;
091 }
092
093 public void setCamelContext(CamelContext camelContext) {
094 this.camelContext = camelContext;
095 }
096
097 public String getCamelContextId() {
098 return camelContextId;
099 }
100
101 public void setCamelContextId(String camelContextId) {
102 this.camelContextId = camelContextId;
103 }
104
105 public Integer getMaximumCacheSize() {
106 return maximumCacheSize;
107 }
108
109 public void setMaximumCacheSize(Integer maximumCacheSize) {
110 this.maximumCacheSize = maximumCacheSize;
111 }
112 }