| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.spring; |
| 18 |
|
|
| 19 |
|
import java.util.ArrayList; |
| 20 |
|
import java.util.List; |
| 21 |
|
|
| 22 |
|
import javax.xml.bind.annotation.XmlAccessType; |
| 23 |
|
import javax.xml.bind.annotation.XmlAccessorType; |
| 24 |
|
import javax.xml.bind.annotation.XmlElement; |
| 25 |
|
import javax.xml.bind.annotation.XmlElements; |
| 26 |
|
import javax.xml.bind.annotation.XmlRootElement; |
| 27 |
|
import javax.xml.bind.annotation.XmlTransient; |
| 28 |
|
|
| 29 |
|
import org.apache.camel.RuntimeCamelException; |
| 30 |
|
import org.apache.camel.builder.RouteBuilder; |
| 31 |
|
import org.apache.camel.model.IdentifiedType; |
| 32 |
|
import org.apache.camel.model.RouteContainer; |
| 33 |
|
import org.apache.camel.model.RouteType; |
| 34 |
|
import org.apache.commons.logging.Log; |
| 35 |
|
import org.apache.commons.logging.LogFactory; |
| 36 |
|
|
| 37 |
|
import org.springframework.beans.factory.DisposableBean; |
| 38 |
|
import org.springframework.beans.factory.FactoryBean; |
| 39 |
|
import org.springframework.beans.factory.InitializingBean; |
| 40 |
|
import org.springframework.context.ApplicationContext; |
| 41 |
|
import org.springframework.context.ApplicationContextAware; |
| 42 |
|
import org.springframework.context.ApplicationEvent; |
| 43 |
|
import org.springframework.context.ApplicationListener; |
| 44 |
|
import org.springframework.context.event.ContextRefreshedEvent; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
@XmlRootElement(name = "camelContext") |
| 55 |
|
@XmlAccessorType(XmlAccessType.FIELD) |
| 56 |
109 |
public class CamelContextFactoryBean extends IdentifiedType implements RouteContainer, FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener { |
| 57 |
1 |
private static final Log LOG = LogFactory.getLog(CamelContextFactoryBean.class); |
| 58 |
|
@XmlElement(name = "package", required = false) |
| 59 |
109 |
private String[] packages = {}; |
| 60 |
|
@XmlElements({@XmlElement(name = "beanPostProcessor", type = CamelBeanPostProcessor.class, required = false), @XmlElement(name = "proxy", type = CamelProxyFactoryType.class, required = false), |
| 61 |
|
@XmlElement(name = "export", type = CamelServiceExporterType.class, required = false) }) |
| 62 |
|
private List beans; |
| 63 |
|
@XmlElement(name = "endpoint", required = false) |
| 64 |
|
private List<EndpointFactoryBean> endpoints; |
| 65 |
|
@XmlElement(name = "route", required = false) |
| 66 |
109 |
private List<RouteType> routes = new ArrayList<RouteType>(); |
| 67 |
|
@XmlTransient |
| 68 |
|
private SpringCamelContext context; |
| 69 |
|
@XmlTransient |
| 70 |
|
private RouteBuilder routeBuilder; |
| 71 |
|
@XmlTransient |
| 72 |
109 |
private List<RouteBuilder> additionalBuilders = new ArrayList<RouteBuilder>(); |
| 73 |
|
@XmlTransient |
| 74 |
|
private ApplicationContext applicationContext; |
| 75 |
|
|
| 76 |
|
public Object getObject() throws Exception { |
| 77 |
54 |
return getContext(); |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
public Class getObjectType() { |
| 81 |
233 |
return SpringCamelContext.class; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
public boolean isSingleton() { |
| 85 |
214 |
return true; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
public void afterPropertiesSet() throws Exception { |
| 89 |
|
|
| 90 |
|
|
| 91 |
56 |
getContext(); |
| 92 |
|
|
| 93 |
56 |
LOG.debug("Found JAXB created routes: " + getRoutes()); |
| 94 |
56 |
String[] names = applicationContext.getBeanNamesForType(SpringInstrumentationAgent.class); |
| 95 |
56 |
if (names.length == 1) { |
| 96 |
3 |
applicationContext.getBean(names[0], SpringInstrumentationAgent.class); |
| 97 |
|
} |
| 98 |
|
|
| 99 |
56 |
findRouteBuiders(); |
| 100 |
56 |
installRoutes(); |
| 101 |
56 |
} |
| 102 |
|
|
| 103 |
|
public void destroy() throws Exception { |
| 104 |
28 |
getContext().stop(); |
| 105 |
28 |
} |
| 106 |
|
|
| 107 |
|
public void onApplicationEvent(ApplicationEvent event) { |
| 108 |
3 |
if (LOG.isDebugEnabled()) { |
| 109 |
0 |
LOG.debug("Publishing event: " + event); |
| 110 |
|
} |
| 111 |
|
|
| 112 |
3 |
if (event instanceof ContextRefreshedEvent) { |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
try { |
| 116 |
3 |
LOG.debug("Starting the context now!"); |
| 117 |
3 |
getContext().start(); |
| 118 |
0 |
} catch (Exception e) { |
| 119 |
0 |
throw new RuntimeCamelException(e); |
| 120 |
3 |
} |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
3 |
} |
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
public SpringCamelContext getContext() throws Exception { |
| 130 |
193 |
if (context == null) { |
| 131 |
56 |
context = new SpringCamelContext(getApplicationContext()); |
| 132 |
|
} |
| 133 |
193 |
return context; |
| 134 |
|
} |
| 135 |
|
|
| 136 |
|
public void setContext(SpringCamelContext context) { |
| 137 |
0 |
this.context = context; |
| 138 |
0 |
} |
| 139 |
|
|
| 140 |
|
public List<RouteType> getRoutes() { |
| 141 |
109 |
return routes; |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
public void setRoutes(List<RouteType> routes) { |
| 145 |
53 |
this.routes = routes; |
| 146 |
53 |
} |
| 147 |
|
|
| 148 |
|
public RouteBuilder getRouteBuilder() { |
| 149 |
0 |
return routeBuilder; |
| 150 |
|
} |
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
public void setRouteBuilder(RouteBuilder routeBuilder) { |
| 157 |
0 |
this.routeBuilder = routeBuilder; |
| 158 |
0 |
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
public void setRouteBuilders(RouteBuilder[] builders) { |
| 165 |
0 |
for (RouteBuilder builder : builders) { |
| 166 |
0 |
additionalBuilders.add(builder); |
| 167 |
|
} |
| 168 |
0 |
} |
| 169 |
|
|
| 170 |
|
public ApplicationContext getApplicationContext() { |
| 171 |
56 |
return applicationContext; |
| 172 |
|
} |
| 173 |
|
|
| 174 |
|
public void setApplicationContext(ApplicationContext applicationContext) { |
| 175 |
56 |
this.applicationContext = applicationContext; |
| 176 |
56 |
} |
| 177 |
|
|
| 178 |
|
public String[] getPackages() { |
| 179 |
57 |
return packages; |
| 180 |
|
} |
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
public void setPackages(String[] packages) { |
| 191 |
7 |
this.packages = packages; |
| 192 |
7 |
} |
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
protected void installRoutes() throws Exception { |
| 201 |
56 |
for (RouteBuilder routeBuilder : additionalBuilders) { |
| 202 |
6 |
getContext().addRoutes(routeBuilder); |
| 203 |
6 |
} |
| 204 |
56 |
if (routeBuilder != null) { |
| 205 |
0 |
getContext().addRoutes(routeBuilder); |
| 206 |
|
} |
| 207 |
56 |
for (RouteType route : routes) { |
| 208 |
39 |
route.addRoutes(getContext()); |
| 209 |
39 |
} |
| 210 |
56 |
} |
| 211 |
|
|
| 212 |
|
|
| 213 |
|
|
| 214 |
|
|
| 215 |
|
|
| 216 |
|
protected void findRouteBuiders() throws Exception, InstantiationException { |
| 217 |
56 |
if (packages != null && packages.length > 0) { |
| 218 |
7 |
RouteBuilderFinder finder = new RouteBuilderFinder(getContext(), packages); |
| 219 |
7 |
finder.appendBuilders(additionalBuilders); |
| 220 |
|
} |
| 221 |
56 |
} |
| 222 |
|
|
| 223 |
|
} |