001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.pojo;
018
019 import java.lang.reflect.InvocationHandler;
020 import java.lang.reflect.InvocationTargetException;
021 import java.lang.reflect.Method;
022 import java.lang.reflect.Proxy;
023 import java.util.HashMap;
024 import java.util.Map;
025
026 import org.apache.camel.Endpoint;
027 import org.apache.camel.Producer;
028 import org.apache.camel.spi.Provider;
029 import org.apache.camel.impl.DefaultComponent;
030
031 /**
032 * Represents the component that manages {@link PojoEndpoint}. It holds the
033 * list of named pojos that queue endpoints reference.
034 *
035 * @version $Revision: 519973 $
036 */
037 public class PojoComponent extends DefaultComponent<PojoExchange> {
038 protected final HashMap<String, Object> services = new HashMap<String, Object>();
039
040 public void addService(String uri, Object pojo) {
041 services.put(uri, pojo);
042 }
043
044 public void removeService(String uri) {
045 services.remove(uri);
046 }
047
048 public Object getService(String uri) {
049 return services.get(uri);
050 }
051
052 @Override
053 protected Endpoint<PojoExchange> createEndpoint(String uri, final String remaining, Map parameters) throws Exception {
054 return new PojoEndpoint(uri, this, remaining);
055 }
056
057 /**
058 * Creates a Proxy which sends PojoExchange to the endpoint.
059 * @throws Exception
060 */
061 static public Object createProxy(final Endpoint endpoint, ClassLoader cl, Class interfaces[]) throws Exception {
062 final Producer producer = endpoint.createProducer();
063 return Proxy.newProxyInstance(cl, interfaces, new InvocationHandler() {
064 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
065 PojoInvocation invocation = new PojoInvocation(proxy, method, args);
066 PojoExchange exchange = new PojoExchange(endpoint.getContext());
067 exchange.setInvocation(invocation);
068 producer.process(exchange);
069 Throwable fault = exchange.getException();
070 if (fault != null) {
071 throw new InvocationTargetException(fault);
072 }
073 return exchange.getOut().getBody();
074 }
075 });
076 }
077
078 /**
079 * Creates a Proxy which sends PojoExchange to the endpoint.
080 * @throws Exception
081 */
082 static public Object createProxy(Endpoint endpoint, Class interfaces[]) throws Exception {
083 if( interfaces.length < 1 ) {
084 throw new IllegalArgumentException("You must provide at least 1 interface class.");
085 }
086 return createProxy(endpoint, interfaces[0].getClassLoader(), interfaces);
087 }
088 /**
089 * Creates a Proxy which sends PojoExchange to the endpoint.
090 * @throws Exception
091 */
092 @SuppressWarnings("unchecked")
093 static public <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class<T> interfaceClass) throws Exception {
094 return (T) createProxy(endpoint, cl, new Class[]{interfaceClass});
095 }
096
097 /**
098 * Creates a Proxy which sends PojoExchange to the endpoint.
099 * @throws Exception
100 */
101 @SuppressWarnings("unchecked")
102 static public <T> T createProxy(Endpoint endpoint, Class<T> interfaceClass) throws Exception {
103 return (T) createProxy(endpoint, new Class[]{interfaceClass});
104 }
105
106
107
108 }