001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.net.server.impl;
016:
017: import org.quickserver.net.server.*;
018: import java.lang.reflect.*;
019: import java.net.*;
020: import java.io.*;
021: import java.util.logging.*;
022:
023: /**
024: * Default ClientEventHandler implementation.
025: * <p>This implementation will try to provide a default ClientEventHandler
026: * implementation. If a ClientCommandHandler is known to have been set then
027: * this implementation will look for ClientEventHandler methods in that
028: * implementation and pass the corresponding call to that method.
029: * This was done to provide backward compatibility with v1.4.5 and prior version
030: * of ClientCommandHandler.</p>
031: * @author Akshathkumar Shetty
032: * @since 1.4.6
033: */
034: public class DefaultClientEventHandler implements ClientEventHandler {
035: private static Logger logger = Logger
036: .getLogger(DefaultClientEventHandler.class.getName());
037:
038: private ClientCommandHandler clientCommandHandler = null;
039: private Method gotConnectedMethod = null;
040: private Method lostConnectionMethod = null;
041: private Method closingConnectionMethod = null;
042:
043: /**
044: * Sets ClientCommandHandler that should be examined to
045: * find any ClientEventHandler methods
046: */
047: public void setClientCommandHandler(ClientCommandHandler handler) {
048: this .clientCommandHandler = handler;
049: if (clientCommandHandler != null)
050: loadMethods();
051: }
052:
053: public void gotConnected(ClientHandler handler)
054: throws SocketTimeoutException, IOException {
055: if (gotConnectedMethod == null)
056: handler.sendSystemMsg("Connection opened: "
057: + handler.getHostAddress());
058: else
059: invoke(gotConnectedMethod, handler);
060: }
061:
062: public void lostConnection(ClientHandler handler)
063: throws IOException {
064: if (lostConnectionMethod == null)
065: handler.sendSystemMsg("Connection lost: "
066: + handler.getHostAddress());
067: else
068: invoke(lostConnectionMethod, handler);
069: }
070:
071: public void closingConnection(ClientHandler handler)
072: throws IOException {
073: if (closingConnectionMethod == null)
074: handler.sendSystemMsg("Connection closing: "
075: + handler.getHostAddress());
076: else
077: invoke(closingConnectionMethod, handler);
078: }
079:
080: private void loadMethods() {
081: Class cls = clientCommandHandler.getClass();
082: try {
083: gotConnectedMethod = cls.getMethod("gotConnected",
084: new Class[] { ClientHandler.class });
085: } catch (NoSuchMethodException ex) {
086: logger.fine("Error finding gotConnected : " + ex);
087: }
088: try {
089: lostConnectionMethod = cls.getMethod("lostConnection",
090: new Class[] { ClientHandler.class });
091: } catch (NoSuchMethodException ex) {
092: logger.fine("Error finding lostConnection : " + ex);
093: }
094: try {
095: closingConnectionMethod = cls.getMethod(
096: "closingConnection",
097: new Class[] { ClientHandler.class });
098: } catch (NoSuchMethodException ex) {
099: logger.fine("Error finding lostConnection : " + ex);
100: }
101: }
102:
103: private void invoke(Method method, ClientHandler handler)
104: throws SocketTimeoutException, IOException {
105: try {
106: method.invoke(clientCommandHandler,
107: new Object[] { handler });
108: } catch (IllegalAccessException e) {
109: logger.warning("Error invoking " + method + " : " + e);
110: } catch (InvocationTargetException e) {
111: Exception cause = (Exception) e.getCause();
112: if (cause != null) {
113: if (SocketTimeoutException.class.isInstance(cause))
114: throw (SocketTimeoutException) cause;
115: else if (IOException.class.isInstance(cause))
116: throw (IOException) cause;
117: }
118: logger.warning("Error invoking " + method + " : " + e
119: + "\n Cause: " + cause);
120: IOException ioe = new IOException();
121: ioe.initCause(cause);
122: throw ioe;
123: }
124: }
125: }
|