Coverage Report - org.mule.ra.JcaComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
JcaComponent
0%
0/53
0%
0/4
1.929
 
 1  
 /*
 2  
  * $Id: JcaComponent.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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.ra;
 12  
 
 13  
 import org.mule.MuleException;
 14  
 import org.mule.MuleManager;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 import org.mule.impl.MuleDescriptor;
 17  
 import org.mule.impl.RequestContext;
 18  
 import org.mule.impl.container.ContainerKeyPair;
 19  
 import org.mule.impl.internal.notifications.ComponentNotification;
 20  
 import org.mule.management.stats.ComponentStatistics;
 21  
 import org.mule.umo.UMOComponent;
 22  
 import org.mule.umo.UMODescriptor;
 23  
 import org.mule.umo.UMOEvent;
 24  
 import org.mule.umo.UMOException;
 25  
 import org.mule.umo.UMOMessage;
 26  
 import org.mule.umo.lifecycle.InitialisationException;
 27  
 import org.mule.umo.manager.ObjectNotFoundException;
 28  
 import org.mule.umo.model.UMOEntryPoint;
 29  
 
 30  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
 31  
 
 32  
 /**
 33  
  * <code>JcaComponent</code> Is the type of component used in Mule when embedded
 34  
  * inside an app server using JCA. In the future we might want to use one of the
 35  
  * existing models.
 36  
  */
 37  
 public class JcaComponent implements UMOComponent
 38  
 {
 39  
     /**
 40  
      * Serial version
 41  
      */
 42  
     private static final long serialVersionUID = -1510441245219710451L;
 43  
 
 44  
     private transient final MuleDescriptor descriptor;
 45  
     private transient UMOEntryPoint entryPoint;
 46  
     private Object component;
 47  
     private ComponentStatistics stats;
 48  
 
 49  
     /**
 50  
      * Determines if the component has been initialised
 51  
      */
 52  0
     private final AtomicBoolean initialised = new AtomicBoolean(false);
 53  
 
 54  
     /**
 55  
      * Determines if the component has been started
 56  
      */
 57  0
     private final AtomicBoolean started = new AtomicBoolean(false);
 58  
 
 59  
     public JcaComponent(MuleDescriptor descriptor)
 60  0
     {
 61  0
         if (descriptor == null)
 62  
         {
 63  0
             throw new IllegalArgumentException("Descriptor cannot be null");
 64  
         }
 65  
 
 66  0
         this.descriptor = descriptor;
 67  0
     }
 68  
 
 69  
     public UMODescriptor getDescriptor()
 70  
     {
 71  0
         return descriptor;
 72  
     }
 73  
 
 74  
     public void dispatchEvent(UMOEvent event) throws UMOException
 75  
     {
 76  
         try
 77  
         {
 78  
             // Invoke method
 79  0
             entryPoint.invoke(component, RequestContext.getEventContext());
 80  
         }
 81  0
         catch (Exception e)
 82  
         {
 83  0
             throw new MuleException(
 84  
                 CoreMessages.failedToInvoke("UMO Component: " + descriptor.getName()), e);
 85  0
         }
 86  0
     }
 87  
 
 88  
     /**
 89  
      * This is the synchronous call method and not supported by components managed in
 90  
      * a JCA container
 91  
      * 
 92  
      * @param event
 93  
      * @return
 94  
      * @throws UMOException
 95  
      */
 96  
     public UMOMessage sendEvent(UMOEvent event) throws UMOException
 97  
     {
 98  0
         throw new UnsupportedOperationException("sendEvent()");
 99  
     }
 100  
 
 101  
     public void pause() throws UMOException
 102  
     {
 103  
         // nothing to do
 104  0
     }
 105  
 
 106  
     public void resume() throws UMOException
 107  
     {
 108  
         // nothing to do
 109  0
     }
 110  
 
 111  
     public boolean isPaused()
 112  
     {
 113  0
         return false;
 114  
     }
 115  
 
 116  
     public void start() throws UMOException
 117  
     {
 118  0
         started.set(true);
 119  0
     }
 120  
 
 121  
     public void stop() throws UMOException
 122  
     {
 123  0
         started.set(false);
 124  0
     }
 125  
 
 126  
     public void dispose()
 127  
     {
 128  0
         ((MuleManager)MuleManager.getInstance()).getStatistics().remove(stats);
 129  0
     }
 130  
 
 131  
     public synchronized void initialise() throws InitialisationException
 132  
     {
 133  0
         if (initialised.get())
 134  
         {
 135  0
             throw new InitialisationException(
 136  
                 CoreMessages.objectAlreadyInitialised("Component '" + descriptor.getName() + "'"), this);
 137  
         }
 138  0
         descriptor.initialise();
 139  
         try
 140  
         {
 141  0
             entryPoint = MuleManager.getInstance().lookupModel(JcaModel.JCA_MODEL_TYPE).getEntryPointResolver().resolveEntryPoint(
 142  
                 descriptor);
 143  
         }
 144  0
         catch (UMOException e)
 145  
         {
 146  0
             throw new InitialisationException(e, this);
 147  0
         }
 148  
 
 149  
         // initialise statistics
 150  0
         stats = new ComponentStatistics(descriptor.getName(), -1);
 151  
 
 152  0
         stats.setEnabled(((MuleManager)MuleManager.getInstance()).getStatistics().isEnabled());
 153  0
         ((MuleManager)MuleManager.getInstance()).getStatistics().add(stats);
 154  0
         stats.setOutboundRouterStat(getDescriptor().getOutboundRouter().getStatistics());
 155  0
         stats.setInboundRouterStat(getDescriptor().getInboundRouter().getStatistics());
 156  
 
 157  0
         component = descriptor.getImplementation();
 158  
 
 159  0
         initialised.set(true);
 160  0
         MuleManager.getInstance().fireNotification(
 161  
             new ComponentNotification(descriptor, ComponentNotification.COMPONENT_INITIALISED));
 162  0
     }
 163  
 
 164  
     protected Object getDelegateComponent() throws InitialisationException
 165  
     {
 166  0
         Object impl = descriptor.getImplementation();
 167  0
         Object component = null;
 168  
 
 169  
         try
 170  
         {
 171  0
             if (impl instanceof ContainerKeyPair)
 172  
             {
 173  0
                 component = MuleManager.getInstance().getContainerContext().getComponent(impl);
 174  
 
 175  0
                 if (descriptor.isSingleton())
 176  
                 {
 177  0
                     descriptor.setImplementation(component);
 178  
                 }
 179  
             }
 180  
             else
 181  
             {
 182  0
                 component = impl;
 183  
             }
 184  
         }
 185  0
         catch (ObjectNotFoundException e)
 186  
         {
 187  0
             throw new InitialisationException(e, this);
 188  0
         }
 189  
 
 190  
         // Call any custom initialisers
 191  0
         descriptor.fireInitialisationCallbacks(component);
 192  0
         return component;
 193  
     }
 194  
 
 195  
     public boolean isStarted()
 196  
     {
 197  0
         return started.get();
 198  
     }
 199  
 
 200  
     /**
 201  
      * Gets the underlying instance form this component Where the Component
 202  
      * implmentation provides pooling this is no 1-2-1 mapping between UMOComponent
 203  
      * and instance, so this method will return the object in initial state. <p/> If
 204  
      * the underlying component is Container managed in Spring or another IoC
 205  
      * container then the object instance in the IoC container will be returned
 206  
      * 
 207  
      * @return the underlying instance form this component
 208  
      */
 209  
     public Object getInstance() throws UMOException
 210  
     {
 211  0
         return component;
 212  
     }
 213  
 }