| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.component.pojo; |
| 18 |
|
|
| 19 |
|
import java.lang.reflect.InvocationHandler; |
| 20 |
|
import java.lang.reflect.InvocationTargetException; |
| 21 |
|
import java.lang.reflect.Method; |
| 22 |
|
import java.lang.reflect.Proxy; |
| 23 |
|
import java.util.HashMap; |
| 24 |
|
import java.util.Map; |
| 25 |
|
|
| 26 |
|
import org.apache.camel.Endpoint; |
| 27 |
|
import org.apache.camel.Producer; |
| 28 |
|
import org.apache.camel.spi.Provider; |
| 29 |
|
import org.apache.camel.impl.DefaultComponent; |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
3 |
public class PojoComponent extends DefaultComponent<PojoExchange> { |
| 38 |
3 |
protected final HashMap<String, Object> services = new HashMap<String, Object>(); |
| 39 |
|
|
| 40 |
|
public void addService(String uri, Object pojo) { |
| 41 |
2 |
services.put(uri, pojo); |
| 42 |
2 |
} |
| 43 |
|
|
| 44 |
|
public void removeService(String uri) { |
| 45 |
0 |
services.remove(uri); |
| 46 |
0 |
} |
| 47 |
|
|
| 48 |
|
public Object getService(String uri) { |
| 49 |
2 |
return services.get(uri); |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
@Override |
| 53 |
|
protected Endpoint<PojoExchange> createEndpoint(String uri, final String remaining, Map parameters) throws Exception { |
| 54 |
2 |
Object pojo = getService(remaining); |
| 55 |
2 |
return new PojoEndpoint(uri, this, pojo); |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
static public Object createProxy(final Endpoint endpoint, ClassLoader cl, Class interfaces[]) throws Exception { |
| 63 |
1 |
final Producer producer = endpoint.createProducer(); |
| 64 |
1 |
return Proxy.newProxyInstance(cl, interfaces, new InvocationHandler() { |
| 65 |
1 |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 66 |
1 |
PojoInvocation invocation = new PojoInvocation(proxy, method, args); |
| 67 |
1 |
PojoExchange exchange = new PojoExchange(endpoint.getContext()); |
| 68 |
1 |
exchange.setInvocation(invocation); |
| 69 |
1 |
producer.process(exchange); |
| 70 |
1 |
Throwable fault = exchange.getException(); |
| 71 |
1 |
if (fault != null) { |
| 72 |
0 |
throw new InvocationTargetException(fault); |
| 73 |
|
} |
| 74 |
1 |
return exchange.getOut().getBody(); |
| 75 |
|
} |
| 76 |
|
}); |
| 77 |
|
} |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
static public Object createProxy(Endpoint endpoint, Class interfaces[]) throws Exception { |
| 84 |
1 |
if( interfaces.length < 1 ) { |
| 85 |
0 |
throw new IllegalArgumentException("You must provide at least 1 interface class."); |
| 86 |
|
} |
| 87 |
1 |
return createProxy(endpoint, interfaces[0].getClassLoader(), interfaces); |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
@SuppressWarnings("unchecked") |
| 94 |
|
static public <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class<T> interfaceClass) throws Exception { |
| 95 |
0 |
return (T) createProxy(endpoint, cl, new Class[]{interfaceClass}); |
| 96 |
|
} |
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
@SuppressWarnings("unchecked") |
| 103 |
|
static public <T> T createProxy(Endpoint endpoint, Class<T> interfaceClass) throws Exception { |
| 104 |
1 |
return (T) createProxy(endpoint, new Class[]{interfaceClass}); |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
} |