org.productivity.java.syslog4j.server.SyslogServer Java Examples

The following examples show how to use org.productivity.java.syslog4j.server.SyslogServer. 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: SyslogHandlerTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void tearDown(final ManagementClient managementClient) throws Exception {
    // stop syslog server
    LOGGER.info("stopping syslog server");
    SyslogServer.shutdown();
    LOGGER.info("syslog server stopped");

    // remove syslog-profile
    final ModelNode op = Operations.createRemoveOperation(SYSLOG_PROFILE_ADDR);
    op.get(OPERATION_HEADERS, ROLLBACK_ON_RUNTIME_FAILURE).set(false);
    op.get(OPERATION_HEADERS, ALLOW_RESOURCE_SERVICE_RESTART).set(true);
    executeOperation(op);
    LOGGER.info("syslog server logging profile removed");

    super.tearDown(managementClient);
}
 
Example #2
Source File: AuditLogToSyslogSetup.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Stops syslog server and removes auditlog configuration.
 *
 */
@Override
public void tearDown(ManagementClient managementClient) throws Exception {
    // stop syslog server
    SyslogServer.shutdown();
    server.setThread(null);
    server.getConfig().removeAllEventHandlers();

    removeResource(managementClient, AUDIT_LOG_LOGGER_SYSLOG_HANDLER_ADDR);
    removeResource(managementClient, AUDIT_LOG_LOGGER_SYSLOG_HANDLER_ADDR2);
    removeResource(managementClient, AUDIT_SYSLOG_HANDLER_ADDR2);
    removeResource(managementClient, AUDIT_SYSLOG_HANDLER_ADDR);

    CoreUtils.applyUpdate(Util.getWriteAttributeOperation(AUDIT_LOG_LOGGER_ADDR, LOG_READ_ONLY, false),
            managementClient.getControllerClient());
}
 
Example #3
Source File: Syslog4jAppenderTest.java    From logback-syslog4j with MIT License 6 votes vote down vote up
protected void setUp() {
    serverStream = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(serverStream);

    final TCPNetSyslogServerConfig tcpNetSyslogServerConfig = new TCPNetSyslogServerConfig(45553);
    tcpNetSyslogServerConfig.addEventHandler(new PrintStreamSyslogServerEventHandler(ps));

    final UDPNetSyslogServerConfig udpNetSyslogServerConfig = new UDPNetSyslogServerConfig(45553);
    udpNetSyslogServerConfig.addEventHandler(new PrintStreamSyslogServerEventHandler(ps));

    final SSLTCPNetSyslogServerConfig ssltcpNetSyslogServerConfig = new SSLTCPNetSyslogServerConfig();
    ssltcpNetSyslogServerConfig.setPort(45554);
    ssltcpNetSyslogServerConfig.addEventHandler(new PrintStreamSyslogServerEventHandler(ps));
    ssltcpNetSyslogServerConfig.setKeyStore(this.getClass().getClassLoader().getResource("test-keystore.jks").getFile());
    ssltcpNetSyslogServerConfig.setKeyStorePassword("password");
    ssltcpNetSyslogServerConfig.setTrustStore(this.getClass().getClassLoader().getResource("test-keystore.jks").getFile());
    ssltcpNetSyslogServerConfig.setTrustStorePassword("password");

    SyslogServer.createThreadedInstance("testTcp", tcpNetSyslogServerConfig);
    SyslogServer.createThreadedInstance("testUdp", udpNetSyslogServerConfig);
    SyslogServer.createThreadedInstance("testTls", ssltcpNetSyslogServerConfig);
}
 
Example #4
Source File: AbstractNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void startServerThread(String protocol) {
	assertTrue(SyslogServer.exists(protocol));
	
	this.server = SyslogServer.getInstance(protocol);
	
	if (isSyslogServerTcpBacklog() && this.server.getConfig() instanceof TCPNetSyslogServerConfig) {
		((TCPNetSyslogServerConfig) this.server.getConfig()).setBacklog(0);
	}
	
	AbstractNetSyslogServerConfig config = (AbstractNetSyslogServerConfig) this.server.getConfig();
	config.setPort(TEST_PORT);
	config.addEventHandler(this.recorderEventHandler);

	if (this.server.getThread() == null) {
		Thread t = new Thread(this.server);
		t.setName("SyslogServer: " + protocol);
		t.start();
		
		this.server.setThread(t);
		
		assertEquals(t,this.server.getThread());
	}
}
 
Example #5
Source File: AbstractNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void tearDown() {
	System.out.print("Shutting down Syslog...");
	Syslog.shutdown();
	System.out.println("done.");

	SyslogUtility.sleep(100);
	
	System.out.print("Shutting down SyslogServer...");
	SyslogServer.shutdown();
	System.out.println("done.");
	
	SyslogUtility.sleep(100);

	Syslog.initialize();
	SyslogServer.initialize();
}
 
Example #6
Source File: FreshConnectionIntervalTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testFreshConnectionInterval() {
	TCPNetSyslogServerConfigIF serverConfig = new TCPNetSyslogServerConfig();
	serverConfig.setPort(8888);
	
	SocketCounter counter = new SocketCounter();
	serverConfig.addEventHandler(counter);
	
	SyslogServerIF server = SyslogServer.createThreadedInstance("tcp_8888",serverConfig);

	SyslogUtility.sleep(100);

	TCPNetSyslogConfigIF config = new TCPNetSyslogConfig();
	config.setPort(8888);
	config.setFreshConnectionInterval(300);
	
	SyslogIF syslog = Syslog.createInstance("tcp_8888",config);

	for(int i=0; i<10; i++) {
		syslog.info("message " + i);
		SyslogUtility.sleep(100);
	}
	
	SyslogUtility.sleep(100);
	
	SyslogServer.destroyInstance(server);
	Syslog.destroyInstance(syslog);

	SyslogUtility.sleep(100);

	assertEquals("OpenCounter",3,counter.openCounter);
	assertEquals("EventCounter",10,counter.eventCounter);
	assertEquals("CloseCounter",3,counter.closeCounter);
}
 
Example #7
Source File: AbstractSyslogReconnectionTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected static void stopSyslogServers() throws InterruptedException {
    SyslogServer.shutdown();
    if (udpServer != null) {
        udpServer.setThread(null);
        udpServer.getConfig().removeAllEventHandlers();
    }
    if (tcpServer != null) {
        tcpServer.setThread(null);
        tcpServer.getConfig().removeAllEventHandlers();
    }
    // wait for 1 second to stop syslog server instances properly
    Thread.sleep(1 * ADJUSTED_SECOND);
}
 
Example #8
Source File: AbstractSyslogReconnectionTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static SyslogServerIF createAndStartSyslogInstance(SyslogServerConfigIF config, String host, int port, String protocol) {
    config.setUseStructuredData(true);
    config.setHost(host);
    config.setPort(port);
    config.addEventHandler(new BlockedAllProtocolsSyslogServerEventHandler(protocol));
    SyslogServerIF syslogServer = SyslogServer.createInstance(protocol, config);
    SyslogServer.getThreadedInstance(protocol);
    return syslogServer;
}
 
Example #9
Source File: ReconnectSyslogServerTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Before
public void setupContainer() throws Exception {
    container.start();
    host = CoreUtils.stripSquareBrackets(TestSuiteEnvironment.getServerAddress());
    final JavaArchive deployment = createDeployment();
    deploy(deployment);
    SyslogServer.shutdown();
    BlockedAllProtocolsSyslogServerEventHandler.initializeForProtocol(SyslogConstants.UDP);
    BlockedAllProtocolsSyslogServerEventHandler.initializeForProtocol(SyslogConstants.TCP);
    startSyslogServers(host);
    setupServer();
}
 
Example #10
Source File: SyslogIsNotAvailableDuringServerBootTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Before
public void setupContainer() throws Exception {
    container.start();
    host = CoreUtils.stripSquareBrackets(TestSuiteEnvironment.getServerAddress());
    final JavaArchive deployment = createDeployment();
    deploy(deployment);
    SyslogServer.shutdown();
    BlockedAllProtocolsSyslogServerEventHandler.initializeForProtocol(SyslogConstants.UDP);
    BlockedAllProtocolsSyslogServerEventHandler.initializeForProtocol(SyslogConstants.TCP);
    setupServer();
    container.stop();
}
 
Example #11
Source File: AuditLogToSyslogSetup.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Starts Syslog server and configures syslog handler in the AS.
 *
 */
@Override
public void setup(ManagementClient managementClient) throws Exception {
    // clear created server instances (TCP/UDP)
    SyslogServer.shutdown();

    // start and set syslog server

    final String host = managementClient.getMgmtAddress();
    SyslogServerConfigIF config = getSyslogConfig();
    config.setUseStructuredData(true);
    config.setHost(host);
    config.setPort(SYSLOG_PORT);
    config.addEventHandler(new BlockedSyslogServerEventHandler());
    final String syslogProtocol = getSyslogProtocol();
    server = SyslogServer.createInstance(syslogProtocol, config);
    // start syslog server
    SyslogServer.getThreadedInstance(syslogProtocol);

    // Add the normal syslog handler
    addSyslogHandler(managementClient, AUDIT_SYSLOG_HANDLER_ADDR, host, null, null);

    // Add the syslog handler we will switch to
    addSyslogHandler(managementClient, AUDIT_SYSLOG_HANDLER_ADDR2, host, "TestApp", Facility.LINE_PRINTER);

    // Reference the first audit logger for now
    ModelNode op = Util.createAddOperation(AUDIT_LOG_LOGGER_SYSLOG_HANDLER_ADDR);
    CoreUtils.applyUpdate(op, managementClient.getControllerClient());

    op = Util.getWriteAttributeOperation(AUDIT_LOG_LOGGER_ADDR, LOG_READ_ONLY, false);
    CoreUtils.applyUpdate(op, managementClient.getControllerClient());

}
 
Example #12
Source File: PooledSSLTCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected String getServerProtocol() throws Exception {
	SSLTCPNetSyslogServerConfigIF config = new SSLTCPNetSyslogServerConfig();
	SSLConfigUtil.configure(config);
	SyslogServer.createThreadedInstance("pooledSslTcp", config);
	
	return "pooledSslTcp";
}
 
Example #13
Source File: UDPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void setUp() throws Exception {
	if (ONCE) {
		ONCE = false;
		
	} else {
		SyslogServer.getInstance(getServerProtocol()).getConfig().setHost("127.0.0.1");
	}

	super.setUp();	
}
 
Example #14
Source File: AbstractLog4jSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected void startServerThread(String protocol) {
	this.server = SyslogServer.getInstance(protocol);
	
	AbstractNetSyslogServerConfig config = (AbstractNetSyslogServerConfig) this.server.getConfig();
	config.setPort(TEST_PORT);
	config.addEventHandler(this.recorderEventHandler);

	this.server = SyslogServer.getThreadedInstance(protocol);
}
 
Example #15
Source File: SyslogCreateAndDestroyTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testCreateAndDestroyServerByProtocol() {
	UDPNetSyslogServerConfig config = new UDPNetSyslogServerConfig();
	config.setPort(999);
	
	SyslogServerIF server = SyslogServer.createInstance("udpToDestroy",config);
	
	SyslogServerIF matchServer = SyslogServer.getInstance("udpToDestroy");
	
	assertEquals(server,matchServer);
	
	SyslogServer.destroyInstance("");
	SyslogServer.destroyInstance("udpToDestroy");
	
	try {
		SyslogServer.getInstance("udpToDestroy");
		fail("udpToDestroy should not exist");
		
	} catch (SyslogRuntimeException sre) {
		//
	}
	
	assertFalse(SyslogServer.getSuppressRuntimeExceptions());
	SyslogServer.setSuppressRuntimeExceptions(true);
	assertTrue(SyslogServer.getSuppressRuntimeExceptions());

	assertNull(SyslogServer.getInstance("udpToDestroy"));

	SyslogServer.setSuppressRuntimeExceptions(false);
}
 
Example #16
Source File: NonDefinedSyslogInstanceTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testNonDefinedSyslogServerInstance() {
	try {
		SyslogServer.getInstance("not_defined");
		fail("not_defined shouldn't exist");
		
	} catch (SyslogRuntimeException sre) {
		assertTrue(sre.getMessage().startsWith("SyslogServer instance \"not_defined\" not defined; use \"tcp\" or \"udp\" or call SyslogServer.createInstance(protocol,config) first"));
	}
}
 
Example #17
Source File: TCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void setUp() throws Exception {
	if (ONCE) {
		ONCE = false;
		
	} else {
		SyslogServer.getInstance(getServerProtocol()).getConfig().setHost("127.0.0.1");
	}

	super.setUp();	
}
 
Example #18
Source File: MaxActiveSocketsTest.java    From syslog4j with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected int _testMaxActiveSockets(TCPNetSyslogServerConfigIF serverConfig) {
	Counter c = new Counter();
	
	serverConfig.setMaxActiveSockets(2);
	serverConfig.addEventHandler(c);
	
	SyslogServer.createThreadedInstance("tcp_maxactivesockets",serverConfig);
	
	TCPNetSyslogConfigIF config = new TCPNetSyslogConfig();
	config.setPort(8888);
		
	SyslogIF syslog1 = Syslog.createInstance("tcp_maxactivesockets1",config);
	syslog1.info("test1");
	syslog1.flush();
	SyslogUtility.sleep(200);
	
	SyslogIF syslog2 = Syslog.createInstance("tcp_maxactivesockets2",config);
	syslog2.info("test2");
	syslog2.flush();
	SyslogUtility.sleep(200);
	
	SyslogIF syslog3 = Syslog.createInstance("tcp_maxactivesockets3",config);
	syslog3.info("test3");
	syslog3.flush();
	SyslogUtility.sleep(200);

	syslog1.shutdown();
	SyslogUtility.sleep(200);

	Syslog.destroyInstance("tcp_maxactivesockets1");
	Syslog.destroyInstance("tcp_maxactivesockets2");
	Syslog.destroyInstance("tcp_maxactivesockets3");
	
	SyslogServer.destroyInstance("tcp_maxactivesockets");
	
	return c.counter;
}
 
Example #19
Source File: BackLogTCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testSendReceive() {
	Thread t = new Thread(new ThreadStarter(2500,"tcp"));
	t.start();
	
	List backLogEvents = new ArrayList();
	
	TestBackLogHandler bh = new TestBackLogHandler(backLogEvents);
	bh.initialize();
	
	Syslog.getInstance("tcp").getConfig().addBackLogHandler(bh);
	((AbstractSyslogConfigIF) Syslog.getInstance("tcp").getConfig()).setThreaded(false);
	
	SyslogServer.getInstance("tcp").getConfig().setShutdownWait(0);
	
	_testThreadedSendReceive(1,true,true,backLogEvents);
}
 
Example #20
Source File: SyslogVersionTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testVersion() {
	assertEquals(Syslog4jVersion.VERSION,Syslog.getVersion());
	assertEquals(Syslog4jVersion.VERSION,SyslogServer.getVersion());
}
 
Example #21
Source File: SyslogParameterTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testSyslogServerExists() {
	assertFalse(SyslogServer.exists(null));
	assertFalse(SyslogServer.exists(""));
	assertTrue(SyslogServer.exists("udp"));
	assertEquals("udp",SyslogServer.getInstance("udp").getProtocol());
}
 
Example #22
Source File: SyslogHandlerTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void setup(final ManagementClient managementClient) throws Exception {
    LOGGER.info("starting syslog server on port " + PORT);

    // clear created server instances (TCP/UDP)
    SyslogServer.shutdown();
    // create a new UDP instance
    final String host = CoreUtils.stripSquareBrackets(managementClient.getMgmtAddress());
    final UDPSyslogServerConfig config = new UDPSyslogServerConfig();
    config.setPort(PORT);
    config.setHost(host);
    config.setUseStructuredData(true);
    config.addEventHandler(new BlockedSyslogServerEventHandler());
    SyslogServer.createInstance(UDP, config);
    // start syslog server
    SyslogServer.getThreadedInstance(SyslogConstants.UDP);

    final CompositeOperationBuilder builder = CompositeOperationBuilder.create();

    // create syslog-profile
    builder.addStep(Operations.createAddOperation(SYSLOG_PROFILE_ADDR));

    ModelNode op = Operations.createAddOperation(SYSLOG_HANDLER_ADDR);
    op.get("level").set("TRACE");
    op.get("port").set(PORT);
    op.get("server-address").set(host);
    op.get("enabled").set("true");
    builder.addStep(op);

    op = Operations.createAddOperation(SYSLOG_PROFILE_ROOT_LOGGER_ADDR);
    op.get("level").set("TRACE");
    op.get("handlers").add("SYSLOG");
    builder.addStep(op);

    // Add a JSON formatter
    builder.addStep(Operations.createAddOperation(JSON_FORMATTER_ADDR));

    executeOperation(builder.build());

    LOGGER.info("syslog server setup complete");
}
 
Example #23
Source File: PrintStreamServerEventTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testPrintStreamEvent() {
	SyslogServerIF server = SyslogServer.getInstance("udp");
	
	String message = "test message";
	
	InetAddress inetAddress = null;
	
	try { inetAddress = InetAddress.getLocalHost(); } catch (UnknownHostException uhe) { }
	
	SyslogServerEventIF event = new SyslogServerEvent(message.getBytes(),message.length(),inetAddress);

	assertEquals(SyslogConstants.CHAR_SET_DEFAULT,event.getCharSet());
	event.setCharSet("xxyyzz");
	assertEquals("xxyyzz",event.getCharSet());
	
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	PrintStream ps = new PrintStream(baos);
	
	SyslogServerSessionEventHandlerIF eventHandler = new PrintStreamSyslogServerEventHandler(ps);
	eventHandler.sessionOpened(server,null);
	eventHandler.event(null,server,null,event);
	eventHandler.exception(null,server,null,null);
	eventHandler.sessionClosed(null,server,null,false);
	assertEquals(event.getMessage(),new String("test message"));

	Date date = new Date();
	event.setDate(date);
	assertTrue(date == event.getDate());
	
	event.setFacility(SyslogConstants.FACILITY_LOCAL0);
	assertEquals(SyslogConstants.FACILITY_LOCAL0,event.getFacility());
	
	event.setHost("foo");
	assertEquals("foo",event.getHost());
	
	event.setLevel(SyslogConstants.LEVEL_DEBUG);
	assertEquals(SyslogConstants.LEVEL_DEBUG,event.getLevel());
	
	event.setMessage(message);
	assertEquals(message,event.getMessage());
	
	eventHandler = SystemOutSyslogServerEventHandler.create();
	eventHandler.event(null,server,null,event);

	eventHandler = new SystemErrSyslogServerEventHandler();
	eventHandler.event(null,server,null,event);
	
	try {
		File f = File.createTempFile("syslog4j-test",".txt");
		
		eventHandler = new FileSyslogServerEventHandler(f.getPath());
		eventHandler.event(null,server,null,event);

		eventHandler = new FileSyslogServerEventHandler(f.getPath(),true);
		eventHandler.event(null,server,null,event);

	} catch (Exception e) {
		fail(e.toString());
	}
}
 
Example #24
Source File: Syslog4jAppenderTest.java    From logback-syslog4j with MIT License 4 votes vote down vote up
protected void tearDown() {
    SyslogServer.shutdown();
}
 
Example #25
Source File: SSLTCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void setupSslServer() throws Exception {
	SSLTCPNetSyslogServerConfigIF config = new SSLTCPNetSyslogServerConfig();
	SSLConfigUtil.configure(config);
	SyslogServer.createInstance("sslTcp", config);
}
 
Example #26
Source File: BackLogTCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void run() {
	SyslogUtility.sleep(this.pause);
	
	SyslogServer.getThreadedInstance(this.protocol);
}
 
Example #27
Source File: BackLogTCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public ThreadStarter(long pause, String protocol) {
	this.pause = pause;
	this.protocol = protocol;
	
	SyslogServer.getInstance(this.protocol).shutdown();
}
 
Example #28
Source File: Server.java    From simple-syslog-server with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static void main(String[] args) throws SyslogRuntimeException, UnknownHostException {

		// Details for the properties -
		// http://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/JSSERefGuide.html
		System.setProperty("jsse.enableSNIExtension", "false");
		// just in case...
		System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
		System.setProperty("sun.security.ssl.allowLegacyHelloMessages", "true");

		// clear created server instances (TCP/UDP)
		SyslogServer.shutdown();

		String syslogProtocol = "tls";
		System.out.println("Simple syslog server (RFC-5424)");
		System.out.println("Usage:");
		System.out.println("  java -jar simple-syslog-server.jar [protocol]");
		System.out.println();
		System.out.println("Possible protocols: udp, tcp, tls");
		System.out.println();

		if (args.length > 0) {
			syslogProtocol = args[0];
		} else {
			System.err.println("No protocol provided. Defaulting to " + syslogProtocol);
		}

		SyslogServerConfigIF config = getSyslogConfig(syslogProtocol);
		if (config == null) {
			System.err.println("Unsupported Syslog protocol: " + syslogProtocol);
			System.exit(1);
		}
		config.setUseStructuredData(true);
//		config.setHost(InetAddress.getByName(null).getHostAddress());
		config.setHost("0.0.0.0");
		config.setPort(SYSLOG_PORT);

		System.out.println("Starting Simple Syslog Server");
		System.out.println("Protocol:     " + syslogProtocol);
		System.out.println("Bind address: " + config.getHost());
		System.out.println("Port:         " + config.getPort());

		// start syslog server
		SyslogServer.createThreadedInstance(syslogProtocol, config);
	}
 
Example #29
Source File: SyslogServerSessionTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testTCPSession() {
	SyslogServerConfigIF serverConfig = new TCPNetSyslogServerConfig(9999);
	
	TCPSessionHandler handler1 = new TCPSessionHandler("a");
	serverConfig.addEventHandler(handler1);
	
	TCPSessionHandler handler2 = new TCPSessionHandler("b");
	serverConfig.addEventHandler(handler2);
	
	SyslogServerIF server = SyslogServer.createThreadedInstance("tcp_session",serverConfig);
	
	SyslogUtility.sleep(100);
	
	assertTrue(handler1.initialized);
	assertTrue(handler2.initialized);
	
	SyslogConfigIF config = new TCPNetSyslogConfig();
	config.setPort(9999);
	
	TCPNetSyslogServer tcpServer = (TCPNetSyslogServer) server;
	
	SyslogIF syslog1 = Syslog.createInstance("tcp_session_1",config);
	
	syslog1.info("1");
	syslog1.info("2");

	SyslogUtility.sleep(100);

	assertEquals(1,tcpServer.getSessions().size());

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

	syslog1.info("5");
	syslog1.info("6");

	syslog2.info("7");
	syslog2.info("8");

	SyslogUtility.sleep(100);
	
	assertEquals(2,tcpServer.getSessions().size());

	syslog1.shutdown();
	syslog2.shutdown();
	SyslogServer.destroyInstance("tcp_session");
	
	try {
		SyslogServer.getInstance("tcp_session");
		fail();
		
	} catch (SyslogRuntimeException sre) {
		//
	}
	
	assertEquals(4,handler1.eventCount[0]);
	assertEquals(0,handler1.eventCount[1]);
	assertEquals(4,handler1.eventCount[2]);
	assertEquals(0,handler1.eventCount[3]);
	
	assertEquals(0,handler2.eventCount[0]);
	assertEquals(4,handler2.eventCount[1]);
	assertEquals(0,handler2.eventCount[2]);
	assertEquals(4,handler2.eventCount[3]);

	assertEquals(1,handler1.closeCount[0]);
	assertEquals(0,handler1.closeCount[1]);
	assertEquals(1,handler1.closeCount[2]);
	assertEquals(0,handler1.closeCount[3]);
	
	assertEquals(0,handler2.closeCount[0]);
	assertEquals(1,handler2.closeCount[1]);
	assertEquals(0,handler2.closeCount[2]);
	assertEquals(1,handler2.closeCount[3]);

	SyslogUtility.sleep(100);

	assertTrue(handler1.destroyed);
	assertTrue(handler2.destroyed);
}
 
Example #30
Source File: TCPNetSyslog4jTest.java    From syslog4j with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testTimeout() {
	TCPNetSyslogServerConfigIF serverConfig = new TCPNetSyslogServerConfig();
	serverConfig.setTimeout(150);
	serverConfig.setPort(7777);
	serverConfig.addEventHandler(new TimeoutHandler());

	TCPNetSyslogConfigIF clientConfig = new TCPNetSyslogConfig();
	clientConfig.setPort(7777);
	clientConfig.setThreaded(false);

	SyslogServer.createThreadedInstance("tcp_to",serverConfig);
	SyslogUtility.sleep(250);
	
	SyslogIF syslog = Syslog.createInstance("tcp_to",clientConfig);
	SyslogUtility.sleep(250);
	
	int mark = 0;
	
	for(int i=1; i<=10; i++) {
		if (i > 5) {
			mark--;
			
		} else {
			mark++;
		}
		
		int timeout = 35 * mark + 25;
		
		syslog.info("Test " + i + " (" + timeout + "ms) Should Timeout: " + (timeout > serverConfig.getTimeout() ? "yes" : "no"));
		SyslogUtility.sleep(timeout);
	}
	
	syslog.flush();
	SyslogUtility.sleep(200);
	
	SyslogServer.destroyInstance("tcp_to");
	
	SyslogUtility.sleep(200);
	
	Syslog.destroyInstance("tcp_to");
}