| 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.Exchange; |
| 19 |
|
import org.apache.camel.FailedToCreateProducerException; |
| 20 |
|
import org.apache.camel.Processor; |
| 21 |
|
import org.apache.servicemix.common.BaseServiceUnitManager; |
| 22 |
|
import org.apache.servicemix.common.DefaultComponent; |
| 23 |
|
import org.apache.servicemix.common.Deployer; |
| 24 |
|
import org.apache.servicemix.id.IdGenerator; |
| 25 |
|
import org.apache.servicemix.jbi.resolver.URIResolver; |
| 26 |
|
import org.apache.servicemix.jbi.util.IntrospectionSupport; |
| 27 |
|
import org.apache.servicemix.jbi.util.URISupport; |
| 28 |
|
|
| 29 |
|
import javax.jbi.servicedesc.ServiceEndpoint; |
| 30 |
|
import javax.xml.namespace.QName; |
| 31 |
|
import java.net.URI; |
| 32 |
|
import java.net.URISyntaxException; |
| 33 |
|
import java.util.ArrayList; |
| 34 |
|
import java.util.List; |
| 35 |
|
import java.util.Map; |
| 36 |
|
import java.util.concurrent.ScheduledExecutorService; |
| 37 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
0 |
public class CamelJbiComponent extends DefaultComponent implements Component<Exchange> { |
| 45 |
|
private JbiBinding binding; |
| 46 |
|
private CamelContext camelContext; |
| 47 |
|
private ScheduledExecutorService executorService; |
| 48 |
|
private IdGenerator idGenerator; |
| 49 |
|
protected CamelSpringDeployer deployer; |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
public BaseServiceUnitManager createServiceUnitManager() { |
| 55 |
0 |
Deployer[] deployers = new Deployer[]{new CamelSpringDeployer(this)}; |
| 56 |
0 |
return new BaseServiceUnitManager(this, deployers); |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
@Override |
| 64 |
|
protected List<CamelJbiEndpoint> getConfiguredEndpoints() { |
| 65 |
0 |
List<CamelJbiEndpoint> answer = new ArrayList<CamelJbiEndpoint>(); |
| 66 |
0 |
return answer; |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
@Override |
| 74 |
|
protected Class[] getEndpointClasses() { |
| 75 |
0 |
return new Class[]{CamelJbiEndpoint.class}; |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
public JbiBinding getBinding() { |
| 82 |
0 |
if (binding == null) { |
| 83 |
0 |
binding = new JbiBinding(); |
| 84 |
|
} |
| 85 |
0 |
return binding; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
public void setBinding(JbiBinding binding) { |
| 92 |
0 |
this.binding = binding; |
| 93 |
0 |
} |
| 94 |
|
|
| 95 |
|
@Override |
| 96 |
|
protected String[] getEPRProtocols() { |
| 97 |
0 |
return new String[]{"camel"}; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
protected org.apache.servicemix.common.Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception { |
| 101 |
0 |
CamelJbiEndpoint endpoint = createEndpoint(ep); |
| 102 |
0 |
endpoint.activate(); |
| 103 |
0 |
return endpoint; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
public CamelJbiEndpoint createEndpoint(ServiceEndpoint ep) throws URISyntaxException { |
| 107 |
0 |
URI uri = new URI(ep.getEndpointName()); |
| 108 |
0 |
Map map = URISupport.parseQuery(uri.getQuery()); |
| 109 |
0 |
String camelUri = uri.getSchemeSpecificPart(); |
| 110 |
0 |
Endpoint camelEndpoint = getCamelContext().getEndpoint(camelUri); |
| 111 |
0 |
Processor processor = createCamelProcessor(camelEndpoint); |
| 112 |
0 |
CamelJbiEndpoint endpoint = new CamelJbiEndpoint(getServiceUnit(), camelEndpoint, getBinding(), processor); |
| 113 |
|
|
| 114 |
0 |
IntrospectionSupport.setProperties(endpoint, map); |
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
0 |
return endpoint; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
public Endpoint<Exchange> createEndpoint(String uri) { |
| 125 |
0 |
if (uri.startsWith("jbi:")) { |
| 126 |
0 |
uri = uri.substring("jbi:".length()); |
| 127 |
0 |
return new JbiEndpoint(this, uri); |
| 128 |
|
} |
| 129 |
0 |
return null; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
public CamelContext getCamelContext() { |
| 133 |
0 |
return camelContext; |
| 134 |
|
} |
| 135 |
|
|
| 136 |
|
public void setCamelContext(CamelContext camelContext) { |
| 137 |
0 |
this.camelContext = camelContext; |
| 138 |
0 |
} |
| 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 |
|
|
| 151 |
|
|
| 152 |
|
public CamelJbiEndpoint activateJbiEndpoint(Endpoint camelEndpoint, Processor processor) throws Exception { |
| 153 |
0 |
CamelJbiEndpoint jbiEndpoint = createJbiEndpointFromCamel(camelEndpoint, processor); |
| 154 |
|
|
| 155 |
|
|
| 156 |
0 |
if (deployer != null) { |
| 157 |
|
|
| 158 |
0 |
deployer.addService(jbiEndpoint); |
| 159 |
|
} |
| 160 |
|
else { |
| 161 |
0 |
addEndpoint(jbiEndpoint); |
| 162 |
|
} |
| 163 |
0 |
return jbiEndpoint; |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
public void deactivateJbiEndpoint(CamelJbiEndpoint jbiEndpoint) throws Exception { |
| 167 |
|
|
| 168 |
|
|
| 169 |
0 |
} |
| 170 |
|
|
| 171 |
|
protected CamelJbiEndpoint createJbiEndpointFromCamel(Endpoint camelEndpoint, Processor processor) { |
| 172 |
|
CamelJbiEndpoint jbiEndpoint; |
| 173 |
0 |
String endpointUri = camelEndpoint.getEndpointUri(); |
| 174 |
0 |
if (camelEndpoint instanceof JbiEndpoint) { |
| 175 |
0 |
QName service = null; |
| 176 |
0 |
String endpoint = null; |
| 177 |
0 |
if (endpointUri.startsWith("name:")) { |
| 178 |
0 |
endpoint = endpointUri.substring("name:".length()); |
| 179 |
0 |
service = CamelJbiEndpoint.SERVICE_NAME; |
| 180 |
|
} |
| 181 |
0 |
else if (endpointUri.startsWith("endpoint:")) { |
| 182 |
0 |
String uri = endpointUri.substring("endpoint:".length()); |
| 183 |
|
|
| 184 |
|
String[] parts; |
| 185 |
|
try { |
| 186 |
0 |
parts = URIResolver.split3(uri); |
| 187 |
|
} |
| 188 |
0 |
catch (IllegalArgumentException e) { |
| 189 |
0 |
throw new IllegalArgumentException("Expected syntax jbi:endpoint:[serviceNamespace][sep][serviceName][sep][endpointName] where sep = '/' or ':' depending on the serviceNamespace, but was given: " + endpointUri + ". Cause: " + e, e); |
| 190 |
0 |
} |
| 191 |
0 |
service = new QName(parts[0], parts[1]); |
| 192 |
0 |
endpoint = parts[2]; |
| 193 |
0 |
} |
| 194 |
0 |
else if (endpointUri.startsWith("service:")) { |
| 195 |
0 |
String uri = endpointUri.substring("service:".length()); |
| 196 |
|
|
| 197 |
|
String[] parts; |
| 198 |
|
try { |
| 199 |
0 |
parts = URIResolver.split2(uri); |
| 200 |
|
} |
| 201 |
0 |
catch (IllegalArgumentException e) { |
| 202 |
0 |
throw new IllegalArgumentException("Expected syntax jbi:endpoint:[serviceNamespace][sep][serviceName] where sep = '/' or ':' depending on the serviceNamespace, but was given: " + endpointUri + ". Cause: " + e, e); |
| 203 |
0 |
} |
| 204 |
0 |
service = new QName(parts[0], parts[1]); |
| 205 |
0 |
endpoint = createEndpointName(); |
| 206 |
0 |
} |
| 207 |
|
else { |
| 208 |
0 |
throw new IllegalArgumentException("Expected syntax jbi:endpoint:[serviceNamespace][sep][serviceName][sep][endpointName] or jbi:service:[serviceNamespace][sep][serviceName or jbi:name:[endpointName] but was given: " + endpointUri); |
| 209 |
|
} |
| 210 |
0 |
jbiEndpoint = new CamelJbiEndpoint(getServiceUnit(), service, endpoint, camelEndpoint, getBinding(), processor); |
| 211 |
0 |
} |
| 212 |
|
else { |
| 213 |
0 |
jbiEndpoint = new CamelJbiEndpoint(getServiceUnit(), camelEndpoint, getBinding(), processor); |
| 214 |
|
} |
| 215 |
0 |
return jbiEndpoint; |
| 216 |
|
} |
| 217 |
|
|
| 218 |
|
protected String createEndpointName() { |
| 219 |
0 |
if (idGenerator == null) { |
| 220 |
0 |
idGenerator = new IdGenerator("camel"); |
| 221 |
|
} |
| 222 |
0 |
return idGenerator.generateSanitizedId(); |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
public CamelJbiEndpoint createJbiEndpointFromCamel(Endpoint camelEndpoint) { |
| 229 |
0 |
Processor processor = createCamelProcessor(camelEndpoint); |
| 230 |
0 |
return createJbiEndpointFromCamel(camelEndpoint, processor); |
| 231 |
|
} |
| 232 |
|
|
| 233 |
|
protected Processor createCamelProcessor(Endpoint camelEndpoint) { |
| 234 |
0 |
Processor processor = null; |
| 235 |
|
try { |
| 236 |
0 |
processor = camelEndpoint.createProducer(); |
| 237 |
|
} |
| 238 |
0 |
catch (Exception e) { |
| 239 |
0 |
throw new FailedToCreateProducerException(camelEndpoint, e); |
| 240 |
0 |
} |
| 241 |
0 |
return processor; |
| 242 |
|
} |
| 243 |
|
|
| 244 |
|
|
| 245 |
|
|
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
public boolean isEndpointExposedOnNmr(Endpoint endpoint) { |
| 253 |
|
|
| 254 |
0 |
return !(endpoint instanceof JbiEndpoint); |
| 255 |
|
} |
| 256 |
|
} |