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