| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
package org.apache.camel.component.jbi; |
| 14 |
|
|
| 15 |
|
import org.apache.camel.CamelContext; |
| 16 |
|
import org.apache.camel.Component; |
| 17 |
|
import org.apache.camel.Endpoint; |
| 18 |
|
import org.apache.camel.Processor; |
| 19 |
|
import org.apache.camel.Exchange; |
| 20 |
|
import org.apache.camel.FailedToCreateProducerException; |
| 21 |
|
import org.apache.servicemix.common.DefaultComponent; |
| 22 |
|
import org.apache.servicemix.jbi.util.IntrospectionSupport; |
| 23 |
|
import org.apache.servicemix.jbi.util.URISupport; |
| 24 |
|
import org.apache.servicemix.jbi.resolver.URIResolver; |
| 25 |
|
|
| 26 |
|
import javax.jbi.servicedesc.ServiceEndpoint; |
| 27 |
|
import javax.xml.namespace.QName; |
| 28 |
|
import java.net.URI; |
| 29 |
|
import java.net.URISyntaxException; |
| 30 |
|
import java.util.ArrayList; |
| 31 |
|
import java.util.List; |
| 32 |
|
import java.util.Map; |
| 33 |
|
import java.util.concurrent.ScheduledExecutorService; |
| 34 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
3 |
public class CamelJbiComponent extends DefaultComponent implements Component<Exchange> { |
| 42 |
|
private JbiBinding binding; |
| 43 |
|
private CamelContext camelContext; |
| 44 |
|
private ScheduledExecutorService executorService; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@Override |
| 51 |
|
protected List<CamelJbiEndpoint> getConfiguredEndpoints() { |
| 52 |
|
|
| 53 |
3 |
List<CamelJbiEndpoint> answer = new ArrayList<CamelJbiEndpoint>(); |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
3 |
return answer; |
| 59 |
|
} |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
@Override |
| 66 |
|
protected Class[] getEndpointClasses() { |
| 67 |
4 |
return new Class[]{CamelJbiEndpoint.class}; |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
public JbiBinding getBinding() { |
| 75 |
7 |
if (binding == null) { |
| 76 |
3 |
binding = new JbiBinding(); |
| 77 |
|
} |
| 78 |
7 |
return binding; |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
public void setBinding(JbiBinding binding) { |
| 85 |
0 |
this.binding = binding; |
| 86 |
0 |
} |
| 87 |
|
|
| 88 |
|
@Override |
| 89 |
|
protected String[] getEPRProtocols() { |
| 90 |
1 |
return new String[]{"camel"}; |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
protected org.apache.servicemix.common.Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception { |
| 94 |
1 |
CamelJbiEndpoint endpoint = createEndpoint(ep); |
| 95 |
1 |
endpoint.activate(); |
| 96 |
1 |
return endpoint; |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
public CamelJbiEndpoint createEndpoint(ServiceEndpoint ep) throws URISyntaxException { |
| 100 |
1 |
URI uri = new URI(ep.getEndpointName()); |
| 101 |
1 |
Map map = URISupport.parseQuery(uri.getQuery()); |
| 102 |
1 |
String camelUri = uri.getSchemeSpecificPart(); |
| 103 |
1 |
Endpoint camelEndpoint = getCamelContext().getEndpoint(camelUri); |
| 104 |
1 |
Processor processor = null; |
| 105 |
|
try { |
| 106 |
1 |
processor = camelEndpoint.createProducer(); |
| 107 |
|
} |
| 108 |
0 |
catch (Exception e) { |
| 109 |
0 |
throw new FailedToCreateProducerException(camelEndpoint, e); |
| 110 |
1 |
} |
| 111 |
1 |
CamelJbiEndpoint endpoint = new CamelJbiEndpoint(getServiceUnit(), camelEndpoint, getBinding(), processor); |
| 112 |
|
|
| 113 |
1 |
IntrospectionSupport.setProperties(endpoint, map); |
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
1 |
return endpoint; |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
public Endpoint<Exchange> createEndpoint(String uri) { |
| 124 |
3 |
if (uri.startsWith("jbi:")) { |
| 125 |
3 |
uri = uri.substring("jbi:".length()); |
| 126 |
|
|
| 127 |
3 |
return new JbiEndpoint(this, uri); |
| 128 |
|
} |
| 129 |
0 |
return null; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
public CamelContext getCamelContext() { |
| 133 |
4 |
return camelContext; |
| 134 |
|
} |
| 135 |
|
|
| 136 |
|
public void setCamelContext(CamelContext camelContext) { |
| 137 |
3 |
this.camelContext = camelContext; |
| 138 |
3 |
} |
| 139 |
|
|
| 140 |
|
public ScheduledExecutorService getExecutorService() { |
| 141 |
0 |
if (executorService == null) { |
| 142 |
0 |
executorService = new ScheduledThreadPoolExecutor(5); |
| 143 |
|
} |
| 144 |
0 |
return executorService; |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
public CamelJbiEndpoint activateJbiEndpoint(JbiEndpoint camelEndpoint, Processor processor) throws Exception { |
| 151 |
|
CamelJbiEndpoint jbiEndpoint; |
| 152 |
1 |
String endpointUri = camelEndpoint.getEndpointUri(); |
| 153 |
1 |
if (endpointUri.startsWith("endpoint:")) { |
| 154 |
|
|
| 155 |
1 |
String uri = endpointUri.substring("endpoint:".length()); |
| 156 |
1 |
String[] parts = new String[0]; |
| 157 |
|
try { |
| 158 |
1 |
parts = URIResolver.split3(uri); |
| 159 |
|
} |
| 160 |
0 |
catch (IllegalArgumentException e) { |
| 161 |
0 |
throw new IllegalArgumentException("Expected syntax endpoint:[serviceNamespace]:[serviceName]:[endpointName] but was given: " + endpointUri + ". Cause: " + e, e); |
| 162 |
1 |
} |
| 163 |
1 |
QName service = new QName(parts[0], parts[1]); |
| 164 |
1 |
String endpoint = parts[2]; |
| 165 |
1 |
jbiEndpoint = new CamelJbiEndpoint(getServiceUnit(), service, endpoint, camelEndpoint, getBinding(), processor); |
| 166 |
1 |
} |
| 167 |
|
else { |
| 168 |
0 |
jbiEndpoint = new CamelJbiEndpoint(getServiceUnit(), camelEndpoint, getBinding(), processor); |
| 169 |
|
} |
| 170 |
|
|
| 171 |
|
|
| 172 |
1 |
addEndpoint(jbiEndpoint); |
| 173 |
1 |
return jbiEndpoint; |
| 174 |
|
} |
| 175 |
|
} |