/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002-03 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.app;

import org.apache.log4j.Logger;
import org.w3c.dom.*;
import com.silverwrist.util.xml.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.event.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.venice.VeniceNamespaces;

public class StartupShutdownAuditor implements NamedObject, ComponentInitialize, ComponentShutdown, ApplicationListener
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Logger logger = Logger.getLogger(StartupShutdownAuditor.class);

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_name;
  private AuditRecordFactory m_audit;
  private ComponentShutdown m_shut_event;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public StartupShutdownAuditor()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface NamedObject
   *--------------------------------------------------------------------------------
   */

  public String getName()
  {
    return m_name;

  } // end getName

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentInitialize
   *--------------------------------------------------------------------------------
   */

  /**
   * Initialize the component.
   *
   * @param config_root Pointer to the section of the Dynamo XML configuration file that configures this
   *                    particular component.  This is to be considered "read-only" by the component.
   * @param services An implementation of {@link com.silverwrist.dynamo.iface.ServiceProvider ServiceProvider}
   *                 which provides initialization services to the component.  This will include an implementation
   *                 of {@link com.silverwrist.dynamo.iface.ObjectProvider ObjectProvider} which may be used to
   *                 get information about other objects previously initialized by the application.
   * @exception com.silverwrist.dynamo.except.ConfigException If an error is encountered in the component
   *            configuration.
   */
  public void initialize(Element config_root, ServiceProvider services) throws ConfigException
  {
    logger.info("StartupShutdownAuditor initializing");

    XMLLoader loader = XMLLoader.get();
    String gprops = null;
    try
    { // verify the right node name
      loader.verifyNodeName(config_root,"object");

      // get the object's name
      m_name = loader.getAttribute(config_root,"name");

    } // end try
    catch (XMLLoadException e)
    { // error loading XML config data
      logger.fatal("XML loader exception in StartupShutdownAuditor",e);
      throw new ConfigException(e);

    } // end catch

    // Get the audit record provider.
    m_audit = (AuditRecordFactory)(services.queryService(AuditRecordFactory.class));

    // Register us as an application listener.
    EventListenerRegistration reg =
        (EventListenerRegistration)(services.queryService(EventListenerRegistration.class));
    m_shut_event = reg.registerApplicationListener(this);

  } // end initialize

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentShutdown
   *--------------------------------------------------------------------------------
   */

  public void shutdown()
  {
    m_shut_event.shutdown();
    m_shut_event = null;
    m_audit = null;

  } // end shutdown

  /*--------------------------------------------------------------------------------
   * Implementations from interface ApplicationListener
   *--------------------------------------------------------------------------------
   */

  public void applicationInitialized(ApplicationEvent event)
  {
    AuditRecordCompose rec =
        m_audit.createSystemAuditRecord(VeniceNamespaces.SYSTEM_EVENT_NAMESPACE,"system.startup");
    rec.write();

  } // end applicationInitialized

  public void applicationExiting(ApplicationEvent event)
  {
    AuditRecordCompose rec =
        m_audit.createSystemAuditRecord(VeniceNamespaces.SYSTEM_EVENT_NAMESPACE,"system.shutdown");
    rec.write();

  } // end applicationExiting

} // end class StartupShutdownAuditor
