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