Coverage Report - org.apache.camel.impl.DefaultCamelContext
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultCamelContext
54% 
81% 
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.ResolveEndpointFailedException;
 25  
 import org.apache.camel.Route;
 26  
 import org.apache.camel.RuntimeCamelException;
 27  
 import org.apache.camel.Service;
 28  
 import org.apache.camel.TypeConverter;
 29  
 import org.apache.camel.builder.RouteBuilder;
 30  
 import org.apache.camel.impl.converter.DefaultTypeConverter;
 31  
 import org.apache.camel.spi.ComponentResolver;
 32  
 import org.apache.camel.spi.ExchangeConverter;
 33  
 import org.apache.camel.spi.Injector;
 34  
 import org.apache.camel.util.FactoryFinder;
 35  
 import org.apache.camel.util.NoFactoryAvailableException;
 36  
 import org.apache.camel.util.ObjectHelper;
 37  
 import static org.apache.camel.util.ServiceHelper.startServices;
 38  
 import static org.apache.camel.util.ServiceHelper.stopServices;
 39  
 
 40  
 import java.io.IOException;
 41  
 import java.util.ArrayList;
 42  
 import java.util.Collection;
 43  
 import java.util.HashMap;
 44  
 import java.util.List;
 45  
 import java.util.Map;
 46  
 import java.util.concurrent.Callable;
 47  
 
 48  
 /**
 49  
  * Represents the context used to configure routes and the policies to use.
 50  
  *
 51  
  * @version $Revision: 520517 $
 52  
  * @org.apache.xbean.XBean element="container" rootElement="true"
 53  
  */
 54  71
 public class DefaultCamelContext extends ServiceSupport implements CamelContext, Service {
 55  71
     private Map<String, Endpoint> endpoints = new HashMap<String, Endpoint>();
 56  71
     private Map<String, Component> components = new HashMap<String, Component>();
 57  
     private List<Route> routes;
 58  71
     private List<Service> servicesToClose = new ArrayList<Service>();
 59  
     private TypeConverter typeConverter;
 60  
     private ExchangeConverter exchangeConverter;
 61  
     private Injector injector;
 62  
     private ComponentResolver componentResolver;
 63  71
     private boolean autoCreateComponents = true;
 64  
 
 65  
     /**
 66  
      * Adds a component to the container.
 67  
      */
 68  
     public void addComponent(String componentName, final Component component) {
 69  68
         if (component == null) {
 70  0
             throw new IllegalArgumentException("Component cannot be null");
 71  
         }
 72  68
         synchronized (components) {
 73  68
             if (components.containsKey(componentName)) {
 74  0
                 throw new IllegalArgumentException("Component previously added: " + componentName);
 75  
             }
 76  68
             component.setCamelContext(this);
 77  68
             components.put(componentName, component);
 78  68
         }
 79  68
     }
 80  
 
 81  
     public Component getComponent(String name) {
 82  
         // synchronize the look up and auto create so that 2 threads can't
 83  
         // concurrently auto create the same component.
 84  106
         synchronized (components) {
 85  106
             Component component = components.get(name);
 86  106
             if (component == null && autoCreateComponents) {
 87  
                 try {
 88  67
                     component = getComponentResolver().resolveComponent(name, this);
 89  67
                     if (component != null) {
 90  67
                         addComponent(name, component);
 91  67
                         if (isStarted()) {
 92  
                             // If the component is looked up after the context is started,
 93  
                             // lets start it up.
 94  1
                             startServices(component);
 95  
                         }
 96  
                     }
 97  
                 }
 98  0
                 catch (Exception e) {
 99  0
                     throw new RuntimeCamelException("Could not auto create component: " + name, e);
 100  67
                 }
 101  
             }
 102  106
             return component;
 103  0
         }
 104  
     }
 105  
 
 106  
     public <T extends Component> T getComponent(String name, Class<T> componentType) {
 107  0
         Component component = getComponent(name);
 108  0
         if (componentType.isInstance(component)) {
 109  0
             return componentType.cast(component);
 110  
         }
 111  
         else {
 112  0
             throw new IllegalArgumentException("The component is not of type: " + componentType + " but is: " + component);
 113  
         }
 114  
     }
 115  
 
 116  
     /**
 117  
      * Removes a previously added component.
 118  
      *
 119  
      * @param componentName
 120  
      * @return the previously added component or null if it had not been previously added.
 121  
      */
 122  
     public Component removeComponent(String componentName) {
 123  0
         synchronized (components) {
 124  0
             return components.remove(componentName);
 125  0
         }
 126  
     }
 127  
 
 128  
     /**
 129  
      * Gets the a previously added component by name or lazily creates the component
 130  
      * using the factory Callback.
 131  
      *
 132  
      * @param componentName
 133  
      * @param factory       used to create a new component instance if the component was not previously added.
 134  
      * @return
 135  
      */
 136  
     public Component getOrCreateComponent(String componentName, Callable<Component> factory) {
 137  0
         synchronized (components) {
 138  0
             Component component = components.get(componentName);
 139  0
             if (component == null) {
 140  
                 try {
 141  0
                     component = factory.call();
 142  0
                     if (component == null) {
 143  0
                         throw new RuntimeCamelException("Factory failed to create the " + componentName + " component, it returned null.");
 144  
                     }
 145  0
                     components.put(componentName, component);
 146  0
                     component.setCamelContext(this);
 147  
                 }
 148  0
                 catch (Exception e) {
 149  0
                     throw new RuntimeCamelException("Factory failed to create the " + componentName + " component", e);
 150  0
                 }
 151  
             }
 152  0
             return component;
 153  0
         }
 154  
     }
 155  
 
 156  
     // Endpoint Management Methods
 157  
     //-----------------------------------------------------------------------
 158  
 
 159  
     public Collection<Endpoint> getSingletonEndpoints() {
 160  0
         synchronized (endpoints) {
 161  0
             return new ArrayList<Endpoint>(endpoints.values());
 162  0
         }
 163  
     }
 164  
 
 165  
     public Endpoint addSingletonEndpoint(String uri, Endpoint endpoint) throws Exception {
 166  
         Endpoint oldEndpoint;
 167  0
         synchronized (endpoints) {
 168  0
             startServices(endpoint);
 169  0
             oldEndpoint = endpoints.remove(uri);
 170  0
             endpoints.put(uri, endpoint);
 171  0
             stopServices(oldEndpoint);
 172  0
         }
 173  0
         return oldEndpoint;
 174  
     }
 175  
 
 176  
     public Endpoint removeSingletonEndpoint(String uri) throws Exception {
 177  
         Endpoint oldEndpoint;
 178  0
         synchronized (endpoints) {
 179  0
             oldEndpoint = endpoints.remove(uri);
 180  0
             stopServices(oldEndpoint);
 181  0
         }
 182  0
         return oldEndpoint;
 183  
     }
 184  
 
 185  
     /**
 186  
      * Resolves the given URI to an endpoint
 187  
      */
 188  
     public Endpoint getEndpoint(String uri) {
 189  
         Endpoint answer;
 190  187
         synchronized (endpoints) {
 191  187
             answer = endpoints.get(uri);
 192  187
             if (answer == null) {
 193  
                 try {
 194  
 
 195  
                     // Use the URI prefix to find the component.
 196  103
                     String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 2);
 197  103
                     if (splitURI[1] == null) {
 198  0
                         throw new IllegalArgumentException("Invalid URI, it did not contain a scheme: " + uri);
 199  
                     }
 200  103
                     String scheme = splitURI[0];
 201  103
                     Component component = getComponent(scheme);
 202  
 
 203  
                     // Ask the component to resolve the endpoint.
 204  103
                     if (component != null) {
 205  
 
 206  
                         // Have the component create the endpoint if it can.
 207  103
                         answer = component.createEndpoint(uri);
 208  
 
 209  
                         // If it's a singleton then auto register it.
 210  103
                         if (answer != null && answer.isSingleton()) {
 211  103
                             if (answer != null) {
 212  103
                                 startServices(answer);
 213  103
                                 endpoints.put(uri, answer);
 214  
                             }
 215  
                         }
 216  
                     }
 217  
                 }
 218  0
                 catch (Exception e) {
 219  0
                     throw new ResolveEndpointFailedException(uri, e);
 220  103
                 }
 221  
             }
 222  187
         }
 223  187
         return answer;
 224  
     }
 225  
 
 226  
     public <T extends Endpoint> T getEndpoint(String name, Class<T> endpointType) {
 227  0
         Endpoint endpoint = getEndpoint(name);
 228  0
         if (endpointType.isInstance(endpoint)) {
 229  0
             return endpointType.cast(endpoint);
 230  
         }
 231  
         else {
 232  0
             throw new IllegalArgumentException("The endpoint is not of type: " + endpointType + " but is: " + endpoint);
 233  
         }
 234  
     }
 235  
 
 236  
     // Route Management Methods
 237  
     //-----------------------------------------------------------------------
 238  
     public List<Route> getRoutes() {
 239  25
         return routes;
 240  
     }
 241  
 
 242  
     public void setRoutes(List<Route> routes) {
 243  0
         this.routes = routes;
 244  0
     }
 245  
 
 246  
     public void addRoutes(Collection<Route> routes) throws Exception {
 247  31
         if (this.routes == null) {
 248  31
             this.routes = new ArrayList<Route>(routes);
 249  31
         }
 250  
         else {
 251  0
             this.routes.addAll(routes);
 252  
         }
 253  31
         if (isStarted()) {
 254  0
             startRoutes(routes);
 255  
         }
 256  31
     }
 257  
 
 258  
     public void addRoutes(RouteBuilder builder) throws Exception {
 259  
         // lets now add the routes from the builder
 260  31
         builder.setContext(this);
 261  31
         addRoutes(builder.getRouteList());
 262  31
     }
 263  
 
 264  
     // Properties
 265  
     //-----------------------------------------------------------------------
 266  
     public ExchangeConverter getExchangeConverter() {
 267  0
         if (exchangeConverter == null) {
 268  0
             exchangeConverter = createExchangeConverter();
 269  
         }
 270  0
         return exchangeConverter;
 271  
     }
 272  
 
 273  
     public void setExchangeConverter(ExchangeConverter exchangeConverter) {
 274  0
         this.exchangeConverter = exchangeConverter;
 275  0
     }
 276  
 
 277  
     public TypeConverter getTypeConverter() {
 278  96
         if (typeConverter == null) {
 279  40
             typeConverter = createTypeConverter();
 280  
         }
 281  96
         return typeConverter;
 282  
     }
 283  
 
 284  
     public void setTypeConverter(TypeConverter typeConverter) {
 285  0
         this.typeConverter = typeConverter;
 286  0
     }
 287  
 
 288  
     public Injector getInjector() {
 289  68
         if (injector == null) {
 290  44
             injector = createInjector();
 291  
         }
 292  68
         return injector;
 293  
     }
 294  
 
 295  
     public void setInjector(Injector injector) {
 296  0
         this.injector = injector;
 297  0
     }
 298  
 
 299  
     public ComponentResolver getComponentResolver() {
 300  67
         if (componentResolver == null) {
 301  43
             componentResolver = createComponentResolver();
 302  
         }
 303  67
         return componentResolver;
 304  
     }
 305  
 
 306  
     public void setComponentResolver(ComponentResolver componentResolver) {
 307  0
         this.componentResolver = componentResolver;
 308  0
     }
 309  
 
 310  
     // Implementation methods
 311  
     //-----------------------------------------------------------------------
 312  
 
 313  
     protected void doStart() throws Exception {
 314  31
         if (components != null) {
 315  31
             for (Component component : components.values()) {
 316  54
                 startServices(component);
 317  54
             }
 318  
         }
 319  31
         startRoutes(routes);
 320  31
     }
 321  
 
 322  
     protected void doStop() throws Exception {
 323  30
         stopServices(servicesToClose);
 324  30
         if (components != null) {
 325  30
             for (Component component : components.values()) {
 326  54
                 stopServices(component);
 327  54
             }
 328  
         }
 329  30
     }
 330  
 
 331  
     protected void startRoutes(Collection<Route> routeList) throws Exception {
 332  31
         if (routeList != null) {
 333  31
             for (Route<Exchange> route : routeList) {
 334  41
                 List<Service> services = route.getServicesForRoute();
 335  41
                 servicesToClose.addAll(services);
 336  41
                 startServices(services);
 337  41
             }
 338  
         }
 339  31
     }
 340  
 
 341  
     /**
 342  
      * Lazily create a default implementation
 343  
      */
 344  
     protected ExchangeConverter createExchangeConverter() {
 345  0
         return new DefaultExchangeConverter();
 346  
     }
 347  
 
 348  
     /**
 349  
      * Lazily create a default implementation
 350  
      */
 351  
     protected TypeConverter createTypeConverter() {
 352  40
         return new DefaultTypeConverter();
 353  
     }
 354  
 
 355  
     /**
 356  
      * Lazily create a default implementation
 357  
      */
 358  
     protected Injector createInjector() {
 359  44
         FactoryFinder finder = new FactoryFinder();
 360  
         try {
 361  44
             return (Injector) finder.newInstance("Injector");
 362  
         }
 363  44
         catch (NoFactoryAvailableException e) {
 364  
             // lets use the default
 365  44
             return new ReflectionInjector();
 366  
         }
 367  0
         catch (IllegalAccessException e) {
 368  0
             throw new RuntimeCamelException(e);
 369  
         }
 370  0
         catch (InstantiationException e) {
 371  0
             throw new RuntimeCamelException(e);
 372  
         }
 373  0
         catch (IOException e) {
 374  0
             throw new RuntimeCamelException(e);
 375  
         }
 376  0
         catch (ClassNotFoundException e) {
 377  0
             throw new RuntimeCamelException(e);
 378  
         }
 379  
     }
 380  
 
 381  
     /**
 382  
      * Lazily create a default implementation
 383  
      */
 384  
     protected ComponentResolver createComponentResolver() {
 385  43
         return new DefaultComponentResolver();
 386  
     }
 387  
 
 388  
     public boolean isAutoCreateComponents() {
 389  0
         return autoCreateComponents;
 390  
     }
 391  
 
 392  
     public void setAutoCreateComponents(boolean autoCreateComponents) {
 393  1
         this.autoCreateComponents = autoCreateComponents;
 394  1
     }
 395  
 }