Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags.
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  *
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 /*
  * Copyright (C) 2005
  *
  * Arjuna Solutions Limited,
  * Newcastle upon Tyne,
  * Tyne and Wear,
  * UK.
  *
  * $Id$
  */
 
 package com.arjuna.ats.internal.jta.resources.arjunacore;
 
 
 
One Phase resource wrapper for XAResources.

Author(s):
Kevin Conner (Kevin.Conner@arjuna.com)
Version:
$Id$
Since:
ATS 4.1
 
 public class XAOnePhaseResource implements OnePhaseResource
 {
    
The one phase XA resource.
 
     private XAResource xaResource ;
    
The recoverable XA connection.
 
     private RecoverableXAConnection recoverableXAConnection ;
    
The transaction identified.
 
     private Xid xid ;

    
Default constructor for deserialising resource.
 
     public XAOnePhaseResource()
     {
     }

    
Construct the one phase wrapper for the specified resource.

Parameters:
xaResource The XA resource being wrapped.
xid The transaction identifier.
params additional params to pass through.
 
     public XAOnePhaseResource(final XAResource xaResourcefinal Xid xidfinal Object[] params)
     {
         this. = xaResource ;
         this. = xid ;
         if ((params != null) && (params.length >= .))
         {
             final Object param = params[.] ;
             if (param instanceof RecoverableXAConnection)
                  = (RecoverableXAConnection)param ;
         }
     }

    
Commit the one phase resource.

Returns:
TwoPhaseOutcome.FINISH_OK, TwoPhaseOutcome.ONE_PHASE_ERROR or TwoPhaseOutcome.FINISH_ERROR
    public int commit()
    {
        boolean doForget = false;
        try
        {
            // TODO we don't do an end here yet we do in 2PC. Check!!
            .commit(true) ;
            return . ;
        }
        catch (final XAException xae)
        {
            if (..isTraceEnabled()) {
                ..trace("XAOnePhaseResource.commit(" +  + ") " + xae.getMessage());
            }
                    .toString(), XAHelper.printXAErrorCode(xae), xae);
            switch (xae.errorCode)
            {
            case .:
            case .:
                return .;
            case .:
                doForget = true;
                return .;
            case .:
                doForget = true;
                return .;
            case .:
            case .:
            case .:
            case .:
            case .:
            case .:
            case .:
            case .:
            case .:
                return .;
            case .:
                return .// something committed or rolled back without asking us!
            case .// resource manager failed, did it rollback?
                return .;
            case .:  // XA does not allow this to be thrown for 1PC!
            case .:
                return .// assume rollback
            case .:
            default:
                return .;  // recovery should retry
            }
        }
        catch (final Throwable ex)
        {
            String xidString =  == null ? "<  >" : XAHelper.xidToString();
            String resourceString =  == null ? "null XA resource" : .toString();
            if (..isTraceEnabled()) {
                ..trace("XAOnePhaseResource.commit(" +  + ") " + ex.getMessage());
            }
            ..warn_resources_arjunacore_commitxaerror(xidStringresourceString"-"ex);
        }
        finally
        {
            try
            {
                if (doForget)
                    .forget();
            }
            catch (final Throwable ex)
            {
                if (..isTraceEnabled()) {
                    ..trace("XAOnePhaseResource.commit(" +  + ") called forget and got " + ex.getMessage());
                }
            }
        }
        return .// presume abort.
    }

    
Commit the one phase resource.

Returns:
TwoPhaseOutcome.FINISH_OK or TwoPhaseOutcome.FINISH_ERROR
    public int rollback()
    {
        try
        {
            .rollback() ;
            return . ;
        }
        catch (final XAException xae)
        {
        }
        catch (final Throwable ex)
        {
            if (..isTraceEnabled()) {
                ..trace("XAOnePhaseResource.rollback(" +  + ") " + ex.getMessage());
            }
        }
        return . ;
    }

    
Pack the state of the resource.

Parameters:
os The object output state.
    public void pack(final OutputObjectState os)
        throws IOException
    {
        XidImple.pack(os);
        if ( != null)
        {
            os.packInt(.);
            os.packString(.getClass().getName());
            .packInto(os);
        }
        else
        {
            os.packInt(.) ;
            final byte[] data ;
            try
            {
                final ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
                final ObjectOutputStream oos = new ObjectOutputStream(baos) ;
                oos.writeObject() ;
                oos.flush() ;
                oos.close() ;
                data = baos.toByteArray() ;
            }
            catch (final IOException ioe)
            {
                final String message = ..get_resources_arjunacore_XAOnePhaseResource_pack();
                IOException ioException = new IOException(message);
                ioException.initCause(ioe);
                throw ioException;
            }
            os.packBytes(data) ;
        }
    }

    
Unpack the state of the resource.

Parameters:
is The object input state.
    public void unpack(final InputObjectState is)
        throws IOException
    {
        XidImple.unpack(is) ;
        final int recoveryType = is.unpackInt() ;
        switch(recoveryType)
        {
            case .:
                final String recoverableXAConnectionClassName = is.unpackString() ;
                 = ClassloadingUtility.loadAndInstantiateClass(RecoverableXAConnection.classrecoverableXAConnectionClassNamenull);
                if( == null) {
                    throw generateUnpackError(new ClassNotFoundException());
                }
                .unpackFrom(is) ;
                try
                {
                     = .getResource() ;
                }
                catch (final SQLException sqle)
                {
                    throw generateUnpackError(sqle) ;
                }
                break ;
            case .:
                final byte[] data = is.unpackBytes() ;
                try
                {
                    final ByteArrayInputStream bais = new ByteArrayInputStream(data) ;
                    final ObjectInputStream ois = new ObjectInputStream(bais) ;
                     = (XAResource)ois.readObject() ;
                }
                catch (final ClassNotFoundException cnfe)
                {
                    throw generateUnpackError(cnfe) ;
                }
                catch (final IOException ioe)
                {
                    throw generateUnpackError(ioe) ;
                }
                catch (final ClassCastException cce)
                {
                    throw generateUnpackError(cce) ;
                }
                break ;
            default:
                final String message = ..get_resources_arjunacore_XAOnePhaseResource_unpackType(Integer.toString(recoveryType));
                throw new IOException(message);
        }
    }
    @Override
    public String toString()
    {
        return "XAOnePhaseResource("++")";
    }

    
Generate the IOException for the corresponding unpack exception.

Parameters:
ex The exception caught in unpack.
Returns:
The corresponding IOException to be thrown.
    private static IOException generateUnpackError(final Exception ex)
    {
        return new IOException(messageex) ;
    }
New to GrepCode? Check out our FAQ X