KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > ClientShutdownManager


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;
6
7 import com.tc.async.api.StageManager;
8 import com.tc.config.schema.dynamic.ConfigItem;
9 import com.tc.logging.TCLogger;
10 import com.tc.logging.TCLogging;
11 import com.tc.net.core.ConnectionInfo;
12 import com.tc.net.protocol.tcm.CommunicationsManager;
13 import com.tc.object.bytecode.hook.impl.PreparedComponentsFromL2Connection;
14 import com.tc.object.handshakemanager.ClientHandshakeManager;
15 import com.tc.object.net.DSOClientMessageChannel;
16 import com.tc.object.tx.RemoteTransactionManager;
17
18 public class ClientShutdownManager {
19   private static final TCLogger logger = TCLogging.getLogger(ClientShutdownManager.class);
20   private final RemoteTransactionManager rtxManager;
21   private final StageManager stageManager;
22   private final ClientObjectManager objectManager;
23   private final DSOClientMessageChannel channel;
24   private final CommunicationsManager commsManager;
25   private final ClientHandshakeManager handshakeManager;
26   private final PreparedComponentsFromL2Connection connectionComponents;
27
28   public ClientShutdownManager(ClientObjectManager objectManager, RemoteTransactionManager rtxManager,
29                                StageManager stageManager, CommunicationsManager commsManager,
30                                DSOClientMessageChannel channel, ClientHandshakeManager handshakeManager,
31                                PreparedComponentsFromL2Connection connectionComponents) {
32     this.objectManager = objectManager;
33     this.rtxManager = rtxManager;
34     this.stageManager = stageManager;
35     this.commsManager = commsManager;
36     this.channel = channel;
37     this.handshakeManager = handshakeManager;
38     this.connectionComponents = connectionComponents;
39   }
40
41   public void execute(boolean fromShutdownHook) {
42     closeLocalWork();
43
44     if (!fromShutdownHook) {
45       shutdown();
46     }
47   }
48
49   private void closeLocalWork() {
50
51     boolean immediate = isImmediate();
52     if (!immediate) {
53       if (rtxManager != null) {
54         try {
55           rtxManager.stop();
56         } catch (Throwable JavaDoc t) {
57           logger.error("Error shutting down remote transaction manager", t);
58         }
59       }
60     } else {
61       logger.warn("DSO Client exiting without flushing local work");
62     }
63
64   }
65
66   private boolean isImmediate() {
67     // XXX: Race condition here --> we can start the non-immediate shutdown procedure becuase we think the channel is
68
// open, but it can die before we start (or in the middle of) flushing the local work
69
if (channel.isConnected()) { return false; }
70
71     // If we've connected to a persisent server, we should try to flush
72
if (handshakeManager.serverIsPersistent()) { return false; }
73
74     // If we think there is more than one server out there, we should try to flush
75
ConfigItem connectionInfoItem = this.connectionComponents.createConnectionInfoConfigItem();
76     ConnectionInfo[] connectionInfo = (ConnectionInfo[]) connectionInfoItem.getObject();
77     return connectionInfo.length == 1;
78   }
79
80   private void shutdown() {
81
82     if (stageManager != null) {
83       try {
84         stageManager.stopAll();
85       } catch (Throwable JavaDoc t) {
86         logger.error("Error stopping stage manager", t);
87       }
88     }
89
90     if (objectManager != null) {
91       try {
92         objectManager.shutdown();
93       } catch (Throwable JavaDoc t) {
94         logger.error("Error shutting down client object manager", t);
95       }
96     }
97
98     if (channel != null) {
99       try {
100         channel.close();
101       } catch (Throwable JavaDoc t) {
102         logger.error("Error closing channel", t);
103       }
104     }
105
106     if (commsManager != null) {
107       try {
108         commsManager.shutdown();
109       } catch (Throwable JavaDoc t) {
110         logger.error("Error shutting down communications manager", t);
111       }
112     }
113   }
114
115 }
116
Popular Tags