001: /**
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */package com.tc.util;
004:
005: import com.tc.logging.TCLogger;
006: import java.lang.reflect.Array;
007:
008: /**
009: * Generic utility methods.
010: */
011: public class Util {
012: private static final Error FATAL_ERROR = new Error(
013: "Fatal error -- Please refer to console output and Terracotta log files for more information");
014:
015: /**
016: * Enumerates the argument, provided that it is an array, in a nice, human-readable format.
017: *
018: * @param array
019: * @return
020: */
021: public static String enumerateArray(Object array) {
022: StringBuffer buf = new StringBuffer();
023: if (array != null) {
024: if (array.getClass().isArray()) {
025: for (int i = 0, n = Array.getLength(array); i < n; i++) {
026: if (i > 0) {
027: buf.append(", ");
028: }
029: buf.append("<<" + Array.get(array, i) + ">>");
030: }
031: } else {
032: buf.append("<").append(array.getClass()).append(
033: " is not an array>");
034: }
035: } else {
036: buf.append("null");
037: }
038: return buf.toString();
039: }
040:
041: public static void printLogAndRethrowError(Throwable t,
042: TCLogger logger) {
043: printLogAndMaybeRethrowError(t, true, logger);
044: }
045:
046: public static void printLogAndMaybeRethrowError(final Throwable t,
047: final boolean rethrow, final TCLogger logger) {
048: // if (t instanceof ReadOnlyException) { throw (ReadOnlyException) t; }
049: // if (t instanceof UnlockedSharedObjectException) { throw (UnlockedSharedObjectException) t; }
050: // if (t instanceof TCNonPortableObjectError) { throw (TCNonPortableObjectError) t; }
051:
052: try {
053: if (t != null)
054: t.printStackTrace();
055: logger.error(t);
056: } catch (Throwable err) {
057: try {
058: err.printStackTrace();
059: } catch (Throwable err2) {
060: // sorry, game over, stop trying
061: }
062: } finally {
063: if (rethrow) {
064: // don't wrap existing Runtime and Error
065: if (t instanceof RuntimeException) {
066: throw (RuntimeException) t;
067: }
068: if (t instanceof Error) {
069: throw (Error) t;
070: }
071:
072: // Try to new up a RuntimeException to throw
073: final RuntimeException re;
074: try {
075: re = new RuntimeException("Unexpected Error "
076: + t.getMessage(), t);
077: } catch (Throwable err3) {
078: try {
079: err3.printStackTrace();
080: } catch (Throwable err4) {
081: // sorry, game over, stop trying
082: }
083: throw FATAL_ERROR;
084: }
085:
086: throw re;
087: }
088: }
089: }
090:
091: public static void selfInterruptIfNeeded(boolean isInterrupted) {
092: if (isInterrupted) {
093: Thread.currentThread().interrupt();
094: }
095: }
096:
097: public static long getMillis(long timeInNanos) {
098: return timeInNanos / 1000000;
099: }
100:
101: public static int getNanos(long timeInNanos, long mills) {
102: return (int) (timeInNanos - mills * 1000000);
103: }
104:
105: public static long getTimeInNanos(long mills, int nanos) {
106: return mills * 1000000 + nanos;
107: }
108:
109: public static int hash(Object key, int limit) {
110: if (limit == 1) {
111: return 0;
112: }
113: int hashValue = hash(key.hashCode());
114: if (hashValue == Integer.MIN_VALUE) {
115: hashValue -= 1;
116: }
117: hashValue = Math.abs(hashValue);
118: return hashValue % limit;
119: }
120:
121: private static int hash(int h) {
122: h += ~(h << 9);
123: h ^= (h >>> 14);
124: h += (h << 4);
125: h ^= (h >>> 10);
126: return h;
127: }
128: }
|