Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2012 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.enterprise.resource.pool;
 
 
 import  javax.transaction.SystemException;
 import  javax.transaction.Transaction;
 import java.util.*;
Transaction helper for the pool to check various states of a resource that is taking part in the transaction.

Author(s):
Jagadish Ramu
 
 public class PoolTxHelper {
 
     private PoolInfo poolInfo;
 
     protected final static Logger _logger = LogDomains.getLogger(PoolTxHelper.class.);
 
     public PoolTxHelper(PoolInfo poolInfo){
         this. = poolInfo;
     }

    
Check whether the local resource can be put back to pool If true, unenlist the resource

Parameters:
h ResourceHandle to be verified
Returns:
boolean
 
     public boolean isLocalResourceEligibleForReuse(ResourceHandle h) {
         boolean result = false;
         if ((!isLocalResourceInTransaction(h))) {
             try {
                 enforceDelistment(h);
             } catch (SystemException se) {
                 if (.isLoggable(.)) {
                     .log(."Exception while delisting the local resource [ of pool : " +  + " ] "
                             + "forcibily from transaction"se);
                 }
                 return result;
             }
             h.getResourceState().setEnlisted(false);
             result = true;
         }
         return result;
     }

    
Remove the resource from book-keeping

Parameters:
h ResourceHandle to be delisted
Throws:
javax.transaction.SystemException when not able to delist the resource
    private void enforceDelistment(ResourceHandle hthrows SystemException {
        JavaEETransaction txn = (JavaEETransaction) ConnectorRuntime.getRuntime().getTransaction();
        if (txn != null) {
            Set set = txn.getResources();
            if (set != null)
                set.remove(h);
        }
    }

    
Check whether a local transaction is in progress.

Returns:
true if a local transaction is in progress.
    public boolean isLocalTransactionInProgress() {
        boolean result = false;
        try {
            JavaEETransaction txn = (JavaEETransaction) ConnectorRuntime.getRuntime().getTransaction();
            if(txn != null) {
                result = txn.isLocalTx();
            }
        } catch (SystemException e) {
            if (.isLoggable(.)) {
                .log(."Exception while checking whether a local "
                        + "transaction is in progress while using pool : " + e);
            }
        }
        return result;
    }
    
Check whether the local resource in question is the one participating in transaction.

Parameters:
h ResourceHandle
Returns:
true if the resource is participating in the transaction
    public boolean isLocalResourceInTransaction(ResourceHandle h) {
        boolean result = true;
        try {
            JavaEETransaction txn = (JavaEETransaction) ConnectorRuntime.getRuntime().getTransaction();
            if (txn != null)
                result = isNonXAResourceInTransaction(txnh);
        } catch (SystemException e) {
            if (.isLoggable(.)) {
                .log(."Exception while checking whether the resource [ of pool : " +  + " ] "
                        + "is nonxa and is enlisted in transaction : "e);
            }
        }
        return result;
    }

    
Check whether the resource is non-xa

Parameters:
resource Resource to be verified
Returns:
boolean indicating whether the resource is non-xa
    public boolean isNonXAResource(ResourceHandle resource) {
        return !resource.getResourceSpec().isXA();
    }

    
Check whether the non-xa resource is enlisted in transaction.

Parameters:
tran Transaction
resource Resource to be verified
Returns:
boolean indicating whether thegiven non-xa resource is in transaction
    private boolean isNonXAResourceInTransaction(JavaEETransaction tranResourceHandle resource) {
        return resource.equals(tran.getNonXAResource());
    }

    
Check whether the resource is non-xa, free and is enlisted in transaction.

Parameters:
tran Transaction
resource Resource to be verified
Returns:
boolean indicating whether the resource is free, non-xa and is enlisted in transaction
    public boolean isNonXAResourceAndFree(JavaEETransaction tranResourceHandle resource) {
        return resource.getResourceState().isFree() && isNonXAResource(resource) && isNonXAResourceInTransaction(tranresource);
    }

    
this method is called when a resource is enlisted in transation tran

Parameters:
tran Transaction to which the resource need to be enlisted
resource Resource to be enlisted in the transaction
    public void resourceEnlisted(Transaction tranResourceHandle resource) {
        try {
            JavaEETransaction j2eetran = (JavaEETransactiontran;
            Set set = j2eetran.getResources();
            if (set == null) {
                set = new HashSet();
                j2eetran.setResources(set);
            }
            set.add(resource);
        } catch (ClassCastException e) {
            if (.isLoggable(.)) {
                .log(."Pool [ " +  + " ]: resourceEnlisted:"
                        + "transaction is not J2EETransaction but a " + tran.getClass().getName(), e);
            }
        }
        ResourceState state = resource.getResourceState();
        state.setEnlisted(true);
        if (.isLoggable(.)) {
            .log(."Pool [ " +" ]: resourceEnlisted: " + resource);
        }
    }

    
this method is called when transaction tran is completed

Parameters:
tran transaction which has completed
status transaction status
poolInfo Pool name
Returns:
delisted resources
    public List<ResourceHandletransactionCompleted(Transaction tranint statusPoolInfo poolInfo) {
        JavaEETransaction j2eetran;
        List<ResourceHandledelistedResources = new ArrayList<ResourceHandle>();
        try {
             j2eetran = (JavaEETransactiontran;
        } catch (ClassCastException e) {
            if (.isLoggable(.)) {
                .log(."Pool: transactionCompleted: "
                        + "transaction is not J2EETransaction but a " + tran.getClass().getName(), e);
            }
            return delistedResources;
        }
        Set set = j2eetran.getResources(poolInfo);
        if (set == nullreturn delistedResources;
        Iterator iter = set.iterator();
        while (iter.hasNext()) {
            ResourceHandle resource = (ResourceHandleiter.next();
            ResourceState state = resource.getResourceState();
            state.setEnlisted(false);
            delistedResources.add(resource);
            iter.remove();
            if (.isLoggable(.)) {
                .log(."Pool: transactionCompleted: " + resource);
            }
        }
        return delistedResources;
    }
New to GrepCode? Check out our FAQ X