001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.core;
025:
026: import java.io.*;
027: import java.net.*;
028: import javax.net.ssl.*;
029:
030: public abstract class AbstractSocket extends AbstractConnection {
031: private static volatile SSLContext s_sslContext = null;
032:
033: private Timer m_timer;
034:
035: protected Socket m_socket;
036: protected ObjectInputStream m_in;
037: protected ObjectOutputStream m_out;
038:
039: public static void setSSLContextDefault(SSLContext sslContext) {
040: s_sslContext = sslContext;
041: }
042:
043: public static SSLContext getSSLContextDefault() {
044: return s_sslContext;
045: }
046:
047: public AbstractSocket(AbstractDatabase db, Socket socket)
048: throws java.io.IOException {
049: super (db, null);
050:
051: m_socket = socket;
052:
053: init();
054: }
055:
056: public AbstractSocket(AbstractDatabase db, Long id, String host,
057: int port, int timeout, boolean secure, SSLContext ssl)
058: throws IOException {
059: super (db, id);
060:
061: if (secure == true) {
062: SSLSocketFactory sslsocketfactory = null;
063:
064: if (ssl != null) {
065: sslsocketfactory = (SSLSocketFactory) ssl
066: .getSocketFactory();
067: }
068:
069: if ((s_sslContext != null) && (sslsocketfactory == null)) {
070: sslsocketfactory = (SSLSocketFactory) s_sslContext
071: .getSocketFactory();
072: }
073:
074: if (sslsocketfactory == null) {
075: sslsocketfactory = (SSLSocketFactory) SSLSocketFactory
076: .getDefault();
077: }
078:
079: m_socket = (Socket) sslsocketfactory.createSocket();
080: } else {
081: m_socket = new Socket();
082: }
083:
084: if (timeout != -1) {
085: m_socket
086: .connect(new InetSocketAddress(host, port), timeout);
087: } else {
088: m_socket.connect(new InetSocketAddress(host, port));
089: }
090:
091: init();
092: }
093:
094: protected void init() throws IOException {
095: m_timer = null;
096:
097: m_socket.setKeepAlive(true);
098: m_socket.setTcpNoDelay(true);
099: m_socket.setSendBufferSize(getBufferSize());
100: m_socket.setReceiveBufferSize(getBufferSize());
101:
102: m_out = getObjectOutputStream(m_socket.getOutputStream());
103: m_out.flush();
104: m_in = getObjectInputStream(m_socket.getInputStream());
105: }
106:
107: protected void setSoTimeout(int timeout) throws SocketException {
108: m_socket.setSoTimeout(timeout);
109: }
110:
111: protected int getSoTimeout() throws SocketException {
112: return m_socket.getSoTimeout();
113: }
114:
115: public void send(Object obj) throws IOException {
116: if (m_out != null) {
117: try {
118: m_out.writeObject(obj);
119: } finally {
120: m_out.flush();
121: m_out.reset();
122: }
123: }
124: }
125:
126: public Object receive(int timeout) throws IOException,
127: ClassNotFoundException,
128: org.myoodb.exception.TimeoutException {
129: if (m_in == null) {
130: throw new IOException("connection has been closed");
131: }
132:
133: if (timeout != -1) {
134: m_timer = new Timer(m_in, timeout);
135: m_timer.setDaemon(true);
136: m_timer.setName("MyOodb Receive Tcp Timeout Thread: "
137: + m_socket.getLocalAddress());
138: m_timer.start();
139: }
140:
141: Object result = null;
142:
143: try {
144: result = m_in.readObject();
145: } catch (IOException e) {
146: if ((timeout != -1) && (m_timer.hasExpired() == true)) {
147: throw new org.myoodb.exception.TimeoutException(
148: "Receive timed out");
149: } else {
150: throw e;
151: }
152: } finally {
153: if (timeout != -1) {
154: m_timer.halt();
155: m_timer = null;
156: }
157: }
158:
159: return result;
160: }
161:
162: public void close() throws IOException {
163: Socket socket = m_socket;
164: m_socket = null;
165:
166: ObjectInputStream in = m_in;
167: m_in = null;
168:
169: ObjectOutputStream out = m_out;
170: m_out = null;
171:
172: if (socket != null) {
173: socket.close();
174: }
175:
176: if (out != null) {
177: out.close();
178: }
179:
180: if (in != null) {
181: in.close();
182: }
183: }
184:
185: public boolean isConnected() {
186: return (m_socket != null) ? m_socket.isInputShutdown() == false
187: : false;
188: }
189:
190: public Socket getSocket() {
191: return m_socket;
192: }
193:
194: public String toString() {
195: return (m_socket != null) ? m_socket.toString() : super
196: .toString();
197: }
198: }
|