Coverage Report - org.mule.module.jca.MuleResourceAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleResourceAdapter
0%
0/200
0%
0/96
0
 
 1  
 /*
 2  
  * $Id: MuleResourceAdapter.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.module.jca;
 12  
 
 13  
 import org.mule.MessageExchangePattern;
 14  
 import org.mule.api.MuleContext;
 15  
 import org.mule.api.MuleException;
 16  
 import org.mule.api.config.ConfigurationBuilder;
 17  
 import org.mule.api.context.MuleContextBuilder;
 18  
 import org.mule.api.endpoint.EndpointBuilder;
 19  
 import org.mule.api.endpoint.InboundEndpoint;
 20  
 import org.mule.api.model.Model;
 21  
 import org.mule.api.service.Service;
 22  
 import org.mule.api.source.CompositeMessageSource;
 23  
 import org.mule.config.DefaultMuleConfiguration;
 24  
 import org.mule.config.builders.DeployableMuleXmlContextListener;
 25  
 import org.mule.config.spring.SpringXmlConfigurationBuilder;
 26  
 import org.mule.context.DefaultMuleContextBuilder;
 27  
 import org.mule.context.DefaultMuleContextFactory;
 28  
 import org.mule.endpoint.EndpointURIEndpointBuilder;
 29  
 import org.mule.endpoint.URIBuilder;
 30  
 import org.mule.util.ClassUtils;
 31  
 
 32  
 import java.io.IOException;
 33  
 import java.io.ObjectInputStream;
 34  
 import java.io.Serializable;
 35  
 import java.util.HashMap;
 36  
 import java.util.Map;
 37  
 
 38  
 import javax.resource.NotSupportedException;
 39  
 import javax.resource.ResourceException;
 40  
 import javax.resource.spi.ActivationSpec;
 41  
 import javax.resource.spi.BootstrapContext;
 42  
 import javax.resource.spi.ResourceAdapter;
 43  
 import javax.resource.spi.ResourceAdapterInternalException;
 44  
 import javax.resource.spi.endpoint.MessageEndpointFactory;
 45  
 import javax.transaction.xa.XAResource;
 46  
 
 47  
 import org.apache.commons.logging.Log;
 48  
 import org.apache.commons.logging.LogFactory;
 49  
 
 50  
 /**
 51  
  * <code>MuleResourceAdapter</code> TODO
 52  
  */
 53  0
 public class MuleResourceAdapter implements ResourceAdapter, Serializable
 54  
 {
 55  
     /**
 56  
      * Serial version
 57  
      */
 58  
     private static final long serialVersionUID = 5727648958127416509L;
 59  
 
 60  
     /**
 61  
      * logger used by this class
 62  
      */
 63  0
     protected transient Log logger = LogFactory.getLog(this.getClass());
 64  
 
 65  
     protected transient MuleContext muleContext;
 66  
 
 67  
     protected transient BootstrapContext bootstrapContext;
 68  0
     protected final Map<MuleEndpointKey, Service> endpoints = new HashMap<MuleEndpointKey, Service>();
 69  
     protected String defaultJcaModelName;
 70  
 
 71  0
     private String configurationBuilder = SpringXmlConfigurationBuilder.class.getName();
 72  
     private String configurations;
 73  
     private String username;
 74  
     private String password;
 75  
 
 76  0
     private DefaultMuleConfiguration muleConfiguration = new DefaultMuleConfiguration();
 77  
 
 78  
     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException
 79  
     {
 80  0
         ois.defaultReadObject();
 81  0
         this.logger = LogFactory.getLog(this.getClass());
 82  0
     }
 83  
 
 84  
     /**
 85  
      * @see javax.resource.spi.ResourceAdapter#start(javax.resource.spi.BootstrapContext)
 86  
      */
 87  
     public void start(BootstrapContext bootstrapContext) throws ResourceAdapterInternalException
 88  
     {
 89  0
         this.bootstrapContext = bootstrapContext;
 90  
 
 91  0
         if (configurations != null)
 92  
         {
 93  0
             ConfigurationBuilder configBuilder = null;
 94  
             try
 95  
             {
 96  0
                 configBuilder = (ConfigurationBuilder) ClassUtils.instanciateClass(configurationBuilder,
 97  
                     configurations);
 98  
             }
 99  0
             catch (Exception e)
 100  
             {
 101  0
                 throw new ResourceAdapterInternalException(
 102  
                     "Failed to instanciate configurationBuilder class: " + configurationBuilder, e);
 103  0
             }
 104  
 
 105  
             try
 106  
             {
 107  0
                 logger.info("Initializing Mule...");
 108  
 
 109  0
                 MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
 110  0
                 muleConfiguration.setSystemModelType(JcaModel.JCA_MODEL_TYPE);
 111  0
                 contextBuilder.setMuleConfiguration(muleConfiguration);
 112  0
                 muleContext = new DefaultMuleContextFactory().createMuleContext(configBuilder, contextBuilder);
 113  
                 
 114  
                 // Make single shared application server instance of mule context
 115  
                 // available to DeployableMuleXmlContextListener to support hot
 116  
                 // deployment of Mule configurations in web applications.
 117  0
                 DeployableMuleXmlContextListener.setMuleContext(muleContext);
 118  
             }
 119  0
             catch (MuleException e)
 120  
             {
 121  0
                 logger.error(e);
 122  0
                 throw new ResourceAdapterInternalException(
 123  
                     "Failed to load configurations: " + configurations, e);
 124  0
             }
 125  
             try
 126  
             {
 127  0
                 logger.info("Starting Mule...");
 128  0
                 muleContext.start();
 129  
             }
 130  0
             catch (MuleException e)
 131  
             {
 132  0
                 logger.error(e);
 133  0
                 throw new ResourceAdapterInternalException("Failed to start management context", e);
 134  0
             }
 135  
         }
 136  0
     }
 137  
 
 138  
     /**
 139  
      * @see javax.resource.spi.ResourceAdapter#stop()
 140  
      */
 141  
     public void stop()
 142  
     {
 143  0
         logger.info("Stopping Mule...");
 144  0
         muleContext.dispose();
 145  0
         muleContext = null;
 146  0
         bootstrapContext = null;
 147  0
     }
 148  
 
 149  
     /**
 150  
      * @return the bootstrap context for this adapter
 151  
      */
 152  
     public BootstrapContext getBootstrapContext()
 153  
     {
 154  0
         return bootstrapContext;
 155  
     }
 156  
 
 157  
     /**
 158  
      * @see javax.resource.spi.ResourceAdapter#endpointActivation(javax.resource.spi.endpoint.MessageEndpointFactory,
 159  
      *      javax.resource.spi.ActivationSpec)
 160  
      */
 161  
     public void endpointActivation(MessageEndpointFactory endpointFactory, ActivationSpec activationSpec)
 162  
         throws ResourceException
 163  
     {
 164  0
         if (activationSpec.getResourceAdapter() != this)
 165  
         {
 166  0
             throw new ResourceException("ActivationSpec not initialized with this ResourceAdapter instance");
 167  
         }
 168  
 
 169  0
         if (activationSpec.getClass().equals(MuleActivationSpec.class))
 170  
         {
 171  0
             MuleActivationSpec muleActivationSpec = (MuleActivationSpec) activationSpec;
 172  
             try
 173  
             {
 174  
                 // Resolve modelName
 175  0
                 String modelName = resolveModelName(muleActivationSpec);
 176  
 
 177  
                 // Lookup/create JCA Model
 178  0
                 JcaModel model = getJcaModel(modelName);
 179  
 
 180  
                 // Create Endpoint
 181  0
                 InboundEndpoint endpoint = createMessageInflowEndpoint(muleActivationSpec);
 182  
 
 183  
                 // Create Service
 184  0
                 Service service = createJcaService(endpointFactory, model, endpoint);
 185  
 
 186  
                 // Keep reference to JcaService descriptor for endpointDeactivation
 187  0
                 MuleEndpointKey key = new MuleEndpointKey(endpointFactory, muleActivationSpec);
 188  0
                 endpoints.put(key, service);
 189  
             }
 190  0
             catch (Exception e)
 191  
             {
 192  0
                 logger.error(e.getMessage(), e);
 193  0
             }
 194  0
         }
 195  
         else
 196  
         {
 197  0
             throw new NotSupportedException("That type of ActicationSpec not supported: "
 198  
                                             + activationSpec.getClass());
 199  
         }
 200  
 
 201  0
     }
 202  
 
 203  
     /**
 204  
      * @see javax.resource.spi.ResourceAdapter#endpointDeactivation(javax.resource.spi.endpoint.MessageEndpointFactory,
 205  
      *      javax.resource.spi.ActivationSpec)
 206  
      */
 207  
     public void endpointDeactivation(MessageEndpointFactory endpointFactory, ActivationSpec activationSpec)
 208  
     {
 209  0
         if (activationSpec.getClass().equals(MuleActivationSpec.class))
 210  
         {
 211  0
             MuleActivationSpec muleActivationSpec = (MuleActivationSpec) activationSpec;
 212  0
             MuleEndpointKey key = new MuleEndpointKey(endpointFactory, (MuleActivationSpec) activationSpec);
 213  0
             Service service = (Service) endpoints.remove(key);
 214  0
             if (service == null)
 215  
             {
 216  0
                 logger.warn("No endpoint was registered with key: " + key);
 217  0
                 return;
 218  
             }
 219  
 
 220  
             // Resolve modelName
 221  0
             String modelName = null;
 222  
             try
 223  
             {
 224  0
                 modelName = resolveModelName(muleActivationSpec);
 225  
             }
 226  0
             catch (ResourceException e)
 227  
             {
 228  0
                 logger.error(e.getMessage(), e);
 229  0
             }
 230  
 
 231  
             try
 232  
             {
 233  0
                 muleContext.getRegistry().unregisterService(service.getName());
 234  
             }
 235  0
             catch (MuleException e)
 236  
             {
 237  0
                 logger.error(e.getMessage(), e);
 238  0
             }
 239  
         }
 240  0
     }
 241  
 
 242  
     protected String resolveModelName(MuleActivationSpec activationSpec) throws ResourceException
 243  
     {
 244  
         // JCA specification mentions activationSpec properties inheriting
 245  
         // resourceAdaptor properties, but this doesn't seem to work, at
 246  
         // least with JBOSS, so do it manually.
 247  0
         String modelName = activationSpec.getModelName();
 248  0
         if (modelName == null)
 249  
         {
 250  0
             modelName = defaultJcaModelName;
 251  
         }
 252  0
         if (modelName == null)
 253  
         {
 254  0
             throw new ResourceException(
 255  
                 "The 'modelName' property has not been configured for either the MuleResourceAdaptor or MuleActicationSpec.");
 256  
         }
 257  0
         return modelName;
 258  
     }
 259  
 
 260  
     protected JcaModel getJcaModel(String modelName) throws MuleException, ResourceException
 261  
     {
 262  0
         Model model = muleContext.getRegistry().lookupModel(modelName);
 263  0
         if (model != null)
 264  
         {
 265  0
             if (model instanceof JcaModel)
 266  
             {
 267  0
                 return (JcaModel) model;
 268  
             }
 269  
             else
 270  
             {
 271  0
                 throw new ResourceException("Model:-" + modelName + "  is not compatible with JCA type");
 272  
             }
 273  
         }
 274  
         else
 275  
         {
 276  0
             JcaModel jcaModel = new JcaModel();
 277  0
             jcaModel.setName(modelName);
 278  0
             muleContext.getRegistry().registerModel(jcaModel);
 279  0
             return jcaModel;
 280  
         }
 281  
     }
 282  
 
 283  
     protected Service createJcaService(MessageEndpointFactory endpointFactory,
 284  
                                        JcaModel model,
 285  
                                        InboundEndpoint endpoint) throws MuleException
 286  
     {
 287  0
         String name = "JcaService#" + endpointFactory.hashCode();
 288  0
         Service service = new JcaService(muleContext);
 289  0
         service.setName(name);
 290  0
         ((CompositeMessageSource) service.getMessageSource()).addSource(endpoint);
 291  
 
 292  
         // Set endpointFactory rather than endpoint here, so we can obtain a
 293  
         // new endpoint instance from factory for each incoming message in
 294  
         // JcaComponet as reccomended by JCA specification
 295  0
         service.setComponent(new JcaComponent(endpointFactory, model.getEntryPointResolverSet(), service,
 296  
             new DelegateWorkManager(bootstrapContext.getWorkManager())));
 297  0
         service.setModel(model);
 298  0
         muleContext.getRegistry().registerService(service);
 299  0
         return service;
 300  
     }
 301  
 
 302  
     protected InboundEndpoint createMessageInflowEndpoint(MuleActivationSpec muleActivationSpec)
 303  
         throws MuleException
 304  
     {
 305  0
         EndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(new URIBuilder(
 306  
             muleActivationSpec.getEndpoint(), muleContext));
 307  
 
 308  0
         endpointBuilder.setExchangePattern(MessageExchangePattern.ONE_WAY);
 309  
 
 310  0
         return muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(endpointBuilder);
 311  
     }
 312  
 
 313  
     /**
 314  
      * We only connect to one resource manager per ResourceAdapter instance, so any
 315  
      * ActivationSpec will return the same XAResource.
 316  
      * 
 317  
      * @see javax.resource.spi.ResourceAdapter#getXAResources(javax.resource.spi.ActivationSpec[])
 318  
      */
 319  
     public XAResource[] getXAResources(ActivationSpec[] activationSpecs) throws ResourceException
 320  
     {
 321  0
         return new XAResource[]{};
 322  
     }
 323  
 
 324  
     /**
 325  
      * @param password
 326  
      */
 327  
     public void setPassword(String password)
 328  
     {
 329  0
         this.password = password;
 330  0
     }
 331  
 
 332  
     /**
 333  
      * @param configurations
 334  
      */
 335  
     public void setConfigurations(String configurations)
 336  
     {
 337  0
         this.configurations = configurations;
 338  0
     }
 339  
 
 340  
     /**
 341  
      * @param userid
 342  
      */
 343  
     public void setUserName(String userid)
 344  
     {
 345  0
         this.username = userid;
 346  0
     }
 347  
 
 348  
     public void setConfigurationBuilder(String configbuilder)
 349  
     {
 350  0
         this.configurationBuilder = configbuilder;
 351  0
     }
 352  
 
 353  
     public void setModelName(String modelName)
 354  
     {
 355  0
         this.defaultJcaModelName = modelName;
 356  0
     }
 357  
 
 358  
     public void setAutoWrapMessageAwareTransform(Boolean autoWrapMessageAwareTransform)
 359  
     {
 360  0
         if (autoWrapMessageAwareTransform != null)
 361  
         {
 362  0
             muleConfiguration.setAutoWrapMessageAwareTransform(autoWrapMessageAwareTransform);
 363  
         }
 364  0
     }
 365  
 
 366  
     public void setCacheMessageAsBytes(Boolean cacheMessageAsBytes)
 367  
     {
 368  0
         if (cacheMessageAsBytes != null)
 369  
         {
 370  0
             muleConfiguration.setCacheMessageAsBytes(cacheMessageAsBytes);
 371  
         }
 372  0
     }
 373  
 
 374  
     public void setCacheMessageOriginalPayload(Boolean cacheMessageOriginalPayload)
 375  
     {
 376  0
         if (cacheMessageOriginalPayload != null)
 377  
         {
 378  0
             muleConfiguration.setCacheMessageOriginalPayload(cacheMessageOriginalPayload);
 379  
         }
 380  0
     }
 381  
 
 382  
     public void setClusterId(String clusterId)
 383  
     {
 384  0
         muleConfiguration.setClusterId(clusterId);
 385  0
     }
 386  
 
 387  
     public void setDefaultEncoding(String encoding)
 388  
     {
 389  0
         muleConfiguration.setDefaultEncoding(encoding);
 390  0
     }
 391  
 
 392  
     public void setDefaultQueueTimeout(Integer defaultQueueTimeout)
 393  
     {
 394  0
         if (defaultQueueTimeout != null)
 395  
         {
 396  0
             muleConfiguration.setDefaultQueueTimeout(defaultQueueTimeout);
 397  
         }
 398  0
     }
 399  
 
 400  
     public void setDefaultResponseTimeout(Integer responseTimeout)
 401  
     {
 402  0
         if (responseTimeout != null)
 403  
         {
 404  0
             muleConfiguration.setDefaultResponseTimeout(responseTimeout);
 405  
         }
 406  0
     }
 407  
 
 408  
     public void setDefaultSynchronousEndpoints(Boolean synchronous)
 409  
     {
 410  0
         if (synchronous != null)
 411  
         {
 412  0
             muleConfiguration.setDefaultSynchronousEndpoints(synchronous);
 413  
         }
 414  0
     }
 415  
 
 416  
     public void setDefaultTransactionTimeout(Integer defaultTransactionTimeout)
 417  
     {
 418  0
         if (defaultTransactionTimeout != null)
 419  
         {
 420  0
             muleConfiguration.setDefaultTransactionTimeout(defaultTransactionTimeout);
 421  
         }
 422  0
     }
 423  
 
 424  
     public void setDomainId(String domainId)
 425  
     {
 426  0
         muleConfiguration.setDomainId(domainId);
 427  0
     }
 428  
 
 429  
     public void setServerId(String serverId)
 430  
     {
 431  0
         muleConfiguration.setId(serverId);
 432  0
     }
 433  
 
 434  
     public void setShutdownTimeout(Integer shutdownTimeout)
 435  
     {
 436  0
         if (shutdownTimeout != null)
 437  
         {
 438  0
             muleConfiguration.setShutdownTimeout(shutdownTimeout);
 439  
         }
 440  0
     }
 441  
 
 442  
     public void setWorkingDirectory(String workingDirectory)
 443  
     {
 444  0
         muleConfiguration.setWorkingDirectory(workingDirectory);
 445  0
     }
 446  
 
 447  
     // Although get methods for config properties aren't really required we need to
 448  
     // include them otherwise Geronimo does not consider them valid properties
 449  
     
 450  
     public String getConfigurationBuilder()
 451  
     {
 452  0
         return configurationBuilder;
 453  
     }
 454  
 
 455  
     public String getConfigurations()
 456  
     {
 457  0
         return configurations;
 458  
     }
 459  
 
 460  
     public String getUserName()
 461  
     {
 462  0
         return username;
 463  
     }
 464  
 
 465  
     public String getPassword()
 466  
     {
 467  0
         return password;
 468  
     }
 469  
 
 470  
     public String getModelName()
 471  
     {
 472  0
         return defaultJcaModelName;
 473  
     }
 474  
     
 475  
     public String getClusterId()
 476  
     {
 477  0
         return muleConfiguration.getClusterId();
 478  
     }
 479  
 
 480  
     public String getDefaultEncoding()
 481  
     {
 482  0
         return muleConfiguration.getDefaultEncoding();
 483  
     }
 484  
 
 485  
     public int getDefaultQueueTimeout()
 486  
     {
 487  0
         return muleConfiguration.getDefaultQueueTimeout();
 488  
     }
 489  
 
 490  
     public int getDefaultResponseTimeout()
 491  
     {
 492  0
         return muleConfiguration.getDefaultResponseTimeout();
 493  
     }
 494  
 
 495  
     public int getDefaultTransactionTimeout()
 496  
     {
 497  0
         return muleConfiguration.getDefaultTransactionTimeout();
 498  
     }
 499  
 
 500  
     public String getDomainId()
 501  
     {
 502  0
         return muleConfiguration.getDomainId();
 503  
     }
 504  
 
 505  
     public String getId()
 506  
     {
 507  0
         return muleConfiguration.getId();
 508  
     }
 509  
 
 510  
     public String getMuleHomeDirectory()
 511  
     {
 512  0
         return muleConfiguration.getMuleHomeDirectory();
 513  
     }
 514  
 
 515  
     public int getShutdownTimeout()
 516  
     {
 517  0
         return muleConfiguration.getShutdownTimeout();
 518  
     }
 519  
 
 520  
     public String getSystemModelType()
 521  
     {
 522  0
         return muleConfiguration.getSystemModelType();
 523  
     }
 524  
 
 525  
     public String getSystemName()
 526  
     {
 527  0
         return muleConfiguration.getSystemName();
 528  
     }
 529  
 
 530  
     public String getWorkingDirectory()
 531  
     {
 532  0
         return muleConfiguration.getWorkingDirectory();
 533  
     }
 534  
 
 535  
     public boolean isAutoWrapMessageAwareTransform()
 536  
     {
 537  0
         return muleConfiguration.isAutoWrapMessageAwareTransform();
 538  
     }
 539  
 
 540  
     public boolean isCacheMessageAsBytes()
 541  
     {
 542  0
         return muleConfiguration.isCacheMessageAsBytes();
 543  
     }
 544  
 
 545  
     public boolean isCacheMessageOriginalPayload()
 546  
     {
 547  0
         return muleConfiguration.isCacheMessageOriginalPayload();
 548  
     }
 549  
 
 550  
     public boolean isClientMode()
 551  
     {
 552  0
         return muleConfiguration.isClientMode();
 553  
     }
 554  
 
 555  
     public boolean isEnableStreaming()
 556  
     {
 557  0
         return muleConfiguration.isEnableStreaming();
 558  
     }
 559  
 
 560  
     @Override
 561  
     public int hashCode()
 562  
     {
 563  0
         final int prime = 31;
 564  0
         int result = 1;
 565  0
         result = prime * result + ((configurationBuilder == null) ? 0 : configurationBuilder.hashCode());
 566  0
         result = prime * result + ((configurations == null) ? 0 : configurations.hashCode());
 567  0
         result = prime * result + ((defaultJcaModelName == null) ? 0 : defaultJcaModelName.hashCode());
 568  0
         result = prime * result + ((endpoints == null) ? 0 : endpoints.hashCode());
 569  0
         result = prime * result + ((muleConfiguration == null) ? 0 : muleConfiguration.hashCode());
 570  0
         result = prime * result + ((password == null) ? 0 : password.hashCode());
 571  0
         result = prime * result + ((username == null) ? 0 : username.hashCode());
 572  0
         return result;
 573  
     }
 574  
 
 575  
     @Override
 576  
     public boolean equals(Object obj)
 577  
     {
 578  0
         if (this == obj) return true;
 579  0
         if (obj == null) return false;
 580  0
         if (getClass() != obj.getClass()) return false;
 581  0
         MuleResourceAdapter other = (MuleResourceAdapter) obj;
 582  0
         if (configurationBuilder == null)
 583  
         {
 584  0
             if (other.configurationBuilder != null) return false;
 585  
         }
 586  0
         else if (!configurationBuilder.equals(other.configurationBuilder)) return false;
 587  0
         if (configurations == null)
 588  
         {
 589  0
             if (other.configurations != null) return false;
 590  
         }
 591  0
         else if (!configurations.equals(other.configurations)) return false;
 592  0
         if (defaultJcaModelName == null)
 593  
         {
 594  0
             if (other.defaultJcaModelName != null) return false;
 595  
         }
 596  0
         else if (!defaultJcaModelName.equals(other.defaultJcaModelName)) return false;
 597  0
         if (endpoints == null)
 598  
         {
 599  0
             if (other.endpoints != null) return false;
 600  
         }
 601  0
         else if (!endpoints.equals(other.endpoints)) return false;
 602  0
         if (muleConfiguration == null)
 603  
         {
 604  0
             if (other.muleConfiguration != null) return false;
 605  
         }
 606  0
         else if (!muleConfiguration.equals(other.muleConfiguration)) return false;
 607  0
         if (password == null)
 608  
         {
 609  0
             if (other.password != null) return false;
 610  
         }
 611  0
         else if (!password.equals(other.password)) return false;
 612  0
         if (username == null)
 613  
         {
 614  0
             if (other.username != null) return false;
 615  
         }
 616  0
         else if (!username.equals(other.username)) return false;
 617  0
         return true;
 618  
     }
 619  
 
 620  
 }