ALMA Computing Group

acsexmplErrorComponentImpl.cpp

Go to the documentation of this file.
00001 /******************************************************************************* 00002 * ALMA - Atacama Large Millimiter Array 00003 * (c) Associated Universities Inc., 2002 * 00004 * (c) European Southern Observatory, 2002 00005 * Copyright by ESO (in the framework of the ALMA collaboration) 00006 * and Cosylab 2002, All rights reserved 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 * 00022 * 00023 * 00024 * "@(#) $Id: acsexmplErrorComponentImpl.cpp,v 1.12 2008/10/09 08:41:11 cparedes Exp $" 00025 * 00026 * who when what 00027 * -------- -------- ---------------------------------------------- 00028 * david 2002-09-26 added many more comments 00029 * david 25/09/02 created 00030 */ 00031 00032 #include <acsexmplErrorComponentImpl.h> 00033 #include <ACSErrTypeCommon.h> 00034 #include <ACSErrTypeOK.h> 00035 #include <iostream> 00036 00037 ACE_RCSID(acsexmpl, acsexmplErrorComponentImpl, "$Id: acsexmplErrorComponentImpl.cpp,v 1.12 2008/10/09 08:41:11 cparedes Exp $") 00038 00039 /* ----------------------------------------------------------------*/ 00040 ErrorComponent::ErrorComponent( 00041 const ACE_CString &name, 00042 maci::ContainerServices * containerServices) : 00043 ACSComponentImpl(name, containerServices) 00044 { 00045 // ACS_TRACE is used for debugging purposes 00046 ACS_TRACE("::ErrorComponent::ErrorComponent"); 00047 } 00048 /* ----------------------------------------------------------------*/ 00049 ErrorComponent::~ErrorComponent() 00050 { 00051 // ACS_TRACE is used for debugging purposes 00052 ACS_TRACE("::ErrorComponent::~ErrorComponent"); 00053 ACS_DEBUG_PARAM("::ErrorComponent::~ErrorComponent", "Destroying %s...", name()); 00054 } 00055 /* --------------------- [ CORBA interface ] ----------------------*/ 00056 void 00057 ErrorComponent::displayMessage () 00058 { 00059 ACS_LOG(LM_RUNTIME_CONTEXT, "ErrorComponent::displayMessage", 00060 (LM_INFO, "Hello World")); 00061 } 00062 /* ----------------------------------------------------------------*/ 00063 void 00064 ErrorComponent::badMethod(CORBA::Short depth) 00065 { 00066 ACS_TRACE("::ErrorComponent::badMethod"); 00067 if(depth>=1){ 00068 try 00069 { 00070 // We decrement the depth, because we are going to add one 00071 // error here in any case. 00072 buildErrorTrace(depth-1); 00073 } 00074 catch(ACSErrTypeCommon::GenericErrorExImpl &ex) 00075 { 00076 ACSErrTypeCommon::GenericErrorExImpl ex2(ex, 00077 __FILE__, __LINE__, 00078 "ErrorComponent::badMethod"); 00079 ex2.setErrorDesc("Generated multi level exception"); 00080 throw ex2.getGenericErrorEx(); 00081 } 00082 catch(...) 00083 { 00084 ACSErrTypeCommon::UnexpectedExceptionExImpl ex2(__FILE__, __LINE__, 00085 "ErrorComponent::badMethod"); 00086 throw ex2.getUnexpectedExceptionEx(); 00087 } 00088 00089 /* 00090 * We should get here only if a depth<=1 was requested. 00091 */ 00092 ACSErrTypeCommon::GenericErrorExImpl ex(__FILE__, __LINE__, 00093 "ErrorComponent::badMethod"); 00094 ex.setErrorDesc("An error trace with depth lower or equal to 1 was requested."); 00095 throw ex.getGenericErrorEx(); 00096 } 00097 } 00098 00099 00100 void ErrorComponent::typeException(CORBA::Short depth) 00101 { 00102 ACS_TRACE("::ErrorComponent::typeException"); 00103 if(depth>=1){ 00104 try 00105 { 00106 // We decrement the depth, because we are going to add one 00107 // error here in any case. 00108 buildErrorTrace(depth-1); 00109 } 00110 catch(ACSErrTypeCommon::GenericErrorExImpl &ex) 00111 { 00112 ACSErrTypeCommon::GenericErrorExImpl ex2(ex, 00113 __FILE__, __LINE__, 00114 "ErrorComponent::badMethod"); 00115 ex2.setErrorDesc("Generated multi level exception"); 00116 throw ex2.getACSErrTypeCommonEx(); 00117 } 00118 catch(...) 00119 { 00120 ACSErrTypeCommon::UnexpectedExceptionExImpl ex2(__FILE__, __LINE__, 00121 "ErrorComponent::badMethod"); 00122 throw ex2.getUnexpectedExceptionEx(); 00123 } 00124 00125 /* 00126 * We should get here only if a depth<=1 was requested. 00127 */ 00128 ACSErrTypeCommon::GenericErrorExImpl ex(__FILE__, __LINE__, 00129 "ErrorComponent::badMethod"); 00130 ex.setErrorDesc("An error trace with depth lower or equal to 1 was requested."); 00131 throw ex.getACSErrTypeCommonEx(); 00132 } 00133 }//typeException 00134 00135 00136 void ErrorComponent::corbaSystemException() 00137 { 00138 throw CORBA::BAD_PARAM ( /* CORBA::OMGVMCID | */ 2, CORBA::COMPLETED_NO);; 00139 } 00140 00141 /* ----------------------------------------------------------------*/ 00142 ACSErr::Completion *ErrorComponent::completionFromException(CORBA::Short depth) 00143 { 00144 ACS_TRACE("::ErrorComponent::completionFromException"); 00145 // here we get LOCAL (C++) completion 00146 CompletionImpl *er = createCompletion(depth); 00147 00148 // returnCompletion() automatically deletes er 00149 // NOTE: you can use returnCompletion() 00150 // just if completion is allocated on the heap. 00151 // If completion is allocated on the stack 00152 // returnCompletion(false) has to be used. 00153 return er->returnCompletion(); 00154 }//completionFromException 00155 00156 ACSErr::Completion *ErrorComponent::completionOnStack(CORBA::Short depth) 00157 { 00158 ACS_TRACE("::ErrorComponent::completionOnStack"); 00159 00160 // here we get LOCAL (C++) completion 00161 CompletionImpl *comp = createCompletion(depth); 00162 00163 00164 // if comp does not contain error (=is error free) we return it 00165 // otherwise we create a new completion which takes the error trace from a completion comp. 00166 if (comp->isErrorFree()) 00167 { 00168 // memory for comp is released in the call 00169 return comp->returnCompletion(); 00170 } 00171 else 00172 { 00173 // The constructor takes care for the memory manamgent 00174 // for the passed completion or exception. 00175 // If a completion or an exception is passed as pointer the constructor assumes that 00176 // completion was created on the heap and thus it deletes it afterwards, 00177 // so it MUST NOT be deleted by the user ! 00178 // If a completion or an exception is passed as an object (or reference to it) 00179 // the constructor assumes that the it was created on the stack 00180 // and thus it does not delete it. 00181 // 00182 // NOTE: do not pass a pointer of a completion or an exception 00183 // which was created on the stack !! In this case just send the completion object. 00184 ACSErrTypeCommon::GenericErrorCompletion erg(comp, 00185 __FILE__, __LINE__, 00186 "ErrorComponent::completionOnStack"); 00187 erg.setErrorDesc("Put an error trace in completionOnStack"); 00188 return erg.returnCompletion(false); 00189 // With false flag we tell returnCompletion() 00190 // not to delete its object 00191 // since it is automatically deleted when we go out of scope 00192 }//if 00193 }//completionOnStack 00194 00195 void ErrorComponent::exceptionFromCompletion(CORBA::Short depth) 00196 { 00197 ACS_TRACE("ErrorComponent::exceptionFromCompletion"); 00198 if(depth==1){ 00199 ACSErrTypeCommon::GenericErrorExImpl ex2( 00200 __FILE__, __LINE__, 00201 "ErrorComponent::exceptionFromCompletion"); 00202 ex2.setErrorDesc("Exception with trace of depth 1 (not generated from a completion)"); 00203 throw ex2.getGenericErrorEx(); 00204 } 00205 00206 00207 CompletionImpl *comp = createCompletion(depth>0?depth-1:0); 00208 ACS_DEBUG("ErrorComponent::exceptionFromCompletion","first step"); 00209 // if comp does not conatin error (=is error free) we just return 00210 // otherwise we create a new exception which takes the error trace from a completion comp. 00211 if (!comp->isErrorFree()) 00212 { 00213 ACS_DEBUG("ErrorComponent::exceptionFromCompletion","second step"); 00214 00215 // The constructor takes care for the memory manamgent 00216 // for the passed completion or exception. 00217 // If a completion or an exception is passed as pointer the constructor assumes that 00218 // completion was created on the heap and thus it deletes it afterwards, 00219 // so it MUST NOT be deleted by the user ! 00220 // If a completion or an exception is passed as an object (or reference to it) 00221 // the constructor assumes that the it was created on the stack 00222 // and thus it does not delete it. 00223 // 00224 // NOTE: do not pass a pointer of a completion or an exception 00225 // which was created on the stack !! In this case just send the completion object. 00226 ACSErrTypeCommon::GenericErrorExImpl ex2(comp, 00227 __FILE__, __LINE__, 00228 "ErrorComponent::exceptionFromCompletion"); 00229 ex2.setErrorDesc("Exception generated by adding an error trace from a completion"); 00230 throw ex2.getGenericErrorEx(); 00231 }//if 00232 } 00233 00234 ACSErr::Completion *ErrorComponent::completionFromCompletion(CORBA::Short depth) 00235 { 00236 ACS_TRACE("ErrorComponent::completionFromCompletion"); 00237 CompletionImpl *comp; 00238 if(depth==1){ 00239 ACSErrTypeCommon::GenericErrorCompletion *erg = 00240 new ACSErrTypeCommon::GenericErrorCompletion( 00241 __FILE__, __LINE__, 00242 "ErrorComponent::completionFromCompletion"); 00243 erg->setErrorDesc("Put an error trace in completionFromCompletion(depth 1, so not generated from another completion)"); 00244 comp = erg; 00245 00246 00247 } 00248 else{ 00249 comp = createCompletion(depth>0?depth-1:0); 00250 00251 // if comp does not conatin error (=is error free) we return it 00252 // otherwise we create a new completion which takes the error trace from a completion comp. 00253 if (!comp->isErrorFree()) 00254 { 00255 // comp is deleted inside the constructor 00256 ACSErrTypeCommon::GenericErrorCompletion *erg = 00257 new ACSErrTypeCommon::GenericErrorCompletion(comp, 00258 __FILE__, __LINE__, 00259 "ErrorComponent::completionFromCompletion"); 00260 erg->setErrorDesc("Put an error trace in completionFromCompletion"); 00261 comp = erg; 00262 }//if 00263 } 00264 return comp->returnCompletion(); 00265 }//completionFromCompletion 00266 00267 void 00268 ErrorComponent::outCompletion(ACSErr::Completion_out comp) 00269 { 00270 ACS_TRACE("ErrorComponent::outCompletion"); 00271 ACSErrTypeOK::ACSErrOKCompletion c; 00272 comp = c.returnCompletion(false); 00273 00274 //comp=new ACSErr::Completion(); 00275 //comp=new ACSErrTypeCommon::GenericErrorCompletion(__FILE__, __LINE__,"ErrorComponent::outCompletion"); 00276 00277 } 00278 CompletionImpl *ErrorComponent::createCompletion(unsigned short depth) 00279 { 00280 ACS_TRACE("::ErrorComponent::createCompletion"); 00281 CompletionImpl *er; 00282 00283 if( depth <= 0 ) 00284 { 00285 er = new ACSErrTypeOK::ACSErrOKCompletion(); 00286 } 00287 else 00288 { 00289 try 00290 { 00291 buildErrorTrace(depth-1); 00292 ACSErrTypeCommon::GenericErrorCompletion *erg = 00293 new ACSErrTypeCommon::GenericErrorCompletion( 00294 __FILE__, __LINE__, 00295 "ErrorComponent::createCompletion"); 00296 erg->setErrorDesc("Building a Completion with an errorTrace"); 00297 er = erg; 00298 } 00299 catch(ACSErrTypeCommon::GenericErrorExImpl &ex) 00300 { 00301 // Here we build a completion from an exception: 00302 // we create a new completion where it is added the error trace from an exception. 00303 ACSErrTypeCommon::GenericErrorCompletion *erg = 00304 new ACSErrTypeCommon::GenericErrorCompletion(ex, 00305 __FILE__, __LINE__, 00306 "ErrorComponent::createCompletion"); 00307 erg->setErrorDesc("Building a Completion with a previous errorTrace"); 00308 er = erg; 00309 } 00310 catch(...) 00311 { 00312 ACSErrTypeCommon::UnexpectedExceptionCompletion *erg = 00313 new ACSErrTypeCommon::UnexpectedExceptionCompletion( 00314 __FILE__, __LINE__, 00315 "ErrorComponent::returncompletion"); 00316 er=erg; 00317 } 00318 } 00319 00320 // we are in local case so caller has to do the memory managment 00321 // i.e. release the memory of CompletionImpl ! 00322 return er; 00323 }//createCompletion 00324 00325 00326 /************ 00327 * Utility method to build a deep ErrorTrace 00328 ************/ 00329 void 00330 ErrorComponent::buildErrorTrace(unsigned short depth) 00331 { 00332 ACS_TRACE("::ErrorComponent::buildErrorTrace"); 00333 00334 /* 00335 * If depth is 1, we are at the bottom and 00336 * we just have to throw an exception. 00337 * Going up the recursive chain this will be 00338 * atteched to all other exceptions 00339 */ 00340 if(depth == 1) 00341 { 00342 ACSErrTypeCommon::GenericErrorExImpl ex(__FILE__, __LINE__, 00343 "ErrorComponent::buildErrorTrace"); 00344 ex.setErrorDesc("Bottom of error trace"); 00345 throw ex; 00346 } 00347 00348 /* 00349 * If depth > 1, make a recursive call. 00350 * We will have to get back an exception with a trace with 00351 * a depth shorter by 1 element. 00352 */ 00353 if(depth > 1) 00354 { 00355 try 00356 { 00357 buildErrorTrace(depth-1); 00358 } 00359 catch(ACSErrTypeCommon::GenericErrorExImpl &ex) 00360 { 00361 ACSErrTypeCommon::GenericErrorExImpl ex2(ex, 00362 __FILE__, __LINE__, 00363 "ErrorComponent::errorTrace"); 00364 00365 ex2.setErrorDesc("Generated multi level exception level"); 00366 throw ex2; 00367 } 00368 catch(...) // This should never happen!!!! 00369 { 00370 ACSErrTypeCommon::UnexpectedExceptionExImpl ex2(__FILE__, __LINE__, 00371 "ErrorComponent::errorTrace"); 00372 throw ex2.getUnexpectedExceptionEx(); 00373 } 00374 } 00375 /* 00376 * We should get here only if depth <= 0, 00377 * I.e. if there is not exception to throw. 00378 */ 00379 return; 00380 } 00381 00382 00383 /* --------------- [ MACI DLL support functions ] -----------------*/ 00384 #include <maciACSComponentDefines.h> 00385 MACI_DLL_SUPPORT_FUNCTIONS(ErrorComponent) 00386 /* ----------------------------------------------------------------*/ 00387 00388 00389 /*___oOo___*/ 00390 00391 00392