Coverage Report - org.apache.camel.component.pojo.PojoComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
PojoComponent
80% 
100% 
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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  
  * Represents the component that manages {@link PojoEndpoint}.  It holds the
 33  
  * list of named pojos that queue endpoints reference.
 34  
  *
 35  
  * @version $Revision: 519973 $
 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  
      * Creates a Proxy which sends PojoExchange to the endpoint.
 60  
      * @throws Exception 
 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  
      * Creates a Proxy which sends PojoExchange to the endpoint.
 81  
      * @throws Exception 
 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  
      * Creates a Proxy which sends PojoExchange to the endpoint.
 91  
      * @throws Exception 
 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  
      * 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  1
         return (T) createProxy(endpoint, new Class[]{interfaceClass});
 105  
     }
 106  
 
 107  
 
 108  
 
 109  
 }