1 23 24 package com.sun.enterprise.admin.jmx.remote.notification; 25 26 import java.io.IOException ; 27 import java.io.EOFException ; 28 import java.io.InputStream ; 29 import java.io.BufferedInputStream ; 30 import java.io.ObjectInputStream ; 31 import java.io.InputStreamReader ; 32 import java.io.BufferedReader ; 33 import java.nio.channels.ClosedChannelException ; 34 import java.net.SocketException ; 35 import java.net.SocketTimeoutException ; 36 import java.rmi.ConnectException ; 37 import java.rmi.ConnectIOException ; 38 import java.net.URLConnection ; 39 import java.net.HttpURLConnection ; 40 import java.util.logging.Logger ; 41 42 import com.sun.enterprise.admin.jmx.remote.notification.NotificationWrapper; 43 import com.sun.enterprise.admin.jmx.remote.comm.HttpConnectorAddress; 44 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration; 45 46 53 class NotificationReceiver implements Runnable { 54 private ClientNotificationManager mgr = null; 55 56 private String notifMgrUri = null; 57 private HttpConnectorAddress ad = null; 58 private URLConnection mConnection = null; 59 private ObjectInputStream objIn = null; 60 private InputStream in = null; 61 private boolean exit = false; 62 private boolean connected = false; 63 private boolean timedout = false; 64 private int nReconnected= 0; 65 private Thread receiveThr = null; 66 67 private static final Logger logger = Logger.getLogger( 68 DefaultConfiguration.JMXCONNECTOR_LOGGER); 70 71 public NotificationReceiver(HttpConnectorAddress ad, ClientNotificationManager mgr) 72 throws IOException { 73 this.mgr = mgr; 74 this.ad = ad; 75 this.notifMgrUri = getNotifMgrURI(); 76 connect(); 77 receiveThr = new Thread (this); 78 receiveThr.start(); 79 } 80 81 private String getNotifMgrURI() { 82 String uri = ad.getPath(); 83 if (uri == null || uri.trim().length() == 0) 84 uri = DefaultConfiguration.DEFAULT_SERVLET_CONTEXT_ROOT; 85 uri = uri + 86 DefaultConfiguration.NOTIF_MGR_PATHINFO + 87 "?" + DefaultConfiguration.NOTIF_ID_PARAM + 88 "=" + mgr.getId(); 89 return uri; 90 } 91 92 private void connect() throws IOException { 93 if (connected) 94 return; 95 connect(null, false); 96 } 97 98 private void connect(String cmd, boolean disconnect) throws IOException { 99 try{ 100 String uri = notifMgrUri; 101 if (cmd != null) 102 uri = uri + "&" + 103 DefaultConfiguration.NOTIF_CMD_PARAM + 104 "=" + DefaultConfiguration.NOTIF_CMD_CLOSE; 105 System.setProperty("sun.net.client.defaultConnectTimeout", 106 Integer.toString(DefaultConfiguration.NOTIF_CONNECT_TIMEOUT)); URLConnection conn = ad.openConnection(uri); 108 InputStream inStream = conn.getInputStream(); 110 if (!disconnect) { 111 mConnection = conn; 112 in = inStream; 113 connected = true; 114 nReconnected = 0; 115 } else { 116 disconnect(conn, inStream); 117 } 118 } catch (IOException ioe){ 119 ioe.printStackTrace(); 120 throw (ioe); 121 } 122 } 123 124 129 public boolean reinit() throws IOException { 130 if (connected) 131 return true; 132 133 timedout = false; 134 try { 135 connect(); 136 } catch (IOException ioe) { 137 timedout = true; 138 throw ioe; 139 } 140 nReconnected = 0; 141 receiveThr = new Thread (this); 142 receiveThr.start(); 143 return true; 144 } 145 146 153 public boolean hasTimedout() { 154 return timedout; 155 } 156 157 163 public void run() { 164 while (!isExiting()) { 165 try { 166 connect(); 167 readNotification(); 168 } catch (IOException ioe) { 169 if (isExiting()) 170 break; 171 ioe.printStackTrace(); 172 if (ioe instanceof SocketTimeoutException ) { 173 timedout = true; 174 break; 175 } 176 if (isDisconnected(ioe)) { 177 connected = false; 178 nReconnected++; 179 if (nReconnected > 3) { 180 timedout = true; 181 break; 182 } 183 continue; 184 } else if (isExiting()) { 185 break; 186 } 187 } 188 } 189 } 190 191 private boolean isDisconnected(IOException ioe) { 192 if (ioe instanceof ClosedChannelException || 193 ioe instanceof SocketException || 194 ioe instanceof ConnectException || 195 ioe instanceof ConnectIOException || 196 ioe instanceof EOFException ) 197 return true; 198 return false; 199 } 200 201 private void disconnect() throws IOException { 202 disconnect(mConnection, in); 203 } 204 205 private void disconnect(URLConnection conn, InputStream in) throws IOException { 206 if (conn instanceof HttpURLConnection ) { 207 ((HttpURLConnection )conn).disconnect(); 208 } else 209 in.close(); 210 } 211 212 public void exit() throws Exception { 213 exit = true; 214 sendCloseMessage(); 215 disconnect(); 216 receiveThr.join(); 217 } 218 219 private void sendCloseMessage() throws IOException { 220 connect("close", true); 221 } 222 223 private boolean isExiting() { 224 return exit; 225 } 226 227 private void readNotification() throws IOException { 228 Object obj = null; 229 try { 230 objIn = new ObjectInputStream (in); 231 obj = objIn.readObject(); 232 } catch (IOException ioe) { 233 String msg = ioe.getMessage(); 234 if (msg != null && msg.indexOf("EOF") != -1) 235 throw (new EOFException (msg)); 236 throw ioe; 237 } catch (ClassNotFoundException notfound) { 238 return; 240 } 241 242 NotificationWrapper wrapr = (NotificationWrapper) obj; 243 if (wrapr.getType() == NotificationWrapper.WAIT) { 244 return; 245 } 246 247 mgr.raiseEvent(wrapr); 248 } 249 } 250 | Popular Tags |