KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > Util


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import com.tc.logging.TCLogger;
7 import java.lang.reflect.Array JavaDoc;
8
9 /**
10  * Generic utility methods.
11  */

12 public class Util {
13   private static final Error JavaDoc FATAL_ERROR = new Error JavaDoc(
14   "Fatal error -- Please refer to console output and Terracotta log files for more information");
15
16   /**
17    * Enumerates the argument, provided that it is an array, in a nice, human-readable format.
18    *
19    * @param array
20    * @return
21    */

22   public static String JavaDoc enumerateArray(Object JavaDoc array) {
23     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
24     if (array != null) {
25       if (array.getClass().isArray()) {
26         for (int i = 0, n = Array.getLength(array); i < n; i++) {
27           if (i > 0) {
28             buf.append(", ");
29           }
30           buf.append("<<" + Array.get(array, i) + ">>");
31         }
32       } else {
33         buf.append("<").append(array.getClass()).append(" is not an array>");
34       }
35     } else {
36       buf.append("null");
37     }
38     return buf.toString();
39   }
40
41   public static void printLogAndRethrowError(Throwable JavaDoc t, TCLogger logger) {
42     printLogAndMaybeRethrowError(t, true, logger);
43   }
44
45   public static void printLogAndMaybeRethrowError(final Throwable JavaDoc t, final boolean rethrow, final TCLogger logger) {
46     // if (t instanceof ReadOnlyException) { throw (ReadOnlyException) t; }
47
// if (t instanceof UnlockedSharedObjectException) { throw (UnlockedSharedObjectException) t; }
48
// if (t instanceof TCNonPortableObjectError) { throw (TCNonPortableObjectError) t; }
49

50     try {
51       if (t != null) t.printStackTrace();
52       logger.error(t);
53     } catch (Throwable JavaDoc err) {
54       try {
55         err.printStackTrace();
56       } catch (Throwable JavaDoc err2) {
57         // sorry, game over, stop trying
58
}
59     } finally {
60       if (rethrow) {
61         // don't wrap existing Runtime and Error
62
if (t instanceof RuntimeException JavaDoc) { throw (RuntimeException JavaDoc) t; }
63         if (t instanceof Error JavaDoc) { throw (Error JavaDoc) t; }
64
65         // Try to new up a RuntimeException to throw
66
final RuntimeException JavaDoc re;
67         try {
68           re = new RuntimeException JavaDoc("Unexpected Error " + t.getMessage(), t);
69         } catch (Throwable JavaDoc err3) {
70           try {
71             err3.printStackTrace();
72           } catch (Throwable JavaDoc err4) {
73             // sorry, game over, stop trying
74
}
75           throw FATAL_ERROR;
76         }
77
78         throw re;
79       }
80     }
81   }
82
83   public static void selfInterruptIfNeeded(boolean interruptFlag) {
84     if (interruptFlag) {
85       Thread.currentThread().interrupt();
86     }
87   }
88 }
Popular Tags