| /******************************************************************************* |
| * Copyright (c) 2008 Innoopract Informationssysteme GmbH. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Innoopract Informationssysteme GmbH - initial API and implementation |
| ******************************************************************************/ |
| package org.eclipse.epp.wizard.internal; |
| |
| import java.io.BufferedWriter; |
| import java.io.File; |
| import java.io.FileWriter; |
| import java.io.PrintWriter; |
| import java.text.DateFormat; |
| import java.text.SimpleDateFormat; |
| import java.util.Date; |
| import java.util.concurrent.BlockingQueue; |
| import java.util.concurrent.LinkedBlockingQueue; |
| |
| import org.apache.log4j.Logger; |
| |
| /** |
| * Class for logging statistics about outgoing clicks Logged items: * current |
| * timestamp * user agent * selected IU roots * destination URL |
| * |
| * @author mwoelker |
| */ |
| public class StatsLogger implements Runnable { |
| |
| private static final String TABLE_HEADER = "'Timestamp ipAdress rootIUs destination user-agent"; |
| private static final Logger logger = Logger.getLogger( StatsLogger.class ); |
| /** |
| * Class encapsulating data for one outgoing click |
| * |
| * @author mwoelker |
| */ |
| public static class Statistic { |
| |
| final static protected String ISO8601_DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ss\t"; |
| final static protected DateFormat dateFormat = new SimpleDateFormat( ISO8601_DATEFORMAT ); |
| final protected Date timestamp = new Date(); |
| final protected String userAgent; |
| final protected String roots; |
| final protected String destination; |
| final protected String ipAdress; |
| |
| public Statistic( String destination, |
| String userAgent, |
| String ipAdress, |
| String roots |
| ) |
| { |
| super(); |
| this.destination = destination; |
| this.userAgent = userAgent; |
| this.ipAdress = ipAdress; |
| this.roots = roots; |
| } |
| |
| @Override |
| public String toString() { |
| StringBuilder builder = new StringBuilder(); |
| builder.append( dateFormat.format( timestamp ) ); |
| builder.append( ipAdress ); |
| builder.append( "\t" ); |
| builder.append( roots ); |
| builder.append( "\t" ); |
| builder.append( destination ); |
| builder.append( "\t" ); |
| builder.append( userAgent ); |
| return builder.toString(); |
| } |
| } |
| // Blocking queue for asynchronous producer/consumer processing |
| protected BlockingQueue<Statistic> queue = new LinkedBlockingQueue<Statistic>(); |
| protected Thread thread; |
| private PrintWriter writer; |
| |
| public StatsLogger( File file ) { |
| try { |
| writer = new PrintWriter( new BufferedWriter( new FileWriter( file, true ) ) ); |
| writer.println( TABLE_HEADER ); |
| thread = new Thread( this ); |
| thread.setName( "Download Statistics Logger" ); |
| thread.setDaemon( true ); |
| thread.start(); |
| } catch( Throwable t ) { |
| logger.error( "Could not initialize StatsLogger", t ); |
| } |
| } |
| |
| public void log( Statistic statistic ) { |
| queue.add( statistic ); |
| } |
| |
| public void run() { |
| try { |
| while( true ) { |
| Statistic statistic; |
| statistic = queue.take(); |
| internalLog( statistic ); |
| } |
| } catch( InterruptedException e ) { |
| Thread.currentThread().interrupt(); |
| } |
| } |
| |
| private void internalLog( Statistic statistic ) { |
| writer.println( statistic ); |
| writer.flush(); |
| } |
| } |