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