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): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * 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: *
043: * $Id$ $Revision$ $Date$
044: *
045: */
046:
047: package org.netbeans.jemmy;
048:
049: import java.io.FileInputStream;
050: import java.io.FileNotFoundException;
051: import java.io.InputStream;
052: import java.io.IOException;
053: import java.io.PrintStream;
054: import java.io.PrintWriter;
055:
056: import java.util.Enumeration;
057: import java.util.Hashtable;
058: import java.util.Properties;
059:
060: /**
061: *
062: * Class to store and process a set of timeout values.
063: *
064: * @see #setDefault(String, long)
065: * @see #getDefault(String)
066: * @see #setTimeout(String, long)
067: * @see #getTimeout(String)
068: *
069: * @author Alexandre Iline (alexandre.iline@sun.com)
070: */
071:
072: public class Timeouts extends Object {
073:
074: private static final long DELTA_TIME = 100;
075:
076: private static Timeouts defaults;
077:
078: private Hashtable timeouts;
079:
080: /**
081: * Creates empty Timeouts object.
082: */
083: public Timeouts() {
084: super ();
085: timeouts = new Hashtable();
086: setTimeout("Timeouts.DeltaTimeout", DELTA_TIME);
087: try {
088: load();
089: } catch (IOException e) {
090: }
091: }
092:
093: /**
094: * Stores default timeout value.
095: * @param name Timeout name.
096: * @param newValue Timeout value.
097: * @see #getDefault(String)
098: * @see #initDefault(String, long)
099: * @see #containsDefault(String)
100: */
101: public static void setDefault(String name, long newValue) {
102: defaults.setTimeout(name, newValue);
103: }
104:
105: /**
106: * Sets default timeout value if it was not set before.
107: * @param name Timeout name.
108: * @param newValue Timeout value.
109: * @see #setDefault(String, long)
110: * @see #getDefault(String)
111: * @see #containsDefault(String)
112: */
113: public static void initDefault(String name, long newValue) {
114: defaults.initTimeout(name, newValue);
115: }
116:
117: /**
118: * Gets default timeout value.
119: * @param name Timeout name.
120: * @return Timeout value or -1 if timeout is not defined.
121: * @see #setDefault(String, long)
122: * @see #initDefault(String, long)
123: * @see #containsDefault(String)
124: */
125: public static long getDefault(String name) {
126: return (defaults.getTimeout(name));
127: }
128:
129: /**
130: * Check that default timeout value was defined.
131: * @param name Timeout name.
132: * @return True if timeout has been defined, false otherwise.
133: * @see #setDefault(String, long)
134: * @see #getDefault(String)
135: * @see #initDefault(String, long)
136: */
137: public static boolean containsDefault(String name) {
138: return (defaults.contains(name));
139: }
140:
141: static {
142: defaults = new Timeouts();
143: }
144:
145: /**
146: * Loads default timeouts values.
147: *
148: * @param stream Stream to load timeouts from.
149: * @see org.netbeans.jemmy.Timeouts#loadDefaults(String)
150: * @see org.netbeans.jemmy.Timeouts#loadDefaults()
151: * @exception IOException
152: */
153: public void loadDefaults(InputStream stream) throws IOException {
154: defaults.load(stream);
155: }
156:
157: /**
158: * Loads default timeouts values from file.
159: *
160: * @param fileName File to load timeouts from.
161: * @see org.netbeans.jemmy.Timeouts#loadDefaults(InputStream)
162: * @see org.netbeans.jemmy.Timeouts#loadDefaults(String)
163: * @exception IOException
164: * @exception FileNotFoundException
165: */
166: public void loadDefaults(String fileName)
167: throws FileNotFoundException, IOException {
168: defaults.load(fileName);
169: }
170:
171: /**
172: * Loads default timeouts values.
173: * Uses jemmy.timeouts system property to get timeouts file.
174: *
175: * @see org.netbeans.jemmy.Timeouts#loadDefaults(InputStream)
176: * @see org.netbeans.jemmy.Timeouts#loadDefaults(String)
177: * @exception IOException
178: * @exception FileNotFoundException
179: */
180: public void loadDefaults() throws FileNotFoundException,
181: IOException {
182: defaults.load();
183: }
184:
185: /**
186: * Creates Timeout new object by name and getTimeout(name) value.
187: * @param name Timeout name.
188: * @return a Timeout instance.
189: */
190: public Timeout create(String name) {
191: return (new Timeout(name, getTimeout(name)));
192: }
193:
194: /**
195: * Create timeout for "Timeouts.DeltaTimeout" name.
196: * @return a Timeout instance.
197: */
198: public Timeout createDelta() {
199: return (create("Timeouts.DeltaTimeout"));
200: }
201:
202: /**
203: * Checks if timeout has already been defined in this timeout instance.
204: * @param name Timeout name.
205: * @return True if timeout has been defined, false otherwise.
206: * @see #containsDefault(String)
207: */
208: public boolean contains(String name) {
209: return (timeouts.containsKey(name));
210: }
211:
212: /**
213: * Sets new timeout value.
214: * @param name Timeout name.
215: * @param newValue Timeout value.
216: * @return old timeout value
217: * @see #getTimeout
218: */
219: public long setTimeout(String name, long newValue) {
220: long oldValue = -1;
221: if (contains(name)) {
222: oldValue = getTimeout(name);
223: timeouts.remove(name);
224: }
225: timeouts.put(name, new Long(newValue));
226: return (oldValue);
227: }
228:
229: /**
230: * Gets timeout value.
231: * It timeout was not defined in this instance,
232: * returns default timeout value.
233: * @param name Timeout name.
234: * @return Timeout value.
235: * @see #getDefault(String)
236: * @see #setTimeout
237: */
238: public long getTimeout(String name) {
239: if (contains(name)) {
240: Long value = (Long) timeouts.get(name);
241: if (value != null) {
242: return (value.longValue());
243: }
244: }
245: if (this != defaults) {
246: return (getDefault(name));
247: } else {
248: return (-1);
249: }
250: }
251:
252: /**
253: * Gets "Timeouts.DeltaTimeout" timeout value.
254: * @return Timeout value.
255: * @see #getDefault(String)
256: */
257: public long getDeltaTimeout() {
258: return (getTimeout("Timeouts.DeltaTimeout"));
259: }
260:
261: /**
262: * Sets timeout value if it was not set before.
263: * @param name Timeout name.
264: * @param newValue Timeout value.
265: * @return old timeout value
266: */
267: public long initTimeout(String name, long newValue) {
268: long result = getTimeout(name);
269: if (!contains(name)) {
270: setTimeout(name, newValue);
271: }
272: return (result);
273: }
274:
275: /**
276: * Creates a copy of the current timeouts set.
277: * @return A copy.
278: */
279: public Timeouts cloneThis() {
280: Timeouts t = new Timeouts();
281: Enumeration e = timeouts.keys();
282: String name = "";
283: while (e.hasMoreElements()) {
284: name = (String) e.nextElement();
285: t.setTimeout(name, getTimeout(name));
286: }
287: return (t);
288: }
289:
290: /**
291: * Sleeps for the "name" timeout value.
292: * Can throw InterruptedException if current thread was interrupted.
293: *
294: * @param name Timeout name.
295: * @exception InterruptedException
296: */
297: public void eSleep(String name) throws InterruptedException {
298: if (contains(name) || defaults.contains(name)) {
299: Thread.currentThread().sleep(getTimeout(name));
300: }
301: }
302:
303: /**
304: * Sleeps for the "name" timeout value.
305: * Does not throw InterruptedException anyway.
306: * @param name Timeout name.
307: */
308: public void sleep(String name) {
309: create(name).sleep();
310: }
311:
312: /**
313: * Prins all defined timeouts.
314: * @param pw PrintWriter to print into.
315: */
316: public void print(PrintWriter pw) {
317: Enumeration e = timeouts.keys();
318: String name = "";
319: while (e.hasMoreElements()) {
320: name = (String) e.nextElement();
321: pw.println(name + " = " + Long.toString(getTimeout(name)));
322: }
323: pw.println("Default values:");
324: e = defaults.timeouts.keys();
325: name = "";
326: while (e.hasMoreElements()) {
327: name = (String) e.nextElement();
328: if (!contains(name)) {
329: pw.println(name + " = "
330: + Long.toString(getDefault(name)));
331: }
332: }
333: }
334:
335: /**
336: * Prins all defined timeouts.
337: * @param ps PrintStream to print into.
338: */
339: public void print(PrintStream ps) {
340: print(new PrintWriter(ps));
341: Timeouts t = new Timeouts();
342: }
343:
344: /**
345: * Loads timeouts values.
346: *
347: * @param stream Stream to load timeouts from.
348: * @see org.netbeans.jemmy.Timeouts#load(String)
349: * @see org.netbeans.jemmy.Timeouts#load()
350: * @exception IOException
351: */
352: public void load(InputStream stream) throws IOException {
353: Properties props = new Properties();
354: props.load(stream);
355: Enumeration propNames = props.propertyNames();
356: long propValue = -1;
357: String propName = null;
358: while (propNames.hasMoreElements()) {
359: propName = (String) propNames.nextElement();
360: propValue = -1;
361: propValue = (new Long(props.getProperty(propName)))
362: .longValue();
363: setTimeout(propName, propValue);
364: }
365: }
366:
367: /**
368: * Loads timeouts values from file.
369: *
370: * @param fileName File to load timeouts from.
371: * @see org.netbeans.jemmy.Timeouts#load(InputStream)
372: * @see org.netbeans.jemmy.Timeouts#load(String)
373: * @exception IOException
374: * @exception FileNotFoundException
375: */
376: public void load(String fileName) throws FileNotFoundException,
377: IOException {
378: load(new FileInputStream(fileName));
379: }
380:
381: /**
382: * Loads timeouts values.
383: * Uses jemmy.timeouts system property to get timeouts file.
384: *
385: * @see org.netbeans.jemmy.Timeouts#load(InputStream)
386: * @see org.netbeans.jemmy.Timeouts#load(String)
387: * @exception IOException
388: * @exception FileNotFoundException
389: */
390: public void load() throws FileNotFoundException, IOException {
391: if (System.getProperty("jemmy.timeouts") != null
392: && !System.getProperty("jemmy.timeouts").equals("")) {
393: load(System.getProperty("jemmy.timeouts"));
394: }
395: }
396:
397: /**
398: * Loads debug timeouts values.
399: *
400: * @exception IOException
401: */
402: public void loadDebugTimeouts() throws IOException {
403: load(getClass().getClassLoader().getResourceAsStream(
404: "org/netbeans/jemmy/debug.timeouts"));
405: }
406: }
|