001: package org.tanukisoftware.wrapper.test;
002:
003: /*
004: * Copyright (c) 1999, 2006 Tanuki Software Inc.
005: *
006: * Permission is hereby granted, free of charge, to any person
007: * obtaining a copy of the Java Service Wrapper and associated
008: * documentation files (the "Software"), to deal in the Software
009: * without restriction, including without limitation the rights
010: * to use, copy, modify, merge, publish, distribute, sub-license,
011: * and/or sell copies of the Software, and to permit persons to
012: * whom the Software is furnished to do so, subject to the
013: * following conditions:
014: *
015: * The above copyright notice and this permission notice shall be
016: * included in all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
020: * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
021: * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
022: * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
023: * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
024: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
025: * OTHER DEALINGS IN THE SOFTWARE.
026: */
027:
028: import java.io.IOException;
029: import java.io.PrintStream;
030:
031: /**
032: * A print stream which can be put into a state in which all calls to write
033: * to it will result in the calling threads deadlocking in the call.
034: * Obviously, this class will not be useful to many as it is for tests.
035: *
036: * @author Leif Mortenson <leif@tanukisoftware.com>
037: */
038: public class DeadlockPrintStream extends PrintStream {
039: /** The Wrapped PrintStream. */
040: private PrintStream m_out;
041:
042: /** True if calling threads should be deadlocked. */
043: private boolean m_deadlock;
044:
045: /*---------------------------------------------------------------
046: * Constructors
047: *-------------------------------------------------------------*/
048: /**
049: * Creates a new DeadlockPrintStream wrapped around another PrintStream.
050: *
051: * @param out The PrintStream which will be wrapped by this new stream.
052: */
053: public DeadlockPrintStream(PrintStream out) {
054: super (out);
055:
056: m_out = out;
057: }
058:
059: /*---------------------------------------------------------------
060: * PrintStream Methods
061: *-------------------------------------------------------------*/
062: public void write(int b) {
063: deadlock();
064: m_out.write(b);
065: }
066:
067: public void write(byte[] b) throws IOException {
068: deadlock();
069: m_out.write(b);
070: }
071:
072: public void write(byte[] b, int off, int len) {
073: deadlock();
074: m_out.write(b, off, len);
075: }
076:
077: public void flush() {
078: deadlock();
079: m_out.flush();
080: }
081:
082: public void close() {
083: deadlock();
084: m_out.close();
085: }
086:
087: /*---------------------------------------------------------------
088: * Methods
089: *-------------------------------------------------------------*/
090: /**
091: * This call will not return as long as the m_deadLock flag is set.
092: * If it is ever cleared with a call to setDeadlock(), stuck threads
093: * will all be released.
094: */
095: private void deadlock() {
096: if (m_deadlock) {
097: synchronized (this ) {
098: while (m_deadlock) {
099: try {
100: this .wait();
101: } catch (InterruptedException e) {
102: // Ignore
103: }
104: }
105: }
106: }
107: }
108:
109: /**
110: * Sets or clears the deadlock flag. If set, calls to any other method
111: * of this class will result in the calling thread being deadlocked.
112: * They will be released if the flag is cleared with this method.
113: *
114: * @param deadlock True to set the flag, false to clear it.
115: */
116: public void setDeadlock(boolean deadlock) {
117: m_deadlock = deadlock;
118: if (!m_deadlock) {
119: synchronized (this ) {
120: // Release any threads that are waiting.
121: this.notifyAll();
122: }
123: }
124: }
125: }
|