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