KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > common > StreamListener


1 package org.jacorb.test.common;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2003 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.io.*;
24
25 /**
26  * A <code>StreamListener</code> listens to a given <code>InputStream</code>
27  * in its own thread of control. It copies anything that it reads from the
28  * stream to its own standard output stream. There is a special function
29  * that allows you to capture an IOR from the <code>InputStream</code>.
30  *
31  * @author <a HREF="mailto:spiegel@gnu.org">Andre Spiegel</a>
32  * @version $Id: StreamListener.java,v 1.4 2005/05/16 11:40:15 andre.spiegel Exp $
33  */

34 public class StreamListener extends Thread JavaDoc
35 {
36     private BufferedReader in = null;
37     private String JavaDoc id = null;
38     private String JavaDoc ior = null;
39
40     public StreamListener(InputStream stream, String JavaDoc id)
41     {
42         this.in = new BufferedReader(new InputStreamReader(stream));
43         this.id = id;
44         setDaemon (true);
45     }
46
47     /**
48      * This method blocks until a line of the form "SERVER IOR: <IOR>"
49      * is received from the InputStream.
50      */

51     public String JavaDoc getIOR()
52     {
53         while (true)
54         {
55             synchronized (this)
56             {
57                 if (this.ior != null)
58                     return this.ior;
59                 else
60                     try
61                     {
62                         this.wait();
63                     }
64                     catch (InterruptedException JavaDoc ex)
65                     {
66                         // ignore
67
}
68             }
69         }
70     }
71
72     public void run()
73     {
74         while (true)
75         {
76             try
77             {
78                 String JavaDoc line = in.readLine();
79                 if (line == null)
80                 {
81                     break;
82                 }
83                 else if (line.startsWith("SERVER IOR: "))
84                 {
85                     synchronized (this)
86                     {
87                         this.ior = line.substring(12);
88                         this.notifyAll();
89                     }
90                 }
91                 else
92                 {
93                     System.out.println("[ SERVER " + id + " " + line + " ]");
94                 }
95             }
96             catch (Exception JavaDoc ex)
97             {
98                 //System.out.println("Exception reading from server: " + ex);
99
System.out.println("StreamListener exiting");
100                 break;
101             }
102         }
103     }
104 }
105
Popular Tags