Coverage Report - org.apache.camel.impl.DefaultEndpoint
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultEndpoint
75% 
80% 
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.impl;
 18  
 
 19  
 import java.lang.reflect.ParameterizedType;
 20  
 import java.lang.reflect.Type;
 21  
 import java.util.concurrent.ScheduledExecutorService;
 22  
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 23  
 
 24  
 import org.apache.camel.CamelContext;
 25  
 import org.apache.camel.Component;
 26  
 import org.apache.camel.Endpoint;
 27  
 import org.apache.camel.Exchange;
 28  
 import org.apache.camel.PollingConsumer;
 29  
 import org.apache.camel.util.ObjectHelper;
 30  
 
 31  
 /**
 32  
  * A default endpoint useful for implementation inheritance
 33  
  *
 34  
  * @version $Revision: 563607 $
 35  
  */
 36  
 public abstract class DefaultEndpoint<E extends Exchange> implements Endpoint<E> {
 37  
     private String endpointUri;
 38  
     private CamelContext context;
 39  
     private Component component;
 40  
     private ScheduledExecutorService executorService;
 41  
 
 42  
     protected DefaultEndpoint(String endpointUri, Component component) {
 43  582
         this(endpointUri, component.getCamelContext());
 44  582
         this.component = component;
 45  582
     }
 46  
 
 47  582
     protected DefaultEndpoint(String endpointUri, CamelContext context) {
 48  582
         this.endpointUri = endpointUri;
 49  582
         this.context = context;
 50  582
     }
 51  
 
 52  
     public int hashCode() {
 53  0
         return endpointUri.hashCode() * 37 + 1;
 54  
     }
 55  
 
 56  
     @Override
 57  
     public boolean equals(Object object) {
 58  0
         if (object instanceof DefaultEndpoint) {
 59  0
             DefaultEndpoint that = (DefaultEndpoint) object;
 60  0
             return ObjectHelper.equals(this.endpointUri, that.endpointUri);
 61  
         }
 62  0
         return false;
 63  
     }
 64  
 
 65  
     @Override
 66  
     public String toString() {
 67  648
         return "Endpoint[" + endpointUri + "]";
 68  
     }
 69  
 
 70  
     public String getEndpointUri() {
 71  996
         return endpointUri;
 72  
     }
 73  
 
 74  
     public CamelContext getContext() {
 75  684
         return context;
 76  
     }
 77  
 
 78  
     public Component getComponent() {
 79  18
         return component;
 80  
     }
 81  
 
 82  
     /**
 83  
      * @return the executor
 84  
      */
 85  
     public synchronized ScheduledExecutorService getExecutorService() {
 86  18
         if (executorService == null) {
 87  18
             Component c = getComponent();
 88  18
             if (c != null && c instanceof DefaultComponent) {
 89  18
                 DefaultComponent dc = (DefaultComponent) c;
 90  18
                 executorService = dc.getExecutorService();
 91  
             }
 92  18
             if (executorService == null) {
 93  0
                 executorService = createExecutorService();
 94  
             }
 95  
         }
 96  18
         return executorService;
 97  
     }
 98  
 
 99  
     /**
 100  
      * @param executorService the executor to set
 101  
      */
 102  
     public synchronized void setExecutorService(ScheduledExecutorService executorService) {
 103  0
         this.executorService = executorService;
 104  0
     }
 105  
 
 106  
     public PollingConsumer<E> createPollingConsumer() throws Exception {
 107  6
         return new DefaultPollingConsumer<E>(this);
 108  
     }
 109  
 
 110  
     /**
 111  
      * Converts the given exchange to the specified exchange type
 112  
      */
 113  
     public E convertTo(Class<E> type, Exchange exchange) {
 114  
         // TODO we could infer type parameter
 115  0
         if (type.isInstance(exchange)) {
 116  0
             return type.cast(exchange);
 117  
         }
 118  0
         return getContext().getExchangeConverter().convertTo(type, exchange);
 119  
     }
 120  
 
 121  
     public E createExchange(Exchange exchange) {
 122  72
         Class<E> exchangeType = getExchangeType();
 123  72
         if (exchangeType != null) {
 124  33
             if (exchangeType.isInstance(exchange)) {
 125  30
                 return exchangeType.cast(exchange);
 126  
             }
 127  
         }
 128  42
         E answer = createExchange();
 129  42
         answer.copyFrom(exchange);
 130  42
         return answer;
 131  
     }
 132  
 
 133  
     public E toExchangeType(Exchange exchange) {
 134  
         // TODO avoid cloning exchanges if E == Exchange!
 135  72
         return createExchange(exchange);
 136  
     }
 137  
 
 138  
     /**
 139  
      * Returns the type of the exchange which is generated by this component
 140  
      */
 141  
     public Class<E> getExchangeType() {
 142  75
         Type type = getClass().getGenericSuperclass();
 143  75
         if (type instanceof ParameterizedType) {
 144  75
             ParameterizedType parameterizedType = (ParameterizedType) type;
 145  75
             Type[] arguments = parameterizedType.getActualTypeArguments();
 146  75
             if (arguments.length > 0) {
 147  75
                 Type argumentType = arguments[0];
 148  75
                 if (argumentType instanceof Class) {
 149  36
                     return (Class<E>) argumentType;
 150  
                 }
 151  
             }
 152  
         }
 153  39
         return null;
 154  
     }
 155  
 
 156  
     protected ScheduledThreadPoolExecutor createExecutorService() {
 157  0
         return new ScheduledThreadPoolExecutor(10);
 158  
     }
 159  
 }