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            Object pojo = getService(remaining);
055            return new PojoEndpoint(uri, this, pojo);
056        }
057        
058        /**
059         * Creates a Proxy which sends PojoExchange to the endpoint.
060         * @throws Exception 
061         */
062        static public Object createProxy(final Endpoint endpoint, ClassLoader cl, Class interfaces[]) throws Exception {
063            final Producer producer = endpoint.createProducer();
064            return Proxy.newProxyInstance(cl, interfaces, new InvocationHandler() {         
065                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
066                    PojoInvocation invocation = new PojoInvocation(proxy, method, args);
067                    PojoExchange exchange = new PojoExchange(endpoint.getContext());                
068                    exchange.setInvocation(invocation);
069                    producer.process(exchange);                
070                    Throwable fault = exchange.getException();
071                    if (fault != null) {
072                        throw new InvocationTargetException(fault);
073                    }
074                    return exchange.getOut().getBody();
075                    }
076            });
077        }
078        
079        /**
080         * Creates a Proxy which sends PojoExchange to the endpoint.
081         * @throws Exception 
082         */
083        static public Object createProxy(Endpoint endpoint, Class interfaces[]) throws Exception {
084            if( interfaces.length < 1 ) {
085                    throw new IllegalArgumentException("You must provide at least 1 interface class.");
086            }
087            return createProxy(endpoint, interfaces[0].getClassLoader(), interfaces);
088        }    
089        /**
090         * Creates a Proxy which sends PojoExchange to the endpoint.
091         * @throws Exception 
092         */
093        @SuppressWarnings("unchecked")
094            static public <T> T createProxy(Endpoint endpoint, ClassLoader cl, Class<T> interfaceClass) throws Exception {
095            return (T) createProxy(endpoint, cl, new Class[]{interfaceClass});
096        }
097        
098        /**
099         * Creates a Proxy which sends PojoExchange to the endpoint.
100         * @throws Exception 
101         */
102        @SuppressWarnings("unchecked")
103            static public <T> T createProxy(Endpoint endpoint, Class<T> interfaceClass) throws Exception {
104            return (T) createProxy(endpoint, new Class[]{interfaceClass});
105        }
106    
107    
108    
109    }