001: // jTDS JDBC Driver for Microsoft SQL Server and Sybase
002: // Copyright (C) 2004 The jTDS Project
003: //
004: // This library is free software; you can redistribute it and/or
005: // modify it under the terms of the GNU Lesser General Public
006: // License as published by the Free Software Foundation; either
007: // version 2.1 of the License, or (at your option) any later version.
008: //
009: // This library is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: // Lesser General Public License for more details.
013: //
014: // You should have received a copy of the GNU Lesser General Public
015: // License along with this library; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: //
018: package net.sourceforge.jtds.jdbc;
019:
020: import java.io.*;
021:
022: /**
023: * This class implements inter-process communication (IPC) to the database
024: * server using local named pipes (will only work on Windows).
025: *
026: * @author Adam Etheredge
027: * @version $Id: SharedLocalNamedPipe.java,v 1.12 2007/07/08 21:38:13 bheineman Exp $
028: */
029: public class SharedLocalNamedPipe extends SharedSocket {
030: /**
031: * The named pipe as a file.
032: */
033: RandomAccessFile pipe;
034:
035: /**
036: * Creates a new instance of <code>SharedLocalNamedPipe</code>.
037: *
038: * @param connection the connection object
039: * @throws IOException if an I/O error occurs
040: */
041: public SharedLocalNamedPipe(ConnectionJDBC2 connection)
042: throws IOException {
043: super (connection.getBufferDir(), connection.getTdsVersion(),
044: connection.getServerType());
045:
046: final String serverName = connection.getServerName();
047: final String instanceName = connection.getInstanceName();
048:
049: final StringBuffer pipeName = new StringBuffer(64);
050: pipeName.append("\\\\");
051: if (serverName == null || serverName.length() == 0) {
052: pipeName.append('.');
053: } else {
054: pipeName.append(serverName);
055: }
056: pipeName.append("\\pipe");
057: if (instanceName != null && instanceName.length() != 0) {
058: pipeName.append("\\MSSQL$").append(instanceName);
059: }
060: String namedPipePath = DefaultProperties
061: .getNamedPipePath(connection.getServerType());
062: pipeName.append(namedPipePath.replace('/', '\\'));
063:
064: this .pipe = new RandomAccessFile(pipeName.toString(), "rw");
065:
066: final int bufferSize = Support.calculateNamedPipeBufferSize(
067: connection.getTdsVersion(), connection.getPacketSize());
068: setOut(new DataOutputStream(new BufferedOutputStream(
069: new FileOutputStream(this .pipe.getFD()), bufferSize)));
070: setIn(new DataInputStream(new BufferedInputStream(
071: new FileInputStream(this .pipe.getFD()), bufferSize)));
072: }
073:
074: /**
075: * Get the connected status of this socket.
076: *
077: * @return <code>true</code> if the underlying named pipe is connected
078: */
079: boolean isConnected() {
080: return pipe != null;
081: }
082:
083: /**
084: * Send an network packet. If output for another virtual socket is in
085: * progress this packet will be sent later.
086: *
087: * @param streamId the originating <code>RequestStream</code> object
088: * @param buffer the data to send
089: * @exception java.io.IOException if an I/O error occurs
090: */
091: byte[] sendNetPacket(int streamId, byte buffer[])
092: throws IOException {
093: byte[] ret = super .sendNetPacket(streamId, buffer);
094: getOut().flush();
095: return ret;
096: }
097:
098: /**
099: * Close the named pipe and virtual sockets and release any resources.
100: */
101: void close() throws IOException {
102: try {
103: // Close virtual sockets
104: super .close();
105:
106: getOut().close();
107: setOut(null);
108: getIn().close();
109: setIn(null);
110:
111: if (pipe != null) {
112: pipe.close();
113: }
114: } finally {
115: pipe = null;
116: }
117: }
118:
119: /**
120: * Force close the socket causing any pending reads/writes to fail.
121: * <p>
122: * Used by the login timer to abort a login attempt.
123: */
124: void forceClose() {
125: try {
126: getOut().close();
127: } catch (Exception e) {
128: // Ignore
129: } finally {
130: setOut(null);
131: }
132:
133: try {
134: getIn().close();
135: } catch (Exception e) {
136: // Ignore
137: } finally {
138: setIn(null);
139: }
140:
141: try {
142: if (pipe != null) {
143: pipe.close();
144: }
145: } catch (IOException ex) {
146: } finally {
147: pipe = null;
148: }
149: }
150:
151: /**
152: * Set the socket timeout.
153: *
154: * @param timeout the timeout value in milliseconds
155: */
156: protected void setTimeout(int timeout) {
157: // FIXME - implement timeout functionality
158: }
159: }
|