Coverage Report - org.apache.camel.component.pojo.timer.TimerConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
TimerConsumer
70% 
62% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.pojo.timer;
 19  
 
 20  
 import java.lang.reflect.InvocationHandler;
 21  
 import java.lang.reflect.InvocationTargetException;
 22  
 import java.lang.reflect.Method;
 23  
 import java.lang.reflect.Proxy;
 24  
 import java.util.Timer;
 25  
 import java.util.TimerTask;
 26  
 
 27  
 import org.apache.camel.Processor;
 28  
 import org.apache.camel.component.pojo.PojoExchange;
 29  
 import org.apache.camel.component.pojo.PojoInvocation;
 30  
 import org.apache.camel.impl.DefaultConsumer;
 31  
 
 32  
 /**
 33  
  * @version $Revision: 523047 $
 34  
  */
 35  
 public class TimerConsumer extends DefaultConsumer<PojoExchange> implements InvocationHandler {
 36  
 
 37  
     private final TimerEndpoint endpoint;
 38  
     private Timer timer;
 39  
     
 40  
 
 41  
         public TimerConsumer(TimerEndpoint endpoint, Processor processor) {
 42  1
         super(endpoint, processor);
 43  1
                 this.endpoint = endpoint;
 44  1
     }
 45  
     
 46  
     @Override
 47  
     protected void doStart() throws Exception {
 48  1
             TimerComponent component = endpoint.getComponent();
 49  1
             component.addConsumer(this);            
 50  1
         timer=createTimerAndTask();
 51  1
     }
 52  
 
 53  
     @Override
 54  
     protected void doStop() throws Exception {
 55  1
         if(timer!=null){
 56  1
             timer.cancel();
 57  
         }
 58  1
             TimerComponent component = endpoint.getComponent();
 59  1
             component.removeConsumer(this);            
 60  1
     }
 61  
     
 62  
     private Timer createTimerAndTask(){
 63  
             
 64  1
             final Runnable proxy = createProxy();
 65  1
         TimerTask task=new TimerTask(){
 66  1
             @Override public void run(){
 67  5
                     proxy.run();
 68  4
             }
 69  
         };
 70  
         
 71  1
         Timer result=new Timer(endpoint.getTimerName(),endpoint.isDaemon());
 72  1
         if(endpoint.isFixedRate()){
 73  1
             if(endpoint.getTime()!=null){
 74  0
                 result.scheduleAtFixedRate(task,endpoint.getTime(),endpoint.getPeriod());
 75  0
             }else{
 76  1
                 result.scheduleAtFixedRate(task,endpoint.getDelay(),endpoint.getPeriod());
 77  
             }
 78  1
         }else{
 79  0
             if(endpoint.getTime()!=null){
 80  0
                 if(endpoint.getPeriod()>=0){
 81  0
                     result.schedule(task,endpoint.getTime(),endpoint.getPeriod());
 82  0
                 }else{
 83  0
                     result.schedule(task,endpoint.getTime());
 84  
                 }
 85  0
             }else{
 86  0
                 if(endpoint.getPeriod()>=0){
 87  0
                     result.schedule(task,endpoint.getDelay(),endpoint.getPeriod());
 88  0
                 }else{
 89  0
                     result.schedule(task,endpoint.getDelay());
 90  
                 }
 91  
             }
 92  
         }
 93  1
         return result;
 94  
     }
 95  
     
 96  
     /**
 97  
      * Creates a Proxy which generates the inbound PojoExchanges
 98  
      */
 99  
     public Runnable createProxy() {
 100  1
         return (Runnable) Proxy.newProxyInstance(Runnable.class.getClassLoader(), new Class[]{Runnable.class}, this);
 101  
     }
 102  
     
 103  
         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 104  5
         if (!isStarted()) {
 105  0
             throw new IllegalStateException("The endpoint is not active: " + getEndpoint().getEndpointUri());
 106  
         }
 107  5
         PojoInvocation invocation = new PojoInvocation(proxy, method, args);
 108  5
         PojoExchange exchange = getEndpoint().createExchange();
 109  5
         exchange.setInvocation(invocation);
 110  5
         getProcessor().process(exchange);
 111  4
         Throwable fault = exchange.getException();
 112  4
         if (fault != null) {
 113  0
             throw new InvocationTargetException(fault);
 114  
         }
 115  4
         return exchange.getOut().getBody();
 116  
         }
 117  
   
 118  
 
 119  
 
 120  
 }