|
What this is
Other links
The source codepackage com.ice.syslogd; import com.ice.syslog.SyslogDefs; import java.lang.*; import java.io.*; import java.net.*; import java.util.*; public class SyslogServer extends Thread { public static final String RCS_ID = "$Id: SyslogServer.java,v 1.1.1.1 1998/02/22 05:47:54 time Exp $"; public static final String RCS_REV = "$Revision: 1.1.1.1 $"; public static final String RCS_NAME = "$Name: $"; private static final int IN_BUF_SZ = (8 * 1024); private static final int SYSLOG_PORT = 514; private boolean socketOpen; private int port; private DatagramSocket inSock; private DatagramPacket inGram; private byte[] inBuffer; private boolean debugPacketsReceived; private boolean debugMessagesReceived; private SyslogConfig configuration; private ConfigEntryVector configEntries; private FeedbackDisplayer feedback; public SyslogServer() { this( SyslogDefs.DEFAULT_PORT ); } public SyslogServer( int port ) { super(); this.port = port; this.socketOpen = false; this.inBuffer = null; this.inGram = null; this.debugPacketsReceived = false; this.debugMessagesReceived = false; this.feedback = null; this.configuration = null; this.configEntries = null; } public SyslogConfig getConfiguration() { return this.configuration; } public void setConfiguration( SyslogConfig configuration ) { this.configuration = configuration; this.configEntries = configuration.getConfigEntries(); } public void startupServices() { try { this.inSock = new DatagramSocket( this.port ); this.inBuffer = new byte[ SyslogServer.IN_BUF_SZ ]; this.socketOpen = true; } catch ( SocketException ex ) { System.err.println ( "FATAL could not create input socket on port '" + this.port + "'\n\t" + ex.getMessage() ); this.stop(); } } public void shutdownServices() { if ( this.socketOpen ) { this.inSock.close(); this.socketOpen = false; } } public void finalize() { this.shutdownServices(); this.closeAllActions(); } public void openAllActions() { for ( int eIdx = 0 ; eIdx < this.configEntries.size() ; ++eIdx ) { ConfigEntry entry = this.configEntries.entryAt( eIdx ); if ( entry != null ) { entry.openAction(); } } } public void closeAllActions() { for ( int eIdx = 0 ; eIdx < this.configEntries.size() ; ++eIdx ) { ConfigEntry entry = this.configEntries.entryAt( eIdx ); if ( entry != null ) { entry.closeAction(); } } } public void registerActionDisplay( String name, SyslogDisplayInterface display ) { for ( int eIdx = 0 ; eIdx < this.configEntries.size() ; ++eIdx ) { ConfigEntry entry = this.configEntries.entryAt( eIdx ); if ( entry != null ) { entry.registerActionDisplay( name, display ); } } } public void setFeedbackDisplayer( FeedbackDisplayer feedback ) { this.feedback = feedback; } public void displayFeedback( String message ) { if ( this.feedback != null ) { this.feedback.displayFeedback( message ); } } public void run() { int packetCount = 0; this.displayFeedback ( "Establishing communications..." ); this.startupServices(); this.displayFeedback ( "Opening all configuration entries..." ); this.openAllActions(); this.displayFeedback ( "Listening for incoming packets..." ); for ( ; ; ) { try { this.inGram = new DatagramPacket ( this.inBuffer, this.inBuffer.length ); this.inSock.receive( this.inGram ); } catch ( IOException ex ) { System.err.println ( "ERROR reading input socket:\n\t" + ex.getMessage() ); break; } String msgBuf = new String( this.inGram.getData(), 0, this.inGram.getLength() ); if ( this.debugPacketsReceived ) System.err.println ( "[" + this.inGram.getLength() + "] {" + msgBuf + "}" ); ++packetCount; this.displayFeedback ( "Packets received: " + packetCount + "." ); String hostName = this.inGram.getAddress().getHostName(); if ( hostName == null ) hostName = "localhost"; this.processMessage( msgBuf, hostName ); } this.displayFeedback ( "Closing all configuration entries..." ); this.closeAllActions(); this.displayFeedback ( "Shutting down communications..." ); this.shutdownServices(); } public void processMessage( String message, String hostName ) { int lbIdx = message.indexOf( '<' ); int rbIdx = message.indexOf( '>' ); if ( lbIdx < 0 || rbIdx < 0 || lbIdx >= (rbIdx - 1) ) { System.err.println ( "BAD MSG {" + message + "}" ); return; } int priCode = 0; String priStr = message.substring( lbIdx + 1, rbIdx ); try { priCode = Integer.parseInt( priStr ); } catch ( NumberFormatException ex ) { System.err.println ( "ERROR Bad priority code '" + priStr + "'" ); return; } int facility = SyslogDefs.extractFacility( priCode ); int priority = SyslogDefs.extractPriority( priCode ); message = message.substring ( rbIdx + 1, (message.length() - 1) ); // // Check to see if msg looks non-standard. // In this case, it means that there is not a standard // date in the front of the message text. // boolean stdMsg = true; if ( message.length() < 16 ) { stdMsg = false; } else if ( message.charAt(3) != ' ' || message.charAt(6) != ' ' || message.charAt(9) != ':' || message.charAt(12) != ':' || message.charAt(15) != ' ' ) { stdMsg = false; } String timestamp; if ( ! stdMsg ) { try { timestamp = TimestampFormat.getInstance().format ( new Date() ); } catch ( IllegalArgumentException ex ) { System.err.println( "ERROR INTERNAL DATE ERROR!" ); timestamp = ""; } } else { timestamp = message.substring( 0, 15 ); message = message.substring( 16 ); } lbIdx = message.indexOf( '[' ); rbIdx = message.indexOf( ']' ); int colonIdx = message.indexOf( ':' ); int spaceIdx = message.indexOf( ' ' ); int processId = 0; String processName = ""; String processIdStr = ""; if ( lbIdx < (rbIdx - 1) && colonIdx == (rbIdx + 1) && spaceIdx == (colonIdx + 1) ) { processName = message.substring( 0, lbIdx ); processIdStr = message.substring( lbIdx + 1, rbIdx ); message = message.substring( colonIdx + 2 ); try { processId = Integer.parseInt( processIdStr ); } catch ( NumberFormatException ex ) { System.err.println ( "ERROR Bad process id '" + processIdStr + "'" ); processId = 0; } } else if ( lbIdx < 0 && rbIdx < 0 && colonIdx > 0 && spaceIdx == (colonIdx + 1) ) { processName = message.substring( 0, colonIdx ); message = message.substring( colonIdx + 2 ); } if ( this.debugMessagesReceived ) System.err.println ( "[" + facility + ":" + SyslogDefs.getFacilityName(facility) + "] [" + priority + ":" + SyslogDefs.getPriorityName(priority) + "] '" + processName + "' '" + processId + "' " + timestamp + " " + message ); SyslogMessage logMessage = new SyslogMessage( facility, priority, timestamp, hostName, processName, processId, message ); for ( int eIdx = 0 ; eIdx < this.configEntries.size() ; ++eIdx ) { ConfigEntry entry = this.configEntries.entryAt( eIdx ); entry.processMessage( logMessage ); } } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.