KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > bytecode > ManagerUtil


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

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 /**
19  * A bunch of static methods that make calling Manager method much easier from instrumented classes
20  */

21 public class ManagerUtil {
22
23   public static final String JavaDoc CLASS = "com/tc/object/bytecode/ManagerUtil";
24   public static final String JavaDoc 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 JavaDoc 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 JavaDoc loggerName) {
48     return getManager().getLogger(loggerName);
49   }
50
51   public static boolean isPhysicallyInstrumented(Class JavaDoc 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 JavaDoc 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 JavaDoc getClientID() {
74     return getManager().getClientID();
75   }
76
77   public static Object JavaDoc deepCopy(Object JavaDoc pojo) {
78     return getManager().deepCopy(pojo);
79   }
80
81   public static Object JavaDoc lookupOrCreateRoot(String JavaDoc name, Object JavaDoc object) {
82     return getManager().lookupOrCreateRoot(name, object);
83   }
84
85   public static Object JavaDoc lookupOrCreateRootNoDepth(String JavaDoc name, Object JavaDoc obj) {
86     return getManager().lookupOrCreateRootNoDepth(name, obj);
87   }
88
89   public static Object JavaDoc createOrReplaceRoot(String JavaDoc rootName, Object JavaDoc object) {
90     return getManager().createOrReplaceRoot(rootName, object);
91   }
92
93   public static void beginVolatileByOffset(Object JavaDoc pojo, long fieldOffset, int type) {
94     TCObject tcObject = lookupExistingOrNull(pojo);
95     if (tcObject == null) { throw new NullPointerException JavaDoc("beginVolatileByOffset called on a null TCObject"); }
96
97     beginVolatile(tcObject, tcObject.getFieldNameByOffset(fieldOffset), type);
98   }
99
100   public static void commitVolatileByOffset(Object JavaDoc pojo, long fieldOffset) {
101     TCObject tcObject = lookupExistingOrNull(pojo);
102     if (tcObject == null) { throw new NullPointerException JavaDoc("commitVolatileByOffset called on a null TCObject"); }
103
104     commitVolatile(tcObject, tcObject.getFieldNameByOffset(fieldOffset));
105   }
106
107   public static void beginVolatile(TCObject tcObject, String JavaDoc fieldName, int type) {
108     getManager().beginVolatile(tcObject, fieldName, type);
109   }
110
111   public static void beginLock(String JavaDoc lockID, int type) {
112     getManager().beginLock(lockID, type);
113   }
114
115   public static boolean tryBeginLock(String JavaDoc lockID, int type) {
116     return getManager().tryBeginLock(lockID, type);
117   }
118
119   public static void commitVolatile(TCObject tcObject, String JavaDoc fieldName) {
120     getManager().commitVolatile(tcObject, fieldName);
121   }
122
123   public static void commitLock(String JavaDoc lockID) {
124     getManager().commitLock(lockID);
125   }
126
127   public static TCObject lookupExistingOrNull(Object JavaDoc pojo) {
128     return getManager().lookupExistingOrNull(pojo);
129   }
130
131   public static TCObject shareObjectIfNecessary(Object JavaDoc pojo) {
132     return getManager().shareObjectIfNecessary(pojo);
133   }
134
135   public static void logicalInvoke(Object JavaDoc object, String JavaDoc methodName, Object JavaDoc[] params) {
136     getManager().logicalInvoke(object, methodName, params);
137   }
138
139   public static void logicalInvokeWithTransaction(Object JavaDoc object, Object JavaDoc lockObject, String JavaDoc methodName, Object JavaDoc[] 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 JavaDoc receiver, String JavaDoc method, Object JavaDoc[] params) {
148     return getManager().distributedMethodCall(receiver, method, params, false);
149   }
150
151   public static boolean distributedMethodCall(Object JavaDoc receiver, String JavaDoc method, Object JavaDoc[] params) {
152     return getManager().distributedMethodCall(receiver, method, params, true);
153   }
154
155   public static Object JavaDoc lookupRoot(String JavaDoc name) {
156     return getManager().lookupRoot(name);
157   }
158
159   public static Object JavaDoc lookupObject(ObjectID id) {
160     try {
161       return getManager().lookupObject(id);
162     } catch (ClassNotFoundException JavaDoc e) {
163       throw new TCClassNotFoundException(e);
164     }
165   }
166
167   public static TCObject lookupOrCreate(Object JavaDoc obj) {
168     return getManager().lookupOrCreate(obj);
169   }
170
171   public static void checkWriteAccess(Object JavaDoc context) {
172     getManager().checkWriteAccess(context);
173   }
174
175   public static boolean isManaged(Object JavaDoc obj) {
176     return getManager().isManaged(obj);
177   }
178
179   public static boolean isLogical(Object JavaDoc obj) {
180     return getManager().isLogical(obj);
181   }
182
183   public static boolean isRoot(String JavaDoc className, String JavaDoc fieldName) {
184     return getManager().isRoot(className, fieldName);
185   }
186
187   public static void objectNotify(Object JavaDoc obj) {
188     getManager().objectNotify(obj);
189   }
190
191   public static void objectNotifyAll(Object JavaDoc obj) {
192     getManager().objectNotifyAll(obj);
193   }
194
195   public static void objectWait0(Object JavaDoc obj) throws InterruptedException JavaDoc {
196     getManager().objectWait0(obj);
197   }
198
199   public static void objectWait1(Object JavaDoc obj, long millis) throws InterruptedException JavaDoc {
200     getManager().objectWait1(obj, millis);
201   }
202
203   public static void objectWait2(Object JavaDoc obj, long millis, int nanos) throws InterruptedException JavaDoc {
204     getManager().objectWait2(obj, millis, nanos);
205   }
206
207   public static void monitorEnter(Object JavaDoc obj, int type) {
208     getManager().monitorEnter(obj, type);
209   }
210
211   public static void monitorExit(Object JavaDoc obj) {
212     getManager().monitorExit(obj);
213   }
214
215   public static boolean isLocked(Object JavaDoc obj) {
216     return getManager().isLocked(obj);
217   }
218
219   public static boolean tryMonitorEnter(Object JavaDoc obj, int type) {
220     return getManager().tryMonitorEnter(obj, type);
221   }
222
223   public static boolean isHeldByCurrentThread(Object JavaDoc obj, int lockLevel) {
224     return getManager().isHeldByCurrentThread(obj, lockLevel);
225   }
226
227   public static int queueLength(Object JavaDoc obj) {
228     return getManager().queueLength(obj);
229   }
230
231   public static int waitLength(Object JavaDoc obj) {
232     return getManager().waitLength(obj);
233   }
234
235   public static boolean isCreationInProgress() {
236     return getManager().isCreationInProgress();
237   }
238
239   private ManagerUtil() {
240     // not for public instantiation
241
}
242
243   private static class GlobalManagerHolder {
244     static final Manager instance;
245     static {
246       instance = ClassProcessorHelper.getGlobalManager();
247     }
248   }
249
250   public static Object JavaDoc get(Object JavaDoc array, int index) throws IllegalArgumentException JavaDoc, ArrayIndexOutOfBoundsException JavaDoc {
251     return ArrayManager.get(array, index);
252   }
253
254   public static void set(Object JavaDoc array, int index, Object JavaDoc value) throws IllegalArgumentException JavaDoc,
255       ArrayIndexOutOfBoundsException JavaDoc {
256     if (array == null) throw new NullPointerException JavaDoc();
257
258     if (array instanceof Object JavaDoc[]) {
259       Class JavaDoc componentType = array.getClass().getComponentType();
260       if (value != null && !componentType.isInstance(value)) {
261         //
262
throw new IllegalArgumentException JavaDoc("Cannot assign an instance of type " + value.getClass().getName()
263                                            + " to array with component type " + componentType.getName());
264       }
265       ArrayManager.objectArrayChanged((Object JavaDoc[]) array, index, value);
266     } else if (value instanceof Byte JavaDoc) setByte(array, index, ((Byte JavaDoc) value).byteValue());
267     else if (value instanceof Short JavaDoc) setShort(array, index, ((Short JavaDoc) value).shortValue());
268     else if (value instanceof Integer JavaDoc) setInt(array, index, ((Integer JavaDoc) value).intValue());
269     else if (value instanceof Long JavaDoc) setLong(array, index, ((Long JavaDoc) value).longValue());
270     else if (value instanceof Float JavaDoc) setFloat(array, index, ((Float JavaDoc) value).floatValue());
271     else if (value instanceof Double JavaDoc) setDouble(array, index, ((Double JavaDoc) value).doubleValue());
272     else if (value instanceof Character JavaDoc) setChar(array, index, ((Character JavaDoc) value).charValue());
273     else if (value instanceof Boolean JavaDoc) setBoolean(array, index, ((Boolean JavaDoc) value).booleanValue());
274     else throw new IllegalArgumentException JavaDoc("Not an array type: " + array.getClass().getName());
275   }
276
277   public static void setBoolean(Object JavaDoc array, int index, boolean z) throws IllegalArgumentException JavaDoc,
278       ArrayIndexOutOfBoundsException JavaDoc {
279     if (array == null) throw new NullPointerException JavaDoc();
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 JavaDoc();
286   }
287
288   public static void setByte(Object JavaDoc array, int index, byte b) throws IllegalArgumentException JavaDoc,
289       ArrayIndexOutOfBoundsException JavaDoc {
290     if (array == null) throw new NullPointerException JavaDoc();
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 JavaDoc array, int index, char c) throws IllegalArgumentException JavaDoc,
297       ArrayIndexOutOfBoundsException JavaDoc {
298     if (array == null) throw new NullPointerException JavaDoc();
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 JavaDoc array, int index, short s) throws IllegalArgumentException JavaDoc,
305       ArrayIndexOutOfBoundsException JavaDoc {
306     if (array == null) throw new NullPointerException JavaDoc();
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 JavaDoc array, int index, int i) throws IllegalArgumentException JavaDoc,
313       ArrayIndexOutOfBoundsException JavaDoc {
314     if (array == null) throw new NullPointerException JavaDoc();
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 JavaDoc array, int index, long l) throws IllegalArgumentException JavaDoc,
321       ArrayIndexOutOfBoundsException JavaDoc {
322     if (array == null) throw new NullPointerException JavaDoc();
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 JavaDoc array, int index, float f) throws IllegalArgumentException JavaDoc,
329       ArrayIndexOutOfBoundsException JavaDoc {
330     if (array == null) throw new NullPointerException JavaDoc();
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 JavaDoc array, int index, double d) throws IllegalArgumentException JavaDoc,
337       ArrayIndexOutOfBoundsException JavaDoc {
338     if (array == null) throw new NullPointerException JavaDoc();
339
340     if (array instanceof double[]) ArrayManager.doubleArrayChanged((double[]) array, index, d);
341     else throw new IllegalArgumentException JavaDoc();
342   }
343
344   public static void objectArrayChanged(Object JavaDoc[] array, int index, Object JavaDoc 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 JavaDoc array, int index, byte value) {
373     ArrayManager.byteOrBooleanArrayChanged(array, index, value);
374   }
375
376   public static void arraycopy(Object JavaDoc src, int srcPos, Object JavaDoc dest, int destPos, int length) {
377     ArrayManager.arraycopy(src, srcPos, dest, destPos, length);
378   }
379
380   public static TCObject getObject(Object JavaDoc 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 JavaDoc 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 JavaDoc appName) {
405     return ClassProcessorHelper.getSessionLockType(appName);
406   }
407
408 }
409
Popular Tags