Coverage Report - org.mule.component.DefaultJavaComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultJavaComponent
72%
29/40
65%
13/20
2.625
 
 1  
 /*
 2  
  * $Id: DefaultJavaComponent.java 12269 2008-07-10 04:19:03Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.component;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.component.JavaComponent;
 15  
 import org.mule.api.component.LifecycleAdapter;
 16  
 import org.mule.api.lifecycle.InitialisationException;
 17  
 import org.mule.api.model.EntryPointResolverSet;
 18  
 import org.mule.api.model.ModelException;
 19  
 import org.mule.api.object.ObjectFactory;
 20  
 import org.mule.api.routing.NestedRouterCollection;
 21  
 import org.mule.config.i18n.CoreMessages;
 22  
 import org.mule.config.i18n.MessageFactory;
 23  
 
 24  
 /**
 25  
  * Default implementation of {@link JavaComponent}. Component lifecycle is
 26  
  * propagated to the component object instance via the {@link LifecycleAdapter}.
 27  
  */
 28  
 public class DefaultJavaComponent extends AbstractJavaComponent
 29  
 {
 30  
 
 31  
     protected LifecycleAdapter singletonComponentLifecycleAdapter;
 32  
 
 33  
     public DefaultJavaComponent()
 34  
     {
 35  
         // For spring only
 36  0
         super();
 37  0
     }
 38  
 
 39  
     public DefaultJavaComponent(ObjectFactory objectFactory)
 40  
     {
 41  404
         super(objectFactory);
 42  404
     }
 43  
 
 44  
     public DefaultJavaComponent(ObjectFactory objectFactory,
 45  
                                 EntryPointResolverSet entryPointResolverSet,
 46  
                                 NestedRouterCollection nestedRouterCollection)
 47  
     {
 48  0
         super(objectFactory, entryPointResolverSet, nestedRouterCollection);
 49  0
     }
 50  
 
 51  
     protected void doStart() throws MuleException
 52  
     {
 53  22
         super.doStart();
 54  
 
 55  
         // If this component is using a SingletonObjectFactory we should create
 56  
         // LifecycleAdaptor wrapper just once now and not on each event. This also
 57  
         // allows start/stop life-cycle methods to be propagated to singleton
 58  
         // component instances.
 59  22
         if (objectFactory != null && objectFactory.isSingleton())
 60  
         {
 61  
             // On first call, create and initialise singleton instance
 62  
             try
 63  
             {
 64  16
                 if (singletonComponentLifecycleAdapter == null)
 65  
                 {
 66  16
                     singletonComponentLifecycleAdapter = createLifeCycleAdaptor();
 67  
                 }
 68  
             }
 69  0
             catch (Exception e)
 70  
             {
 71  0
                 throw new InitialisationException(
 72  
                     MessageFactory.createStaticMessage("Unable to create instance of POJO service"), e, this);
 73  
 
 74  16
             }
 75  
             // On all calls, start if not started.
 76  16
             if (!singletonComponentLifecycleAdapter.isStarted())
 77  
             {
 78  
                 try
 79  
                 {
 80  16
                     singletonComponentLifecycleAdapter.start();
 81  
                 }
 82  0
                 catch (Exception e)
 83  
                 {
 84  0
                     throw new ModelException(CoreMessages.failedToStart("Service '" + service.getName() + "'"), e);
 85  16
                 }
 86  
             }
 87  
         }
 88  22
     }
 89  
 
 90  
     protected void doStop() throws MuleException
 91  
     {
 92  20
         super.doStop();
 93  
         // It only makes sense to propagate this life-cycle to singleton component
 94  
         // implementations
 95  20
         if (singletonComponentLifecycleAdapter != null && singletonComponentLifecycleAdapter.isStarted())
 96  
         {
 97  
             try
 98  
             {
 99  16
                 singletonComponentLifecycleAdapter.stop();
 100  
             }
 101  0
             catch (Exception e)
 102  
             {
 103  0
                 throw new ModelException(CoreMessages.failedToStop("Service '" + service.getName() + "'"), e);
 104  16
             }
 105  
         }
 106  20
     }
 107  
 
 108  
     protected void doDispose()
 109  
     {
 110  14
         super.doDispose();
 111  
         // It only makes sense to propagating this life-cycle to singleton component
 112  
         // implementations
 113  14
         if (singletonComponentLifecycleAdapter != null)
 114  
         {
 115  6
             singletonComponentLifecycleAdapter.dispose();
 116  
         }
 117  14
     }
 118  
 
 119  
     protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception
 120  
     {
 121  
         LifecycleAdapter componentLifecycleAdapter;
 122  10
         if (singletonComponentLifecycleAdapter != null)
 123  
         {
 124  0
             componentLifecycleAdapter = singletonComponentLifecycleAdapter;
 125  
         }
 126  
         else
 127  
         {
 128  10
             componentLifecycleAdapter = createLifeCycleAdaptor();
 129  10
             componentLifecycleAdapter.start();
 130  
         }
 131  10
         return componentLifecycleAdapter;
 132  
     }
 133  
 
 134  
     protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter) throws Exception
 135  
     {
 136  2
         if (singletonComponentLifecycleAdapter == null && lifecycleAdapter != null)
 137  
         {
 138  2
             lifecycleAdapter.stop();
 139  2
             lifecycleAdapter.dispose();
 140  2
             lifecycleAdapter = null;
 141  
         }
 142  2
     }
 143  
 
 144  
 }