KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tanukisoftware > wrapper > test > DeadlockPrintStream


1 package org.tanukisoftware.wrapper.test;
2
3 /*
4  * Copyright (c) 1999, 2006 Tanuki Software Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of the Java Service Wrapper and associated
8  * documentation files (the "Software"), to deal in the Software
9  * without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sub-license,
11  * and/or sell copies of the Software, and to permit persons to
12  * whom the Software is furnished to do so, subject to the
13  * following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */

27
28 import java.io.IOException JavaDoc;
29 import java.io.PrintStream JavaDoc;
30
31 /**
32  * A print stream which can be put into a state in which all calls to write
33  * to it will result in the calling threads deadlocking in the call.
34  * Obviously, this class will not be useful to many as it is for tests.
35  *
36  * @author Leif Mortenson <leif@tanukisoftware.com>
37  */

38 public class DeadlockPrintStream extends PrintStream JavaDoc {
39     /** The Wrapped PrintStream. */
40     private PrintStream JavaDoc m_out;
41     
42     /** True if calling threads should be deadlocked. */
43     private boolean m_deadlock;
44     
45     /*---------------------------------------------------------------
46      * Constructors
47      *-------------------------------------------------------------*/

48     /**
49      * Creates a new DeadlockPrintStream wrapped around another PrintStream.
50      *
51      * @param out The PrintStream which will be wrapped by this new stream.
52      */

53     public DeadlockPrintStream( PrintStream JavaDoc out )
54     {
55         super( out );
56         
57         m_out = out;
58     }
59     
60     /*---------------------------------------------------------------
61      * PrintStream Methods
62      *-------------------------------------------------------------*/

63     public void write( int b )
64     {
65         deadlock();
66         m_out.write( b );
67     }
68     
69     public void write( byte[] b )
70         throws IOException JavaDoc
71     {
72         deadlock();
73         m_out.write( b );
74     }
75     
76     public void write( byte[] b, int off, int len )
77     {
78         deadlock();
79         m_out.write( b, off, len );
80     }
81     
82     public void flush()
83     {
84         deadlock();
85         m_out.flush();
86     }
87     
88     public void close()
89     {
90         deadlock();
91         m_out.close();
92     }
93     
94     /*---------------------------------------------------------------
95      * Methods
96      *-------------------------------------------------------------*/

97     /**
98      * This call will not return as long as the m_deadLock flag is set.
99      * If it is ever cleared with a call to setDeadlock(), stuck threads
100      * will all be released.
101      */

102     private void deadlock()
103     {
104         if ( m_deadlock )
105         {
106             synchronized( this )
107             {
108                 while( m_deadlock )
109                 {
110                     try
111                     {
112                         this.wait();
113                     }
114                     catch ( InterruptedException JavaDoc e )
115                     {
116                         // Ignore
117
}
118                 }
119             }
120         }
121     }
122     
123     /**
124      * Sets or clears the deadlock flag. If set, calls to any other method
125      * of this class will result in the calling thread being deadlocked.
126      * They will be released if the flag is cleared with this method.
127      *
128      * @param deadlock True to set the flag, false to clear it.
129      */

130     public void setDeadlock( boolean deadlock )
131     {
132         m_deadlock = deadlock;
133         if ( !m_deadlock )
134         {
135             synchronized( this )
136             {
137                 // Release any threads that are waiting.
138
this.notifyAll();
139             }
140         }
141     }
142 }
143
144
Popular Tags