OW_CIMException.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_ExceptionIds.hpp"
00038 #include "OW_CIMException.hpp"
00039 #include "OW_String.hpp"
00040 #include "OW_Assertion.hpp"
00041 #include "OW_StringBuffer.hpp"
00042 
00043 #include <cstring>
00044 #include <cstdlib>
00045 #include <algorithm> // for std::swap
00046 
00047 namespace OW_NAMESPACE
00048 {
00049 
00050 namespace
00051 {
00052 String createLongMessage(CIMException::ErrNoType errval, const char* msg)
00053 {
00054    const char* rv(0);
00055    try
00056    {
00057       StringBuffer codeDesc(CIMException::getCodeDescription(errval));
00058       String msgStr(msg);
00059         // avoid multiple appendings of the exception message
00060       if (codeDesc == msgStr.substring(0, codeDesc.length()))
00061       {
00062          codeDesc = msgStr;
00063       }
00064       else if (!msgStr.empty())
00065       {
00066          codeDesc += " (";
00067          codeDesc += msgStr;
00068          codeDesc += ')';
00069       }
00070       return codeDesc.releaseString();
00071    }
00072    catch (std::bad_alloc&)
00073    {
00074       return String();
00075    }
00076 }
00077 
00078 }
00079 
00081 CIMException::CIMException(const char* file, int line, CIMException::ErrNoType errval,
00082    const char* msg, const Exception* otherException)
00083    : Exception(file, line, createLongMessage(errval, msg).c_str(), errval, otherException, ExceptionIds::CIMExceptionId)
00084    , m_description(Exception::dupString(msg))
00085 {
00086 }
00088 CIMException::~CIMException() throw()
00089 {
00090    if (m_description)
00091    {
00092       delete[] m_description;
00093    }
00094 }
00096 CIMException::CIMException(const CIMException& x)
00097    : Exception(x)
00098    , m_description(Exception::dupString(x.m_description))
00099 {
00100 }
00102 CIMException&
00103 CIMException::operator=(const CIMException& x)
00104 {
00105    CIMException(x).swap(*this);
00106    return *this;
00107 }
00109 void
00110 CIMException::swap(CIMException& x)
00111 {
00112    std::swap(m_description, x.m_description);
00113    Exception::swap(x);
00114 }
00116 const char*
00117 CIMException::type() const
00118 {
00119    return "CIMException";
00120 }
00121 
00123 const char*
00124 CIMException::getDescription() const
00125 {
00126    return m_description;
00127 }
00128 
00130 struct MsgRec
00131 {
00132    CIMException::ErrNoType errval;
00133    const char* msg;
00134 };
00135 
00136 static MsgRec _pmsgs[] =
00137 {
00138    { CIMException::SUCCESS, "no error" },
00139    { CIMException::FAILED, "general error" },
00140    { CIMException::ACCESS_DENIED, "Access to CIM resource unavailable to client" },
00141    { CIMException::INVALID_NAMESPACE, "namespace does not exist" },
00142    { CIMException::INVALID_PARAMETER, "invalid parameter passed to method" },
00143    { CIMException::INVALID_CLASS, "class does not exist" },
00144    { CIMException::NOT_FOUND, "requested object could not be found" },
00145    { CIMException::NOT_SUPPORTED, "requested operation is not supported" },
00146    { CIMException::CLASS_HAS_CHILDREN, "operation cannot be done on class with subclasses" },
00147    { CIMException::CLASS_HAS_INSTANCES, "operator cannot be done on class with instances" },
00148    { CIMException::INVALID_SUPERCLASS, "specified superclass does not exist" },
00149    { CIMException::ALREADY_EXISTS, "object already exists" },
00150    { CIMException::NO_SUCH_PROPERTY, "specified property does not exist" },
00151    { CIMException::TYPE_MISMATCH, "value supplied is incompatible with type" },
00152    { CIMException::QUERY_LANGUAGE_NOT_SUPPORTED, "query language is not recognized or supported" },
00153    { CIMException::INVALID_QUERY, "query is not valid for the specified query language" },
00154    { CIMException::METHOD_NOT_AVAILABLE, "extrinsic method could not be executed" },
00155    { CIMException::METHOD_NOT_FOUND, "extrinsic method does not exist" }
00156 };
00157 
00158 // static
00159 const char*
00160 CIMException::getCodeDescription(ErrNoType errCode)
00161 {
00162    if (errCode >= SUCCESS && errCode <= METHOD_NOT_FOUND)
00163    {
00164       OW_ASSERT(_pmsgs[errCode].errval == errCode);
00165       return _pmsgs[errCode].msg;
00166    }
00167    return "unknown error";
00168 }
00169 
00170 CIMException*
00171 CIMException::clone() const throw()
00172 {
00173    return new(std::nothrow) CIMException(*this);
00174 }
00175 
00176 } // end namespace OW_NAMESPACE
00177 

Generated on Thu Feb 9 08:47:53 2006 for openwbem by  doxygen 1.4.6