Java源码示例:org.productivity.java.syslog4j.SyslogIF

示例1
public String modify(SyslogIF syslog, int facility, int level, String message) {
	synchronized(this.mac) {
		byte[] messageBytes = SyslogUtility.getBytes(syslog.getConfig(),message);
		
		StringBuffer buffer = new StringBuffer(message);
		
		byte[] macBytes = this.mac.doFinal(messageBytes);
		
		String macString = Base64.encodeBytes(macBytes,Base64.DONT_BREAK_LINES);
		
		buffer.append(this.config.getPrefix());
		buffer.append(macString);
		buffer.append(this.config.getSuffix());
		
		return buffer.toString();
	}
}
 
示例2
public String modify(SyslogIF syslog, int facility, int level, String message) {
	synchronized(this.config.getChecksum()) {
		StringBuffer messageBuffer = new StringBuffer(message);
		
		byte[] messageBytes = SyslogUtility.getBytes(syslog.getConfig(),message);
		
		if (!this.config.isContinuous()) {
			this.config.getChecksum().reset();
		}
		
		this.config.getChecksum().update(messageBytes,0,message.length());
		
		messageBuffer.append(this.config.getPrefix());
		messageBuffer.append(Long.toHexString(this.config.getChecksum().getValue()).toUpperCase());
		messageBuffer.append(this.config.getSuffix());
		
		return messageBuffer.toString();
	}
}
 
示例3
public void testHashVerify() {
	SyslogIF syslog = Syslog.getInstance("tcp");
	
	HashSyslogMessageModifier modifier = HashSyslogMessageModifier.createMD5();
	modifier.getConfig().setSuffix(null);
	
	String message = "md5 hash Test 1212 ABAB";
	
	String modifiedMessage = modifier.modify(syslog,0,0,message);
	System.out.println(modifiedMessage);
	
	if (!modifier.verify(message,"fqfK2PYV76Wv9yNQjLoVeg==")) {
		fail();
	}

	if (!modifier.verify(message,Base64.decode("fqfK2PYV76Wv9yNQjLoVeg=="))) {
		fail();
	}

	if (!modifier.verify(modifiedMessage)) {
		fail();
	}
	
	assertFalse(modifier.verify(null));
}
 
示例4
public void testMacVerify() {
	SyslogIF syslog = Syslog.getInstance("udp");
	
	MacSyslogMessageModifier modifier = MacSyslogMessageModifier.createHmacMD5("fb7Jl0VGnzY5ehJCdeff7bSZ5Vk=");
	modifier.getConfig().setPrefix(" ");
	modifier.getConfig().setSuffix(null);
	
	String message = "hmacmd5 Test 3434 DCDC";
	
	String modifiedMessage = modifier.modify(syslog,0,0,message);
	System.out.println(modifiedMessage);
	
	if (!modifier.verify(message,"MfWJ4XhFiMlPwnFEJ401zA==")) {
		fail();
	}

	if (!modifier.verify(message,Base64.decode("MfWJ4XhFiMlPwnFEJ401zA=="))) {
		fail();
	}

	if (!modifier.verify(modifiedMessage)) {
		fail();
	}
}
 
示例5
public void testPCISyslogMessage() {
	// PREPARE
	
	List events = new ArrayList();
	String message = null;
	
	String protocol = getClientProtocol();
	SyslogIF syslog = getSyslog(protocol);
	
	message = new PCISyslogMessage("a","b",new Date(),"c","d","e").createMessage();
	syslog.info("[TEST] " + message);
	events.add("[TEST] " + message);

	// VERIFY
	
	SyslogUtility.sleep(pause);
	syslog.flush();
	verifySendReceive(events,false,false);
}
 
示例6
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));
}
 
示例7
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) {
		//
	}
}
 
示例8
protected SyslogIF getSyslog(String protocol) {
	if (!Syslog.exists(protocol)) {
		fail("Protocol \"" + protocol + "\" does not exist");
	}
	
	SyslogIF syslog = Syslog.getInstance(protocol);
	
	if (!(syslog instanceof MultipleSyslog)) {
		syslog.getConfig().setIdent(APP_ID);
	}
	
	if (!(syslog.getConfig() instanceof MultipleSyslogConfig)) {
		syslog.getConfig().setPort(TEST_PORT);
	}
	
	return syslog;
}
 
示例9
protected void assertSyslog(SyslogIF syslog, String[] expectedMessages) {
	SplitSyslog splitSyslog = (SplitSyslog) syslog;
	
	List lastMessages = splitSyslog.getLastMessages();
	
	if (lastMessages.size() < 1) {
		fail("No messages received");
	}
	
	for(int i=0; i<lastMessages.size(); i++) {
		String lastMessage = ((String) lastMessages.get(i)).substring(21 + this.localNameLength);
		
		assertEquals(expectedMessages[i],lastMessage);
	}
	
	syslog.flush();
}
 
示例10
public void testTruncate() {
	setMessageLength(10);
	
	SyslogIF syslog = Syslog.getInstance("split");
	syslog.getConfig().setTruncateMessage(true);
	
	syslog.info("T");
	syslog.info("Test 1234");
	syslog.info("Test 12345");
	syslog.info("Test 654321");
	syslog.info("Test 12345 For now is the time for all good men to come to the aid of their country");
	
	assertSyslog(syslog,new String[] {"T", "Test 1234", "Test 12345", "Test 65432", "Test 12345" } );
	
	syslog.flush();
}
 
示例11
@Override
public void start() {
    super.start();

    synchronized (this) {
        try {
            Class syslogClass = syslogConfig.getSyslogClass();
            syslog = (SyslogIF) syslogClass.newInstance();

            syslog.initialize(syslogClass.getSimpleName(), syslogConfig);
        } catch (ClassCastException cse) {
            throw new SyslogRuntimeException(cse);
        } catch (IllegalAccessException iae) {
            throw new SyslogRuntimeException(iae);
        } catch (InstantiationException ie) {
            throw new SyslogRuntimeException(ie);
        }
    }
}
 
示例12
public String modify(SyslogIF syslog, int facility, int level, String message) {
	String _message = message;
	
	if (message != null) {
		if (this.stringCase == LOWER_CASE) {
			_message = _message.toLowerCase();
			
		} else if (this.stringCase == UPPER_CASE) {
			_message = _message.toUpperCase();
		}
	}
	
	return _message;
}
 
示例13
public String modify(SyslogIF syslog, int facility, int level, String message) {
	if (this.suffix == null || "".equals(this.suffix.trim())) {
		return message;
	}

	return message + this.delimiter + this.suffix;
}
 
示例14
public String modify(SyslogIF syslog, int facility, int level, String message) {
	if (this.prefix == null || "".equals(this.prefix.trim())) {
		return message;
	}

	return this.prefix + this.delimiter + message;
}
 
示例15
public String modify(SyslogIF syslog, int facility, int level, String message) {
	StringBuffer buffer = new StringBuffer(message);
	
	buffer.append(this.config.getPrefix());
	buffer.append(nextSequence(level));
	buffer.append(this.config.getSuffix());
	
	return buffer.toString();
}
 
示例16
public String modify(SyslogIF syslog, int facility, int level, String message) {
	if (message != null && !"".equals(message.trim())) {
		String escapedMessage = escapeHtml(message);
		
		return escapedMessage;
	}
	
	return message;
}
 
示例17
public void log(SyslogIF syslog, int level, String message, String reason) {
	String combinedMessage = combine(syslog,level,message,reason);
	
	if (this.appendLinefeed) {
		this.printStream.println(combinedMessage);
		
	} else {
		this.printStream.print(combinedMessage);
	}
}
 
示例18
public void log(SyslogIF syslog, int level, String message, String reason) throws SyslogRuntimeException {
	if (this.syslog.getProtocol().equals(syslog.getProtocol())) {
		throw new SyslogRuntimeException("Ignoring this log entry since the backLog protocol \"" + this.syslog.getProtocol() + "\" is the same as the main protocol");
	}
	
	String combinedMessage = combine(syslog,level,message,reason);
	
	this.syslog.log(level,combinedMessage);
}
 
示例19
protected String combine(SyslogIF syslog, int level, String message, String reason) {
	// Note: syslog is explicitly ignored by default
	
	String _message = message != null ? message : "UNKNOWN";
	String _reason = reason != null ? reason : "UNKNOWN";
	
	String combinedMessage = SyslogUtility.getLevelString(level) + " " + _message;
	
	if (this.appendReason) {
		combinedMessage += " [" + _reason + "]";
	}
	
	return combinedMessage;
}
 
示例20
public void log(int level, String message) {
	for(int i=0; i<this.multipleSyslogConfig.getProtocols().size(); i++) {
		String protocol = (String) this.multipleSyslogConfig.getProtocols().get(i);
		
		SyslogIF syslog = Syslog.getInstance(protocol);
		
		syslog.log(level,message);
	}
}
 
示例21
public void log(int level, SyslogMessageIF message) {
	for(int i=0; i<this.multipleSyslogConfig.getProtocols().size(); i++) {
		String protocol = (String) this.multipleSyslogConfig.getProtocols().get(i);
		
		SyslogIF syslog = Syslog.getInstance(protocol);
		
		syslog.log(level,message);
	}
}
 
示例22
public void flush() throws SyslogRuntimeException {
	for(int i=0; i<this.multipleSyslogConfig.getProtocols().size(); i++) {
		String protocol = (String) this.multipleSyslogConfig.getProtocols().get(i);
		
		SyslogIF syslog = Syslog.getInstance(protocol);
		
		syslog.flush();
	}
}
 
示例23
public void shutdown() throws SyslogRuntimeException {
	for(int i=0; i<this.multipleSyslogConfig.getProtocols().size(); i++) {
		String protocol = (String) this.multipleSyslogConfig.getProtocols().get(i);
		
		SyslogIF syslog = Syslog.getInstance(protocol);
		
		syslog.shutdown();
	}
}
 
示例24
public void testHTMLEntityEscapeMessageModifier() {
	SyslogMessageModifierIF messageModifier = new HTMLEntityEscapeSyslogMessageModifier();

	String nullHtml = messageModifier.modify(null,0,0,null);
	assertNull(nullHtml);
	
	String emptyHtml = messageModifier.modify(null,0,0,"");
	assertEquals("",emptyHtml);

	// PREPARE
	
	List events = new ArrayList();
	String message = null;
	
	String protocol = getClientProtocol();
	SyslogIF syslog = getSyslog(protocol);
	syslog.getConfig().removeAllMessageModifiers();		

	syslog.getConfig().addMessageModifier(HTMLEntityEscapeSyslogMessageModifier.createDefault());
	
	// SEND HTML

	message = "[TEST] <html>&\"test\"&'" + "\t" + "</html>" + (char) 255;
	syslog.info(message);
	events.add("[TEST] &lt;html&gt;&amp;&quot;test&quot;&amp;&#39;&#9;&lt;/html&gt;&#255;");

	// VERIFY
	
	SyslogUtility.sleep(pause);
	syslog.flush();
	verifySendReceive(events,false,false);
}
 
示例25
public void testSyslog4jBackLogHandler() {
	SyslogIF udp = Syslog.getInstance("udp");
	SyslogIF tcp = Syslog.getInstance("tcp");
	
	SyslogBackLogHandlerIF syslog4j = new Syslog4jBackLogHandler("udp");
	syslog4j.initialize();
	
	syslog4j.log(tcp,SyslogConstants.LEVEL_INFO,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.log(tcp,-1,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.down(tcp,null);
	syslog4j.up(tcp);

	syslog4j = new Syslog4jBackLogHandler("udp",false);
	
	syslog4j.log(tcp,SyslogConstants.LEVEL_INFO,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.log(tcp,-1,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.down(udp,null);
	syslog4j.up(udp);

	syslog4j = new Syslog4jBackLogHandler(udp);
	
	syslog4j.log(tcp,SyslogConstants.LEVEL_INFO,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.log(tcp,-1,"Log4j BackLog Test Message - IGNORE","really");
	
	syslog4j = new Syslog4jBackLogHandler(udp,false);
	
	syslog4j.log(tcp,SyslogConstants.LEVEL_INFO,"Log4j BackLog Test Message - IGNORE","really");
	syslog4j.log(tcp,-1,"Log4j BackLog Test Message - IGNORE","really");
	
	try {
		syslog4j.log(Syslog.getInstance("udp"),SyslogConstants.LEVEL_INFO,"Log4j BackLog Test Message - IGNORE","really");
		fail();
		
	} catch (SyslogRuntimeException sre) {
		//
	}
}
 
示例26
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;
}
 
示例27
public void testMaxQueueSize() {
	int catchCount = 5;
	int maxQueueSize = 5;
	int messagesToSend = 15;
	int port = 7777;
	
	FakeSyslogServer server = new FakeSyslogServer(port,catchCount);
	Thread thread = new Thread(server);
	thread.start();
	
	BackLogCounter counter = new BackLogCounter();
	
	TCPNetSyslogConfigIF syslogConfig = new TCPNetSyslogConfig();
	syslogConfig.setPort(port);
	assertEquals(syslogConfig.getMaxQueueSize(),SyslogConstants.MAX_QUEUE_SIZE_DEFAULT);
	syslogConfig.setMaxQueueSize(maxQueueSize);
	syslogConfig.addBackLogHandler(counter);
	syslogConfig.addBackLogHandler(NullSyslogBackLogHandler.INSTANCE);
	
	SyslogIF syslog = Syslog.createInstance("maxQueueSizeTest",syslogConfig);
	
	for(int i=1; i<=messagesToSend; i++) {
		syslog.log(SyslogConstants.LEVEL_INFO,"test line " + i);
	}
	
	SyslogUtility.sleep(500);
	
	server.shutdown = true;

	SyslogUtility.sleep(500);

	System.out.println("Sent Messages:       " + messagesToSend);	
	System.out.println("Received Messages:   " + server.count);
	System.out.println("Backlogged Messages: " + counter.count);
	
	assertEquals(messagesToSend,(server.count+counter.count));
}
 
示例28
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);
}
 
示例29
/**
 * Convert a level to equivalent syslog severity. Only levels for printing
 * methods i.e DEBUG, WARN, INFO and ERROR are converted.
 *
 * @see ch.qos.logback.core.net.SyslogAppenderBase#getSeverityForEvent(java.lang.Object)
 */
public int getSeverityForEvent(Object eventObject) {
    if (eventObject instanceof ILoggingEvent) {
        ILoggingEvent event = (ILoggingEvent) eventObject;
        return LevelToSyslogSeverity.convert(event);
    } else {
        return SyslogIF.LEVEL_INFO;
    }
}
 
示例30
protected SyslogIF getSyslog() {
  return new UDPNetSyslog();
}