Coverage Report - org.mule.ra.MuleResourceAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleResourceAdapter
0%
0/85
0%
0/10
2.1
MuleResourceAdapter$1
0%
0/2
N/A
2.1
 
 1  
 /*
 2  
  * $Id: MuleResourceAdapter.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.MuleManager;
 14  
 import org.mule.config.ConfigurationBuilder;
 15  
 import org.mule.config.ConfigurationException;
 16  
 import org.mule.config.ThreadingProfile;
 17  
 import org.mule.impl.MuleDescriptor;
 18  
 import org.mule.impl.endpoint.MuleEndpointURI;
 19  
 import org.mule.providers.AbstractConnector;
 20  
 import org.mule.providers.service.TransportFactory;
 21  
 import org.mule.umo.UMODescriptor;
 22  
 import org.mule.umo.UMOException;
 23  
 import org.mule.umo.endpoint.UMOEndpoint;
 24  
 import org.mule.umo.endpoint.UMOEndpointURI;
 25  
 import org.mule.umo.manager.UMOManager;
 26  
 import org.mule.umo.manager.UMOWorkManager;
 27  
 import org.mule.util.ClassUtils;
 28  
 
 29  
 import java.io.IOException;
 30  
 import java.io.ObjectInputStream;
 31  
 import java.io.Serializable;
 32  
 import java.util.HashMap;
 33  
 import java.util.Map;
 34  
 
 35  
 import javax.resource.NotSupportedException;
 36  
 import javax.resource.ResourceException;
 37  
 import javax.resource.spi.ActivationSpec;
 38  
 import javax.resource.spi.BootstrapContext;
 39  
 import javax.resource.spi.ResourceAdapter;
 40  
 import javax.resource.spi.ResourceAdapterInternalException;
 41  
 import javax.resource.spi.endpoint.MessageEndpoint;
 42  
 import javax.resource.spi.endpoint.MessageEndpointFactory;
 43  
 import javax.transaction.xa.XAResource;
 44  
 
 45  
 import org.apache.commons.logging.Log;
 46  
 import org.apache.commons.logging.LogFactory;
 47  
 
 48  
 /**
 49  
  * <code>MuleResourceAdapter</code> TODO
 50  
  */
 51  0
 public class MuleResourceAdapter implements ResourceAdapter, Serializable
 52  
 {
 53  
     /**
 54  
      * Serial version
 55  
      */
 56  
     private static final long serialVersionUID = 5727648958127416509L;
 57  
 
 58  
     /**
 59  
      * logger used by this class
 60  
      */
 61  0
     protected transient Log logger = LogFactory.getLog(this.getClass());
 62  
 
 63  
     private transient UMOManager manager;
 64  
 
 65  
     private transient BootstrapContext bootstrapContext;
 66  0
     private MuleConnectionRequestInfo info = new MuleConnectionRequestInfo();
 67  0
     private final Map endpoints = new HashMap();
 68  
 
 69  
     public MuleResourceAdapter()
 70  0
     {
 71  0
         MuleManager.getConfiguration().setModelType(JcaModel.JCA_MODEL_TYPE);
 72  0
     }
 73  
 
 74  
     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException
 75  
     {
 76  0
         ois.defaultReadObject();
 77  0
         this.logger = LogFactory.getLog(this.getClass());
 78  0
         this.manager = MuleManager.getInstance();
 79  0
     }
 80  
 
 81  
     /**
 82  
      * @see javax.resource.spi.ResourceAdapter#start(javax.resource.spi.BootstrapContext)
 83  
      */
 84  
     public void start(BootstrapContext bootstrapContext) throws ResourceAdapterInternalException
 85  
     {
 86  0
         this.bootstrapContext = bootstrapContext;
 87  0
         if (info.getConfigurations() != null)
 88  
         {
 89  0
             if (MuleManager.isInstanciated())
 90  
             {
 91  0
                 throw new ResourceAdapterInternalException(
 92  
                     "A manager is already configured, cannot configure a new one using the configurations set on the Resource Adapter");
 93  
             }
 94  
             else
 95  
             {
 96  0
                 ConfigurationBuilder builder = null;
 97  
                 try
 98  
                 {
 99  0
                     builder = (ConfigurationBuilder)ClassUtils.instanciateClass(
 100  
                         info.getConfigurationBuilder(), ClassUtils.NO_ARGS);
 101  
 
 102  
                 }
 103  0
                 catch (Exception e)
 104  
                 {
 105  0
                     throw new ResourceAdapterInternalException(
 106  
                         "Failed to instanciate configurationBuilder class: " + info.getConfigurationBuilder(),
 107  
                         e);
 108  0
                 }
 109  
 
 110  
                 try
 111  
                 {
 112  
 
 113  0
                     manager = builder.configure(info.getConfigurations(), null);
 114  
                 }
 115  0
                 catch (ConfigurationException e)
 116  
                 {
 117  0
                     throw new ResourceAdapterInternalException("Failed to load configurations: "
 118  
                                                                + info.getConfigurations(), e);
 119  0
                 }
 120  
             }
 121  
         }
 122  0
         manager = MuleManager.getInstance();
 123  0
     }
 124  
 
 125  
     /**
 126  
      * @see javax.resource.spi.ResourceAdapter#stop()
 127  
      */
 128  
     public void stop()
 129  
     {
 130  0
         manager.dispose();
 131  0
         manager = null;
 132  0
         bootstrapContext = null;
 133  0
     }
 134  
 
 135  
     /**
 136  
      * @return the bootstrap context for this adapter
 137  
      */
 138  
     public BootstrapContext getBootstrapContext()
 139  
     {
 140  0
         return bootstrapContext;
 141  
     }
 142  
 
 143  
     /**
 144  
      * @see javax.resource.spi.ResourceAdapter#endpointActivation(javax.resource.spi.endpoint.MessageEndpointFactory,
 145  
      *      javax.resource.spi.ActivationSpec)
 146  
      */
 147  
     public void endpointActivation(MessageEndpointFactory endpointFactory, ActivationSpec activationSpec)
 148  
         throws ResourceException
 149  
     {
 150  0
         if (activationSpec.getResourceAdapter() != this)
 151  
         {
 152  0
             throw new ResourceException("ActivationSpec not initialized with this ResourceAdapter instance");
 153  
         }
 154  
 
 155  0
         if (activationSpec.getClass().equals(MuleActivationSpec.class))
 156  
         {
 157  
 
 158  
             try
 159  
             {
 160  0
                 UMOEndpointURI uri = new MuleEndpointURI(((MuleActivationSpec)activationSpec).getEndpoint());
 161  0
                 UMOEndpoint endpoint = TransportFactory.createEndpoint(uri,
 162  
                     UMOEndpoint.ENDPOINT_TYPE_RECEIVER);
 163  
 
 164  0
                 ((AbstractConnector)endpoint.getConnector()).getReceiverThreadingProfile()
 165  
                     .setWorkManagerFactory(new ThreadingProfile.WorkManagerFactory()
 166  
                     {
 167  0
                         public UMOWorkManager createWorkManager(ThreadingProfile profile, String name)
 168  
                         {
 169  0
                             return new DelegateWorkManager(bootstrapContext.getWorkManager());
 170  
 
 171  
                         }
 172  
                     });
 173  
                 // TODO manage transactions
 174  0
                 MessageEndpoint messageEndpoint = null;
 175  0
                 messageEndpoint = endpointFactory.createEndpoint(null);
 176  
 
 177  0
                 String name = "JcaComponent#" + messageEndpoint.hashCode();
 178  0
                 MuleDescriptor descriptor = new MuleDescriptor(name);
 179  0
                 descriptor.getInboundRouter().addEndpoint(endpoint);
 180  0
                 descriptor.setImplementationInstance(messageEndpoint);
 181  0
                 MuleManager.getInstance().lookupModel(JcaModel.JCA_MODEL_TYPE).registerComponent(descriptor);
 182  
 
 183  0
                 MuleEndpointKey key = new MuleEndpointKey(endpointFactory, (MuleActivationSpec)activationSpec);
 184  
 
 185  0
                 endpoints.put(key, descriptor);
 186  
             }
 187  0
             catch (Exception e)
 188  
             {
 189  0
                 logger.error(e.getMessage(), e);
 190  0
             }
 191  
         }
 192  
         else
 193  
         {
 194  0
             throw new NotSupportedException("That type of ActicationSpec not supported: "
 195  
                                             + activationSpec.getClass());
 196  
         }
 197  
 
 198  0
     }
 199  
 
 200  
     /**
 201  
      * @see javax.resource.spi.ResourceAdapter#endpointDeactivation(javax.resource.spi.endpoint.MessageEndpointFactory,
 202  
      *      javax.resource.spi.ActivationSpec)
 203  
      */
 204  
     public void endpointDeactivation(MessageEndpointFactory endpointFactory, ActivationSpec activationSpec)
 205  
     {
 206  
 
 207  0
         if (activationSpec.getClass().equals(MuleActivationSpec.class))
 208  
         {
 209  0
             MuleEndpointKey key = new MuleEndpointKey(endpointFactory, (MuleActivationSpec)activationSpec);
 210  0
             UMODescriptor descriptor = (UMODescriptor)endpoints.get(key);
 211  0
             if (descriptor == null)
 212  
             {
 213  0
                 logger.warn("No endpoint was registered with key: " + key);
 214  0
                 return;
 215  
             }
 216  
             try
 217  
             {
 218  0
                 manager.lookupModel(JcaModel.JCA_MODEL_TYPE).unregisterComponent(descriptor);
 219  
             }
 220  0
             catch (UMOException e)
 221  
             {
 222  0
                 logger.error(e.getMessage(), e);
 223  0
             }
 224  
 
 225  
         }
 226  
 
 227  0
     }
 228  
 
 229  
     /**
 230  
      * We only connect to one resource manager per ResourceAdapter instance, so any
 231  
      * ActivationSpec will return the same XAResource.
 232  
      * 
 233  
      * @see javax.resource.spi.ResourceAdapter#getXAResources(javax.resource.spi.ActivationSpec[])
 234  
      */
 235  
     public XAResource[] getXAResources(ActivationSpec[] activationSpecs) throws ResourceException
 236  
     {
 237  0
         return new XAResource[]{};
 238  
     }
 239  
 
 240  
     /**
 241  
      * @return
 242  
      */
 243  
     public String getPassword()
 244  
     {
 245  0
         return info.getPassword();
 246  
     }
 247  
 
 248  
     /**
 249  
      * @return
 250  
      */
 251  
     public String getConfigurations()
 252  
     {
 253  0
         return info.getConfigurations();
 254  
     }
 255  
 
 256  
     /**
 257  
      * @return
 258  
      */
 259  
     public String getUserName()
 260  
     {
 261  0
         return info.getUserName();
 262  
     }
 263  
 
 264  
     /**
 265  
      * @param password
 266  
      */
 267  
     public void setPassword(String password)
 268  
     {
 269  0
         info.setPassword(password);
 270  0
     }
 271  
 
 272  
     /**
 273  
      * @param configurations
 274  
      */
 275  
     public void setConfigurations(String configurations)
 276  
     {
 277  0
         info.setConfigurations(configurations);
 278  0
     }
 279  
 
 280  
     /**
 281  
      * @param userid
 282  
      */
 283  
     public void setUserName(String userid)
 284  
     {
 285  0
         info.setUserName(userid);
 286  0
     }
 287  
 
 288  
     public String getConfigurationBuilder()
 289  
     {
 290  0
         return info.getConfigurationBuilder();
 291  
     }
 292  
 
 293  
     public void setConfigurationBuilder(String configbuilder)
 294  
     {
 295  0
         info.setConfigurationBuilder(configbuilder);
 296  0
     }
 297  
 
 298  
     /**
 299  
      * @return Returns the info.
 300  
      */
 301  
     public MuleConnectionRequestInfo getInfo()
 302  
     {
 303  0
         return info;
 304  
     }
 305  
 
 306  
     public boolean equals(Object o)
 307  
     {
 308  0
         if (this == o)
 309  
         {
 310  0
             return true;
 311  
         }
 312  0
         if (!(o instanceof MuleResourceAdapter))
 313  
         {
 314  0
             return false;
 315  
         }
 316  
 
 317  0
         final MuleResourceAdapter muleResourceAdapter = (MuleResourceAdapter)o;
 318  
 
 319  0
         if (info != null ? !info.equals(muleResourceAdapter.info) : muleResourceAdapter.info != null)
 320  
         {
 321  0
             return false;
 322  
         }
 323  
 
 324  0
         return true;
 325  
     }
 326  
 
 327  
     public int hashCode()
 328  
     {
 329  0
         return (info != null ? info.hashCode() : 0);
 330  
     }
 331  
 
 332  
 }