1 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 { 64 private ServerSocket fServerSocket; 65 private Socket fClientSocket; 66 private Thread m_thread; 67 private int m_port; 68 private ObjectServer m_oserver; 69 private boolean m_accept = true; 70 private Object m_acceptLock = new Object (); 71 72 public GatedListener(ObjectServer oserver, int port) { 73 m_oserver = oserver; 74 m_port = port; 75 m_thread = new Thread (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 e) { 87 if (e instanceof SecurityException ) { 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 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 { 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 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 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 |