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