001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2006 Continuent, Inc.
004: * Contact: sequoia@continuent.org
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * Initial developer(s): Gilles Rayrat.
019: * Contributor(s): ______________________.
020: */package org.continuent.sequoia.driver;
021:
022: import java.io.IOException;
023: import java.net.Socket;
024: import java.util.Date;
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import org.continuent.sequoia.driver.connectpolicy.AbstractControllerConnectPolicy;
032:
033: public class SocketKillerCallBack implements
034: ControllerStateChangedCallback {
035: /** The list of controllers with their set of sockets */
036: Map controllersAndSockets;
037: /** Policy that created us, to tell about controller state changes */
038: AbstractControllerConnectPolicy policy;
039: /** Level of logging (logs are printed to stdout, see {@link SequoiaUrl}) */
040: int logLevel;
041:
042: public SocketKillerCallBack(AbstractControllerConnectPolicy policy,
043: int logLevel) {
044: controllersAndSockets = new HashMap();
045: this .policy = policy;
046: this .logLevel = logLevel;
047: }
048:
049: public void registerSocket(ControllerInfo c, Socket s) {
050: Set sockets = null;
051: synchronized (controllersAndSockets) {
052: if (controllersAndSockets.containsKey(c))
053: sockets = (HashSet) controllersAndSockets.get(c);
054: else
055: sockets = new HashSet();
056: sockets.add(s);
057: controllersAndSockets.put(c, sockets);
058: }
059: }
060:
061: public void onControllerDown(ControllerInfo ctrl) {
062: // tell policy asap
063: if (policy != null)
064: policy.controllerDown(ctrl);
065: synchronized (controllersAndSockets) {
066: if (controllersAndSockets.containsKey(ctrl)) {
067: if (logLevel >= SequoiaUrl.DEBUG_LEVEL_DEBUG) {
068: System.out
069: .println(new Date()
070: + " Controller "
071: + ctrl
072: + " down - shutting down connected sockets");
073: }
074: Set sockets = (HashSet) controllersAndSockets.get(ctrl);
075: for (Iterator iter = sockets.iterator(); iter.hasNext();) {
076: Socket s = (Socket) iter.next();
077: if (s != null) {
078: try {
079: s.shutdownInput();
080: s.shutdownOutput();
081: } catch (IOException ignored) { // ignore errors
082: }
083: // close inside its own try/catch to make sure it is called
084: try {
085: s.close();
086: } catch (IOException ignored) { // ignore errors
087: }
088: }
089: }
090: // all sockets have been killed, remove them
091: sockets.clear();
092: }
093: }
094: }
095:
096: public void onControllerUp(ControllerInfo ctrl) {
097: // tell policy asap
098: if (policy != null)
099: policy.controllerUp(ctrl);
100: if (logLevel >= SequoiaUrl.DEBUG_LEVEL_DEBUG) {
101: System.out.println(new Date() + " Controller " + ctrl
102: + " is up again");
103: }
104: }
105:
106: }
|