Coverage Report - org.apache.camel.component.pojo.PojoEndpoint
 
Classes in this File Line Coverage Branch Coverage Complexity
PojoEndpoint
60% 
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 org.apache.camel.Consumer;
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.NoSuchEndpointException;
 22  
 import org.apache.camel.Processor;
 23  
 import org.apache.camel.Producer;
 24  
 import org.apache.camel.Component;
 25  
 import org.apache.camel.impl.DefaultEndpoint;
 26  
 import org.apache.camel.impl.DefaultProducer;
 27  
 import org.apache.camel.spi.Provider;
 28  
 
 29  
 import java.lang.reflect.InvocationTargetException;
 30  
 
 31  
 /**
 32  
  * Represents a pojo endpoint that uses reflection
 33  
  * to send messages around.
 34  
  *
 35  
  * @version $Revision: 519973 $
 36  
  */
 37  0
 public class PojoEndpoint extends DefaultEndpoint<PojoExchange> {
 38  
     private Object pojo;
 39  
 
 40  
     public PojoEndpoint(String uri, Component component, Object pojo) {
 41  2
         super(uri, component);
 42  2
         this.pojo = pojo;
 43  2
     }
 44  
 
 45  
     public Producer<PojoExchange> createProducer() throws Exception {
 46  2
         final Object pojo = getPojo();
 47  2
         if (pojo == null) {
 48  0
             throw new NoPojoAvailableException(this);
 49  
         }
 50  
 
 51  2
         return new DefaultProducer(this) {
 52  2
             public void process(Exchange exchange) {
 53  5
                 PojoExchange pojoExchange = toExchangeType(exchange);
 54  5
                 invoke(pojo, pojoExchange);
 55  5
                 exchange.copyFrom(pojoExchange);
 56  5
             }
 57  
         };
 58  
     }
 59  
 
 60  
     public Consumer<PojoExchange> createConsumer(Processor processor) throws Exception {
 61  0
         throw new Exception("You cannot consume from pojo endpoints.");
 62  
     }
 63  
 
 64  
     /**
 65  
      * This causes us to invoke the endpoint Pojo using reflection.
 66  
      *
 67  
      * @param pojo
 68  
      */
 69  
     public static void invoke(Object pojo, PojoExchange exchange) {
 70  5
         PojoInvocation invocation = exchange.getInvocation();
 71  
         try {
 72  5
             Object response = invocation.getMethod().invoke(pojo, invocation.getArgs());
 73  5
             exchange.getOut().setBody(response);
 74  
         }
 75  0
         catch (InvocationTargetException e) {
 76  0
             exchange.setException(e.getCause());
 77  
         }
 78  0
         catch (RuntimeException e) {
 79  0
             throw e;
 80  
         }
 81  0
         catch (Throwable e) {
 82  0
             throw new RuntimeException(e);
 83  5
         }
 84  5
     }
 85  
 
 86  
     public PojoExchange createExchange() {
 87  0
         return new PojoExchange(getContext());
 88  
     }
 89  
 
 90  
     public boolean isSingleton() {
 91  2
         return true;
 92  
     }
 93  
 
 94  
     public Object getPojo() {
 95  2
         return pojo;
 96  
     }
 97  
 
 98  
     public void setPojo(Object pojo) {
 99  0
         this.pojo = pojo;
 100  0
     }
 101  
 }