Java tutorial
/* * Java-based syslog server and log search engine * Copyright (C) 2011 Henning Peters <pete@dexterslab.de> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.io.IOException; import java.io.StringReader; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.HashSet; import java.util.Set; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.TermAttribute; import org.json.*; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFutureListener; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelUpstreamHandler; import org.jboss.netty.util.CharsetUtil; public class SyslogHandler extends SimpleChannelUpstreamHandler { private Index index; public SyslogHandler(Index myIndex) { index = myIndex; } public void addTokens(Set<String> tokens, String input) { TokenStream stream; TermAttribute termAttr; stream = index.analyzer.tokenStream("field", new StringReader(input)); termAttr = stream.addAttribute(TermAttribute.class); while (true) { try { if (!stream.incrementToken()) break; } catch (IOException e) { } tokens.add(termAttr.term()); } } @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { InetSocketAddress socketAddress = (InetSocketAddress) e.getRemoteAddress(); LogEntry entry = SyslogParser.decode(socketAddress.getAddress().getHostAddress(), (String) e.getMessage()); try { index.append(entry); } catch (IOException e1) { e1.printStackTrace(); return; } if (index.channels.size() == 0) { return; } Set<String> tokens = new HashSet<String>(); addTokens(tokens, entry.host); addTokens(tokens, entry.message); JSONObject json = entry.toJSON(); // for (Channel c: index.channels) { // if (c.isWritable()) { // // String queryString = index.queries.get(c); // Set<String> queryTokens = new HashSet<String>(); // addTokens(queryTokens, queryString); // queryTokens.retainAll(tokens); // intersection // // if (queryTokens.size() > 0) { // ChannelFuture future = c.write(ChannelBuffers.copiedBuffer(json.toString(), CharsetUtil.UTF_8)); // future.addListener(ChannelFutureListener.CLOSE); // } // } // } for (Channel c : index.channels) { if (c.isWritable()) { String queryString = index.queries.get(c); ArrayList<LogEntry> result; try { result = index.search(queryString, 1, -1, -1, entry.id, entry.id); } catch (IOException e1) { e1.printStackTrace(); return; } if (result.size() > 0) { ChannelFuture future = c.write(ChannelBuffers.copiedBuffer(json.toString(), CharsetUtil.UTF_8)); future.addListener(ChannelFutureListener.CLOSE); } } } } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { e.getChannel().close(); } }