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

import java.util.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;

class Capabilities implements XmlRpcDispatcher, XmlRpcTypeNames
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String XMLRPC_NAME = "xmlrpc";
  private static final String XMLRPC_SPEC = "http://www.xmlrpc.com/spec";
  private static final int XMLRPC_VERSION = 1;

  private static final String FAULTCODE_NAME = "faults_interop";
  private static final String FAULTCODE_SPEC = "http://xmlrpc-epi.sourceforge.net/cpecs/rfc.fault_codes.php";
  private static final int FAULTCODE_VERSION = 20010516;

  private static final List SUPPORTED_METHODS;
  private static final List SIGNATURES;

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

  private Hashtable m_capas = new Hashtable();

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

  Capabilities()
  {
    insertCapability(XMLRPC_NAME,XMLRPC_SPEC,XMLRPC_VERSION);
    insertCapability(FAULTCODE_NAME,FAULTCODE_SPEC,FAULTCODE_VERSION);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface XmlRpcDispatcher
   *--------------------------------------------------------------------------------
   */

  public String getSessionIDParamValue(Request r, Application app)
  {
    return null;  // dummy value

  } // end getSessionIDParamValue

  public Object dispatchXmlRpcCall(Request r, Application app) throws FaultCode
  {
    if (r.getParameters().size()!=0)
      throw new XmlRpcParameterError("system.getCapabilities: wrong parameter count");
    return new XmlRpcResult(m_capas);

  } // end dispatchXmlRpcCall

  public Collection getSupportedMethods(Request r)
  {
    return SUPPORTED_METHODS;

  } // end getSupportedMethods

  public List getMethodSignature(Request r, String method) throws FaultCode
  {
    return SIGNATURES;

  } // end getMethodSignature

  public String getMethodHelp(Request r, String method) throws FaultCode
  {
    ResourceBundle b = ResourceBundle.getBundle("com.silverwrist.dynamo.xmlrpc.XmlRpcMessages",
						Locale.getDefault());
    return b.getString("help.getCaps");

  } // end getMethodHelp

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  void insertCapability(String name, String url, int version)
  {
    HashMap tmp = new HashMap();
    tmp.put("specUrl",url);
    tmp.put("specVersion",new Integer(version));
    m_capas.put(name,Collections.unmodifiableMap(tmp));

  } // end insertCapability

  ComponentShutdown addCapability(String name, String url, int version)
  {
    insertCapability(name,url,version);
    return new ShutdownHashtableRemove(m_capas,name);

  } // end addCapability

  /*--------------------------------------------------------------------------------
   * Static initializer
   *--------------------------------------------------------------------------------
   */

  static
  { // Set up the supported methods list.
    SUPPORTED_METHODS = Collections.singletonList("system.getCapabilities");

    // Set up the signature list.
    List l = Collections.singletonList(TYPE_STRUCT);
    SIGNATURES = Collections.singletonList(l);

  } // end static initializer

} // end class Capabilities
