FileDocCategorySizeDatePackage
XAManagedConnectionFactory.javaAPI DocJBoss 4.2.110320Fri Jul 13 21:01:14 BST 2007org.jboss.resource.adapter.jdbc.xa

XAManagedConnectionFactory

public class XAManagedConnectionFactory extends org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnectionFactory
XAManagedConnectionFactory
author
David Jencks
author
Adrian Brock
author
Weston Price
version
$Revision: 60398 $

Fields Summary
private static final long
serialVersionUID
private String
xaDataSourceClass
private String
xaDataSourceProperties
protected final Properties
xaProps
private Boolean
isSameRMOverrideValue
private XADataSource
xads
Constructors Summary
public XAManagedConnectionFactory()


    
   
   
Methods Summary
public javax.resource.spi.ManagedConnectioncreateManagedConnection(javax.security.auth.Subject subject, javax.resource.spi.ConnectionRequestInfo cri)

      Properties props = getConnectionProperties(subject, cri);
      try
      {
         final String user = props.getProperty("user");
         final String password = props.getProperty("password");

         XAConnection xaConnection = (user != null)
               ? getXADataSource().getXAConnection(user, password)
               : getXADataSource().getXAConnection();

         return newXAManagedConnection(props, xaConnection);
      }
      catch (Exception e)
      {
         throw new JBossResourceException("Could not create connection", e);
      }
   
public booleanequals(java.lang.Object other)

      if (this == other)
         return true;
      if (getClass() != other.getClass())
         return false;
      XAManagedConnectionFactory otherMcf = (XAManagedConnectionFactory) other;
      return this.xaDataSourceClass.equals(otherMcf.xaDataSourceClass) && this.xaProps.equals(otherMcf.xaProps)
            && ((this.userName == null) ? otherMcf.userName == null : this.userName.equals(otherMcf.userName))
            && ((this.password == null) ? otherMcf.password == null : this.password.equals(otherMcf.password))
            && this.transactionIsolation == otherMcf.transactionIsolation;

   
protected synchronized javax.sql.XADataSourcegetXADataSource()

      if (xads == null)
      {
         if (xaDataSourceClass == null)
            throw new JBossResourceException("No XADataSourceClass supplied!");
         try
         {
            Class clazz = Thread.currentThread().getContextClassLoader().loadClass(xaDataSourceClass);
            xads = (XADataSource) clazz.newInstance();
            Class[] NOCLASSES = new Class[] {};
            for (Iterator i = xaProps.keySet().iterator(); i.hasNext();)
            {
               String name = (String) i.next();
               String value = xaProps.getProperty(name);
               //This is a bad solution.  On the other hand the only known example
               // of a setter with no getter is for Oracle with password.
               //Anyway, each xadatasource implementation should get its
               //own subclass of this that explicitly sets the
               //properties individually.
               Class type = null;
               try
               {
                  Method getter = clazz.getMethod("get" + name, NOCLASSES);
                  type = getter.getReturnType();
               }
               catch (NoSuchMethodException e)
               {
                  type = String.class;
                  
                  try
                  {
                     //HACK for now until we can rethink the XADataSourceProperties variable and pass type information
                     Method getter = clazz.getMethod("is" + name, NOCLASSES);
                     type = getter.getReturnType();
                     
                  }catch(NoSuchMethodException nsme)
                  {
                     type = String.class;                     
                  
                  }
                                    
               }

               Method setter = clazz.getMethod("set" + name, new Class[] { type });
               PropertyEditor editor = PropertyEditorManager.findEditor(type);
               if (editor == null)
                  throw new JBossResourceException("No property editor found for type: " + type);
               editor.setAsText(value);
               setter.invoke(xads, new Object[] { editor.getValue() });
            }
         }
         catch (ClassNotFoundException cnfe)
         {
            throw new JBossResourceException("Class not found for XADataSource " + xaDataSourceClass, cnfe);
         }
         catch (InstantiationException ie)
         {
            throw new JBossResourceException("Could not create an XADataSource: ", ie);
         }
         catch (IllegalAccessException iae)
         {
            throw new JBossResourceException("Could not set a property: ", iae);
         }
         catch (IllegalArgumentException iae)
         {
            throw new JBossResourceException("Could not set a property: ", iae);
         }
         catch (InvocationTargetException ite)
         {
            throw new JBossResourceException("Could not invoke setter on XADataSource: ", ite);
         }
         catch (NoSuchMethodException nsme)
         {
            throw new JBossResourceException("Could not find accessor on XADataSource: ", nsme);
         }
      }
      return xads;
   
public java.lang.StringgetXADataSourceClass()
Get the XaDataSourceClass value.

return
the XaDataSourceClass value.

      return xaDataSourceClass;
   
public java.lang.StringgetXADataSourceProperties()
Get the XADataSourceProperties value.

return
the XADataSourceProperties value.

      return xaDataSourceProperties;
   
protected java.util.PropertiesgetXaProps()

      return xaProps;
   
public inthashCode()

      int result = 17;
      result = result * 37 + ((xaDataSourceClass == null) ? 0 : xaDataSourceClass.hashCode());
      result = result * 37 + xaProps.hashCode();
      result = result * 37 + ((userName == null) ? 0 : userName.hashCode());
      result = result * 37 + ((password == null) ? 0 : password.hashCode());
      result = result * 37 + transactionIsolation;
      return result;
   
public javax.resource.spi.ManagedConnectionmatchManagedConnections(java.util.Set mcs, javax.security.auth.Subject subject, javax.resource.spi.ConnectionRequestInfo cri)

      Properties newProps = getConnectionProperties(subject, cri);
      for (Iterator i = mcs.iterator(); i.hasNext();)
      {
         Object o = i.next();
         if (o instanceof XAManagedConnection)
         {
            XAManagedConnection mc = (XAManagedConnection) o;
          
            if (mc.getProps().equals(newProps))
            {
               //Next check to see if we are validating on matchManagedConnections
               if((getValidateOnMatch() && mc.checkValid()) || !getValidateOnMatch())
               {
                
                  return mc;

               }            
               
            }
        
         }
      }
      return null;
   
protected javax.resource.spi.ManagedConnectionnewXAManagedConnection(java.util.Properties props, javax.sql.XAConnection xaConnection)
This method can be overwritten by sublcasses to provide rm specific implementation of XAManagedConnection

      return new XAManagedConnection(this, xaConnection, props, transactionIsolation, preparedStatementCacheSize);
   
public voidsetXADataSourceClass(java.lang.String xaDataSourceClass)
Set the XaDataSourceClass value.

param
xaDataSourceClass The new XaDataSourceClass value.

      this.xaDataSourceClass = xaDataSourceClass;
   
public voidsetXADataSourceProperties(java.lang.String xaDataSourceProperties)
Set the XADataSourceProperties value.

param
xaDataSourceProperties The new XADataSourceProperties value.

      this.xaDataSourceProperties = xaDataSourceProperties;
      xaProps.clear();
      if (xaDataSourceProperties != null)
      {
         // Map any \ to \\
         xaDataSourceProperties = xaDataSourceProperties.replaceAll("\\\\", "\\\\\\\\");

         InputStream is = new ByteArrayInputStream(xaDataSourceProperties.getBytes());
         try
         {
            xaProps.load(is);
         }
         catch (IOException ioe)
         {
            throw new JBossResourceException("Could not load connection properties", ioe);
         }
      }