Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   * 
   * Copyright 1997-2007 Sun Microsystems, Inc. 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.html
  * or glassfish/bootstrap/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 glassfish/bootstrap/legal/LICENSE.txt.
  * Sun designates this particular file as subject to the "Classpath" exception
  * as provided by Sun in the GPL Version 2 section of the License file that
  * accompanied this code.  If applicable, add the following below the License
  * Header, with the fields enclosed by brackets [] replaced by your own
  * identifying information: "Portions Copyrighted [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.
  */
 
 /*
  * Copyright 2004-2005 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
 package com.sun.jts.utils.RecoveryHooks;
 
 import  org.omg.CosTransactions.Coordinator;
 
This class defines API's for the test harness to induce recovery by setting crash points (in the case of TM crashes) and wait points (in the case of RM crashes). In order to induce TM crashes, the test harness sets the failure points by calling setCrashPoint(), after transaction.begin() from each thread. Thus, multiple threads involving different transactions can set different points of failure, which will cause the TM to wait at the failure points, until crash() method is invoked. Note: Only those transactions calling setCrashPoint() will be affected. The crash() method needs to be invoked from a seperate thread. The TM would increment TestRecovery.waitCount field to indicate the total number of failure points reached. When the waitCount reaches the expected value (obtained through getWaitCount()), the test harness shall call crash(). In order to test RM recovery, the test harness sets the failure points by calling setWaitPoint(), after transaction.begin() from each thread. The TM waits at predefined failure points for the stipulated time duration, during which the RM is expected to crash; after which the TM will proceed with regular completion (a human is expected to crash the RM manually during the wait duration). As in the TM recovery case, the waitCount will be incremented to indicate the total number of failure points reached. If the RM does not crash during the stipulated time duration, the TM will proceed with normal completion for the specific transaction. It does not matter if the RM comes back alive, since the TM would anyway retry completion. Note: Only those transactions calling setWaitPoint() will be affected.

Author(s):
Ram Jeyaraman 04/21/1999
 
 public class FailureInducer {
 
     // static finals
     public static final Integer ACTIVE = new Integer(0);
     public static final Integer PREPARING = new Integer(1);
     public static final Integer PREPARED = new Integer(2);
     public static final Integer COMPLETING = new Integer(3);
     public static final Integer COMPLETED = new Integer(4);
    // static fields
    private static boolean failureInducerIsActive = false;
    private static boolean crash = false;
    private static int waitPeriod = 0;
    private static int waitCount = 0;
    private static Hashtable crashList = new Hashtable();
    private static Hashtable waitList = new Hashtable();
    private static Hashtable waitTime = new Hashtable();
    private static ResourceBundle messages = ResourceBundle.
        getBundle("com.sun.jts.utils.RecoveryHooks.Messages"/*#Frozen*/);
	/*
		Logger to log transaction messages 
	*/  
	    static Logger _logger = LogDomains.getLogger(FailureInducer.class.);
    // static initializer
    static {
    }
    // static methods

    
This activates the FailureInducer. An application needs to activate the failure inducer first, before setting the fail or wait points.
    public static void activateFailureInducer() {
         = true;
    }

    
This deactivates the FailureInducer. An application deactivate the failure inducer, to temporarily stop failure inducement. The fail or wait points are not forgotten during the dormant state.
    public static void deactivateFailureInducer() {
         = false;
    }

    

Returns:
the current state (active or inactive) of failure inducer.
    public static boolean isFailureInducerActive() {
        return ;
    }

    
Setting a crash point will cause the TM to wait at the failure point, until crash() is called.

Parameters:
crashPoint pre-defined failure points (PREPARING, PREPARED, COMMITTING, COMMITTED).
    public static void setCrashPoint(Integer crashPoint) {
        // sanity check
        if (crashPoint == null) {
			.log(.,"jts.invalid_crash_point");
            return;
        }
        Coordinator coord = Utility.getCoordinator(Utility.getControl());
        JCoordinator jcoord = JCoordinatorHelper.narrow(coord);
        if (jcoord != null) {
            GlobalTID gtid = new GlobalTID(jcoord.getGlobalTID());
            .put(gtidcrashPoint);
        }
    }

    
Setting a wait point will cause the TM to wait at the failure point, for the stipulated wait duration.

Parameters:
crashPoint pre-defined failure points (PREPARING, PREPARED, COMMITTING, COMMITTED).
waitDuration time duration (seconds) for RM failure to happen.
    public static void setWaitPoint(Integer waitPointint waitDuration) {
        // sanity check
        if (waitPoint == null) {
			.log(.,"jts.invalid_wait_point");
            return;
        }
        Coordinator coord = Utility.getCoordinator(Utility.getControl());
        JCoordinator jcoord = JCoordinatorHelper.narrow(coord);
        if (jcoord != null) {
            GlobalTID gtid = new GlobalTID(jcoord.getGlobalTID());
            .put(gtidwaitPoint);
            .put(gtidnew Integer(waitDuration));
        }
    }

    
Forces the TM to crash.
    public static void crash() {
         = true;
    }

    
Increments the wait count (called only by TM).
    private static void incrementWaitCount() {
        ++;
    }

    

Returns:
the total number of failure points reached.
    public static int getWaitCount() {
        return ;
    }

    
This method is called by the coordinator at every valid failure point. If the crash point or the wait point set for the current transaction matches the current failure point, an appropriate action (crash or wait) is taken. Note: Crash action takes precedence over wait actions for the same failure point.

Parameters:
coord the coordinator object (which represents the transaction.
failPoint indicates the current failure point in coordinator code.
    public static void waitForFailure(GlobalTID gtidInteger failPoint) {
        // sanity check
        if (gtid == null)
            return;
        Integer crashPoint = (Integer.get(gtid);
        Integer waitPoint = (Integer.get(gtid);
        // no crash point or wait point has been set for the transaction
        if (crashPoint == null && waitPoint == null) {
            return;
        }
        .log(.,"jts.failpoint",failPoint);
        // increment wait count and wait for the crash flag to be set
        if (crashPoint != null && crashPoint.equals(failPoint)) {
            incrementWaitCount();
            while ( == false) {
                try {
                    Thread.sleep(3000);
                } catch (Exception e) {}
            }
            System.exit(0);
        }
        // wait for the wait duration and continue
        if (waitPoint != null && waitPoint.equals(failPoint)) {
            Integer waitDuration = (Integer.get(gtid);
            // wait duration has not be set or is invalid
            if (waitDuration == null || waitDuration.intValue() < 0)
                return;
            // wait for the stipulated duration
            try {
                Thread.sleep(waitDuration.intValue() * 1000);
            } catch (Exception e) {}
        }
    }
    // Utility methods for fetching i18n/l10n messages
    private static String getMessage(String key) {
        return getMessage(keynull);
    }
    private static String getMessage(String keyObject[] inserts) {
        if ( == null || key == null) {
            return null;
        }
        String msg = .getString(key);
        if (msg == null) {
            return null;
        }
        if (inserts == null) {
            return msg;
        } else {
            return MessageFormat.format(msginserts);
        }
    }
New to GrepCode? Check out our FAQ X