org.productivity.java.syslog4j.impl.net.udp.UDPNetSyslogConfig Java Examples

The following examples show how to use org.productivity.java.syslog4j.impl.net.udp.UDPNetSyslogConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractLog4jSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void setUp() {
	UDPNetSyslogConfig config = new UDPNetSyslogConfig();
	
	assertTrue(config.isCacheHostAddress());
	config.setCacheHostAddress(false);
	assertFalse(config.isCacheHostAddress());
	
	assertTrue(config.isThrowExceptionOnInitialize());
	config.setThrowExceptionOnInitialize(false);
	assertFalse(config.isThrowExceptionOnInitialize());
	
	assertFalse(config.isThrowExceptionOnWrite());
	config.setThrowExceptionOnWrite(true);
	assertTrue(config.isThrowExceptionOnWrite());
	
	Syslog.createInstance("log4jUdp",config);
	
	String protocol = getServerProtocol();
	
	startServerThread(protocol);
	SyslogUtility.sleep(100);
}
 
Example #2
Source File: SyslogParameterTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testStructuredSyslogMessage() {
	SyslogConfigIF config = new UDPNetSyslogConfig();
	config.setUseStructuredData(true);
	assertTrue(config.isUseStructuredData());
	config.setUseStructuredData(false);
	assertFalse(config.isUseStructuredData());
	
	SyslogIF syslog = Syslog.createInstance("testStructuredSyslog",config);
	
	SyslogMessageProcessorIF messageProcessor = new StructuredSyslogMessageProcessor();
	syslog.setStructuredMessageProcessor(messageProcessor);
	assertEquals(messageProcessor,syslog.getStructuredMessageProcessor());
	
	SyslogMessageIF m1 = new StructuredSyslogMessage("test1",new HashMap(),"test2");
	SyslogMessageIF m2 = new StructuredSyslogMessage("test1",new HashMap(),"test2");
	SyslogMessageIF m3 = new StructuredSyslogMessage("test3",new HashMap(),"test2");
	SyslogMessageIF m4 = new StructuredSyslogMessage("test1",new HashMap(),"test4");
	
	assertFalse(m1.equals(null));
	assertFalse(m1.equals("Wrong Class"));
	assertTrue(m1.equals(m1));
	assertTrue(m2.equals(m2));
	
	assertFalse(m1.equals(m3));
	assertFalse(m1.equals(m4));
}
 
Example #3
Source File: SyslogCreateAndDestroyTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testCreateAndDestroyByProtocol() {
	UDPNetSyslogConfig config = new UDPNetSyslogConfig();
	config.setPort(999);
	
	SyslogIF syslog = Syslog.createInstance("udpToDestroy",config);

	assertEquals(SyslogMessageProcessor.getDefault(),syslog.getMessageProcessor());		
	SyslogMessageProcessorIF messageProcessor = new FakeMessageProcessor();
	syslog.setMessageProcessor(messageProcessor);
	assertEquals(messageProcessor,syslog.getMessageProcessor());	
	
	SyslogIF matchSyslog = Syslog.getInstance("udpToDestroy");
	
	assertEquals(syslog,matchSyslog);
	
	Syslog.destroyInstance("");
	Syslog.destroyInstance("udpToDestroy");
	
	try {
		Syslog.getInstance("udpToDestroy");
		fail("udpToDestroy should not exist");
		
	} catch (SyslogRuntimeException sre) {
		//
	}
}
 
Example #4
Source File: Syslog.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * initialize() sets up the default TCP and UDP Syslog protocols, as
 * well as UNIX_SYSLOG and UNIX_SOCKET (if running on a Unix-based system).
 */
public synchronized static final void initialize() {
	createInstance(UDP,new UDPNetSyslogConfig());
	createInstance(TCP,new TCPNetSyslogConfig());
	
	if (OSDetectUtility.isUnix()
		&& SyslogUtility.isClassExists(JNA_NATIVE_CLASS)
		&& !Boolean.getBoolean(DISABLE_UNIX_PROPERTY)) {
		createInstance(UNIX_SYSLOG,new UnixSyslogConfig());
		createInstance(UNIX_SOCKET,new UnixSocketSyslogConfig());
	}
}
 
Example #5
Source File: SyslogMessage.java    From hop with Apache License 2.0 4 votes vote down vote up
public boolean init() {

    if ( super.init() ) {
      String servername = environmentSubstitute( meta.getServerName() );

      // Check target server
      if ( Utils.isEmpty( servername ) ) {
        logError( BaseMessages.getString( PKG, "SyslogMessage.MissingServerName" ) );
      }

      // Check if message field is provided
      if ( Utils.isEmpty( meta.getMessageFieldName() ) ) {
        logError( BaseMessages.getString( PKG, "SyslogMessage.Error.MessageFieldMissing" ) );
        return false;
      }

      int nrPort = Const.toInt( environmentSubstitute( meta.getPort() ), SyslogDefs.DEFAULT_PORT );

      if ( meta.isAddTimestamp() ) {
        // add timestamp to message
        data.datePattern = environmentSubstitute( meta.getDatePattern() );
        if ( Utils.isEmpty( data.datePattern ) ) {
          logError( BaseMessages.getString( PKG, "SyslogMessage.DatePatternEmpty" ) );
          return false;
        }
      }

      try {
        // Connect to syslog ...
        data.syslog = getSyslog();
        data.syslog.initialize( SyslogConstants.UDP, new UDPNetSyslogConfig() );
        data.syslog.getConfig().setHost( servername );
        data.syslog.getConfig().setPort( nrPort );
        data.syslog.getConfig().setFacility( meta.getFacility() );
        data.syslog.getConfig().setSendLocalName( false );
        data.syslog.getConfig().setSendLocalTimestamp( false );
      } catch ( Exception ex ) {
        logError( BaseMessages.getString( PKG, "SyslogMessage.UnknownHost", servername, ex.getMessage() ) );
        logError( Const.getStackTracker( ex ) );
        return false;
      }
      return true;
    }
    return false;
  }
 
Example #6
Source File: SyslogMessage.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
  meta = (SyslogMessageMeta) smi;
  data = (SyslogMessageData) sdi;

  if ( super.init( smi, sdi ) ) {
    String servername = environmentSubstitute( meta.getServerName() );

    // Check target server
    if ( Utils.isEmpty( servername ) ) {
      logError( BaseMessages.getString( PKG, "SyslogMessage.MissingServerName" ) );
    }

    // Check if message field is provided
    if ( Utils.isEmpty( meta.getMessageFieldName() ) ) {
      logError( BaseMessages.getString( PKG, "SyslogMessage.Error.MessageFieldMissing" ) );
      return false;
    }

    int nrPort = Const.toInt( environmentSubstitute( meta.getPort() ), SyslogDefs.DEFAULT_PORT );

    if ( meta.isAddTimestamp() ) {
      // add timestamp to message
      data.datePattern = environmentSubstitute( meta.getDatePattern() );
      if ( Utils.isEmpty( data.datePattern ) ) {
        logError( BaseMessages.getString( PKG, "SyslogMessage.DatePatternEmpty" ) );
        return false;
      }
    }

    try {
      // Connect to syslog ...
      data.syslog = getSyslog();
      data.syslog.initialize( SyslogConstants.UDP, new UDPNetSyslogConfig() );
      data.syslog.getConfig().setHost( servername );
      data.syslog.getConfig().setPort( nrPort );
      data.syslog.getConfig().setFacility( meta.getFacility() );
      data.syslog.getConfig().setSendLocalName( false );
      data.syslog.getConfig().setSendLocalTimestamp( false );
    } catch ( Exception ex ) {
      logError( BaseMessages.getString( PKG, "SyslogMessage.UnknownHost", servername, ex.getMessage() ) );
      logError( Const.getStackTracker( ex ) );
      return false;
    }
    return true;
  }
  return false;
}
 
Example #7
Source File: SyslogServerSessionTest.java    From syslog4j with GNU Lesser General Public License v2.1 2 votes vote down vote up
public void testUDPSession() {
	SyslogServerConfigIF serverConfig = new UDPNetSyslogServerConfig(9999);
	
	UDPSessionHandler handler1 = new UDPSessionHandler("a");
	serverConfig.addEventHandler(handler1);
	
	UDPSessionHandler handler2 = new UDPSessionHandler("b");
	serverConfig.addEventHandler(handler2);
	
	SyslogServer.createThreadedInstance("udp_session",serverConfig);
	
	SyslogUtility.sleep(100);
	
	assertTrue(handler1.initialized);
	assertTrue(handler2.initialized);

	SyslogConfigIF config = new UDPNetSyslogConfig();
	config.setPort(9999);
	
	SyslogIF syslog1 = Syslog.createInstance("udp_session_1",config);
	
	syslog1.info("1");
	syslog1.info("2");

	SyslogIF syslog2 = Syslog.createInstance("udp_session_2",config);
	
	syslog2.info("3");
	syslog2.info("4");

	SyslogUtility.sleep(100);
	
	syslog1.shutdown();
	syslog2.shutdown();
	SyslogServer.destroyInstance("udp_session");
	
	try {
		SyslogServer.getInstance("udp_session");
		fail();
		
	} catch (SyslogRuntimeException sre) {
		//
	}
	
	assertTrue(handler1.okay);
	assertTrue(handler2.okay);
	
	SyslogUtility.sleep(250);
	
	assertTrue(handler1.destroyed);
	assertTrue(handler2.destroyed);
}