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