/*
 * 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.venice.app;

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

public class UserDefaultNamespaceHolder
    implements NamedObject, ComponentInitialize, ComponentShutdown, UserDefaultPropertyNamespace,
               UserDefaultPropertyNamespaceRegistration
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

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

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

  private String m_name;
  private Vector m_ns_list = new Vector();
  private ComponentShutdown m_shut1;
  private ComponentShutdown m_shut2;

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

  public UserDefaultNamespaceHolder()
  {
    m_ns_list.add(VeniceNamespaces.USER_SETTINGS_NAMESPACE);
    m_ns_list.add(VeniceNamespaces.USER_PROFILE_NAMESPACE);

  } // 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("UserDefaultNamespaceHolder 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 UserDefaultNamespaceHolder",e);
      throw new ConfigException(e);

    } // end catch

    // Create ServiceProvider interfaces.
    SingletonServiceProvider runtime_svcs =
        new SingletonServiceProvider("UserDefaultNamespaceHolder",UserDefaultPropertyNamespace.class,this);
    SingletonServiceProvider init_svcs = 
        new SingletonServiceProvider("UserDefaultNamespaceHolder",runtime_svcs,
				     UserDefaultPropertyNamespaceRegistration.class,this);

    // Hook the services provided by Dynamo.
    HookServiceProviders hooker = (HookServiceProviders)(services.queryService(HookServiceProviders.class));
    m_shut1 = hooker.hookInitServiceProvider(init_svcs);
    m_shut2 = hooker.hookRuntimeServiceProvider(runtime_svcs);

  } // end initialize

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

  /**
   * Shuts down the component associated with this interface, in a component-specific manner.
   */
  public void shutdown()
  {
    m_shut2.shutdown();
    m_shut2 = null;
    m_shut1.shutdown();
    m_shut1 = null;

  } // end shutdown

  /*--------------------------------------------------------------------------------
   * Implementations from interface UserDefaultPropertyNamespace
   *--------------------------------------------------------------------------------
   */

  public Collection getDefaultPropertyNamespaces()
  {
    ArrayList rc = new ArrayList(m_ns_list);
    return Collections.unmodifiableList(rc);

  } // end getDefaultPropertyNamespaces

  /*--------------------------------------------------------------------------------
   * Implementations from interface UserDefaultPropertyNamespaceRegistration
   *--------------------------------------------------------------------------------
   */

  public ComponentShutdown registerUserDefaultPropertyNamespace(String namespace)
  {
    m_ns_list.add(namespace);
    return new ShutdownVectorRemove(m_ns_list,namespace);

  } // end registerUserDefaultPropertyNamespace

} // end class UserDefaultNamespaceHolder
