/*
 * 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 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.test;

import org.apache.log4j.Logger;
import org.w3c.dom.*;
import com.silverwrist.util.xml.*;
import com.silverwrist.dynamo.DynamoVersion;
import com.silverwrist.dynamo.Namespaces;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.module.ModuleOperations;

public class TestApplication implements Application
{
  /*--------------------------------------------------------------------------------
   * Internal class implementing initialization code
   *--------------------------------------------------------------------------------
   */

  private class AppInit implements ComponentInitialize
  {
    /*====================================================================
     * Constructor
     *====================================================================
     */

    AppInit()
    { // do nothing
    } // end constructor

    /*====================================================================
     * Implementations from interface ComponentInitialize
     *====================================================================
     */

    public void initialize(Element config_root, ServiceProvider services) throws ConfigException
    {
      XMLLoader loader = XMLLoader.get();
      try
      { // verify the right node name
	loader.verifyNodeName(config_root,"application");

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

	logger.info("Application '" + m_name + "' initialized");

      } // end try
      catch (XMLLoadException e)
      { // error loading XML config data
	throw new ConfigException(e);

      } // end catch

      // Load the test module.
      ModuleOperations mod = (ModuleOperations)(services.queryService(ModuleOperations.class));
      try
      { // load it!
	mod.loadModule("dynamo-test-module.jar",true);

      } // end try
      catch (ModuleException e)
      { // translate ModuleException into ConfigException
	throw new ConfigException(e);

      } // end catch

    } // end initialize

  } // end class AppInit

  /*--------------------------------------------------------------------------------
   * Internal class implementing shutdown code
   *--------------------------------------------------------------------------------
   */

  private class AppTerm implements ComponentShutdown
  {
    /*====================================================================
     * Constructor
     *====================================================================
     */

    AppTerm()
    { // do nothing
    } // end constructor

    /*====================================================================
     * Implementations from interface ComponentShutdown
     *====================================================================
     */

    public void shutdown()
    {
      logger.info("Application '" + m_name + "' shutting down!");

    } // end shutdown

  } // end class AppTerm

  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

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

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

  private String m_name;    // the name of this object

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

  public TestApplication()
  {
    logger.info("Constructing new TestApplication");

  } // end constructor

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

  public String getName()
  {
    return m_name;

  } // end getName

  /*--------------------------------------------------------------------------------
   * Implementations from interface ServiceProvider
   *--------------------------------------------------------------------------------
   */

  /**
   * Queries this object for a specified service.
   *
   * @param klass The class of the object that should be returned as a service.
   * @return A service object.  The service object is guaranteed to be of the class
   *         specified by <CODE>klass</CODE>; that is, if <CODE>queryService(klass)</CODE>
   *         yields some object <CODE>x</CODE>, then the expression <CODE>klass.isInstance(x)</CODE>
   *         is true.
   * @exception com.silverwrist.dynamo.except.NoSuchServiceException If no service is available in
   *            the specified class.
   */
  public Object queryService(Class klass)
  {
    if (klass==ComponentInitialize.class)
      return (ComponentInitialize)(new AppInit());
    if (klass==ComponentShutdown.class)
      return (ComponentShutdown)(new AppTerm());
    throw new NoSuchServiceException("TestApplication",klass);

  } // end queryService

  /**
   * Queries this object for a specified service.
   *
   * @param klass The class of the object that should be returned as a service.
   * @param serviceid ID for the service to be requested, to further discriminate between requests.
   * @return A service object.  The service object is guaranteed to be of the class
   *         specified by <CODE>klass</CODE>; that is, if <CODE>queryService(klass)</CODE>
   *         yields some object <CODE>x</CODE>, then the expression <CODE>klass.isInstance(x)</CODE>
   *         is true.
   * @exception com.silverwrist.dynamo.except.NoSuchServiceException If no service is available in
   *            the specified class.
   */
  public Object queryService(Class klass, String serviceid)
  {
    return queryService(klass);

  } // end queryService

  /*--------------------------------------------------------------------------------
   * Implementations from interface Application
   *--------------------------------------------------------------------------------
   */

  public String getIdentity()
  {
    return "DynamoTestApp/" + DynamoVersion.VERSION;

  } // end getIdentity

  public Object getNullContentError(Request r)
  {
    return "ERROR!  Content is NULL!";

  } // end getNullContentError

} // end class TestApplication
