Listing of Source cgi/HitCounter.java
package se.entra.phantom.server.http;

import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import se.entra.phantom.server.EventID;
import se.entra.phantom.server.EventManager;
import org.w3c.dom.Element;

/**
 * A hit counter for an HTML resource. The hits are saved in a file called
 * 'hitcount.properties'.
 */
public class HitCounter implements IncludeCgiPrintInterface
{
  /**
   * The file name.
   */
  private static final String fileName = "hitcount.properties";

  /**
   * These are the properties for the hit count.
   */
  private static Properties hitCount;

  /**
   * Tries to load the 'hitcount.properties' file, otherwise a new file is created later.
   */
  static
    {
    hitCount=new Properties();
    BufferedInputStream in=null;
    try
      {
      in=new BufferedInputStream(new FileInputStream(fileName));
      hitCount.load(in);
      in.close();
      in=null;
      }
    catch(FileNotFoundException e)
      {
      // This we don't case about.
      }
    catch(IOException e)
      {
      // Log to NetPhantom event log.
      EventManager.logEvent("HitCount: Failure writing "+fileName+": "+e,EventID.EVENTCLASS_WARNING);
      }

    // Make sure the file is closed.
    try { if ( in!=null ) in.close(); }
    catch(IOException e) {}
    }

  /**
   * Updates the hit counter properties for the current document.
   *
   * @throws  IOException  for IO errors.
   */
  @Override
  @SuppressWarnings("deprecation")
  public void performAction(HttpSession session,HttpResource resource,Element element,PrintWriter out) throws IOException
    {
    // Try getting hit count from the properties.
    int hits=0;
    String resourceName=resource.name;

    synchronized(hitCount)
      {
      String previousCount=hitCount.getProperty(resourceName);
      if ( previousCount!=null )
        {
        // Parse the result if possible.
        try { hits=Integer.valueOf(previousCount).intValue(); }
        catch(NumberFormatException e) {}
        }

      // Increase hit count and save to properties.
      ++hits;
      hitCount.put(resourceName,Integer.toString(hits));

      // Build a padded string (i.e. 00123) and output it for the HTML document.
      String hc=Integer.toString(hits);
      hc="00000".substring(0,Math.max(5-hc.length(),0))+hc;
      out.print(hc);

      // Now save the properties file.
      BufferedOutputStream o=null;
      try
        {
        o=new BufferedOutputStream(new FileOutputStream(fileName));
        hitCount.save(o,"Hit counter file used by Include-type CGI");
        o.close();
        o=null;
        }
      catch(IOException e)
        {
        // Make sure file is closed.
        try { if ( o!=null ) o.close(); }
        catch(IOException e2) {}

        // Log to NetPhantom event log.
        EventManager.logEvent("HitCount: Failure writing "+fileName+": "+e,EventID.EVENTCLASS_WARNING);
        }
      }
    }
}