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