00001 /* 00002 * ################################################################ 00003 * 00004 * ProActive: The Java(TM) library for Parallel, Distributed, 00005 * Concurrent computing with Security and Mobility 00006 * 00007 * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis 00008 * Contact: proactive@objectweb.org 00009 * 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 * USA 00024 * 00025 * Initial developer(s): The ProActive Team 00026 * http://www.inria.fr/oasis/ProActive/contacts.html 00027 * Contributor(s): 00028 * 00029 * ################################################################ 00030 */ 00031 package org.objectweb.proactive.core.exceptions.manager; 00032 00033 import java.lang.reflect.Method; 00034 import java.util.Collection; 00035 00036 import org.objectweb.proactive.core.body.future.FutureProxy; 00037 import org.objectweb.proactive.core.mop.MethodCall; 00038 import org.objectweb.proactive.core.mop.MethodCallExceptionContext; 00039 00040 00041 public class ExceptionHandler { 00042 00043 /* Called by the user */ 00044 public static void tryWithCatch(Class[] exceptions) { 00045 ExceptionMaskStack stack = ExceptionMaskStack.get(); 00046 synchronized (stack) { 00047 stack.waitForIntersection(exceptions); 00048 stack.push(exceptions); 00049 } 00050 } 00051 00052 public static void throwArrivedException() { 00053 ExceptionMaskStack stack = ExceptionMaskStack.get(); 00054 synchronized (stack) { 00055 stack.throwArrivedException(); 00056 } 00057 } 00058 00059 public static void waitForPotentialException() { 00060 ExceptionMaskStack stack = ExceptionMaskStack.get(); 00061 synchronized (stack) { 00062 stack.waitForPotentialException(true); 00063 } 00064 } 00065 00066 public static void endTryWithCatch() { 00067 ExceptionMaskStack stack = ExceptionMaskStack.get(); 00068 synchronized (stack) { 00069 stack.waitForPotentialException(false); 00070 } 00071 } 00072 00073 public static void removeTryWithCatch() { 00074 ExceptionMaskStack stack = ExceptionMaskStack.get(); 00075 synchronized (stack) { 00076 stack.pop(); 00077 } 00078 } 00079 00080 public static Collection getAllExceptions() { 00081 ExceptionMaskStack stack = ExceptionMaskStack.get(); 00082 synchronized (stack) { 00083 return stack.getAllExceptions(); 00084 } 00085 } 00086 00087 /* Called by ProActive on the client side */ 00088 public static void addRequest(MethodCall methodCall, FutureProxy future) { 00089 MethodCallExceptionContext context = methodCall.getExceptionContext(); 00090 if (context.isExceptionAsynchronously() || 00091 context.isRuntimeExceptionHandled()) { 00092 ExceptionMaskStack stack = ExceptionMaskStack.get(); 00093 synchronized (stack) { 00094 Method m = methodCall.getReifiedMethod(); 00095 ExceptionMaskLevel level = stack.findBestLevel(m.getExceptionTypes()); 00096 level.addFuture(future); 00097 } 00098 } 00099 } 00100 00101 public static void addResult(FutureProxy future) { 00102 ExceptionMaskLevel level = future.getExceptionLevel(); 00103 if (level != null) { 00104 level.removeFuture(future); 00105 } 00106 } 00107 00108 public static MethodCallExceptionContext getContextForCall(Method m) { 00109 ExceptionMaskStack stack = ExceptionMaskStack.get(); 00110 synchronized (stack) { 00111 boolean runtime = stack.isRuntimeExceptionHandled(); 00112 boolean async = stack.areExceptionTypesCaught(m.getExceptionTypes()); 00113 MethodCallExceptionContext res = new MethodCallExceptionContext(runtime, async); 00114 00115 // System.out.println(m + " => " + res); 00116 return res; 00117 } 00118 } 00119 00120 public static void throwException(Throwable exception) { 00121 if (exception instanceof RuntimeException) { 00122 throw (RuntimeException) exception; 00123 } 00124 00125 if (exception instanceof Error) { 00126 00127 /* Maybe an Error should result in an NFE */ 00128 throw (Error) exception; 00129 } 00130 00131 ExceptionMaskStack stack = ExceptionMaskStack.get(); 00132 synchronized (stack) { 00133 if (!stack.isExceptionTypeCaught(exception.getClass())) { 00134 RuntimeException re = new IllegalStateException( 00135 "Invalid Future Usage"); 00136 re.initCause(exception); 00137 throw re; 00138 } 00139 00140 ExceptionThrower.throwException(exception); 00141 } 00142 } 00143 }