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

HAXAManagedConnectionFactory

public class HAXAManagedConnectionFactory extends XAManagedConnectionFactory
author
Alexey Loubyansky
version
$Revision: 59774 $

Fields Summary
private static final long
serialVersionUID
private String
urlProperty
private String
urlDelimiter
private XADataSelector
xadsSelector
Constructors Summary
Methods Summary
public javax.resource.spi.ManagedConnectioncreateManagedConnection(javax.security.auth.Subject subject, javax.resource.spi.ConnectionRequestInfo cri)

      if(xadsSelector == null)
      {
         JBossStringBuilder buffer = new JBossStringBuilder();
         buffer.append("Missing configuration for HA XA datasource.");
         if (urlProperty == null)
            buffer.append(" No url property.");
         else if (xaProps.containsKey(urlProperty) == false)
            buffer.append(" ").append(urlProperty).append(" not found in datasource properties.");
         if (urlDelimiter == null)
            buffer.append(" No url-delimiter.");
         throw new JBossResourceException(buffer.toString());
      }

      // try to get a connection as many times as many urls we have in the list
      for(int i = 0; i < xadsSelector.getXADataSourceList().size(); ++i)
      {
         XAData xaData = xadsSelector.getXAData();

         if(log.isTraceEnabled())
         {
            log.trace("Trying to create an XA connection to " + xaData.url);
         }

         try
         {
            return super.createManagedConnection(subject, cri);
         }
         catch(ResourceException e)
         {
            log.warn("Failed to create an XA connection to " + xaData.url + ": " + e.getMessage());
            xadsSelector.failedXAData(xaData);
         }
      }

      // we have supposedly tried all the urls
      throw new JBossResourceException(
         "Could not create connection using any of the URLs: " + xadsSelector.getXADataSourceList()
      );
   
private javax.sql.XADataSourcecreateXaDataSource(java.util.Properties xaProps)

      if(getXADataSourceClass() == null)
      {
         throw new JBossResourceException("No XADataSourceClass supplied!");
      }

      XADataSource xads;
      try
      {
         Class clazz = Thread.currentThread().getContextClassLoader().loadClass(getXADataSourceClass());
         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;
            } // end of try-catch

            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);
            } // end of if ()
            editor.setAsText(value);
            setter.invoke(xads, new Object[]{editor.getValue()});

         } // end of for ()
      }
      catch(ClassNotFoundException cnfe)
      {
         throw new JBossResourceException("Class not found for XADataSource " + getXADataSourceClass(), cnfe);
      } // end of try-catch
      catch(InstantiationException ie)
      {
         throw new JBossResourceException("Could not create an XADataSource: ", ie);
      } // end of catch
      catch(IllegalAccessException iae)
      {
         throw new JBossResourceException("Could not set a property: ", iae);
      } // end of catch

      catch(IllegalArgumentException iae)
      {
         throw new JBossResourceException("Could not set a property: ", iae);
      } // end of catch

      catch(InvocationTargetException ite)
      {
         throw new JBossResourceException("Could not invoke setter on XADataSource: ", ite);
      } // end of catch
      catch(NoSuchMethodException nsme)
      {
         throw new JBossResourceException("Could not find accessor on XADataSource: ", nsme);
      } // end of catch

      return xads;
   
public java.lang.StringgetURLDelimiter()

      return urlDelimiter;
   
public java.lang.StringgetURLProperty()


     
   
      return urlProperty;
   
protected synchronized javax.sql.XADataSourcegetXADataSource()

      return xadsSelector.getXAData().xads;
   
private voidinitSelector()

      if(urlProperty != null && urlProperty.length() > 0)
      {
         String urlsStr = xaProps.getProperty(urlProperty);
         if(urlsStr != null && urlsStr.trim().length() > 0 && urlDelimiter != null && urlDelimiter.trim().length() > 0)
         {
            List xaDataList = new ArrayList();

            // copy xaProps
            // ctor doesn't work because iteration won't include defaults
            // Properties xaPropsCopy = new Properties(xaProps);
            Properties xaPropsCopy = new Properties();
            for(Iterator i = xaProps.keySet().iterator(); i.hasNext();)
            {
               Object key = i.next();
               xaPropsCopy.put(key, xaProps.get(key));
            }

            int urlStart = 0;
            int urlEnd = urlsStr.indexOf(urlDelimiter);
            while(urlEnd > 0)
            {
               String url = urlsStr.substring(urlStart, urlEnd);
               xaPropsCopy.setProperty(urlProperty, url);
               XADataSource xads = createXaDataSource(xaPropsCopy);
               xaDataList.add(new XAData(xads, url));
               urlStart = ++urlEnd;
               urlEnd = urlsStr.indexOf(urlDelimiter, urlEnd);
               log.debug("added XA HA connection url: " + url);
            }

            if(urlStart != urlsStr.length())
            {
               String url = urlsStr.substring(urlStart, urlsStr.length());
               xaPropsCopy.setProperty(urlProperty, url);
               XADataSource xads = createXaDataSource(xaPropsCopy);
               xaDataList.add(new XAData(xads, url));
               log.debug("added XA HA connection url: " + url);
            }

            xadsSelector = new XADataSelector(xaDataList);
         }
      }
   
public voidsetURLDelimiter(java.lang.String urlDelimetir)

      this.urlDelimiter = urlDelimetir;
      initSelector();
   
public voidsetURLProperty(java.lang.String urlProperty)

      this.urlProperty = urlProperty;
      initSelector();
   
public voidsetXADataSourceProperties(java.lang.String xaDataSourceProperties)

      super.setXADataSourceProperties(xaDataSourceProperties);
      initSelector();