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 org.apache.camel.CamelContext; |
20 |
|
import org.apache.camel.Component; |
21 |
|
import org.apache.camel.Endpoint; |
22 |
|
import org.apache.camel.Exchange; |
23 |
|
import org.apache.camel.PollingConsumer; |
24 |
|
import org.apache.camel.util.ObjectHelper; |
25 |
|
|
26 |
|
import java.lang.reflect.ParameterizedType; |
27 |
|
import java.lang.reflect.Type; |
28 |
|
import java.util.concurrent.ScheduledExecutorService; |
29 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
public abstract class DefaultEndpoint<E extends Exchange> implements Endpoint<E> { |
37 |
|
private String endpointUri; |
38 |
|
private final Component component; |
39 |
|
private CamelContext context; |
40 |
|
private ScheduledExecutorService executorService; |
41 |
|
|
42 |
104 |
protected DefaultEndpoint(String endpointUri, Component component) { |
43 |
104 |
this.endpointUri = endpointUri; |
44 |
104 |
this.component = component; |
45 |
104 |
this.context = component.getCamelContext(); |
46 |
104 |
} |
47 |
|
|
48 |
|
public int hashCode() { |
49 |
0 |
return endpointUri.hashCode() * 37 + 1; |
50 |
|
} |
51 |
|
|
52 |
|
@Override |
53 |
|
public boolean equals(Object object) { |
54 |
0 |
if (object instanceof DefaultEndpoint) { |
55 |
0 |
DefaultEndpoint that = (DefaultEndpoint) object; |
56 |
0 |
return ObjectHelper.equals(this.endpointUri, that.endpointUri); |
57 |
|
} |
58 |
0 |
return false; |
59 |
|
} |
60 |
|
|
61 |
|
@Override |
62 |
|
public String toString() { |
63 |
165 |
return "Endpoint[" + endpointUri + "]"; |
64 |
|
} |
65 |
|
|
66 |
|
public String getEndpointUri() { |
67 |
128 |
return endpointUri; |
68 |
|
} |
69 |
|
|
70 |
|
public CamelContext getContext() { |
71 |
762 |
return context; |
72 |
|
} |
73 |
|
|
74 |
|
public Component getComponent() { |
75 |
1 |
return component; |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
public synchronized ScheduledExecutorService getExecutorService() { |
82 |
1 |
if (executorService == null) { |
83 |
1 |
Component c = getComponent(); |
84 |
1 |
if (c != null && c instanceof DefaultComponent) { |
85 |
1 |
DefaultComponent dc = (DefaultComponent) c; |
86 |
1 |
executorService = dc.getExecutorService(); |
87 |
|
} |
88 |
1 |
if (executorService == null) { |
89 |
0 |
executorService = createExecutorService(); |
90 |
|
} |
91 |
|
} |
92 |
1 |
return executorService; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
public synchronized void setExecutorService(ScheduledExecutorService executorService) { |
99 |
0 |
this.executorService = executorService; |
100 |
0 |
} |
101 |
|
|
102 |
|
public PollingConsumer<E> createPollingConsumer() throws Exception { |
103 |
1 |
return new DefaultPollingConsumer<E>(this); |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
public E convertTo(Class<E> type, Exchange exchange) { |
110 |
|
|
111 |
0 |
if (type.isInstance(exchange)) { |
112 |
0 |
return type.cast(exchange); |
113 |
|
} |
114 |
0 |
return getContext().getExchangeConverter().convertTo(type, exchange); |
115 |
|
} |
116 |
|
|
117 |
|
public E createExchange(Exchange exchange) { |
118 |
17 |
Class<E> exchangeType = getExchangeType(); |
119 |
17 |
if (exchangeType != null) { |
120 |
10 |
if (exchangeType.isInstance(exchange)) { |
121 |
9 |
return exchangeType.cast(exchange); |
122 |
|
} |
123 |
|
} |
124 |
8 |
E answer = createExchange(); |
125 |
8 |
answer.copyFrom(exchange); |
126 |
8 |
return answer; |
127 |
|
} |
128 |
|
|
129 |
|
public E toExchangeType(Exchange exchange) { |
130 |
|
|
131 |
11 |
return createExchange(exchange); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
public Class<E> getExchangeType() { |
138 |
18 |
Type type = getClass().getGenericSuperclass(); |
139 |
18 |
if (type instanceof ParameterizedType) { |
140 |
18 |
ParameterizedType parameterizedType = (ParameterizedType) type; |
141 |
18 |
Type[] arguments = parameterizedType.getActualTypeArguments(); |
142 |
18 |
if (arguments.length > 0) { |
143 |
18 |
Type argumentType = arguments[0]; |
144 |
18 |
if (argumentType instanceof Class) { |
145 |
11 |
return (Class<E>) argumentType; |
146 |
|
} |
147 |
|
} |
148 |
|
} |
149 |
7 |
return null; |
150 |
|
} |
151 |
|
|
152 |
|
protected ScheduledThreadPoolExecutor createExecutorService() { |
153 |
0 |
return new ScheduledThreadPoolExecutor(10); |
154 |
|
} |
155 |
|
} |