//
// CIMException.java
//
// The contents of this file are subject to the SNIA Public License Version 1.0
// (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.snia.org/resources/openSource.html
//
// 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 CIMException.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s):
//
// This source uses the Solaris WBEM APIs version 2.2. 
// Synchronization will be maintained with the Solaris WBEM APIS,
// which are being standardized through the Java Community Process.
//
//
// $Id: CIMException.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: CIMException.java,v $
// Revision 1.1  2003/07/17 18:32:55  kumpf
// HP-BAPU: PEP-64 CIM Client and CIM Listener Java Interface
//
// Revision 1.1.1.1  2001/10/31 18:14:20  karl
// import of SNIA 0_7
//
// Revision 1.6  2000/11/06 17:56:01  davidsi
// - Reconciled error ids to narrow gap with WBEM APIs.
//
// Revision 1.5  2000/08/16 05:59:52  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.4.8.1  2000/08/10 00:03:27  shawn
// head merge
//
// Revision 1.4  2000/05/11 10:12:19  davidsi
// - Fixed tabs.
//
// Revision 1.3  2000/05/06 17:04:21  davidsi
// - Modifications to bring classes inline with the SUN WBEM APIs 2.2.
//
// Revision 1.2  2000/02/13 15:09:06  davidsi
// - Added text to cover re-use of Solaris WBEM APIs.
//
// Revision 1.1.1.1  2000/02/06 16:41:04  davidsi
// Initial version.
//
//
//
package org.snia.wbem.cim;

import java.lang.Exception;
import java.util.Vector;
import org.snia.wbemcmd.xml.XMLError;

/**
 *  Superclass of classes used to represent exceptional CIM conditions.
 *  These exceptions are recoverable by user programs and therefore
 *  must be declared in the throw clause of a method and caught by 
 *  using try/catch statements.  
 */
public class CIMException  extends Exception 
{
    ///////////////////////////////////////////////////////////////////////
    //
    // Standard CIM Exceptions (as defined in the DMTF specification).
    //
    ///////////////////////////////////////////////////////////////////////

    /** 
     * A general error occurred that is not covered by a more
     * specific error code.
     */
    public static final String CIM_ERR_FAILED = "CIM_ERR_FAILED";


    /** 
     * Access to a CIM resource was not available to the client.
     */
    public static final String CIM_ERR_ACCESS_DENIED = "CIM_ERR_ACCESS_DENIED";


    /** 
     * The specified namespace does not exist.
     */
    public static final String CIM_ERR_INVALID_NAMESPACE = "CIM_ERR_INVALID_NAMESPACE";


    /** 
     * One or more parameter values passed to the method were invalid.
     */
    public static final String CIM_ERR_INVALID_PARAMETER = "CIM_ERR_INVALID_PARAMETER";


    /** 
     * The specified class does not exist.
     */
    public static final String CIM_ERR_INVALID_CLASS = "CIM_ERR_INVALID_CLASS";


    /** 
     * The requested object could not be found.
     */
    public static final String CIM_ERR_NOT_FOUND = "CIM_ERR_NOT_FOUND";


    /** 
     * The requested operation is not supported.
     */
    public static final String CIM_ERR_NOT_SUPPORTED = "CIM_ERR_NOT_SUPPORTED";


    /** 
     * Operation cannot be carried out on this class since it has subclasses.
     */
    public static final String CIM_ERR_CLASS_HAS_CHILDREN = "CIM_ERR_CLASS_HAS_CHILDREN";


    /** 
     * Operation cannot be carried out on this class since it has instances.
     */
    public static final String CIM_ERR_CLASS_HAS_INSTANCES = "CIM_ERR_CLASS_HAS_INSTANCES";


    /** 
     * Operation cannot be carried out since the specified superclass does not exist.
     */
    public static final String CIM_ERR_INVALID_SUPERCLASS = "CIM_ERR_INVALID_SUPERCLASS";


    /** 
     * Operation cannot be carried out because an object already exists.
     */
    public static final String CIM_ERR_ALREADY_EXISTS = "CIM_ERR_ALREADY_EXISTS";


    /** 
     * The specified property does not exist in the class or instance being manipulated.
     */
    public static final String CIM_ERR_NO_SUCH_PROPERTY = "CIM_ERR_NO_SUCH_PROPERTY";


    /** 
     * The value supplied is incompatible with the type.
     */
    public static final String CIM_ERR_TYPE_MISMATCH = "CIM_ERR_TYPE_MISMATCH";


    /** 
     * The query language is not recognized or supported.
     */
    public static final String CIM_ERR_QUERY_LANGUAGE_NOT_SUPPORTED = 
        "CIM_ERR_QUERY_LANGUAGE_NOT_SUPPORTED";


    /** 
     * The query is not valid for the specified query language.
     */
    public static final String CIM_ERR_INVALID_QUERY = "CIM_ERR_INVALID_QUERY";


    /** 
     * The extrinsic method could not be executed.
     */
    public static final String CIM_ERR_METHOD_NOT_AVAILABLE = 
        "CIM_ERR_METHOD_NOT_AVAILABLE";


    /** 
     * The specified extrinsic method does not exist.
     */
    public static final String CIM_ERR_METHOD_NOT_FOUND = "CIM_ERR_METHOD_NOT_FOUND";


    ///////////////////////////////////////////////////////////////////////
    //
    // Connection related exceptions
    //
    ///////////////////////////////////////////////////////////////////////

    /** 
     * Connection to CIM server failed. 
     */
    public static final String CIMCLIENT_ERR_CONNECTION_FAILED = 
        "CIMCLIENT_ERR_CONNECTION_FAILED";


    /** 
     * HTTP error occured when the CIM operation request was sent to the CIM server.
     */
    public static final String CIMCLIENT_ERR_HTTP_ERROR = "CIMCLIENT_ERR_HTTP_ERROR";


    /** 
     * SSL handshake failed when the CIM operation request was sent to
     * the SSL enabled CIM server.
     */
    public static final String CIMCLIENT_ERR_SSL_HANDSHAKE_FAILED = 
        "CIMCLIENT_ERR_SSL_HANDSHAKE_FAILED";


    /** 
     * The request action timed out.
     */
    public static final String CIMCLIENT_ERR_TIMED_OUT = "CIMCLIENT_ERR_TIMED_OUT";



    ///////////////////////////////////////////////////////////////////////
    //
    // Other CIM Exceptions
    //
    ///////////////////////////////////////////////////////////////////////


    /** 
     * XML parsing error.
     */
    public static final String XMLERROR = "XMLERROR";


    /**
     * Client tries to connect to a CIM object manager which does 
     * not support the version of the client.
     */
    public static final String VER_ERROR = "VER_ERROR"; 


    /** 
     * Default CIM Exception.
     */
    public static final String DEFAULT = "DEFAULT";  


    /** 
     * Default cim exception with one parameter.
     */
    public static final String PDEFAULT = "PDEFAULT"; 

    /** 
     * EMPTY SRTING for Exception params.
     */
    public static final String EMPTY = "";  

    //
    // Low memory.
    //
    //public static final String CIM_ERR_LOW_ON_MEMORY = "CIM_ERR_LOW_ON_MEMORY";

    /////////////////////////////////////////////////////////////////////////////


    private String localId=null;
    private Object []param=null;
    private static Vector cimErrors=new Vector();
    private static Vector xmlErrors=new Vector();                  
    
    static {
        addErrorMap(CIM_ERR_FAILED,XMLError.XML_ERR_FAILED);
        addErrorMap(CIM_ERR_ACCESS_DENIED,XMLError.XML_ERR_ACCESS_DENIED);
        addErrorMap(CIM_ERR_INVALID_NAMESPACE,XMLError.XML_ERR_INVALID_NAMESPACE);
        addErrorMap(CIM_ERR_INVALID_PARAMETER,XMLError.XML_ERR_INVALID_PARAMETER);
        addErrorMap(CIM_ERR_INVALID_CLASS,XMLError.XML_ERR_INVALID_CLASS);
        addErrorMap(CIM_ERR_NOT_FOUND,XMLError.XML_ERR_NOT_FOUND);
        addErrorMap(CIM_ERR_NOT_SUPPORTED,XMLError.XML_ERR_NOT_SUPPORTED);
        addErrorMap(CIM_ERR_CLASS_HAS_CHILDREN,XMLError.XML_ERR_CLASS_HAS_CHILDREN);
        addErrorMap(CIM_ERR_CLASS_HAS_INSTANCES,XMLError.XML_ERR_CLASS_HAS_INSTANCES);
        addErrorMap(CIM_ERR_INVALID_SUPERCLASS,XMLError.XML_ERR_INVALID_SUPERCLASS);
        addErrorMap(CIM_ERR_ALREADY_EXISTS,XMLError.XML_ERR_ALREADY_EXISTS);
        addErrorMap(CIM_ERR_NO_SUCH_PROPERTY,XMLError.XML_ERR_NO_SUCH_PROPERTY);
        addErrorMap(CIM_ERR_TYPE_MISMATCH,XMLError.XML_ERR_TYPE_MISMATCH);
        addErrorMap(CIM_ERR_QUERY_LANGUAGE_NOT_SUPPORTED,
            XMLError.XML_ERR_QUERY_LANGUAGE_NOT_SUPPORTED);
        addErrorMap(CIM_ERR_INVALID_QUERY,XMLError.XML_ERR_INVALID_QUERY);
        addErrorMap(CIM_ERR_METHOD_NOT_AVAILABLE,XMLError.XML_ERR_METHOD_NOT_AVAILABLE);
        addErrorMap(CIM_ERR_METHOD_NOT_FOUND,XMLError.XML_ERR_METHOD_NOT_FOUND);

        addErrorMap(CIMCLIENT_ERR_CONNECTION_FAILED, 
            XMLError.XML_CIMCLIENT_ERR_CONNECTION_FAILED);
        addErrorMap(CIMCLIENT_ERR_HTTP_ERROR, XMLError.XML_CIMCLIENT_ERR_HTTP_ERROR);
        addErrorMap(CIMCLIENT_ERR_SSL_HANDSHAKE_FAILED, 
            XMLError.XML_CIMCLIENT_ERR_SSL_HANDSHAKE_FAILED);
        addErrorMap(CIMCLIENT_ERR_TIMED_OUT, XMLError.XML_CIMCLIENT_ERR_TIMED_OUT);

    }

    protected static void addErrorMap(String cimError, int xmlError) 
    {
        cimErrors.addElement(cimError);
        xmlErrors.addElement(new Integer(xmlError));
    }
    
    /**
     * CIMException 
     */
    public CIMException() 
    {
        localId=DEFAULT;
    }

    /**
     * CIMException 
     *
     * Constructor with message
     */
    public CIMException(String ID)
    {
        localId=ID;
        if (ID==null)
            throw new NullPointerException();
    }

    /**
     * CIMException 
     *
     * Constructor with message + object
     */
    public CIMException(String ID,Object iparam) 
    {
        param=new Object[1];
        localId=ID;
	if ( iparam != null )
          param[0]=iparam;
        else 
          param[0]=EMPTY;
	 
        if (ID==null)
            throw new NullPointerException();
    }
 
    /**
     * CIMException 
     *
     * Constructor with message + 2 objects
     */
    public CIMException(String ID, Object param1, Object param2) 
    {
        localId=ID;
        if (ID==null)
            throw new NullPointerException();

        param=new Object[2];

	if ( param1 != null )
          param[0]=param1;
        else 
          param[0]=EMPTY;

	if ( param2 != null )
          param[1]=param2;
        else 
          param[1]=EMPTY;

    } 
 
 
    /**
     * CIMException 
     *
     * Constructor with message + 3 objects
     */
    public CIMException(String ID,Object param1,Object param2,Object param3)
    {
        localId=ID;
        if (ID==null)
            throw new NullPointerException();

        param=new Object[3];

	if ( param1 != null )
          param[0]=param1;
        else 
          param[0]=EMPTY;

	if ( param2 != null )
          param[1]=param2;
        else 
          param[1]=EMPTY;

	if ( param3 != null )
          param[2]=param3;
        else 
          param[2]=EMPTY;
    }
             

    /**
     * CIMException 
     *
     * Constructor for message + any number of parameters
     */
    public CIMException(String ID,Object iparam[]) 
    {
        localId=ID;
        param=iparam;
        if (ID==null)
            throw new NullPointerException();
    }
 
    /** Returns the exception ID
     *
     */
    public String getID() 
    {
        return(localId);
    }

    /** Returns the exception parameters
     *
     */
    public Object[] getParams() 
    {
        return(param);
    }

    /** Sets the parameters
     *
     */
    public void setParams(Object prm[])
    {
        param=prm;
    }

    /** Returns string form of the Exception
     *
     */
    public String toString() 
    {
        String ret=localId;
    
        if (param!=null) 
        {
            ret=ret.concat("(");
            for(int i=0;i<param.length;i++) 
            {
                if (i>0)
                    ret=ret.concat(",");
                ret=ret.concat(param[i].toString());
            }
            ret=ret.concat(")");
        }
        return(ret);
    }

    /** Returns the Exception message
     *
     */
    public String getMessage() 
    {
        String ret = "";
    
        if (param != null) 
        {
            for(int i = 0; i < param.length; i++) 
            {
                if (i > 0)
                    ret = ret.concat(",");
                ret = ret.concat(param[i].toString());
            }
        }

        return(ret);
    }

    
    /** 
     *
     */
    public int getXmlCode(String id) 
    {
        for(int i=0;i<cimErrors.size();i++) 
        {
            String str=(String)(cimErrors.elementAt(i));

            if (str.equalsIgnoreCase(localId))
            {
                return( ((Integer)(xmlErrors.elementAt(i))).intValue());
            }
        }
        return(-1);
    }
    
    
    /** 
     *
     */
    public String getXmlCode(int code) 
    {
        for(int i=0;i<cimErrors.size();i++) 
        {
            int testCode=((Integer)(xmlErrors.elementAt(i))).intValue();

            if (testCode==code) 
            {
                return((String)(cimErrors.elementAt(i)));
            }
        }
        return(CIM_ERR_FAILED);
    }

    /** 
     *
     */
    public boolean isXmlCode(String id) 
    {
        for(int i=0;i<cimErrors.size();i++) 
        {
            String str=(String)(cimErrors.elementAt(i));

            if (str.equalsIgnoreCase(localId))
            {
                return(true);
            }
        }
        return(false);
    }

}
