ALMA Computing Group

ErrorComponentImpl.java

Go to the documentation of this file.
00001 /* 00002 * ALMA - Atacama Large Millimiter Array 00003 * (c) European Southern Observatory, 2005 00004 * Copyright by ESO (in the framework of the ALMA collaboration), 00005 * All rights reserved 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00020 * MA 02111-1307 USA 00021 */ 00022 00023 package alma.acsexmplErrorComponent.ErrorComponentImpl; 00024 00025 import alma.ACSErr.Completion; 00026 import alma.ACSErrTypeCommon.ACSErrTypeCommonEx; 00027 import alma.ACSErrTypeCommon.GenericErrorEx; 00028 import alma.ACSErrTypeCommon.wrappers.AcsJACSErrTypeCommonEx; 00029 import alma.ACSErrTypeCommon.wrappers.AcsJGenericErrorEx; 00030 import alma.ACSErrTypeCommon.wrappers.GenericErrorAcsJCompletion; 00031 import alma.ACSErrTypeOK.wrappers.ACSErrOKAcsJCompletion; 00032 import alma.acs.component.ComponentImplBase; 00033 import alma.acs.exceptions.AcsJCompletion; 00034 import alma.acs.exceptions.AcsJException; 00035 import alma.acsexmplErrorComponent.ErrorComponentOperations; 00036 00037 import org.omg.CORBA.SystemException; 00038 00048 public class ErrorComponentImpl extends ComponentImplBase implements ErrorComponentOperations { 00049 00050 public void displayMessage() { 00051 m_logger.info("ErrorComponent"); 00052 } 00053 00054 00064 public void badMethod(short depth) throws GenericErrorEx { 00065 try { 00066 // our internal method uses the convenient "AcsJ-" JDK-style variant of GenericErrorEx 00067 internalBadMethod(depth); 00068 } catch (AcsJGenericErrorEx e) { 00069 // when leaving the Java component boundary, we must convert the JDK-style-exception to its plain CORBA/ACS-style peer. 00070 throw e.toGenericErrorEx(); 00071 } catch (Throwable thr) { 00072 throw new AcsJGenericErrorEx("Got unexpected exception", thr).toGenericErrorEx(); 00073 } 00074 } 00075 00076 00082 public void corbaSystemException() throws org.omg.CORBA.SystemException { 00083 throw new org.omg.CORBA.BAD_PARAM("Test throwing a CORBA Exception"); 00084 } 00085 00086 00093 public void exceptionFromCompletion(short depth) throws GenericErrorEx { 00094 try { 00095 if (depth == 1) { 00096 // this case is ugly if we really want to fulfill the contract and throw a single exception (i.e. error trace of length one). 00097 // It seems that for an example component we should work without a 'depth' parameter, and rather have two different methods instead of this highly powerful one method. 00098 AcsJCompletion completion = internalCompletionMethod(depth); 00099 // here we cannot wrap the contained exception, because then the total error trace would be of depth == 2. 00100 // Therefore we must check for the correct exception type, and use that exception directly. 00101 if (completion.getAcsJException() instanceof AcsJGenericErrorEx) { 00102 AcsJGenericErrorEx knownEx = (AcsJGenericErrorEx) completion.getAcsJException(); 00103 throw knownEx.toGenericErrorEx(); 00104 } 00105 else { 00106 // this should never happen 00107 throw new AcsJGenericErrorEx("Unexpected component implementation bug. AcsJGenericErrorEx was expected in Completion object.").toGenericErrorEx(); 00108 } 00109 } 00110 else { 00111 // with depth != 1 we can construct a completion with (depth-1) exceptions, and wrap those with a new exception if they exist 00112 AcsJCompletion completion = internalCompletionMethod(depth - 1); 00113 // completion should have an error unless depth was 0 00114 if (completion.isError()) { 00115 AcsJException acsjEx = completion.getAcsJException(); 00116 throw (new AcsJGenericErrorEx(acsjEx)).toGenericErrorEx(); 00117 } 00118 } 00119 } catch (GenericErrorEx ex) { 00120 throw ex; // that's our checked exception 00121 } catch (Throwable thr) { 00122 // that's an unchecked exception which we must wrap 00123 throw new AcsJGenericErrorEx("Got unexpected exception", thr).toGenericErrorEx(); 00124 } 00125 } 00126 00127 00128 public void typeException(short depth) throws ACSErrTypeCommonEx { //, GenericErrorEx { 00129 try { 00130 internalBadMethod(depth); 00131 } catch (AcsJACSErrTypeCommonEx ex) { 00132 // here we made use of the inheritance hierarchy in the AcsJ-style exceptions, 00133 // and caught the flying AcsJGenericErrorEx through its base type AcsJACSErrTypeCommonEx 00134 throw ex.toACSErrTypeCommonEx(); 00135 } catch (Throwable thr) { 00136 throw new AcsJGenericErrorEx("Got unexpected exception", thr).toACSErrTypeCommonEx(); 00137 } 00138 } 00139 00140 00145 public Completion completionFromException(short depth) { 00146 // this completion will be built from a chain of exceptions if depth > 0 00147 AcsJCompletion completion = internalCompletionMethod(depth); 00148 00149 return completion.toCorbaCompletion(); 00150 } 00151 00152 00157 public Completion completionFromCompletion(short depth) { 00158 AcsJCompletion completion=null; 00159 if(depth<=1){ 00160 completion = internalCompletionMethod(depth); 00161 return completion.toCorbaCompletion(); 00162 }else{ 00163 completion = internalCompletionMethod(depth-1); 00164 // here we show how to wrap the error from a given completion with a new completion 00165 AcsJCompletion newCompletion = new GenericErrorAcsJCompletion(completion); 00166 return newCompletion.toCorbaCompletion(); 00167 } 00168 } 00169 00170 00176 public Completion completionOnStack(short depth) { 00177 return completionFromException(depth); 00178 } 00179 00184 public void outCompletion(alma.ACSErr.CompletionHolder comp){ 00185 00186 comp.value=(new ACSErrOKAcsJCompletion()).toCorbaCompletion(); 00187 } 00189 00200 private void internalBadMethod(int depth) throws AcsJGenericErrorEx { 00201 if (depth < 1) { 00202 return; 00203 } 00204 00205 if (depth == 1) { 00206 throw new AcsJGenericErrorEx("This exception is the original cause."); 00207 } 00208 00209 try { 00210 // recursion 00211 internalBadMethod(depth - 1); 00212 } 00213 catch (AcsJGenericErrorEx ex) { 00214 // we use the causing exception to build up the JDK-style chain of exceptions. 00215 // That chain will later be converted into an ACS-style ErrorTrace. 00216 throw new AcsJGenericErrorEx("This exception was caused by another exception...", ex); 00217 } 00218 } 00219 00225 private AcsJCompletion internalCompletionMethod(int depth) { 00226 if (depth <= 0) { 00227 // a completion type that has no exception associated 00228 return new ACSErrOKAcsJCompletion(); 00229 } 00230 else if (depth == 1) { 00231 return new GenericErrorAcsJCompletion(); 00232 } 00233 else { 00234 try { 00235 internalBadMethod(depth - 1); 00236 } catch (AcsJGenericErrorEx ex) { 00237 // a completion which takes associated exceptions (and wraps them with a GenericError exception) 00238 return new GenericErrorAcsJCompletion(ex); 00239 } 00240 } 00241 // this should never happen, given that for depth>0 we always get an exception from internalBadMethod. 00242 // We throw an unchecked exception to give a real-world example for such a case, 00243 // because at the Java impl / CORBA boundary it is often not good enough to just consider checked exceptions. 00244 throw new RuntimeException("Program error: was never supposed to get here!"); 00245 } 00246 }