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.BufferedInputStream;
021: import java.io.DataInputStream;
022: import java.io.DataOutputStream;
023: import java.io.IOException;
024: import java.net.UnknownHostException;
025:
026: import jcifs.smb.NtlmPasswordAuthentication;
027: import jcifs.smb.SmbNamedPipe;
028:
029: /**
030: * This class implements inter-process communication (IPC) to the
031: * database server using named pipes.
032: *
033: * @todo Extract abstract base class SharedIpc from <code>SharedSocket</code> and this class.
034: * @todo Implement connection timeouts for named pipes.
035: *
036: * @author David D. Kilzer
037: * @version $Id: SharedNamedPipe.java,v 1.19 2007/07/12 20:34:47 bheineman Exp $
038: */
039: public class SharedNamedPipe extends SharedSocket {
040: /**
041: * The shared named pipe.
042: */
043: private SmbNamedPipe pipe;
044:
045: /**
046: * Creates a new instance of <code>SharedNamedPipe</code>.
047: *
048: * @param connection
049: * @throws IOException if the named pipe or its input or output streams do
050: * not open
051: * @throws UnknownHostException if host cannot be found for the named pipe
052: */
053: public SharedNamedPipe(ConnectionJDBC2 connection)
054: throws IOException {
055: super (connection.getBufferDir(), connection.getTdsVersion(),
056: connection.getServerType());
057:
058: NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
059: connection.getDomainName(), connection.getUser(),
060: connection.getPassword());
061:
062: StringBuffer url = new StringBuffer(32);
063:
064: url.append("smb://");
065: url.append(connection.getServerName());
066: url.append("/IPC$");
067:
068: final String instanceName = connection.getInstanceName();
069: if (instanceName != null && instanceName.length() != 0) {
070: url.append("/MSSQL$");
071: url.append(instanceName);
072: }
073:
074: String namedPipePath = DefaultProperties
075: .getNamedPipePath(connection.getServerType());
076: url.append(namedPipePath);
077:
078: setPipe(new SmbNamedPipe(url.toString(),
079: SmbNamedPipe.PIPE_TYPE_RDWR, auth));
080:
081: setOut(new DataOutputStream(getPipe()
082: .getNamedPipeOutputStream()));
083:
084: final int bufferSize = Support.calculateNamedPipeBufferSize(
085: connection.getTdsVersion(), connection.getPacketSize());
086: setIn(new DataInputStream(new BufferedInputStream(getPipe()
087: .getNamedPipeInputStream(), bufferSize)));
088: }
089:
090: /**
091: * Get the connected status of this socket.
092: *
093: * @return true if the underlying socket is connected
094: */
095: boolean isConnected() {
096: return getPipe() != null;
097: }
098:
099: /**
100: * Close the socket (noop if in shared mode).
101: */
102: void close() throws IOException {
103: super .close();
104: getOut().close();
105: getIn().close();
106: //getPipe().close();
107: }
108:
109: /**
110: * Force close the socket causing any pending reads/writes to fail.
111: * <p/>
112: * Used by the login timer to abort a login attempt.
113: */
114: void forceClose() {
115: try {
116: getOut().close();
117: } catch (IOException e) {
118: // Ignore
119: } finally {
120: setOut(null);
121: }
122:
123: try {
124: getIn().close();
125: } catch (IOException e) {
126: // Ignore
127: } finally {
128: setIn(null);
129: }
130:
131: setPipe(null);
132: }
133:
134: /**
135: * Getter for {@link SharedNamedPipe#pipe} field.
136: *
137: * @return {@link SmbNamedPipe} used for communication
138: */
139: private SmbNamedPipe getPipe() {
140: return pipe;
141: }
142:
143: /**
144: * Setter for {@link SharedNamedPipe#pipe} field.
145: *
146: * @param pipe {@link SmbNamedPipe} to be used for communication
147: */
148: private void setPipe(SmbNamedPipe pipe) {
149: this .pipe = pipe;
150: }
151:
152: /**
153: * Set the socket timeout.
154: * <p/>
155: * Noop for now; timeouts are not implemented for SMB named pipes.
156: *
157: * @param timeout timeout value in milliseconds
158: */
159: protected void setTimeout(int timeout) {
160: // FIXME - implement timeout functionality
161: }
162: }
|