1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.builder; |
18 |
|
|
19 |
|
import java.util.ArrayList; |
20 |
|
import java.util.List; |
21 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
22 |
|
|
23 |
|
import org.apache.camel.CamelContext; |
24 |
|
import org.apache.camel.Endpoint; |
25 |
|
import org.apache.camel.Exchange; |
26 |
|
import org.apache.camel.Processor; |
27 |
|
import org.apache.camel.Route; |
28 |
|
import org.apache.camel.impl.DefaultCamelContext; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
public abstract class RouteBuilder extends BuilderSupport { |
37 |
43 |
private List<FromBuilder> fromBuilders = new ArrayList<FromBuilder>(); |
38 |
43 |
private AtomicBoolean initalized = new AtomicBoolean(false); |
39 |
43 |
private List<Route> routes = new ArrayList<Route>(); |
40 |
|
|
41 |
|
protected RouteBuilder() { |
42 |
43 |
this(null); |
43 |
43 |
} |
44 |
|
|
45 |
|
protected RouteBuilder(CamelContext context) { |
46 |
43 |
super(context); |
47 |
43 |
} |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
public abstract void configure() throws Exception; |
53 |
|
|
54 |
|
@Fluent |
55 |
|
public FromBuilder from( @FluentArg("uri") String uri) { |
56 |
54 |
if( uri == null ) { |
57 |
0 |
throw new IllegalArgumentException("uri parameter cannot be null"); |
58 |
|
} |
59 |
54 |
Endpoint endpoint = endpoint(uri); |
60 |
54 |
if( endpoint == null ) { |
61 |
0 |
throw new IllegalArgumentException("uri '"+uri+"' could not be resolved."); |
62 |
|
} |
63 |
54 |
return from(endpoint); |
64 |
|
} |
65 |
|
|
66 |
|
@Fluent |
67 |
|
public FromBuilder from( @FluentArg("ref") Endpoint endpoint) { |
68 |
54 |
FromBuilder answer = new FromBuilder(this, endpoint); |
69 |
54 |
addFromBuilder(answer); |
70 |
54 |
return answer; |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
public RouteBuilder errorHandler(ErrorHandlerBuilder errorHandlerBuilder) { |
80 |
0 |
setErrorHandlerBuilder(errorHandlerBuilder); |
81 |
0 |
return this; |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
public RouteBuilder inheritErrorHandler(boolean value) { |
91 |
1 |
setInheritErrorHandler(value); |
92 |
1 |
return this; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
public CamelContext getContext() { |
98 |
110 |
CamelContext context = super.getContext(); |
99 |
110 |
if (context == null) { |
100 |
12 |
context = createContainer(); |
101 |
12 |
setContext(context); |
102 |
|
} |
103 |
110 |
return context; |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
public List<Route> getRouteList() throws Exception { |
110 |
43 |
checkInitialized(); |
111 |
43 |
return routes; |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
public List<FromBuilder> getFromBuilders() throws Exception { |
118 |
0 |
checkInitialized(); |
119 |
0 |
return fromBuilders; |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
public void addFromBuilder(FromBuilder answer) { |
125 |
54 |
fromBuilders.add(answer); |
126 |
54 |
} |
127 |
|
|
128 |
|
protected void checkInitialized() throws Exception { |
129 |
43 |
if (initalized.compareAndSet(false, true)) { |
130 |
43 |
configure(); |
131 |
43 |
populateRoutes(routes); |
132 |
|
} |
133 |
43 |
} |
134 |
|
|
135 |
|
protected void populateRoutes(List<Route> routes) throws Exception { |
136 |
43 |
for (FromBuilder builder : fromBuilders) { |
137 |
54 |
Route route = builder.createRoute(); |
138 |
54 |
routes.add(route); |
139 |
54 |
} |
140 |
43 |
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
protected CamelContext createContainer() { |
146 |
12 |
return new DefaultCamelContext(); |
147 |
|
} |
148 |
|
} |