Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2011, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY 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 along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.as.ejb3.remote.protocol.versionone;
 
 
Handles a transaction message which complies with the EJB remote protocol specification

Author(s):
Jaikiran Pai
 
 
     private static final byte HEADER_TX_INVOCATION_RESPONSE = 0x14;
 
     private final ExecutorService executorService;
     private final TransactionRequestType txRequestType;
     private final MarshallerFactory marshallerFactory;
 
 
     enum TransactionRequestType {
         COMMIT,
         ROLLBACK,
         PREPARE,
         FORGET,
         BEFORE_COMPLETION
     }
 
     TransactionRequestHandler(final EJBRemoteTransactionsRepository transactionsRepositoryfinal MarshallerFactory marshallerFactory,
                               final ExecutorService executorServicefinal TransactionRequestType txRequestType) {
         this. = executorService;
         this. = transactionsRepository;
         this. = txRequestType;
         this. = marshallerFactory;
     }
 
     @Override
     public void processMessage(final ChannelAssociation channelAssociationfinal MessageInputStream messageInputStreamthrows IOException {
         final DataInputStream input = new DataInputStream(messageInputStream);
         // read the invocation id
         final short invocationId = input.readShort();
         // read the transaction id length
         final int transactionIDBytesLength = PackedInteger.readPackedInteger(input);
         // read the transaction id bytes
         final byte[] transactionIDBytes = new byte[transactionIDBytesLength];
         input.read(transactionIDBytes);
         final TransactionID transactionID = TransactionID.createTransactionID(transactionIDBytes);
         // if it's a commit request, the read the additional "bit" which indicates whether it's a one-phase
         // commit request
         boolean onePhaseCommit = false;
         if (this. == .) {
             onePhaseCommit = input.readBoolean();
         }
 
         // start processing
         if (transactionID instanceof UserTransactionID) {
             // handle UserTransaction
             final UserTransactionManagementTask userTransactionManagementTask;
             switch (this.) {
                 case :
                     userTransactionManagementTask = new UserTransactionCommitTask(thisthis.this., (UserTransactionIDtransactionIDchannelAssociationinvocationId);
                     break;
                 case :
                     userTransactionManagementTask = new UserTransactionRollbackTask(thisthis.this., (UserTransactionIDtransactionIDchannelAssociationinvocationId);
                     break;
                default:
                    throw ..unknownTransactionRequestType(this..name());
            }
            // submit to a seperate thread for processing the request
            this..submit(userTransactionManagementTask);
        } else if (transactionID instanceof XidTransactionID) {
            // handle XidTransactionID
            final XidTransactionID xidTransactionID = (XidTransactionIDtransactionID;
            final XidTransactionManagementTask xidTransactionManagementTask;
            switch (this.) {
                case :
                    xidTransactionManagementTask = new XidTransactionCommitTask(thisthis.this.xidTransactionIDchannelAssociationinvocationIdonePhaseCommit);
                    break;
                case :
                    xidTransactionManagementTask = new XidTransactionPrepareTask(thisthis.this.xidTransactionIDchannelAssociationinvocationId);
                    break;
                case :
                    xidTransactionManagementTask = new XidTransactionRollbackTask(thisthis.this.xidTransactionIDchannelAssociationinvocationId);
                    break;
                case :
                    xidTransactionManagementTask = new XidTransactionForgetTask(thisthis.this.xidTransactionIDchannelAssociationinvocationId);
                    break;
                case :
                    xidTransactionManagementTask = new XidTransactionBeforeCompletionTask(thisthis.this.xidTransactionIDchannelAssociationinvocationId);
                    break;
                default:
                    throw ..unknownTransactionRequestType(this..name());
            }
            // submit to a separate thread for processing the request
            this..submit(xidTransactionManagementTask);
        }
    }
    protected void writeTxPrepareResponseMessage(final ChannelAssociation channelAssociationfinal short invocationIdfinal int xaResourceStatusCodethrows IOException {
        final DataOutputStream dataOutputStream;
        final MessageOutputStream messageOutputStream;
        try {
            messageOutputStream = channelAssociation.acquireChannelMessageOutputStream();
        } catch (Exception e) {
            throw ..failedToOpenMessageOutputStream(e);
        }
        dataOutputStream = new DataOutputStream(messageOutputStream);
        try {
            // write header
            dataOutputStream.writeByte();
            // write invocation id
            dataOutputStream.writeShort(invocationId);
            // write a "bit" to indicate that this message contains the XAResource status for a "prepare"
            // invocation
            dataOutputStream.writeBoolean(true);
            // write the XAResource status
            PackedInteger.writePackedInteger(dataOutputStreamxaResourceStatusCode);
        } finally {
            channelAssociation.releaseChannelMessageOutputStream(messageOutputStream);
            dataOutputStream.close();
        }
    }
    protected void writeTxInvocationResponseMessage(final ChannelAssociation channelAssociationfinal short invocationIdthrows IOException {
        final DataOutputStream dataOutputStream;
        final MessageOutputStream messageOutputStream;
        try {
            messageOutputStream = channelAssociation.acquireChannelMessageOutputStream();
        } catch (Exception e) {
            throw ..failedToOpenMessageOutputStream(e);
        }
        dataOutputStream = new DataOutputStream(messageOutputStream);
        try {
            // write header
            dataOutputStream.writeByte();
            // write invocation id
            dataOutputStream.writeShort(invocationId);
            // write a "bit" to indicate that this message doesn't contain any XAResource status (i.e. not a
            // "prepare" invocation
            dataOutputStream.writeBoolean(false);
        } finally {
            channelAssociation.releaseChannelMessageOutputStream(messageOutputStream);
            dataOutputStream.close();
        }
    }
New to GrepCode? Check out our FAQ X