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.BufferedInputStream;
025: import java.io.IOException;
026: import java.io.StreamCorruptedException;
027: import java.net.Socket;
028:
029: import org.continuent.sequoia.driver.SequoiaUrl;
030:
031: /**
032: * LongUTFDataInputStream subclass used between the controller and the driver.
033: * <p>
034: * Creates a buffered LongUTFDataInputStream upon a given
035: * {@link java.net.Socket} and adds its creation date (as a <code>long</code>)
036: * for statistics purposes on the socket (eg.
037: * {@link org.continuent.sequoia.controller.virtualdatabase.VirtualDatabaseWorkerThread#retrieveClientData()})
038: * This class is now an implementation detail and references to it should be
039: * replaced by references to the more abstract LongUTFDataInputStream.
040: * Ultimately this class could/should be moved out of the protocol package,
041: * closer to the very few classes that really need to reference it.
042: *
043: * @see org.continuent.sequoia.common.stream.LongUTFDataInputStream
044: * @see org.continuent.sequoia.common.stream.DriverBufferedOutputStream
045: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
046: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
047: * @author <a href="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
048: * @author <a href="mailto:Gilles.Rayrat@emicnetworks.com">Gilles Rayrat</a>
049: */
050: public class DriverBufferedInputStream extends LongUTFDataInputStream {
051: private Socket socket;
052: private long dateCreated;
053: private final int debugLevel;
054:
055: /**
056: * @see java.lang.Object#finalize()
057: */
058: protected void finalize() throws Throwable {
059: try {
060: /*
061: * This is just an extra safety net: this socket must be ALREADY closed at
062: * this time (check finalize()-related litterature)
063: */
064: if (!socket.isClosed()) {
065: if (debugLevel >= SequoiaUrl.DEBUG_LEVEL_DEBUG) {
066: System.err
067: .println("Socket was not closed, either someone forgot to"
068: + " call Connection.close() on "
069: + socket);
070: System.err
071: .println("or a finally { close(); } block is missing");
072: }
073:
074: socket.close();
075: }
076:
077: } finally {
078: super .finalize();
079: }
080: }
081:
082: /**
083: * Controller has a different logging scheme: debugLevel = OFF
084: *
085: * @see #DriverBufferedInputStream(Socket, int)
086: */
087: public DriverBufferedInputStream(Socket clientSocket)
088: throws IOException, StreamCorruptedException {
089: this (clientSocket, SequoiaUrl.DEBUG_LEVEL_OFF);
090: }
091:
092: /**
093: * Creates a buffered Stream on a socket and sets the creation date to the
094: * current system time.
095: *
096: * @param socket socket for this stream
097: * @param debugLevel debug level
098: * @throws IOException if an error occurs
099: * @throws StreamCorruptedException if an error occurs
100: */
101: public DriverBufferedInputStream(Socket socket, int debugLevel)
102: throws IOException, StreamCorruptedException {
103: super (new BufferedInputStream(socket.getInputStream()));
104: this .socket = socket;
105: this .debugLevel = debugLevel;
106: dateCreated = System.currentTimeMillis();
107: }
108:
109: /**
110: * @return Returns the socket.
111: */
112: public Socket getSocket() {
113: return socket;
114: }
115:
116: /**
117: * @return Returns the creation date.
118: */
119: public long getDateCreated() {
120: return dateCreated;
121: }
122: }
|