org/objectweb/proactive/core/exceptions/manager/ExceptionMaskLevel.java

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.util.Arrays;
00034 import java.util.Collection;
00035 import java.util.Iterator;
00036 import java.util.LinkedList;
00037 
00038 import org.objectweb.proactive.core.body.future.FutureProxy;
00039 import org.objectweb.proactive.core.body.future.FutureResult;
00040 import org.objectweb.proactive.core.exceptions.NonFunctionalException;
00041 
00042 
00043 public class ExceptionMaskLevel {
00044 
00045     /* Exception types in the catch blocks */
00046     private Collection caughtExceptionTypes;
00047 
00048     /* Actual caught exceptions in this level */
00049     private Collection caughtExceptions;
00050 
00051     /* Pending futures */
00052     private int nbFutures;
00053 
00054     /* The stack this level belongs to */
00055     private ExceptionMaskStack parent;
00056 
00057     /* Do we catch a subtype of RuntimeException */
00058     private boolean catchRuntimeException;
00059 
00060     /* Do we catch a Non Functional Exception */
00061     /* TODO: private boolean catchNFE; */
00062     ExceptionMaskLevel(ExceptionMaskStack parent, Class[] exceptions) {
00063         for (int i = 0; i < exceptions.length; i++) {
00064             if (!Throwable.class.isAssignableFrom(exceptions[i])) {
00065                 throw new IllegalArgumentException(
00066                     "Only exceptions can be catched");
00067             }
00068 
00069             catchRuntimeException = catchRuntimeException ||
00070                 RuntimeException.class.isAssignableFrom(exceptions[i]) ||
00071                 exceptions[i].isAssignableFrom(RuntimeException.class);
00072         }
00073 
00074         if (exceptions.length < 1) {
00075             throw new IllegalArgumentException(
00076                 "At least one exception must be catched");
00077         }
00078 
00079         caughtExceptionTypes = Arrays.asList(exceptions);
00080         caughtExceptions = new LinkedList();
00081         nbFutures = 0;
00082         this.parent = parent;
00083     }
00084 
00085     /* Empty constructor for ExceptionHandler */
00086     ExceptionMaskLevel() {
00087         caughtExceptionTypes = new LinkedList();
00088     }
00089 
00090     boolean isExceptionTypeCaught(Class c) {
00091         Iterator iter = caughtExceptionTypes.iterator();
00092         while (iter.hasNext()) {
00093             Class cc = (Class) iter.next();
00094             if (cc.isAssignableFrom(c) || c.isAssignableFrom(cc)) {
00095                 return true;
00096             }
00097         }
00098 
00099         return false;
00100     }
00101 
00102     /* We do an OR */
00103     boolean areExceptionTypesCaught(Class[] exceptions) {
00104         if (caughtExceptionTypes.isEmpty()) {
00105             return false;
00106         }
00107 
00108         for (int i = 0; i < exceptions.length; i++) {
00109             if (isExceptionTypeCaught(exceptions[i])) {
00110                 return true;
00111             }
00112         }
00113 
00114         return false;
00115     }
00116 
00117     void addExceptionTypes(ExceptionMaskLevel level) {
00118         Iterator iter = level.caughtExceptionTypes.iterator();
00119         while (iter.hasNext()) {
00120             Class c = (Class) iter.next();
00121             if (!isExceptionTypeCaught(c)) {
00122                 caughtExceptionTypes.add(c);
00123             }
00124         }
00125 
00126         catchRuntimeException = catchRuntimeException ||
00127             level.catchRuntimeException();
00128     }
00129 
00130     synchronized void waitForPotentialException() {
00131         parent.throwArrivedException();
00132         while (nbFutures != 0) {
00133             try {
00134                 wait();
00135             } catch (InterruptedException ie) {
00136                 ie.printStackTrace();
00137                 break;
00138             }
00139             parent.throwArrivedException();
00140         }
00141     }
00142 
00143     boolean catchRuntimeException() {
00144         return catchRuntimeException;
00145     }
00146 
00147     /* A call is launched */
00148     synchronized void addFuture(FutureProxy f) {
00149         if (f != null) {
00150             f.setExceptionLevel(this);
00151             nbFutures++;
00152         }
00153     }
00154 
00155     /* A future has returned */
00156     synchronized void removeFuture(FutureProxy f) {
00157         nbFutures--;
00158         FutureResult res = f.getFutureResult();
00159 
00160         if (res != null) {
00161             NonFunctionalException nfe = res.getNFE();
00162             if ((nfe != null) && isExceptionTypeCaught(nfe.getClass())) {
00163                 synchronized (caughtExceptions) {
00164                     caughtExceptions.add(nfe);
00165                 }
00166             }
00167 
00168             Throwable exception = f.getFutureResult().getExceptionToRaise();
00169             if (exception != null) {
00170                 synchronized (caughtExceptions) {
00171                     caughtExceptions.add(exception);
00172                 }
00173             }
00174         }
00175 
00176         notifyAll();
00177     }
00178 
00179     Collection getCaughtExceptions() {
00180         return caughtExceptions;
00181     }
00182 
00183     synchronized Collection getAllExceptions() {
00184         while (nbFutures != 0) {
00185             try {
00186                 wait();
00187             } catch (InterruptedException ie) {
00188                 ie.printStackTrace();
00189                 break;
00190             }
00191         }
00192         return caughtExceptions;
00193     }
00194 }

Generated on Mon Jan 22 15:16:07 2007 for ProActive by  doxygen 1.5.1