KickJava   Java API By Example, From Geeks To Geeks.

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


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.asm.Type;
8 import com.tc.cluster.Cluster;
9 import com.tc.cluster.ClusterEventListener;
10 import com.tc.lang.StartupHelper;
11 import com.tc.lang.TCThreadGroup;
12 import com.tc.lang.ThrowableHandler;
13 import com.tc.lang.StartupHelper.StartupAction;
14 import com.tc.logging.TCLogger;
15 import com.tc.logging.TCLogging;
16 import com.tc.management.beans.sessions.SessionMonitorMBean;
17 import com.tc.object.ClientObjectManager;
18 import com.tc.object.ClientShutdownManager;
19 import com.tc.object.DistributedObjectClient;
20 import com.tc.object.LiteralValues;
21 import com.tc.object.ObjectID;
22 import com.tc.object.Portability;
23 import com.tc.object.SerializationUtil;
24 import com.tc.object.TCObject;
25 import com.tc.object.bytecode.hook.impl.PreparedComponentsFromL2Connection;
26 import com.tc.object.config.DSOClientConfigHelper;
27 import com.tc.object.event.DmiManager;
28 import com.tc.object.loaders.ClassProvider;
29 import com.tc.object.lockmanager.api.LockLevel;
30 import com.tc.object.logging.NullRuntimeLogger;
31 import com.tc.object.logging.RuntimeLogger;
32 import com.tc.object.tx.ClientTransactionManager;
33 import com.tc.object.tx.WaitInvocation;
34 import com.tc.object.tx.optimistic.OptimisticTransactionManager;
35 import com.tc.object.tx.optimistic.OptimisticTransactionManagerImpl;
36 import com.tc.properties.TCProperties;
37 import com.tc.properties.TCPropertiesImpl;
38 import com.tc.util.Assert;
39 import com.tc.util.Util;
40 import com.tc.util.concurrent.SetOnceFlag;
41
42 import java.util.Collection JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.Map JavaDoc;
46
47 /**
48  * @author teck
49  */

50 public class ManagerImpl implements Manager {
51   private static final TCLogger logger = TCLogging.getLogger(Manager.class);
52
53   private final LiteralValues literals = new LiteralValues();
54
55   private final SetOnceFlag clientStarted = new SetOnceFlag();
56   private final DSOClientConfigHelper config;
57   private final ClassProvider classProvider;
58   private final boolean startClient;
59   private final PreparedComponentsFromL2Connection connectionComponents;
60   private final Thread JavaDoc shutdownAction;
61   private final Portability portability;
62   private final Cluster cluster;
63
64   private RuntimeLogger runtimeLogger = new NullRuntimeLogger();
65   private ClientObjectManager objectManager;
66   private ClientShutdownManager shutdownManager;
67   private ClientTransactionManager txManager;
68   private DistributedObjectClient dso;
69   private DmiManager methodCallManager;
70   private OptimisticTransactionManager optimisticTransactionManager;
71   private SerializationUtil serializer = new SerializationUtil();
72   private MethodDisplayNames methodDisplay = new MethodDisplayNames(serializer);
73
74   public ManagerImpl(DSOClientConfigHelper config, ClassProvider classProvider,
75                      PreparedComponentsFromL2Connection connectionComponents) {
76     this(true, null, null, config, classProvider, connectionComponents, true);
77   }
78
79   // For tests
80
public ManagerImpl(boolean startClient, ClientObjectManager objectManager, ClientTransactionManager txManager,
81                      DSOClientConfigHelper config, ClassProvider classProvider,
82                      PreparedComponentsFromL2Connection connectionComponents) {
83     this(startClient, objectManager, txManager, config, classProvider, connectionComponents, true);
84   }
85
86   // For tests
87
public ManagerImpl(boolean startClient, ClientObjectManager objectManager, ClientTransactionManager txManager,
88                      DSOClientConfigHelper config, ClassProvider classProvider,
89                      PreparedComponentsFromL2Connection connectionComponents, boolean shutdownActionRequired) {
90     this.objectManager = objectManager;
91     this.portability = config.getPortability();
92     this.txManager = txManager;
93     this.config = config;
94     this.startClient = startClient;
95     this.classProvider = classProvider;
96     this.connectionComponents = connectionComponents;
97     this.cluster = new Cluster();
98     if (shutdownActionRequired) {
99       shutdownAction = new Thread JavaDoc(new ShutdownAction());
100       // Register a shutdown hook for the DSO client
101
Runtime.getRuntime().addShutdownHook(shutdownAction);
102     } else {
103       shutdownAction = null;
104     }
105
106   }
107
108   public SessionMonitorMBean getSessionMonitorMBean() {
109     return dso.getSessionMonitorMBean();
110   }
111
112   public void init() {
113     resolveClasses(); // call this before starting any threads (SEDA, DistributedMethod call stuff, etc)
114

115     if (startClient) {
116       if (clientStarted.attemptSet()) {
117         startClient();
118       }
119     }
120   }
121
122   public String JavaDoc getClientID() {
123     return String.valueOf(this.dso.getChannel().getChannelIDProvider().getChannelID().toLong());
124   }
125
126   private void resolveClasses() {
127     // See LKC-2323 -- A number of Manager methods can be entered from the internals of URLClassLoader (specifically
128
// sun.misc.URLClassPath.getLoader()) and can cause deadlocks. Making sure these methods are invoked once, thus
129
// resolving any class loads, should eliminate the problem.
130
//
131
// NOTE: it is entirely possible more signatures might need to added here
132

133     Object JavaDoc o = new Manageable() {
134       public void __tc_managed(TCObject t) {
135         throw new AssertionError JavaDoc();
136       }
137
138       public TCObject __tc_managed() {
139         return null;
140       }
141
142       public boolean __tc_isManaged() {
143         return false;
144       }
145     };
146     lookupExistingOrNull(o);
147     monitorEnter(o, LOCK_TYPE_WRITE);
148     monitorExit(o);
149     logicalInvoke(new HashMap JavaDoc(), SerializationUtil.CLEAR_SIGNATURE, new Object JavaDoc[] {});
150   }
151
152   private void startClient() {
153     final TCThreadGroup group = new TCThreadGroup(new ThrowableHandler(TCLogging
154         .getLogger(DistributedObjectClient.class)));
155
156     StartupAction action = new StartupHelper.StartupAction() {
157       public void execute() throws Throwable JavaDoc {
158         dso = new DistributedObjectClient(config, group, classProvider, connectionComponents, ManagerImpl.this, cluster);
159         dso.start();
160         objectManager = dso.getObjectManager();
161         txManager = dso.getTransactionManager();
162         runtimeLogger = dso.getRuntimeLogger();
163
164         optimisticTransactionManager = new OptimisticTransactionManagerImpl(objectManager, txManager);
165
166         methodCallManager = dso.getDmiManager();
167
168         shutdownManager = new ClientShutdownManager(objectManager, dso.getRemoteTransactionManager(), dso
169             .getStageManager(), dso.getCommunicationsManager(), dso.getChannel(), dso.getClientHandshakeManager(),
170                                                     connectionComponents);
171       }
172     };
173
174     StartupHelper startupHelper = new StartupHelper(group, action);
175     startupHelper.startUp();
176   }
177
178   public void stop() {
179     shutdown(false);
180   }
181
182   private void shutdown(boolean fromShutdownHook) {
183     if (shutdownManager != null) {
184       try {
185         // XXX: This "fromShutdownHook" flag should be removed. It's only here temporarily to make shutdown behave
186
// before I started futzing with it
187
shutdownManager.execute(fromShutdownHook);
188       } finally {
189         // If we're not being called as a result of the shutdown hook, de-register the hook
190
if (Thread.currentThread() != shutdownAction) {
191           try {
192             Runtime.getRuntime().removeShutdownHook(shutdownAction);
193           } catch (Exception JavaDoc e) {
194             // ignore
195
}
196         }
197       }
198     }
199   }
200
201   public void logicalInvoke(Object JavaDoc object, String JavaDoc methodSignature, Object JavaDoc[] params) {
202     Manageable m = (Manageable) object;
203     if (m.__tc_managed() != null) {
204       TCObject tco = lookupExistingOrNull(object);
205
206       try {
207         if (tco != null) {
208
209           if (SerializationUtil.ADD_ALL_SIGNATURE.equals(methodSignature)) {
210             logicalAddAllInvoke(serializer.methodToID(methodSignature), methodSignature, (Collection JavaDoc) params[0], tco);
211           } else if (SerializationUtil.ADD_ALL_AT_SIGNATURE.equals(methodSignature)) {
212             logicalAddAllAtInvoke(serializer.methodToID(methodSignature), methodSignature, ((Integer JavaDoc) params[0])
213                 .intValue(), (Collection JavaDoc) params[1], tco);
214           } else {
215             adjustForJava1ParametersIfNecessary(methodSignature, params);
216             tco.logicalInvoke(serializer.methodToID(methodSignature), methodDisplay
217                 .getDisplayForSignature(methodSignature), params);
218           }
219         }
220       } catch (Throwable JavaDoc t) {
221         Util.printLogAndRethrowError(t, logger);
222       }
223     }
224   }
225
226   public void logicalInvokeWithTransaction(Object JavaDoc object, Object JavaDoc lockObject, String JavaDoc methodName, Object JavaDoc[] params) {
227     monitorEnter(lockObject, LockLevel.WRITE);
228     try {
229       logicalInvoke(object, methodName, params);
230     } finally {
231       monitorExit(lockObject);
232     }
233   }
234
235   private void adjustForJava1ParametersIfNecessary(String JavaDoc methodName, Object JavaDoc[] params) {
236     if ((params.length == 2) && (params[1] != null) && (params[1].getClass().equals(Integer JavaDoc.class))) {
237       if (SerializationUtil.SET_ELEMENT_SIGNATURE.equals(methodName)
238           || SerializationUtil.INSERT_ELEMENT_AT_SIGNATURE.equals(methodName)) {
239         // special case for reversing parameters
240
Object JavaDoc tmp = params[0];
241         params[0] = params[1];
242         params[1] = tmp;
243       }
244     }
245   }
246
247   private void logicalAddAllInvoke(int method, String JavaDoc methodSignature, Collection JavaDoc collection, TCObject tcobj) {
248     for (Iterator JavaDoc i = collection.iterator(); i.hasNext();) {
249       tcobj.logicalInvoke(method, methodDisplay.getDisplayForSignature(methodSignature), new Object JavaDoc[] { i.next() });
250     }
251   }
252
253   private void logicalAddAllAtInvoke(int method, String JavaDoc methodSignature, int index, Collection JavaDoc collection,
254                                      TCObject tcobj) {
255
256     for (Iterator JavaDoc i = collection.iterator(); i.hasNext();) {
257       tcobj.logicalInvoke(method, methodDisplay.getDisplayForSignature(methodSignature), new Object JavaDoc[] {
258           new Integer JavaDoc(index++), i.next() });
259     }
260   }
261
262   public Object JavaDoc lookupOrCreateRoot(String JavaDoc name, Object JavaDoc object) {
263     return lookupOrCreateRoot(name, object, false);
264   }
265
266   public Object JavaDoc lookupOrCreateRootNoDepth(String JavaDoc name, Object JavaDoc obj) {
267     return lookupOrCreateRoot(name, obj, true);
268   }
269
270   public Object JavaDoc createOrReplaceRoot(String JavaDoc name, Object JavaDoc object) {
271     try {
272       return this.objectManager.createOrReplaceRoot(name, object);
273     } catch (Throwable JavaDoc t) {
274       Util.printLogAndRethrowError(t, logger);
275
276       // shouldn't get here
277
throw new AssertionError JavaDoc();
278     }
279   }
280
281   private Object JavaDoc lookupOrCreateRoot(String JavaDoc rootName, Object JavaDoc object, boolean noDepth) {
282     try {
283       if (noDepth) { return this.objectManager.lookupOrCreateRootNoDepth(rootName, object); }
284       return this.objectManager.lookupOrCreateRoot(rootName, object, true);
285     } catch (Throwable JavaDoc t) {
286       Util.printLogAndRethrowError(t, logger);
287
288       // shouldn't get here
289
throw new AssertionError JavaDoc();
290     }
291   }
292
293   public void beginLock(String JavaDoc lockID, int type) {
294     try {
295       begin(lockID, type, null, null);
296     } catch (Throwable JavaDoc t) {
297       Util.printLogAndRethrowError(t, logger);
298     }
299   }
300
301   public void beginVolatile(TCObject tcObject, String JavaDoc fieldName, int type) {
302     if (tcObject == null) { throw new NullPointerException JavaDoc("beginVolatile called on a null TCObject"); }
303
304     begin(generateVolatileLockName(tcObject, fieldName), type, null, null);
305   }
306
307   private void begin(String JavaDoc lockID, int type, Object JavaDoc instance, TCObject tcobj) {
308     this.txManager.begin(lockID, type);
309     if (runtimeLogger.lockDebug()) {
310       runtimeLogger.lockAcquired(lockID, type, instance, tcobj);
311     }
312   }
313
314   private boolean tryBegin(String JavaDoc lockID, int type, Object JavaDoc instance, TCObject tcobj) {
315     boolean locked = this.txManager.tryBegin(lockID, type);
316     if (locked && runtimeLogger.lockDebug()) {
317       runtimeLogger.lockAcquired(lockID, type, instance, tcobj);
318     }
319     return locked;
320   }
321
322   public void commitVolatile(TCObject tcObject, String JavaDoc fieldName) {
323     if (tcObject == null) { throw new NullPointerException JavaDoc("commitVolatile called on a null TCObject"); }
324
325     commitLock(generateVolatileLockName(tcObject, fieldName));
326   }
327
328   public void commitLock(String JavaDoc lockName) {
329
330     try {
331       this.txManager.commit(lockName);
332     } catch (Throwable JavaDoc t) {
333       Util.printLogAndRethrowError(t, logger);
334     }
335   }
336
337   public void objectNotify(Object JavaDoc obj) {
338     if (obj == null) { throw new NullPointerException JavaDoc("notify() called on a null reference"); }
339
340     TCObject tco = lookupExistingOrNull(obj);
341
342     if (tco != null) {
343       managedObjectNotify(obj, tco, false);
344     } else {
345       obj.notify();
346     }
347   }
348
349   public void objectNotifyAll(Object JavaDoc obj) {
350     if (obj == null) { throw new NullPointerException JavaDoc("notifyAll() called on a null reference"); }
351
352     TCObject tco = lookupExistingOrNull(obj);
353
354     if (tco != null) {
355       managedObjectNotify(obj, tco, true);
356     } else {
357       obj.notifyAll();
358     }
359   }
360
361   private void managedObjectNotify(Object JavaDoc obj, TCObject tco, boolean all) {
362     try {
363       if (runtimeLogger.waitNotifyDebug()) {
364         runtimeLogger.objectNotify(all, obj, tco);
365       }
366       this.txManager.notify(generateAutolockName(tco), all, obj);
367     } catch (Throwable JavaDoc t) {
368       Util.printLogAndRethrowError(t, logger);
369     }
370   }
371
372   public void objectWait0(Object JavaDoc obj) throws InterruptedException JavaDoc {
373     TCObject tco = lookupExistingOrNull(obj);
374
375     if (tco != null) {
376       try {
377         WaitInvocation call = new WaitInvocation();
378         if (runtimeLogger.waitNotifyDebug()) {
379           runtimeLogger.objectWait(call, obj, tco);
380         }
381         this.txManager.wait(generateAutolockName(tco), call, obj);
382       } catch (InterruptedException JavaDoc ie) {
383         throw ie;
384       } catch (Throwable JavaDoc t) {
385         Util.printLogAndRethrowError(t, logger);
386       }
387     } else {
388       obj.wait();
389     }
390   }
391
392   public void objectWait1(Object JavaDoc obj, long millis) throws InterruptedException JavaDoc {
393     TCObject tco = lookupExistingOrNull(obj);
394     if (tco != null) {
395       try {
396         WaitInvocation call = new WaitInvocation(millis);
397         if (runtimeLogger.waitNotifyDebug()) {
398           runtimeLogger.objectWait(call, obj, tco);
399         }
400         this.txManager.wait(generateAutolockName(tco), call, obj);
401       } catch (InterruptedException JavaDoc ie) {
402         throw ie;
403       } catch (Throwable JavaDoc t) {
404         Util.printLogAndRethrowError(t, logger);
405       }
406     } else {
407       obj.wait(millis);
408     }
409   }
410
411   public void objectWait2(Object JavaDoc obj, long millis, int nanos) throws InterruptedException JavaDoc {
412     TCObject tco = lookupExistingOrNull(obj);
413
414     if (tco != null) {
415       try {
416         WaitInvocation call = new WaitInvocation(millis, nanos);
417         if (runtimeLogger.waitNotifyDebug()) {
418           runtimeLogger.objectWait(call, obj, tco);
419         }
420         this.txManager.wait(generateAutolockName(tco), call, obj);
421       } catch (InterruptedException JavaDoc ie) {
422         throw ie;
423       } catch (Throwable JavaDoc t) {
424         Util.printLogAndRethrowError(t, logger);
425       }
426     } else {
427       obj.wait(millis, nanos);
428     }
429   }
430
431   private boolean isLiteralAutolock(Object JavaDoc o) {
432     if (o instanceof Manageable) { return false; }
433     return (!(o instanceof Class JavaDoc)) && literals.isLiteralInstance(o);
434   }
435
436   public void monitorEnter(Object JavaDoc obj, int type) {
437     if (obj == null) { throw new NullPointerException JavaDoc("monitorEnter called on a null object"); }
438
439     TCObject tco = lookupExistingOrNull(obj);
440
441     try {
442       if (tco != null) {
443         if (tco.autoLockingDisabled()) { return; }
444
445         begin(generateAutolockName(tco), type, obj, tco);
446       } else if (isLiteralAutolock(obj)) {
447         begin(generateLiteralLockName(obj), type, obj, null);
448       }
449     } catch (Throwable JavaDoc t) {
450       Util.printLogAndRethrowError(t, logger);
451     }
452   }
453
454   public void monitorExit(Object JavaDoc obj) {
455     if (obj == null) { throw new NullPointerException JavaDoc("monitorExit called on a null object"); }
456
457     TCObject tco = lookupExistingOrNull(obj);
458
459     try {
460       if (tco != null) {
461         if (tco.autoLockingDisabled()) { return; }
462
463         // don't call this.commit() here, the error handling would happen twice in that case
464
this.txManager.commit(generateAutolockName(tco));
465       } else if (isLiteralAutolock(obj)) {
466         this.txManager.commit(generateLiteralLockName(obj));
467       }
468     } catch (Throwable JavaDoc t) {
469       Util.printLogAndRethrowError(t, logger);
470     }
471   }
472
473   public boolean isLocked(Object JavaDoc obj) {
474     if (obj == null) { throw new NullPointerException JavaDoc("isLocked called on a null object"); }
475
476     TCObject tco = lookupExistingOrNull(obj);
477
478     if (tco != null) {
479       return this.txManager.isLocked(generateAutolockName(tco));
480     } else {
481       return this.txManager.isLocked(generateLiteralLockName(obj));
482     }
483   }
484
485   public boolean tryMonitorEnter(Object JavaDoc obj, int type) {
486     if (obj == null) { throw new NullPointerException JavaDoc("monitorEnter called on a null object"); }
487
488     TCObject tco = lookupExistingOrNull(obj);
489
490     try {
491       if (tco != null) {
492         if (tco.autoLockingDisabled()) { return false; }
493
494         return tryBegin(generateAutolockName(tco), type, obj, tco);
495       } else if (isLiteralAutolock(obj)) { return tryBegin(generateLiteralLockName(obj), type, obj, null); }
496     } catch (Throwable JavaDoc t) {
497       Util.printLogAndRethrowError(t, logger);
498     }
499     return false;
500   }
501
502   public boolean tryBeginLock(String JavaDoc lockID, int type) {
503     return tryBegin(lockID, type, null, null);
504   }
505
506   public boolean isHeldByCurrentThread(Object JavaDoc obj, int lockLevel) {
507     if (obj == null) { throw new NullPointerException JavaDoc("isHeldByCurrentThread called on a null object"); }
508
509     TCObject tco = lookupExistingOrNull(obj);
510
511     if (tco != null) {
512       return this.txManager.isHeldByCurrentThread(generateAutolockName(tco), lockLevel);
513     } else {
514       return this.txManager.isHeldByCurrentThread(generateLiteralLockName(obj), lockLevel);
515     }
516
517   }
518
519   public int queueLength(Object JavaDoc obj) {
520     if (obj == null) { throw new NullPointerException JavaDoc("queueLength called on a null object"); }
521
522     TCObject tco = lookupExistingOrNull(obj);
523
524     if (tco != null) {
525       return this.txManager.queueLength(generateAutolockName(tco));
526     } else {
527       return this.txManager.queueLength(generateLiteralLockName(obj));
528     }
529   }
530
531   public int waitLength(Object JavaDoc obj) {
532     if (obj == null) { throw new NullPointerException JavaDoc("waitLength called on a null object"); }
533
534     TCObject tco = lookupExistingOrNull(obj);
535
536     if (tco != null) {
537       return this.txManager.waitLength(generateAutolockName(tco));
538     } else {
539       return this.txManager.waitLength(generateLiteralLockName(obj));
540     }
541   }
542
543   public boolean isCreationInProgress() {
544     return this.objectManager.isCreationInProgress() || this.txManager.isTransactionLoggingDisabled();
545   }
546
547   public TCObject shareObjectIfNecessary(Object JavaDoc pojo) {
548     TCObject tobj = ((Manageable) pojo).__tc_managed();
549     if (tobj != null) { return tobj; }
550
551     try {
552       return this.objectManager.lookupOrShare(pojo);
553     } catch (Throwable JavaDoc t) {
554       Util.printLogAndRethrowError(t, logger);
555
556       // shouldn't get here
557
throw new AssertionError JavaDoc();
558     }
559   }
560
561   public TCObject lookupOrCreate(Object JavaDoc obj) {
562     if (obj instanceof Manageable) { return ((Manageable) obj).__tc_managed(); }
563     return this.objectManager.lookupOrCreate(obj);
564   }
565
566   public TCObject lookupExistingOrNull(Object JavaDoc pojo) {
567     if (pojo instanceof Manageable) { return ((Manageable) pojo).__tc_managed(); }
568
569     try {
570       return this.objectManager.lookupExistingOrNull(pojo);
571     } catch (Throwable JavaDoc t) {
572       Util.printLogAndRethrowError(t, logger);
573
574       // shouldn't get here
575
throw new AssertionError JavaDoc();
576     }
577   }
578
579   public Object JavaDoc lookupObject(ObjectID id) throws ClassNotFoundException JavaDoc {
580     return this.objectManager.lookupObject(id);
581   }
582
583   public boolean distributedMethodCall(Object JavaDoc receiver, String JavaDoc method, Object JavaDoc[] params, boolean runOnAllNodes) {
584     TCObject tco = lookupExistingOrNull(receiver);
585
586     try {
587       if (tco != null) {
588         return methodCallManager.distributedInvoke(receiver, method, params, runOnAllNodes);
589       } else {
590         return false;
591       }
592     } catch (Throwable JavaDoc t) {
593       Util.printLogAndRethrowError(t, logger);
594       return false;
595     }
596   }
597
598   public void distributedMethodCallCommit() {
599     methodCallManager.distributedInvokeCommit();
600   }
601
602   public void checkWriteAccess(Object JavaDoc context) {
603     // XXX: make sure that "context" is the ALWAYS the right object to check here, and then rename it
604
if (isManaged(context)) {
605       try {
606         txManager.checkWriteAccess(context);
607       } catch (Throwable JavaDoc t) {
608         Util.printLogAndRethrowError(t, logger);
609       }
610     }
611   }
612
613   public boolean isManaged(Object JavaDoc obj) {
614     if (obj instanceof Manageable) {
615       TCObject tcobj = ((Manageable) obj).__tc_managed();
616
617       return tcobj != null && tcobj.isShared();
618     }
619     return this.objectManager.isManaged(obj);
620   }
621
622   public Object JavaDoc lookupRoot(String JavaDoc name) {
623     try {
624       return this.objectManager.lookupRoot(name);
625     } catch (Throwable JavaDoc t) {
626       Util.printLogAndRethrowError(t, logger);
627
628       // shouldn't get here
629
throw new AssertionError JavaDoc();
630     }
631   }
632
633   private static String JavaDoc generateVolatileLockName(TCObject tcobj, String JavaDoc fieldName) {
634     Assert.assertNotNull(tcobj);
635     return ByteCodeUtil.generateVolatileLockName(tcobj.getObjectID(), fieldName);
636   }
637
638   private static String JavaDoc generateAutolockName(TCObject tcobj) {
639     Assert.assertNotNull(tcobj);
640     return ByteCodeUtil.generateAutolockName(tcobj.getObjectID());
641   }
642
643   private static String JavaDoc generateLiteralLockName(Object JavaDoc obj) {
644     Assert.assertNotNull(obj);
645     return ByteCodeUtil.generateLiteralLockName(obj);
646   }
647
648   public boolean isLogical(Object JavaDoc object) {
649     return this.config.isLogical(object.getClass().getName());
650   }
651
652   public boolean isRoot(String JavaDoc className, String JavaDoc fieldName) {
653     return this.config.isRoot(className, fieldName);
654   }
655
656   public Object JavaDoc deepCopy(Object JavaDoc source) {
657     Object JavaDoc ret = null;
658     try {
659       ret = this.objectManager.deepCopy(source, optimisticTransactionManager);
660     } catch (Throwable JavaDoc t) {
661       Util.printLogAndRethrowError(t, logger);
662     }
663     return ret;
664   }
665
666   public TCProperties getTCProperites() {
667     return TCPropertiesImpl.getProperties();
668   }
669
670   private class ShutdownAction implements Runnable JavaDoc {
671     public void run() {
672       // XXX: we should just call stop(), but for the 1.5 (chex) release, I'm reverting the behavior
673
// stop();
674

675       shutdown(true);
676     }
677   }
678
679   public void optimisticBegin() {
680     this.optimisticTransactionManager.begin();
681   }
682
683   public void optimisticCommit() throws ClassNotFoundException JavaDoc {
684     this.optimisticTransactionManager.commit();
685   }
686
687   public void optimisticRollback() {
688     this.optimisticTransactionManager.rollback();
689   }
690
691   public boolean isPhysicallyInstrumented(Class JavaDoc clazz) {
692     return this.portability.isClassPhysicallyInstrumented(clazz);
693   }
694
695   public TCLogger getLogger(String JavaDoc loggerName) {
696     return TCLogging.getLogger(loggerName);
697   }
698
699   private static class MethodDisplayNames {
700
701     private final Map JavaDoc display = new HashMap JavaDoc();
702
703     public MethodDisplayNames(SerializationUtil serializer) {
704       String JavaDoc[] sigs = serializer.getSignatures();
705       for (int i = 0; i < sigs.length; i++) {
706         display.put(sigs[i], getDisplayStringFor(sigs[i]));
707       }
708     }
709
710     private String JavaDoc getDisplayStringFor(String JavaDoc signature) {
711       String JavaDoc methodName = signature.substring(0, signature.indexOf('('));
712       StringBuffer JavaDoc rv = new StringBuffer JavaDoc(methodName);
713       rv.append('(');
714
715       Type[] args = Type.getArgumentTypes(signature.substring(signature.indexOf('(')));
716       for (int i = 0; i < args.length; i++) {
717         if (i > 0) {
718           rv.append(',');
719         }
720         Type t = args[i];
721         int sort = t.getSort();
722         switch (sort) {
723           case Type.ARRAY:
724             Type elemType = t.getElementType();
725             if (elemType.getSort() == Type.OBJECT) {
726               rv.append(getShortName(elemType));
727             } else {
728               rv.append(elemType.getClassName());
729             }
730             for (int d = t.getDimensions(); d > 0; --d) {
731               rv.append("[]");
732             }
733             break;
734           case Type.OBJECT:
735             rv.append(getShortName(t));
736             break;
737           case Type.BOOLEAN:
738           case Type.BYTE:
739           case Type.CHAR:
740           case Type.DOUBLE:
741           case Type.FLOAT:
742           case Type.INT:
743           case Type.LONG:
744           case Type.SHORT:
745             rv.append(t.getClassName());
746             break;
747           default:
748             throw new AssertionError JavaDoc("unknown sort: " + sort);
749         }
750       }
751
752       rv.append(')');
753       return rv.toString();
754     }
755
756     private String JavaDoc getShortName(Type t) {
757       String JavaDoc fqName = t.getClassName();
758       int lastDot = fqName.lastIndexOf('.');
759       if (lastDot > -1) { return fqName.substring(lastDot + 1); }
760       return fqName;
761     }
762
763     String JavaDoc getDisplayForSignature(String JavaDoc methodSignature) {
764       String JavaDoc rv = (String JavaDoc) display.get(methodSignature);
765       if (rv == null) { throw new AssertionError JavaDoc("missing display string for signature: " + methodSignature); }
766       return rv;
767     }
768   }
769
770   public void addClusterEventListener(ClusterEventListener cel) {
771     cluster.addClusterEventListener(cel);
772   }
773
774   public DmiManager getDmiManager() {
775     return this.methodCallManager;
776   }
777
778 }
779
Popular Tags