Coverage Report - org.apache.camel.impl.DefaultComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultComponent
84% 
100% 
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.impl;
 19  
 
 20  
 import org.apache.camel.CamelContext;
 21  
 import org.apache.camel.Component;
 22  
 import org.apache.camel.Endpoint;
 23  
 import org.apache.camel.Exchange;
 24  
 import org.apache.camel.util.IntrospectionSupport;
 25  
 import org.apache.camel.util.URISupport;
 26  
 import org.apache.camel.util.ObjectHelper;
 27  
 
 28  
 import java.net.URI;
 29  
 import java.util.Map;
 30  
 import java.util.concurrent.ScheduledExecutorService;
 31  
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 32  
 import java.util.concurrent.ThreadFactory;
 33  
 
 34  
 /**
 35  
  * @version $Revision: 541335 $
 36  
  */
 37  
 public abstract  class DefaultComponent<E extends Exchange> extends ServiceSupport implements Component<E> {
 38  
 
 39  42
         private int defaultThreadPoolSize = 5;
 40  
     private CamelContext camelContext;
 41  
     private ScheduledExecutorService executorService;
 42  
 
 43  42
     public DefaultComponent() {
 44  42
     }
 45  
 
 46  0
     public DefaultComponent(CamelContext context) {
 47  0
         this.camelContext = context;
 48  0
     }
 49  
 
 50  
 
 51  
     public Endpoint<E> createEndpoint(String uri) throws Exception {
 52  65
         ObjectHelper.notNull(getCamelContext(), "camelContext");        
 53  65
         URI u = new URI(uri);
 54  65
         String path = u.getHost();
 55  65
         if (path == null) {
 56  63
             path = u.getSchemeSpecificPart();
 57  
         }
 58  65
         Map parameters = URISupport.parseParamters(u);
 59  
 
 60  65
         Endpoint<E> endpoint = createEndpoint(uri, path, parameters);
 61  65
         if (endpoint == null) {
 62  0
             return null;
 63  
         }
 64  65
         if (parameters != null) {
 65  65
             if (endpoint instanceof ScheduledPollEndpoint) {
 66  1
                 ScheduledPollEndpoint scheduledPollEndpoint = (ScheduledPollEndpoint) endpoint;
 67  1
                 scheduledPollEndpoint.configureProperties(parameters);
 68  
             }
 69  65
             IntrospectionSupport.setProperties(endpoint, parameters);
 70  
         }
 71  65
         return endpoint;
 72  
     }
 73  
 
 74  
     public CamelContext getCamelContext() {
 75  131
         return camelContext;
 76  
     }
 77  
 
 78  
     public void setCamelContext(CamelContext context) {
 79  41
         this.camelContext = context;
 80  41
     }
 81  
 
 82  
     public ScheduledExecutorService getExecutorService() {
 83  1
         if (executorService == null) {
 84  1
             executorService = createExecutorService();
 85  
         }
 86  1
         return executorService;
 87  
     }
 88  
 
 89  
     public void setExecutorService(ScheduledExecutorService executorService) {
 90  0
         this.executorService = executorService;
 91  0
     }
 92  
 
 93  
     /**
 94  
      * A factory method to create a default thread pool and executor
 95  
      */
 96  
     protected ScheduledExecutorService createExecutorService() {
 97  1
         return new ScheduledThreadPoolExecutor(defaultThreadPoolSize, new ThreadFactory() {
 98  
             int counter;
 99  
 
 100  1
             public synchronized Thread newThread(Runnable runnable) {
 101  1
                 Thread thread = new Thread(runnable);
 102  1
                 thread.setName("Thread" + (++counter) + " " + DefaultComponent.this.toString());
 103  1
                 return thread;
 104  
             }
 105  
         });
 106  
     }
 107  
 
 108  
     protected void doStart() throws Exception {
 109  28
     }
 110  
 
 111  
     protected void doStop() throws Exception {
 112  28
         if (executorService != null) {
 113  1
             executorService.shutdown();
 114  
         }
 115  28
     }
 116  
 
 117  
 
 118  
     /**
 119  
      * A factory method allowing derived components to create a new endpoint from the given URI,
 120  
      * remaining path and optional parameters
 121  
      *
 122  
      * @param uri        the full URI of the endpoint
 123  
      * @param remaining  the remaining part of the URI without the query parameters or component prefix
 124  
      * @param parameters the optional parameters passed in
 125  
      * @return a newly created endpoint or null if the endpoint cannot be created based on the inputs
 126  
      */
 127  
     abstract protected Endpoint<E> createEndpoint(String uri, String remaining, Map parameters) throws Exception;
 128  
 }