/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software
 * License version 1.1, a copy of which has been included with this
 * distribution in the LICENSE.APL file.
 */

package org.apache.log4j;

import java.util.List;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

public class AppenderFlusher extends TimerTask {
  /**
     Time in milliseconds between successive task executions.
  */
  static long period        = 2000;
  static List appenders     = new ArrayList();
  static Timer timer        = new Timer(true);
  static TimerTask flusher  = new AppenderFlusher();
  
  public
  static
  synchronized
  void add(Appender appender) {
    appenders.add(appender);
    if (appenders.size() == 1) {
      timer.schedule(flusher, period, period);
    }
  }
  
  public
  static
  synchronized
  void remove(Appender appender) {
    appenders.remove(appender);
    if (appenders.size() == 0) {
      flusher.cancel();
    }
  }
  
  public
  synchronized
  void run() {
    System.err.println(".");
    for (int i = 0; i < appenders.size(); i++) {
        ((Appender) appenders.get(i)).flush();
    }
  }
}
