1 5 package com.tc.object.bytecode; 6 7 import com.tc.cluster.ClusterEventListener; 8 import com.tc.exception.TCClassNotFoundException; 9 import com.tc.logging.TCLogger; 10 import com.tc.management.beans.sessions.SessionMonitorMBean; 11 import com.tc.object.ObjectID; 12 import com.tc.object.TCObject; 13 import com.tc.object.bytecode.hook.impl.ArrayManager; 14 import com.tc.object.bytecode.hook.impl.ClassProcessorHelper; 15 import com.tc.object.lockmanager.api.LockLevel; 16 import com.tc.properties.TCProperties; 17 18 21 public class ManagerUtil { 22 23 public static final String CLASS = "com/tc/object/bytecode/ManagerUtil"; 24 public static final String TYPE = "L" + CLASS + ";"; 25 26 private static final Manager NULL_MANAGER = NullManager.getInstance(); 27 28 private static volatile boolean enabled = false; 29 30 public static void enable() { 31 enabled = true; 32 } 33 34 private static Manager getManager() { 35 if (!enabled) { return NULL_MANAGER; } 36 37 if (ClassProcessorHelper.USE_GLOBAL_CONTEXT) { 38 return GlobalManagerHolder.instance; 39 } else { 40 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 41 Manager rv = ClassProcessorHelper.getManager(loader); 42 if (rv == null) { return NULL_MANAGER; } 43 return rv; 44 } 45 } 46 47 public static TCLogger getLogger(String loggerName) { 48 return getManager().getLogger(loggerName); 49 } 50 51 public static boolean isPhysicallyInstrumented(Class clazz) { 52 return getManager().isPhysicallyInstrumented(clazz); 53 } 54 55 public static void optimisticBegin() { 56 getManager().optimisticBegin(); 57 } 58 59 public static void optimisticCommit() { 60 beginLock("test", LockLevel.WRITE); 61 try { 62 getManager().optimisticCommit(); 63 } catch (ClassNotFoundException e) { 64 throw new TCClassNotFoundException(e); 65 } 66 commitLock("test"); 67 } 68 69 public static void optimisticRollback() { 70 getManager().optimisticRollback(); 71 } 72 73 public static String getClientID() { 74 return getManager().getClientID(); 75 } 76 77 public static Object deepCopy(Object pojo) { 78 return getManager().deepCopy(pojo); 79 } 80 81 public static Object lookupOrCreateRoot(String name, Object object) { 82 return getManager().lookupOrCreateRoot(name, object); 83 } 84 85 public static Object lookupOrCreateRootNoDepth(String name, Object obj) { 86 return getManager().lookupOrCreateRootNoDepth(name, obj); 87 } 88 89 public static Object createOrReplaceRoot(String rootName, Object object) { 90 return getManager().createOrReplaceRoot(rootName, object); 91 } 92 93 public static void beginVolatileByOffset(Object pojo, long fieldOffset, int type) { 94 TCObject tcObject = lookupExistingOrNull(pojo); 95 if (tcObject == null) { throw new NullPointerException ("beginVolatileByOffset called on a null TCObject"); } 96 97 beginVolatile(tcObject, tcObject.getFieldNameByOffset(fieldOffset), type); 98 } 99 100 public static void commitVolatileByOffset(Object pojo, long fieldOffset) { 101 TCObject tcObject = lookupExistingOrNull(pojo); 102 if (tcObject == null) { throw new NullPointerException ("commitVolatileByOffset called on a null TCObject"); } 103 104 commitVolatile(tcObject, tcObject.getFieldNameByOffset(fieldOffset)); 105 } 106 107 public static void beginVolatile(TCObject tcObject, String fieldName, int type) { 108 getManager().beginVolatile(tcObject, fieldName, type); 109 } 110 111 public static void beginLock(String lockID, int type) { 112 getManager().beginLock(lockID, type); 113 } 114 115 public static boolean tryBeginLock(String lockID, int type) { 116 return getManager().tryBeginLock(lockID, type); 117 } 118 119 public static void commitVolatile(TCObject tcObject, String fieldName) { 120 getManager().commitVolatile(tcObject, fieldName); 121 } 122 123 public static void commitLock(String lockID) { 124 getManager().commitLock(lockID); 125 } 126 127 public static TCObject lookupExistingOrNull(Object pojo) { 128 return getManager().lookupExistingOrNull(pojo); 129 } 130 131 public static TCObject shareObjectIfNecessary(Object pojo) { 132 return getManager().shareObjectIfNecessary(pojo); 133 } 134 135 public static void logicalInvoke(Object object, String methodName, Object [] params) { 136 getManager().logicalInvoke(object, methodName, params); 137 } 138 139 public static void logicalInvokeWithTransaction(Object object, Object lockObject, String methodName, Object [] params) { 140 getManager().logicalInvokeWithTransaction(object, lockObject, methodName, params); 141 } 142 143 public static void distributedMethodCallCommit() { 144 getManager().distributedMethodCallCommit(); 145 } 146 147 public static boolean prunedDistributedMethodCall(Object receiver, String method, Object [] params) { 148 return getManager().distributedMethodCall(receiver, method, params, false); 149 } 150 151 public static boolean distributedMethodCall(Object receiver, String method, Object [] params) { 152 return getManager().distributedMethodCall(receiver, method, params, true); 153 } 154 155 public static Object lookupRoot(String name) { 156 return getManager().lookupRoot(name); 157 } 158 159 public static Object lookupObject(ObjectID id) { 160 try { 161 return getManager().lookupObject(id); 162 } catch (ClassNotFoundException e) { 163 throw new TCClassNotFoundException(e); 164 } 165 } 166 167 public static TCObject lookupOrCreate(Object obj) { 168 return getManager().lookupOrCreate(obj); 169 } 170 171 public static void checkWriteAccess(Object context) { 172 getManager().checkWriteAccess(context); 173 } 174 175 public static boolean isManaged(Object obj) { 176 return getManager().isManaged(obj); 177 } 178 179 public static boolean isLogical(Object obj) { 180 return getManager().isLogical(obj); 181 } 182 183 public static boolean isRoot(String className, String fieldName) { 184 return getManager().isRoot(className, fieldName); 185 } 186 187 public static void objectNotify(Object obj) { 188 getManager().objectNotify(obj); 189 } 190 191 public static void objectNotifyAll(Object obj) { 192 getManager().objectNotifyAll(obj); 193 } 194 195 public static void objectWait0(Object obj) throws InterruptedException { 196 getManager().objectWait0(obj); 197 } 198 199 public static void objectWait1(Object obj, long millis) throws InterruptedException { 200 getManager().objectWait1(obj, millis); 201 } 202 203 public static void objectWait2(Object obj, long millis, int nanos) throws InterruptedException { 204 getManager().objectWait2(obj, millis, nanos); 205 } 206 207 public static void monitorEnter(Object obj, int type) { 208 getManager().monitorEnter(obj, type); 209 } 210 211 public static void monitorExit(Object obj) { 212 getManager().monitorExit(obj); 213 } 214 215 public static boolean isLocked(Object obj) { 216 return getManager().isLocked(obj); 217 } 218 219 public static boolean tryMonitorEnter(Object obj, int type) { 220 return getManager().tryMonitorEnter(obj, type); 221 } 222 223 public static boolean isHeldByCurrentThread(Object obj, int lockLevel) { 224 return getManager().isHeldByCurrentThread(obj, lockLevel); 225 } 226 227 public static int queueLength(Object obj) { 228 return getManager().queueLength(obj); 229 } 230 231 public static int waitLength(Object obj) { 232 return getManager().waitLength(obj); 233 } 234 235 public static boolean isCreationInProgress() { 236 return getManager().isCreationInProgress(); 237 } 238 239 private ManagerUtil() { 240 } 242 243 private static class GlobalManagerHolder { 244 static final Manager instance; 245 static { 246 instance = ClassProcessorHelper.getGlobalManager(); 247 } 248 } 249 250 public static Object get(Object array, int index) throws IllegalArgumentException , ArrayIndexOutOfBoundsException { 251 return ArrayManager.get(array, index); 252 } 253 254 public static void set(Object array, int index, Object value) throws IllegalArgumentException , 255 ArrayIndexOutOfBoundsException { 256 if (array == null) throw new NullPointerException (); 257 258 if (array instanceof Object []) { 259 Class componentType = array.getClass().getComponentType(); 260 if (value != null && !componentType.isInstance(value)) { 261 throw new IllegalArgumentException ("Cannot assign an instance of type " + value.getClass().getName() 263 + " to array with component type " + componentType.getName()); 264 } 265 ArrayManager.objectArrayChanged((Object []) array, index, value); 266 } else if (value instanceof Byte ) setByte(array, index, ((Byte ) value).byteValue()); 267 else if (value instanceof Short ) setShort(array, index, ((Short ) value).shortValue()); 268 else if (value instanceof Integer ) setInt(array, index, ((Integer ) value).intValue()); 269 else if (value instanceof Long ) setLong(array, index, ((Long ) value).longValue()); 270 else if (value instanceof Float ) setFloat(array, index, ((Float ) value).floatValue()); 271 else if (value instanceof Double ) setDouble(array, index, ((Double ) value).doubleValue()); 272 else if (value instanceof Character ) setChar(array, index, ((Character ) value).charValue()); 273 else if (value instanceof Boolean ) setBoolean(array, index, ((Boolean ) value).booleanValue()); 274 else throw new IllegalArgumentException ("Not an array type: " + array.getClass().getName()); 275 } 276 277 public static void setBoolean(Object array, int index, boolean z) throws IllegalArgumentException , 278 ArrayIndexOutOfBoundsException { 279 if (array == null) throw new NullPointerException (); 280 281 if (array instanceof boolean[]) { 282 byte b = z ? (byte) 1 : (byte) 0; 283 284 ArrayManager.byteOrBooleanArrayChanged(array, index, b); 285 } else throw new IllegalArgumentException (); 286 } 287 288 public static void setByte(Object array, int index, byte b) throws IllegalArgumentException , 289 ArrayIndexOutOfBoundsException { 290 if (array == null) throw new NullPointerException (); 291 292 if (array instanceof byte[]) ArrayManager.byteOrBooleanArrayChanged(array, index, b); 293 else setShort(array, index, b); 294 } 295 296 public static void setChar(Object array, int index, char c) throws IllegalArgumentException , 297 ArrayIndexOutOfBoundsException { 298 if (array == null) throw new NullPointerException (); 299 300 if (array instanceof char[]) ArrayManager.charArrayChanged((char[]) array, index, c); 301 else setInt(array, index, c); 302 } 303 304 public static void setShort(Object array, int index, short s) throws IllegalArgumentException , 305 ArrayIndexOutOfBoundsException { 306 if (array == null) throw new NullPointerException (); 307 308 if (array instanceof short[]) ArrayManager.shortArrayChanged((short[]) array, index, s); 309 else setInt(array, index, s); 310 } 311 312 public static void setInt(Object array, int index, int i) throws IllegalArgumentException , 313 ArrayIndexOutOfBoundsException { 314 if (array == null) throw new NullPointerException (); 315 316 if (array instanceof int[]) ArrayManager.intArrayChanged((int[]) array, index, i); 317 else setLong(array, index, i); 318 } 319 320 public static void setLong(Object array, int index, long l) throws IllegalArgumentException , 321 ArrayIndexOutOfBoundsException { 322 if (array == null) throw new NullPointerException (); 323 324 if (array instanceof long[]) ArrayManager.longArrayChanged((long[]) array, index, l); 325 else setFloat(array, index, l); 326 } 327 328 public static void setFloat(Object array, int index, float f) throws IllegalArgumentException , 329 ArrayIndexOutOfBoundsException { 330 if (array == null) throw new NullPointerException (); 331 332 if (array instanceof float[]) ArrayManager.floatArrayChanged((float[]) array, index, f); 333 else setDouble(array, index, f); 334 } 335 336 public static void setDouble(Object array, int index, double d) throws IllegalArgumentException , 337 ArrayIndexOutOfBoundsException { 338 if (array == null) throw new NullPointerException (); 339 340 if (array instanceof double[]) ArrayManager.doubleArrayChanged((double[]) array, index, d); 341 else throw new IllegalArgumentException (); 342 } 343 344 public static void objectArrayChanged(Object [] array, int index, Object value) { 345 ArrayManager.objectArrayChanged(array, index, value); 346 } 347 348 public static void shortArrayChanged(short[] array, int index, short value) { 349 ArrayManager.shortArrayChanged(array, index, value); 350 } 351 352 public static void longArrayChanged(long[] array, int index, long value) { 353 ArrayManager.longArrayChanged(array, index, value); 354 } 355 356 public static void intArrayChanged(int[] array, int index, int value) { 357 ArrayManager.intArrayChanged(array, index, value); 358 } 359 360 public static void floatArrayChanged(float[] array, int index, float value) { 361 ArrayManager.floatArrayChanged(array, index, value); 362 } 363 364 public static void doubleArrayChanged(double[] array, int index, double value) { 365 ArrayManager.doubleArrayChanged(array, index, value); 366 } 367 368 public static void charArrayChanged(char[] array, int index, char value) { 369 ArrayManager.charArrayChanged(array, index, value); 370 } 371 372 public static void byteOrBooleanArrayChanged(Object array, int index, byte value) { 373 ArrayManager.byteOrBooleanArrayChanged(array, index, value); 374 } 375 376 public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) { 377 ArrayManager.arraycopy(src, srcPos, dest, destPos, length); 378 } 379 380 public static TCObject getObject(Object array) { 381 return ArrayManager.getObject(array); 382 } 383 384 public static void charArrayCopy(char[] src, int srcPos, char[] dest, int destPos, int length, TCObject tco) { 385 ArrayManager.charArrayCopy(src, srcPos, dest, destPos, length, tco); 386 } 387 388 public static void register(Object pojo, TCObject obj) { 389 ArrayManager.register(pojo, obj); 390 } 391 392 public static SessionMonitorMBean getSessionMonitorMBean() { 393 return getManager().getSessionMonitorMBean(); 394 } 395 396 public static TCProperties getTCProperties() { 397 return getManager().getTCProperites(); 398 } 399 400 public static void addClusterEventListener(ClusterEventListener cel) { 401 getManager().addClusterEventListener(cel); 402 } 403 404 public static int getSessionLockType(String appName) { 405 return ClassProcessorHelper.getSessionLockType(appName); 406 } 407 408 } 409 | Popular Tags |