1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.impl; |
18 |
|
|
19 |
|
import java.net.URI; |
20 |
|
import java.util.Map; |
21 |
|
import java.util.concurrent.ScheduledExecutorService; |
22 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
23 |
|
import java.util.concurrent.ThreadFactory; |
24 |
|
|
25 |
|
import org.apache.camel.CamelContext; |
26 |
|
import org.apache.camel.Component; |
27 |
|
import org.apache.camel.Endpoint; |
28 |
|
import org.apache.camel.Exchange; |
29 |
|
import org.apache.camel.util.IntrospectionSupport; |
30 |
|
import org.apache.camel.util.ObjectHelper; |
31 |
|
import org.apache.camel.util.URISupport; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
public abstract class DefaultComponent<E extends Exchange> extends ServiceSupport implements Component<E> { |
37 |
|
|
38 |
261 |
private int defaultThreadPoolSize = 5; |
39 |
|
private CamelContext camelContext; |
40 |
|
private ScheduledExecutorService executorService; |
41 |
|
|
42 |
261 |
public DefaultComponent() { |
43 |
261 |
} |
44 |
|
|
45 |
0 |
public DefaultComponent(CamelContext context) { |
46 |
0 |
this.camelContext = context; |
47 |
0 |
} |
48 |
|
|
49 |
|
public Endpoint<E> createEndpoint(String uri) throws Exception { |
50 |
396 |
ObjectHelper.notNull(getCamelContext(), "camelContext"); |
51 |
396 |
URI u = new URI(uri); |
52 |
396 |
String path = u.getSchemeSpecificPart(); |
53 |
|
|
54 |
|
|
55 |
396 |
if (path.startsWith("//")) { |
56 |
9 |
path = path.substring(2); |
57 |
|
} |
58 |
396 |
int idx = path.indexOf('?'); |
59 |
396 |
if (idx > 0) { |
60 |
30 |
path = path.substring(0, idx); |
61 |
|
} |
62 |
396 |
Map parameters = URISupport.parseParamters(u); |
63 |
|
|
64 |
396 |
Endpoint<E> endpoint = createEndpoint(uri, path, parameters); |
65 |
393 |
if (endpoint == null) { |
66 |
0 |
return null; |
67 |
|
} |
68 |
393 |
if (parameters != null) { |
69 |
393 |
if (endpoint instanceof ScheduledPollEndpoint) { |
70 |
30 |
ScheduledPollEndpoint scheduledPollEndpoint = (ScheduledPollEndpoint)endpoint; |
71 |
30 |
scheduledPollEndpoint.configureProperties(parameters); |
72 |
|
} |
73 |
393 |
IntrospectionSupport.setProperties(endpoint, parameters); |
74 |
|
} |
75 |
393 |
return endpoint; |
76 |
|
} |
77 |
|
|
78 |
|
public CamelContext getCamelContext() { |
79 |
804 |
return camelContext; |
80 |
|
} |
81 |
|
|
82 |
|
public void setCamelContext(CamelContext context) { |
83 |
258 |
this.camelContext = context; |
84 |
258 |
} |
85 |
|
|
86 |
|
public ScheduledExecutorService getExecutorService() { |
87 |
18 |
if (executorService == null) { |
88 |
15 |
executorService = createExecutorService(); |
89 |
|
} |
90 |
18 |
return executorService; |
91 |
|
} |
92 |
|
|
93 |
|
public void setExecutorService(ScheduledExecutorService executorService) { |
94 |
0 |
this.executorService = executorService; |
95 |
0 |
} |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
protected ScheduledExecutorService createExecutorService() { |
101 |
15 |
return new ScheduledThreadPoolExecutor(defaultThreadPoolSize, new ThreadFactory() { |
102 |
|
int counter; |
103 |
|
|
104 |
15 |
public synchronized Thread newThread(Runnable runnable) { |
105 |
18 |
Thread thread = new Thread(runnable); |
106 |
18 |
thread.setName("Thread: " + (++counter) + " " + DefaultComponent.this.toString()); |
107 |
18 |
return thread; |
108 |
|
} |
109 |
|
}); |
110 |
|
} |
111 |
|
|
112 |
|
protected void doStart() throws Exception { |
113 |
219 |
} |
114 |
|
|
115 |
|
protected void doStop() throws Exception { |
116 |
219 |
if (executorService != null) { |
117 |
15 |
executorService.shutdown(); |
118 |
|
} |
119 |
219 |
} |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
protected abstract Endpoint<E> createEndpoint(String uri, String remaining, Map parameters) |
133 |
|
throws Exception; |
134 |
|
} |