001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Contact: sequoia@continuent.org
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: * Initial developer(s): Nicolas Modrzyk
021: * Contributor(s): Emmanuel Cecchet, Marc Herbert
022: */package org.continuent.sequoia.common.stream;
023:
024: import java.io.BufferedOutputStream;
025: import java.io.IOException;
026: import java.net.Socket;
027:
028: import org.continuent.sequoia.driver.SequoiaUrl;
029:
030: /**
031: * {@link java.io.DataOutputStream} subclass used between the controller and the
032: * driver.
033: * <p>
034: * Creates a buffered DataOutputStream upon a given {@link java.net.Socket} and
035: * adds its creation date (as a <code>long</code>) for statistics purposes on
036: * the socket. This class is now an implementation detail and references to it
037: * should be replaced by references to the more abstract
038: * LongUTFDataOutputStream. Ultimaltely this class could/should be moved out of
039: * the protocol package, closer to the very few classes that really need to
040: * reference it.
041: *
042: * @see java.io.DataInputStream
043: * @see org.continuent.sequoia.common.stream.DriverBufferedOutputStream
044: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
045: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
046: * @author <a href="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
047: * @author <a href="mailto:Gilles.Rayrat@emicnetworks.com">Gilles Rayrat</a>
048: */
049: public class DriverBufferedOutputStream extends LongUTFDataOutputStream {
050: private Socket socket;
051: private long dateCreated;
052: private final int debugLevel;
053:
054: /**
055: * @see java.lang.Object#finalize()
056: */
057: protected void finalize() throws Throwable {
058: try {
059: /*
060: * This is just an extra safety net: this socket must be ALREADY closed at
061: * this time (check finalize()-related litterature)
062: */
063: if (!socket.isClosed()) {
064: if (debugLevel >= SequoiaUrl.DEBUG_LEVEL_DEBUG) {
065: System.err
066: .println("Socket was not closed, either someone forgot to"
067: + " call Connection.close() on "
068: + socket);
069: System.err
070: .println("or a finally { close(); } block is missing");
071: }
072:
073: socket.close();
074: }
075: } finally {
076: super .finalize();
077: }
078: }
079:
080: /**
081: * Controller has a different logging scheme: debugLevel = OFF
082: *
083: * @see #DriverBufferedOutputStream(Socket, int)
084: */
085: public DriverBufferedOutputStream(Socket clientSocket)
086: throws IOException {
087: this (clientSocket, SequoiaUrl.DEBUG_LEVEL_OFF);
088: }
089:
090: /**
091: * Creates a new <code>DriverBufferedOutputStream</code> on a socket and
092: * sets the creation date to the current system time.
093: *
094: * @param socket socket to monitor
095: * @param debugLevel debug level
096: * @throws IOException if an IO error occurs
097: */
098: public DriverBufferedOutputStream(Socket socket, int debugLevel)
099: throws IOException {
100: super (new BufferedOutputStream((socket.getOutputStream())));
101: this .socket = socket;
102: this .debugLevel = debugLevel;
103: dateCreated = System.currentTimeMillis();
104: }
105:
106: /**
107: * @return Returns the socket.
108: */
109: public Socket getSocket() {
110: return socket;
111: }
112:
113: /**
114: * @return Returns the creation date.
115: */
116: public long getDateCreated() {
117: return dateCreated;
118: }
119: }
|