KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > debug > serverImpl > GatedListener


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

55
56 package org.apache.bsf.debug.serverImpl;
57
58 import java.io.*;
59 import java.net.*;
60 import java.security.*;
61 import org.apache.bsf.debug.util.*;
62
63 public class GatedListener implements Runnable JavaDoc {
64     private ServerSocket fServerSocket;
65     private Socket fClientSocket;
66     private Thread JavaDoc m_thread;
67     private int m_port;
68     private ObjectServer m_oserver;
69     private boolean m_accept = true;
70     private Object JavaDoc m_acceptLock = new Object JavaDoc();
71
72     public GatedListener(ObjectServer oserver, int port) {
73         m_oserver = oserver;
74         m_port = port;
75         m_thread = new Thread JavaDoc(this, "JSDI Connection Thread");
76
77         m_thread.start();
78     }
79
80     public void run() {
81         int num_retries = 3;
82
83         while (m_accept) {
84             try {
85                 accept();
86             } catch (Exception JavaDoc e) {
87                 if (e instanceof SecurityException JavaDoc) {
88                     DebugLog.stdoutPrintln("Security violation during " +
89                                            "socket operation.",
90                                            DebugLog.BSF_LOG_L0);
91                     e.printStackTrace();
92                 }
93                 else if (e instanceof ProtocolException) {
94                     DebugLog.stdoutPrintln("Client attempted to connect " +
95                                            "using unsupported protocol " +
96                                            "version",
97                                            DebugLog.BSF_LOG_L0);
98                 }
99                 else if (e instanceof IOException) {
100                     DebugLog.stdoutPrintln("Reason: I/O error opening socket.",
101                                            DebugLog.BSF_LOG_L0);
102                 }
103                 else {
104                     if (num_retries != 0) {
105                         // Possible temporary problem
106
// Retry num_retries times...
107
DebugLog.stdoutPrintln("**** Error in accept() - " +
108                                                num_retries +
109                                                " retry attempts remaining.",
110                                                DebugLog.BSF_LOG_L0);
111                         num_retries--;
112                     }
113                     else {
114                         DebugLog.stdoutPrintln("**** accept() failure. " +
115                                                "Please correct error " +
116                                                "and restart server.",
117                                                DebugLog.BSF_LOG_L0);
118                         DebugLog.stdoutPrintln(e.getMessage(),
119                                                DebugLog.BSF_LOG_L0);
120                         m_accept = false;
121                     }
122                 }
123             }
124         }
125     }
126
127     private void accept() throws Exception JavaDoc {
128         int major = -1, minor = -1;
129         InputStream istream;
130         OutputStream ostream;
131         DataInputStream distream;
132         DataOutputStream dostream;
133         
134         synchronized(m_acceptLock) {
135             try {
136                 fServerSocket = new ServerSocket(m_port);
137             } catch (Exception JavaDoc e) {
138                 DebugLog.stdoutPrintln("**** Could not listen on port: "
139                                        + m_port,
140                                        DebugLog.BSF_LOG_L0);
141                 m_accept = false;
142                 throw e;
143             }
144             
145             try {
146                 DebugLog.stdoutPrintln("Listener accepting on port: "
147                                        + m_port,
148                                        DebugLog.BSF_LOG_L1);
149                 fClientSocket =
150                     (Socket)
151                     AccessController.doPrivileged(new PrivilegedExceptionAction() {
152                             public Object JavaDoc run() throws IOException {
153                                 return fServerSocket.accept();
154                             }
155                         }
156                                                   );
157             } catch (PrivilegedActionException e) {
158                 DebugLog.stdoutPrintln("Accept failed on port: " + m_port,
159                                        DebugLog.BSF_LOG_L0);
160                 throw e.getException();
161             }
162             
163             DebugLog.stdoutPrintln("Accepted a connection on port: " + m_port,
164                                    DebugLog.BSF_LOG_L1);
165             
166             fServerSocket.close();
167             fServerSocket = null;
168             
169             istream = fClientSocket.getInputStream();
170             ostream = fClientSocket.getOutputStream();
171             dostream = new DataOutputStream(ostream);
172             distream = new DataInputStream(istream);
173             
174             major = distream.readInt();
175             minor = distream.readInt();
176             if (major != DebugConstants.BSF_DEBUG_PROTOCOL_MAJOR ||
177                 minor != DebugConstants.BSF_DEBUG_PROTOCOL_MINOR) {
178                 dostream.writeInt(DebugConstants.BSF_DEBUG_PROTOCOL_REJECT);
179                 fClientSocket.close();
180                 fClientSocket = null;
181                 throw new ProtocolException("Protocol version mismatch!");
182             }
183             else {
184                 DebugLog.stdoutPrintln("Debug client attached on port: " +
185                                        m_port,
186                                        DebugLog.BSF_LOG_L1);
187                 m_oserver.setIOStreams(istream, ostream, distream, dostream);
188                 m_oserver.awake();
189                 dostream.writeInt(DebugConstants.BSF_DEBUG_PROTOCOL_ACCEPT);
190                 m_acceptLock.wait();
191                 fClientSocket.close();
192                 fClientSocket = null;
193             }
194         }
195     }
196
197     protected void awake() {
198         synchronized(m_acceptLock) {
199             m_acceptLock.notify();
200         }
201     }
202 }
203
Popular Tags