00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
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     
00046     private Collection caughtExceptionTypes;
00047 
00048     
00049     private Collection caughtExceptions;
00050 
00051     
00052     private int nbFutures;
00053 
00054     
00055     private ExceptionMaskStack parent;
00056 
00057     
00058     private boolean catchRuntimeException;
00059 
00060     
00061     
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     
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     
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     
00148     synchronized void addFuture(FutureProxy f) {
00149         if (f != null) {
00150             f.setExceptionLevel(this);
00151             nbFutures++;
00152         }
00153     }
00154 
00155     
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 }