Coverage Report - org.mule.component.DefaultJavaComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultJavaComponent
0%
0/40
0%
0/20
0
 
 1  
 /*
 2  
  * $Id: DefaultJavaComponent.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.InterfaceBinding;
 15  
 import org.mule.api.component.JavaComponent;
 16  
 import org.mule.api.component.LifecycleAdapter;
 17  
 import org.mule.api.lifecycle.InitialisationException;
 18  
 import org.mule.api.model.EntryPointResolverSet;
 19  
 import org.mule.api.object.ObjectFactory;
 20  
 import org.mule.api.registry.ServiceException;
 21  
 import org.mule.config.i18n.CoreMessages;
 22  
 import org.mule.config.i18n.MessageFactory;
 23  
 
 24  
 import java.util.List;
 25  
 
 26  
 /**
 27  
  * Default implementation of {@link JavaComponent}. Component lifecycle is
 28  
  * propagated to the component object instance via the {@link LifecycleAdapter}.
 29  
  */
 30  
 public class DefaultJavaComponent extends AbstractJavaComponent
 31  
 {
 32  
 
 33  
     protected LifecycleAdapter singletonComponentLifecycleAdapter;
 34  
 
 35  
     /**
 36  
      * For spring only
 37  
      */
 38  
     public DefaultJavaComponent()
 39  
     {
 40  0
         super();
 41  0
     }
 42  
 
 43  
     public DefaultJavaComponent(ObjectFactory objectFactory)
 44  
     {
 45  0
         super(objectFactory);
 46  0
     }
 47  
 
 48  
     public DefaultJavaComponent(ObjectFactory objectFactory,
 49  
                                 EntryPointResolverSet entryPointResolverSet,
 50  
                                 List<InterfaceBinding> bindings)
 51  
     {
 52  0
         super(objectFactory, entryPointResolverSet, bindings);
 53  0
     }
 54  
 
 55  
     @Override
 56  
     protected void doStart() throws MuleException
 57  
     {
 58  0
         super.doStart();
 59  
 
 60  
         // If this component is using a SingletonObjectFactory we should create
 61  
         // LifecycleAdaptor wrapper just once now and not on each event. This also
 62  
         // allows start/stop life-cycle methods to be propagated to singleton
 63  
         // component instances.
 64  0
         if (objectFactory != null && objectFactory.isSingleton())
 65  
         {
 66  
             // On first call, create and initialise singleton instance
 67  
             try
 68  
             {
 69  0
                 if (singletonComponentLifecycleAdapter == null)
 70  
                 {
 71  0
                     singletonComponentLifecycleAdapter = createLifecycleAdaptor();
 72  
                 }
 73  
             }
 74  0
             catch (Exception e)
 75  
             {
 76  0
                 throw new InitialisationException(
 77  
                     MessageFactory.createStaticMessage("Unable to create instance of POJO service"), e, this);
 78  
 
 79  0
             }
 80  
             // On all calls, start if not started.
 81  0
             if (!singletonComponentLifecycleAdapter.isStarted())
 82  
             {
 83  
                 try
 84  
                 {
 85  0
                     singletonComponentLifecycleAdapter.start();
 86  
                 }
 87  0
                 catch (Exception e)
 88  
                 {
 89  0
                     throw new ServiceException(CoreMessages.failedToStart("Service '" + flowConstruct.getName() + "'"), e);
 90  0
                 }
 91  
             }
 92  
         }
 93  0
     }
 94  
 
 95  
     @Override
 96  
     protected void doStop() throws MuleException
 97  
     {
 98  0
         super.doStop();
 99  
         // It only makes sense to propagate this life-cycle to singleton component
 100  
         // implementations
 101  0
         if (singletonComponentLifecycleAdapter != null && singletonComponentLifecycleAdapter.isStarted())
 102  
         {
 103  
             try
 104  
             {
 105  0
                 singletonComponentLifecycleAdapter.stop();
 106  
             }
 107  0
             catch (Exception e)
 108  
             {
 109  0
                 throw new ServiceException(CoreMessages.failedToStop("Service '" + flowConstruct.getName() + "'"), e);
 110  0
             }
 111  
         }
 112  0
     }
 113  
 
 114  
     @Override
 115  
     protected void doDispose()
 116  
     {
 117  0
         super.doDispose();
 118  
         // It only makes sense to propagating this life-cycle to singleton component
 119  
         // implementations
 120  0
         if (singletonComponentLifecycleAdapter != null)
 121  
         {
 122  0
             singletonComponentLifecycleAdapter.dispose();
 123  
         }
 124  0
     }
 125  
 
 126  
     @Override
 127  
     protected LifecycleAdapter borrowComponentLifecycleAdaptor() throws Exception
 128  
     {
 129  
         LifecycleAdapter componentLifecycleAdapter;
 130  0
         if (singletonComponentLifecycleAdapter != null)
 131  
         {
 132  0
             componentLifecycleAdapter = singletonComponentLifecycleAdapter;
 133  
         }
 134  
         else
 135  
         {
 136  0
             componentLifecycleAdapter = createLifecycleAdaptor();
 137  0
             componentLifecycleAdapter.start();
 138  
         }
 139  0
         return componentLifecycleAdapter;
 140  
     }
 141  
 
 142  
     @Override
 143  
     protected void returnComponentLifecycleAdaptor(LifecycleAdapter lifecycleAdapter) throws Exception
 144  
     {
 145  0
         if (singletonComponentLifecycleAdapter == null && lifecycleAdapter != null)
 146  
         {
 147  0
             lifecycleAdapter.stop();
 148  0
             lifecycleAdapter.dispose();
 149  0
             lifecycleAdapter = null;
 150  
         }
 151  0
     }
 152  
 
 153  
 }