001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.gravy;
043:
044: import org.netbeans.jemmy.JemmyProperties;
045: import org.netbeans.jemmy.Timeout;
046:
047: /**
048: * This class is intended to be used as a base for anonymous classes
049: * aimed on several repeating of some action which is possible to failed
050: * from first (second, third..) attempt
051: */
052: public class Repeater {
053:
054: private int repeatCount = 3;
055: private long repeatTimeout = 2000;
056:
057: /** Creates a new instance of Repeater
058: * Uses default of 3 attempts with 2 sec timeout
059: */
060: public Repeater() {
061: }
062:
063: /** Creates a new instance of Repeater
064: * Uses default 2 sec timeout
065: * @param repeatCount Number of attempts to try
066: */
067: public Repeater(int repeatCount) {
068: this .repeatCount = repeatCount;
069: }
070:
071: /** Creates a new instance of Repeater
072: * @param repeatCount Number of attempts to try
073: * @param repeatTimout Timeout between attempts
074: */
075: public Repeater(int repeatCount, long repeatTimeout) {
076: this .repeatCount = repeatCount;
077: this .repeatTimeout = repeatTimeout;
078: }
079:
080: /**
081: * Trys to perform action() several attempts, if any errors are
082: * thrown, tryis to repeat it until specified repeat number of
083: * attempts is reached
084: */
085: public void tryAction() {
086: boolean success = false;
087: for (int i = 0; !success && i < repeatCount; i++) {
088: try {
089: action();
090: success = true;
091: } catch (Error e) {
092: JemmyProperties.getCurrentOutput().printTrace(
093: "Attempt = " + (i + 1)
094: + " failed with the following error:");
095: e.printStackTrace(JemmyProperties.getCurrentOutput()
096: .getOutput());
097: // We re-throw exception if this is the last attempt
098: if (i == repeatCount - 1)
099: throw e;
100: new Timeout("repeatTimeout", repeatTimeout).sleep();
101: } catch (RuntimeException re) {
102: JemmyProperties
103: .getCurrentOutput()
104: .printTrace(
105: "Attempt = "
106: + (i + 1)
107: + " failed with the following exception:");
108: re.printStackTrace(JemmyProperties.getCurrentOutput()
109: .getOutput());
110: // We re-throw exception if this is the last attempt
111: if (i == repeatCount - 1)
112: throw re;
113: new Timeout("repeatTimeout", repeatTimeout).sleep();
114: }
115: }
116: }
117:
118: /**
119: * Trys to perform action() several attempts, if any errors are
120: * thrown, tryis to repeat it until specified repeatCount number of
121: * attempts is reached
122: * @param repeatCount Number of attempts to perform
123: */
124: public void tryAction(int repeatCount) {
125: this .repeatCount = repeatCount;
126: tryAction();
127: }
128:
129: /**
130: * Trys to perform action() several attempts, if any errors are
131: * thrown, tryis to repeat it until specified repeatCount number of
132: * attempts is reached. Uses specified repeatTimeout between attempts
133: * @param repeatCount Number of attempts to perform
134: * @param repeatTimeout Timeout between attempts
135: */
136: public void tryAction(int repeatCount, long repeatTimeout) {
137: this .repeatCount = repeatCount;
138: this .repeatTimeout = repeatTimeout;
139: tryAction();
140: }
141:
142: /**
143: * Action to perform. Implement it in your anonymous class
144: */
145: public void action() {
146: JemmyProperties.getCurrentOutput().printTrace(
147: "Repeater: Nothing set to do!");
148: }
149: }
|