| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.phoenix.servlet; |
| 34 | | | |
| 35 | | | import java.awt.Color; |
| 36 | | | import java.io.DataInputStream; |
| 37 | | | import java.io.IOException; |
| 38 | | | import java.io.PrintWriter; |
| 39 | | | import java.io.StringWriter; |
| 40 | | | import java.net.InetAddress; |
| 41 | | | import java.net.Socket; |
| 42 | | | import java.net.UnknownHostException; |
| 43 | | | import java.text.DateFormat; |
| 44 | | | import java.text.SimpleDateFormat; |
| 45 | | | import java.util.Collections; |
| 46 | | | import java.util.ConcurrentModificationException; |
| 47 | | | import java.util.Date; |
| 48 | | | import java.util.HashMap; |
| 49 | | | import java.util.Iterator; |
| 50 | | | import java.util.Locale; |
| 51 | | | import java.util.Map; |
| 52 | | | import java.util.TimeZone; |
| 53 | | | |
| 54 | | | import javax.servlet.http.HttpServlet; |
| 55 | | | import javax.servlet.http.HttpServletRequest; |
| 56 | | | import javax.servlet.http.HttpServletResponse; |
| 57 | | | |
| 58 | | | import org.jcoderz.commons.util.IoUtil; |
| 59 | | | |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | | |
| 66 | | | |
| 67 | | | @author |
| 68 | | | |
| 69 | 0 | | public final class PingServlet |
| 70 | | | extends HttpServlet |
| 71 | | | { |
| 72 | | | |
| 73 | | | |
| 74 | | | |
| 75 | 0 | | public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC"); |
| 76 | | | |
| 77 | | | |
| 78 | | | public static final String DATE_TIME_FORMAT |
| 79 | | | = "yyyy-MM-dd HH:mm:ss.SSS"; |
| 80 | | | |
| 81 | | | private static final int HOST_NOT_REACHED = -1; |
| 82 | | | private static final int UNKNOWN_HOST = -2; |
| 83 | | | |
| 84 | | | private static final int ILLEGAL_ARGUMENT = -3; |
| 85 | | | private static final int HOST_REFUSED = -4; |
| 86 | | | |
| 87 | | | private static final long serialVersionUID = 1L; |
| 88 | | | |
| 89 | | | private static final int MAX_PING_CACHE_SIZE = 1000; |
| 90 | 0 | | private static final Map PING_CACHE |
| 91 | | | = Collections.synchronizedMap(new HashMap()); |
| 92 | 0 | | private static long sLastCacheUpdate = 0; |
| 93 | 0 | | private static boolean sUpdateInProgreess = false; |
| 94 | | | |
| 95 | | | private static final long VALIDITY_TIME = 10 * 60 * 1000; |
| 96 | | | |
| 97 | | | |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | private static final long MAX_IDLE_TIME = 7 * 24 * 60 * 60 * 1000; |
| 102 | | | |
| 103 | | | private static final int SOCKET_TIMEOUT = 1000; |
| 104 | | | |
| 105 | | | private static final long FAST_RESPONSE = 5; |
| 106 | | | |
| 107 | | | private static final int COLOR_OFFSET_RED = 13; |
| 108 | | | private static final int COLOR_OFFSET_GREEN = COLOR_OFFSET_RED + 1; |
| 109 | | | private static final int COLOR_OFFSET_BLUE = COLOR_OFFSET_GREEN + 1; |
| 110 | | | private static final int BG_OFFSET_RED = COLOR_OFFSET_BLUE + 1; |
| 111 | | | private static final int BG_OFFSET_GREEN = BG_OFFSET_RED + 1; |
| 112 | | | private static final int BG_OFFSET_BLUE = BG_OFFSET_GREEN + 1; |
| 113 | | | |
| 114 | 0 | | private static final byte [] IMAGE_DATA |
| 115 | | | = { |
| 116 | | | 'G', 'I', 'F', '8', '7', 'a', |
| 117 | | | 1, 0, |
| 118 | | | 1, 0, |
| 119 | | | (byte) 0x80, |
| 120 | | | 2, |
| 121 | | | 0, |
| 122 | | | 0, 0, 0, |
| 123 | | | 0, 0, 0, |
| 124 | | | 44, |
| 125 | | | 0, 0, |
| 126 | | | 0, 0, |
| 127 | | | 1, 0, |
| 128 | | | 1, 0, |
| 129 | | | 0, |
| 130 | | | 2, 2, 68, 01, 0, 59 |
| 131 | | | }; |
| 132 | | | |
| 133 | | | private static final int ECHO_PORT = 22; |
| 134 | | | |
| 135 | | | protected void doPost (HttpServletRequest req, HttpServletResponse rsp) |
| 136 | | | throws IOException |
| 137 | | | { |
| 138 | 0 | | doGet(req, rsp); |
| 139 | 0 | | } |
| 140 | | | |
| 141 | | | protected void doGet (HttpServletRequest req, HttpServletResponse rsp) |
| 142 | | | throws IOException |
| 143 | | | { |
| 144 | 0 | | String hostname = req.getParameter("host"); |
| 145 | | | |
| 146 | 0 | | final String clear = req.getParameter("clear"); |
| 147 | 0 | | if (clear != null && clear.length() > 0) |
| 148 | | | { |
| 149 | 0 | | PING_CACHE.clear(); |
| 150 | | | } |
| 151 | | | |
| 152 | 0 | | if (hostname == null || hostname.length() == 0) |
| 153 | | | { |
| 154 | 0 | | hostname = req.getPathInfo(); |
| 155 | 0 | | if (hostname != null && hostname.charAt(0) == '/') |
| 156 | | | { |
| 157 | 0 | | hostname = hostname.substring(1); |
| 158 | | | } |
| 159 | | | } |
| 160 | | | |
| 161 | 0 | | if (hostname == null || hostname.length() == 0) |
| 162 | | | { |
| 163 | 0 | | dumpCacheInfo(rsp); |
| 164 | | | } |
| 165 | | | else |
| 166 | | | { |
| 167 | 0 | | sendAnswer(check(hostname), rsp); |
| 168 | | | } |
| 169 | | | |
| 170 | 0 | | updateCache(); |
| 171 | 0 | | } |
| 172 | | | |
| 173 | | | private void dumpCacheInfo (HttpServletResponse rsp) |
| 174 | | | throws IOException |
| 175 | | | { |
| 176 | 0 | | rsp.setContentType("text/plain"); |
| 177 | | | |
| 178 | 0 | | final StringWriter sw = new StringWriter(); |
| 179 | 0 | | final PrintWriter data = new PrintWriter(sw); |
| 180 | 0 | | synchronized (PING_CACHE) |
| 181 | | | { |
| 182 | 0 | | data.println("Last Update: " + dateToString(sLastCacheUpdate)); |
| 183 | 0 | | data.println("Update in progress: " + sUpdateInProgreess); |
| 184 | 0 | | data.println("Number of stored results: " + PING_CACHE.size()); |
| 185 | 0 | | data.println("Cached Data:"); |
| 186 | 0 | | data.println("---"); |
| 187 | 0 | | } |
| 188 | 0 | | final PrintWriter out = rsp.getWriter(); |
| 189 | 0 | | out.print(sw.getBuffer().toString()); |
| 190 | | | |
| 191 | | | try |
| 192 | | | { |
| 193 | 0 | | final Iterator i = PING_CACHE.values().iterator(); |
| 194 | | | |
| 195 | 0 | | while (i.hasNext()) |
| 196 | | | { |
| 197 | 0 | | final PingResult result = (PingResult) i.next(); |
| 198 | 0 | | out.println(result); |
| 199 | 0 | | } |
| 200 | 0 | | out.println("---"); |
| 201 | | | } |
| 202 | 0 | | catch (ConcurrentModificationException ex) |
| 203 | | | { |
| 204 | 0 | | out.println("Uups... somebody updated the cache... try again!"); |
| 205 | 0 | | } |
| 206 | 0 | | } |
| 207 | | | |
| 208 | | | private void updateCache () |
| 209 | | | { |
| 210 | | | try |
| 211 | | | { |
| 212 | 0 | | final long now = System.currentTimeMillis(); |
| 213 | | | final long lastUpdate; |
| 214 | | | final boolean alreadyUpdating; |
| 215 | 0 | | synchronized (PING_CACHE) |
| 216 | | | { |
| 217 | 0 | | lastUpdate = sLastCacheUpdate; |
| 218 | 0 | | alreadyUpdating = sUpdateInProgreess; |
| 219 | 0 | | sUpdateInProgreess = true; |
| 220 | 0 | | } |
| 221 | | | |
| 222 | 0 | | if (lastUpdate + VALIDITY_TIME < now && !alreadyUpdating) |
| 223 | | | { |
| 224 | 0 | | final Iterator i = PING_CACHE.values().iterator(); |
| 225 | | | |
| 226 | 0 | | while (i.hasNext()) |
| 227 | | | { |
| 228 | 0 | | final PingResult result = (PingResult) i.next(); |
| 229 | 0 | | if (result.getLastUsed() < (now - MAX_IDLE_TIME)) |
| 230 | | | { |
| 231 | 0 | | i.remove(); |
| 232 | | | } |
| 233 | 0 | | else if (result.getExpires() < now) |
| 234 | | | { |
| 235 | 0 | | new Thread() |
| 236 | 0 | | { |
| 237 | | | public void run () |
| 238 | | | { |
| 239 | 0 | | check(result.getHostname()); |
| 240 | 0 | | }; |
| 241 | | | } .start(); |
| 242 | | | } |
| 243 | 0 | | } |
| 244 | | | |
| 245 | 0 | | synchronized (PING_CACHE) |
| 246 | | | { |
| 247 | 0 | | sLastCacheUpdate = now; |
| 248 | 0 | | } |
| 249 | | | } |
| 250 | | | } |
| 251 | 0 | | catch (ConcurrentModificationException ex) |
| 252 | | | { |
| 253 | | | |
| 254 | | | } |
| 255 | | | finally |
| 256 | | | { |
| 257 | 0 | | synchronized (PING_CACHE) |
| 258 | | | { |
| 259 | 0 | | sUpdateInProgreess = false; |
| 260 | 0 | | } |
| 261 | 0 | | } |
| 262 | 0 | | } |
| 263 | | | |
| 264 | | | |
| 265 | | | private void sendAnswer (PingResult result, HttpServletResponse rsp) |
| 266 | | | throws IOException |
| 267 | | | { |
| 268 | 0 | | rsp.setContentType("image/gif"); |
| 269 | 0 | | rsp.setContentLength(IMAGE_DATA.length); |
| 270 | 0 | | rsp.setHeader("Cache-Control", "public"); |
| 271 | | | |
| 272 | 0 | | final byte[] responseData = new byte[IMAGE_DATA.length]; |
| 273 | 0 | | System.arraycopy(IMAGE_DATA, 0, responseData, 0, IMAGE_DATA.length); |
| 274 | | | |
| 275 | 0 | | if (result != null) |
| 276 | | | { |
| 277 | 0 | | rsp.setDateHeader("Last-Modified", result.getLastModified()); |
| 278 | 0 | | rsp.setDateHeader("Expires", result.getExpires()); |
| 279 | 0 | | final Color color = resultToColor(result.getResult()); |
| 280 | | | |
| 281 | 0 | | responseData[COLOR_OFFSET_RED] = (byte) color.getRed(); |
| 282 | 0 | | responseData[COLOR_OFFSET_GREEN] = (byte) color.getGreen(); |
| 283 | 0 | | responseData[COLOR_OFFSET_BLUE] = (byte) color.getBlue(); |
| 284 | 0 | | responseData[BG_OFFSET_RED] = (byte) color.getRed(); |
| 285 | 0 | | responseData[BG_OFFSET_GREEN] = (byte) color.getGreen(); |
| 286 | 0 | | responseData[BG_OFFSET_BLUE] = (byte) color.getBlue(); |
| 287 | 0 | | rsp.setHeader("response-value", String.valueOf(result.getResult())); |
| 288 | 0 | | rsp.setHeader("response-host", result.getHostname()); |
| 289 | | | } |
| 290 | | | |
| 291 | 0 | | rsp.getOutputStream().write(responseData); |
| 292 | 0 | | } |
| 293 | | | |
| 294 | | | |
| 295 | | | private Color resultToColor (long result) |
| 296 | | | { |
| 297 | | | final Color responseColor; |
| 298 | 0 | | if (result == UNKNOWN_HOST) |
| 299 | | | { |
| 300 | 0 | | responseColor = Color.RED; |
| 301 | | | } |
| 302 | 0 | | else if (result == ILLEGAL_ARGUMENT) |
| 303 | | | { |
| 304 | 0 | | responseColor = Color.ORANGE; |
| 305 | | | } |
| 306 | 0 | | else if (result == HOST_REFUSED) |
| 307 | | | { |
| 308 | 0 | | responseColor = Color.YELLOW; |
| 309 | | | } |
| 310 | 0 | | else if (result < 0) |
| 311 | | | { |
| 312 | 0 | | responseColor = Color.BLACK; |
| 313 | | | } |
| 314 | 0 | | else if (result <= FAST_RESPONSE) |
| 315 | | | { |
| 316 | 0 | | responseColor = Color.GREEN.brighter(); |
| 317 | | | } |
| 318 | 0 | | else if (result <= (FAST_RESPONSE + FAST_RESPONSE)) |
| 319 | | | { |
| 320 | 0 | | responseColor = Color.GREEN; |
| 321 | | | } |
| 322 | 0 | | else if (result <= (FAST_RESPONSE + FAST_RESPONSE + FAST_RESPONSE)) |
| 323 | | | { |
| 324 | 0 | | responseColor = Color.GREEN.darker(); |
| 325 | | | } |
| 326 | | | else |
| 327 | | | { |
| 328 | 0 | | responseColor = Color.GREEN.darker().darker(); |
| 329 | | | } |
| 330 | 0 | | return responseColor; |
| 331 | | | } |
| 332 | | | |
| 333 | | | protected PingResult check (String host) |
| 334 | | | { |
| 335 | 0 | | final long now = System.currentTimeMillis(); |
| 336 | | | |
| 337 | 0 | | PingResult result = (PingResult) PING_CACHE.get(host); |
| 338 | 0 | | if (result == null) |
| 339 | | | { |
| 340 | 0 | | result = new PingResult(host, ping(host)); |
| 341 | 0 | | PING_CACHE.put(host, result); |
| 342 | | | } |
| 343 | | | else |
| 344 | | | { |
| 345 | | | |
| 346 | 0 | | synchronized (result) |
| 347 | | | { |
| 348 | 0 | | if (result.getExpires() < now) |
| 349 | | | { |
| 350 | 0 | | result.setResult(ping(host)); |
| 351 | | | } |
| 352 | 0 | | } |
| 353 | | | } |
| 354 | | | |
| 355 | | | |
| 356 | 0 | | if (PING_CACHE.size() > MAX_PING_CACHE_SIZE) |
| 357 | | | { |
| 358 | 0 | | PING_CACHE.clear(); |
| 359 | | | } |
| 360 | | | |
| 361 | 0 | | return result; |
| 362 | | | } |
| 363 | | | |
| 364 | | | |
| 365 | | | protected static long ping (String hostname) |
| 366 | | | { |
| 367 | | | final long result; |
| 368 | 0 | | if (hostname == null || hostname.length() == 0) |
| 369 | | | { |
| 370 | 0 | | result = ILLEGAL_ARGUMENT; |
| 371 | | | } |
| 372 | | | else |
| 373 | | | { |
| 374 | 0 | | InetAddress host = null; |
| 375 | | | try |
| 376 | | | { |
| 377 | 0 | | host = InetAddress.getByName(hostname); |
| 378 | | | } |
| 379 | 0 | | catch (UnknownHostException ex) |
| 380 | | | { |
| 381 | | | |
| 382 | 0 | | } |
| 383 | | | |
| 384 | 0 | | if (host == null) |
| 385 | | | { |
| 386 | 0 | | result = UNKNOWN_HOST; |
| 387 | | | } |
| 388 | | | else |
| 389 | | | { |
| 390 | 0 | | result = ping(host); |
| 391 | | | } |
| 392 | | | } |
| 393 | 0 | | return result; |
| 394 | | | } |
| 395 | | | |
| 396 | | | |
| 397 | | | protected static long ping (InetAddress host) |
| 398 | | | { |
| 399 | 0 | | final long timer = System.currentTimeMillis(); |
| 400 | 0 | | DataInputStream dis = null; |
| 401 | 0 | | Socket t = null; |
| 402 | 0 | | long result = -1; |
| 403 | | | try |
| 404 | | | { |
| 405 | 0 | | t = new Socket(host, ECHO_PORT); |
| 406 | 0 | | t.setTcpNoDelay(true); |
| 407 | 0 | | t.setSoTimeout(SOCKET_TIMEOUT); |
| 408 | 0 | | dis = new DataInputStream(t.getInputStream()); |
| 409 | 0 | | dis.readByte(); |
| 410 | 0 | | result = System.currentTimeMillis() - timer; |
| 411 | | | } |
| 412 | 0 | | catch (IOException e) |
| 413 | | | { |
| 414 | 0 | | final String message = e.getMessage(); |
| 415 | 0 | | if (message != null && message.indexOf("refused") != -1) |
| 416 | | | { |
| 417 | 0 | | result = HOST_REFUSED; |
| 418 | | | } |
| 419 | | | else |
| 420 | | | { |
| 421 | 0 | | result = HOST_NOT_REACHED; |
| 422 | | | } |
| 423 | | | } |
| 424 | | | finally |
| 425 | | | { |
| 426 | 0 | | IoUtil.close(dis); |
| 427 | 0 | | IoUtil.close(t); |
| 428 | 0 | | } |
| 429 | 0 | | return result; |
| 430 | | | } |
| 431 | | | |
| 432 | | | static String dateToString (long time) |
| 433 | | | { |
| 434 | 0 | | final DateFormat formater |
| 435 | | | = new SimpleDateFormat(DATE_TIME_FORMAT, Locale.US); |
| 436 | 0 | | formater.setTimeZone(TIME_ZONE); |
| 437 | 0 | | return formater.format(new Date(time)); |
| 438 | | | } |
| 439 | | | |
| 440 | 0 | | private static class PingResult |
| 441 | | | { |
| 442 | | | private final String mHostname; |
| 443 | | | private long mResult; |
| 444 | | | private long mExpires; |
| 445 | | | private long mLastModified; |
| 446 | | | private long mLastUsed; |
| 447 | | | |
| 448 | | | PingResult (String hostname, long result) |
| 449 | 0 | | { |
| 450 | 0 | | final long now = System.currentTimeMillis(); |
| 451 | 0 | | mExpires = now + VALIDITY_TIME; |
| 452 | 0 | | mLastModified = now; |
| 453 | 0 | | mHostname = hostname; |
| 454 | 0 | | mResult = result; |
| 455 | 0 | | mLastUsed = now; |
| 456 | 0 | | } |
| 457 | | | |
| 458 | | | {@inheritDoc} |
| 459 | | | public synchronized String toString () |
| 460 | | | { |
| 461 | 0 | (1) | final StringBuffer buffer = new StringBuffer(); |
| 462 | 0 | | buffer.append("[PingResult: "); |
| 463 | 0 | | buffer.append(mHostname); |
| 464 | 0 | | buffer.append(" result: "); |
| 465 | 0 | | buffer.append(mResult); |
| 466 | 0 | | buffer.append(" expires: "); |
| 467 | 0 | | buffer.append(dateToString(mExpires)); |
| 468 | 0 | | buffer.append(" lastModified: "); |
| 469 | 0 | | buffer.append(dateToString(mLastModified)); |
| 470 | 0 | | buffer.append(" lastUsed: "); |
| 471 | 0 | | buffer.append(dateToString(mLastUsed)); |
| 472 | 0 | | buffer.append(']'); |
| 473 | 0 | | return buffer.toString(); |
| 474 | | | } |
| 475 | | | |
| 476 | | | String getHostname () |
| 477 | | | { |
| 478 | 0 | | return mHostname; |
| 479 | | | } |
| 480 | | | |
| 481 | | | synchronized long getExpires () |
| 482 | | | { |
| 483 | 0 | | return mExpires; |
| 484 | | | } |
| 485 | | | |
| 486 | | | synchronized long getLastModified () |
| 487 | | | { |
| 488 | 0 | | return mLastModified; |
| 489 | | | } |
| 490 | | | |
| 491 | | | synchronized void setResult (long result) |
| 492 | | | { |
| 493 | 0 | | final long now = System.currentTimeMillis(); |
| 494 | 0 | | mExpires = now + VALIDITY_TIME; |
| 495 | 0 | | if (result != mResult) |
| 496 | | | { |
| 497 | 0 | | mLastModified = now; |
| 498 | 0 | | mResult = result; |
| 499 | | | } |
| 500 | 0 | | } |
| 501 | | | |
| 502 | | | synchronized long getResult () |
| 503 | | | { |
| 504 | 0 | | mLastUsed = System.currentTimeMillis(); |
| 505 | 0 | | return mResult; |
| 506 | | | } |
| 507 | | | |
| 508 | | | synchronized long getLastUsed () |
| 509 | | | { |
| 510 | 0 | | return mLastUsed; |
| 511 | | | } |
| 512 | | | } |
| 513 | | | } |