Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.graylog2.syslog4j.server.impl;
  
  import java.net.Socket;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
 import java.util.Map;
 
AbstractSyslogServer provides a base abstract implementation of the SyslogServerIF.

Syslog4j is licensed under the Lesser GNU Public License v2.1. A copy of the LGPL license is available in the META-INF folder in all distributions of Syslog4j and in the base directory of the "doc" ZIP.

Author(s):
<syslog4j@productivity.org>
Version:
$Id: AbstractSyslogServer.java,v 1.12 2011/01/11 05:11:13 cvs Exp $
 
 public abstract class AbstractSyslogServer implements SyslogServerIF {
     public static class Sessions extends HashMap {
         private static final long serialVersionUID = -4438949276263772580L;
 
         public static final Object syncObject = new Object();
 
         public void addSocket(Socket socket) {
             synchronized () {
                 put(socketnew HashMap());
             }
         }
 
         public Iterator getSockets() {
             if (size() > 0) {
                 return keySet().iterator();
 
             } else {
                 return null;
             }
         }
 
         public void addSession(Socket socketSyslogServerEventHandlerIF eventHandlerObject session) {
             synchronized () {
                 Map handlerMap = getHandlerMap(socket);
 
                 if (handlerMap == null) {
                     handlerMap = new HashMap();
                 }
 
                 handlerMap.put(eventHandlersession);
             }
         }
 
         public void removeSocket(Socket socket) {
             synchronized () {
                 Map handlerMap = getHandlerMap(socket);
 
                 if (handlerMap != null) {
                     handlerMap.clear();
                 }
             }
         }
 
         protected Map getHandlerMap(Socket socket) {
             Map handlerMap = null;
 
             if (containsKey(socket)) {
                 handlerMap = (Mapget(socket);
             }
 
             return handlerMap;
         }
 
         public Object getSession(Socket socketSyslogServerEventHandlerIF eventHandler) {
             synchronized () {
                 Map handlerMap = getHandlerMap(socket);
 
                 Object session = handlerMap.get(eventHandler);
 
                 return session;
             }
         }
     }
 
     protected String syslogProtocol = null;
     protected AbstractSyslogServerConfig syslogServerConfig = null;
    protected Thread thread = null;
    protected boolean shutdown = false;
    public void initialize(String protocolSyslogServerConfigIF configthrows SyslogRuntimeException {
        this. = protocol;
        try {
            this. = (AbstractSyslogServerConfigconfig;
        } catch (ClassCastException cce) {
            throw new SyslogRuntimeException(cce);
        }
        initialize();
    }
    public String getProtocol() {
        return this.;
    }
    public SyslogServerConfigIF getConfig() {
        return this.;
    }
    protected abstract void initialize() throws SyslogRuntimeException;
    public void shutdown() throws SyslogRuntimeException {
        this. = true;
    }
    public Thread getThread() {
        return this.;
    }
    public void setThread(Thread thread) {
        this. = thread;
    }
    protected static boolean isStructuredMessage(SyslogCharSetIF syslogCharSetbyte[] receiveData) {
        String receiveDataString = SyslogUtility.newString(syslogCharSetreceiveData);
        boolean isStructuredMessage = isStructuredMessage(syslogCharSetreceiveDataString);
        return isStructuredMessage;
    }
    protected static boolean isStructuredMessage(SyslogCharSetIF syslogCharSetString receiveData) {
        int idx = receiveData.indexOf('>');
        if (idx != -1) {
            // If there's a numerical VERSION field after the <priority>, return true.
            if (receiveData.length() > idx + 1 && Character.isDigit(receiveData.charAt(idx + 1))) {
                return true;
            }
        }
        return false;
    }
    protected static SyslogServerEventIF createEvent(SyslogServerConfigIF serverConfigbyte[] lineBytesint lineBytesLengthInetAddress inetAddr) {
        SyslogServerEventIF event = null;
        if (serverConfig.isUseStructuredData() && AbstractSyslogServer.isStructuredMessage(serverConfiglineBytes)) {
            event = new StructuredSyslogServerEvent(lineByteslineBytesLengthinetAddr);
            if (serverConfig.getDateTimeFormatter() != null) {
                ((StructuredSyslogServerEventevent).setDateTimeFormatter(serverConfig.getDateTimeFormatter());
            }
        } else {
            event = new SyslogServerEvent(lineByteslineBytesLengthinetAddr);
        }
        return event;
    }
    protected static SyslogServerEventIF createEvent(SyslogServerConfigIF serverConfigString lineInetAddress inetAddr) {
        SyslogServerEventIF event = null;
        if (serverConfig.isUseStructuredData() && AbstractSyslogServer.isStructuredMessage(serverConfigline)) {
            event = new StructuredSyslogServerEvent(lineinetAddr);
        } else {
            event = new SyslogServerEvent(lineinetAddr);
        }
        return event;
    }
    public static void handleInitialize(SyslogServerIF syslogServer) {
        List eventHandlers = syslogServer.getConfig().getEventHandlers();
        for (int i = 0; i < eventHandlers.size(); i++) {
            SyslogServerEventHandlerIF eventHandler = (SyslogServerEventHandlerIFeventHandlers.get(i);
            try {
                eventHandler.initialize(syslogServer);
            } catch (Exception exception) {
                //
            }
        }
    }
    public static void handleDestroy(SyslogServerIF syslogServer) {
        List eventHandlers = syslogServer.getConfig().getEventHandlers();
        for (int i = 0; i < eventHandlers.size(); i++) {
            SyslogServerEventHandlerIF eventHandler = (SyslogServerEventHandlerIFeventHandlers.get(i);
            try {
                eventHandler.destroy(syslogServer);
            } catch (Exception exception) {
                //
            }
        }
    }
    public static void handleSessionOpen(Sessions sessionsSyslogServerIF syslogServerSocket socket) {
        List eventHandlers = syslogServer.getConfig().getEventHandlers();
        for (int i = 0; i < eventHandlers.size(); i++) {
            SyslogServerEventHandlerIF eventHandler = (SyslogServerEventHandlerIFeventHandlers.get(i);
            if (eventHandler instanceof SyslogServerSessionEventHandlerIF) {
                try {
                    Object session = ((SyslogServerSessionEventHandlerIFeventHandler).sessionOpened(syslogServersocket.getRemoteSocketAddress());
                    if (session != null) {
                        sessions.addSession(socketeventHandlersession);
                    }
                } catch (Exception exception) {
                    try {
                        ((SyslogServerSessionEventHandlerIFeventHandler).exception(nullsyslogServersocket.getRemoteSocketAddress(), exception);
                    } catch (Exception e) {
                        //
                    }
                }
            }
        }
    }
    public static void handleSessionClosed(Sessions sessionsSyslogServerIF syslogServerSocket socketboolean timeout) {
        List eventHandlers = syslogServer.getConfig().getEventHandlers();
        for (int i = 0; i < eventHandlers.size(); i++) {
            SyslogServerEventHandlerIF eventHandler = (SyslogServerEventHandlerIFeventHandlers.get(i);
            if (eventHandler instanceof SyslogServerSessionEventHandlerIF) {
                Object session = sessions.getSession(socketeventHandler);
                try {
                    ((SyslogServerSessionEventHandlerIFeventHandler).sessionClosed(sessionsyslogServersocket.getRemoteSocketAddress(), timeout);
                } catch (Exception exception) {
                    try {
                        ((SyslogServerSessionEventHandlerIFeventHandler).exception(sessionsyslogServersocket.getRemoteSocketAddress(), exception);
                    } catch (Exception e) {
                        //
                    }
                }
            }
        }
    }
    public static void handleEvent(Sessions sessionsSyslogServerIF syslogServerDatagramPacket packetSyslogServerEventIF event) {
        handleEvent(sessionssyslogServernullpacket.getSocketAddress(), event);
    }
    public static void handleEvent(Sessions sessionsSyslogServerIF syslogServerSocket socketSyslogServerEventIF event) {
        handleEvent(sessionssyslogServersocketsocket.getRemoteSocketAddress(), event);
    }
    protected static void handleEvent(Sessions sessionsSyslogServerIF syslogServerSocket socketSocketAddress socketAddressSyslogServerEventIF event) {
        List eventHandlers = syslogServer.getConfig().getEventHandlers();
        for (int i = 0; i < eventHandlers.size(); i++) {
            SyslogServerEventHandlerIF eventHandler = (SyslogServerEventHandlerIFeventHandlers.get(i);
            Object session = (sessions != null && socket != null) ? sessions.getSession(socketeventHandler) : null;
            if (eventHandler instanceof SyslogServerSessionEventHandlerIF) {
                try {
                    ((SyslogServerSessionEventHandlerIFeventHandler).event(sessionsyslogServersocketAddressevent);
                } catch (Exception exception) {
                    try {
                        ((SyslogServerSessionEventHandlerIFeventHandler).exception(sessionsyslogServersocketAddressexception);
                    } catch (Exception e) {
                        //
                    }
                }
            } else if (eventHandler instanceof SyslogServerSessionlessEventHandlerIF) {
                try {
                    ((SyslogServerSessionlessEventHandlerIFeventHandler).event(syslogServersocketAddressevent);
                } catch (Exception exception) {
                    try {
                        ((SyslogServerSessionlessEventHandlerIFeventHandler).exception(syslogServersocketAddressexception);
                    } catch (Exception e) {
                        //
                    }
                }
            }
        }
    }
    public static void handleException(Object sessionSyslogServerIF syslogServerSocketAddress socketAddressException exception) {
        List eventHandlers = syslogServer.getConfig().getEventHandlers();
        for (int i = 0; i < eventHandlers.size(); i++) {
            SyslogServerEventHandlerIF eventHandler = (SyslogServerEventHandlerIFeventHandlers.get(i);
            if (eventHandler instanceof SyslogServerSessionEventHandlerIF) {
                try {
                    ((SyslogServerSessionEventHandlerIFeventHandler).exception(sessionsyslogServersocketAddressexception);
                } catch (Exception e) {
                    //
                }
            } else if (eventHandler instanceof SyslogServerSessionlessEventHandlerIF) {
                try {
                    ((SyslogServerSessionlessEventHandlerIFeventHandler).exception(syslogServersocketAddressexception);
                } catch (Exception e) {
                    //
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X