KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > core > AbstractSocket


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.core;
25
26 import java.io.*;
27 import java.net.*;
28 import javax.net.ssl.*;
29
30 import org.myoodb.util.*;
31
32 public abstract class AbstractSocket implements AbstractConnection, java.lang.Comparable JavaDoc
33 {
34     private static javax.net.ssl.SSLContext m_sslContext = null;
35
36     public final static int BUFFER_SIZE = 8192;
37
38     private Timer m_timer;
39     private Long JavaDoc m_identifier;
40
41     protected Socket m_socket;
42     protected ObjectInputStream m_in;
43     protected ObjectOutputStream m_out;
44
45     public static void setSSLContextDefault(javax.net.ssl.SSLContext sslContext)
46     {
47         m_sslContext = sslContext;
48     }
49
50     public static javax.net.ssl.SSLContext getSSLContextDefault()
51     {
52         return m_sslContext;
53     }
54
55     public AbstractSocket(Socket socket) throws java.io.IOException JavaDoc
56     {
57         m_timer = null;
58         m_identifier = null;
59
60         m_socket = socket;
61
62         init();
63     }
64
65     public AbstractSocket(Long JavaDoc id, String JavaDoc host, int port, boolean secure, int timeout, javax.net.ssl.SSLContext ssl) throws java.io.IOException JavaDoc
66     {
67         m_timer = null;
68         m_identifier = id;
69
70         if (secure == true)
71         {
72             SSLSocketFactory sslsocketfactory = null;
73
74             if (ssl != null)
75             {
76                 sslsocketfactory = (SSLSocketFactory) ssl.getSocketFactory();
77             }
78
79             if ((m_sslContext != null) && (sslsocketfactory == null))
80             {
81                 sslsocketfactory = (SSLSocketFactory) m_sslContext.getSocketFactory();
82             }
83
84             if (sslsocketfactory == null)
85             {
86                 sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
87             }
88
89             m_socket = (Socket) sslsocketfactory.createSocket();
90         }
91         else
92         {
93             m_socket = new Socket();
94         }
95  
96         if (timeout != -1)
97         {
98             m_socket.connect(new InetSocketAddress(host, port), timeout);
99         }
100         else
101         {
102             m_socket.connect(new InetSocketAddress(host, port));
103         }
104
105         init();
106     }
107     
108     protected void init() throws java.io.IOException JavaDoc
109     {
110         m_socket.setKeepAlive(true);
111         m_socket.setTcpNoDelay(true);
112         m_socket.setSendBufferSize(BUFFER_SIZE);
113         m_socket.setReceiveBufferSize(BUFFER_SIZE);
114
115         m_out = getObjectOutputStream(m_socket.getOutputStream());
116         m_out.flush();
117         m_in = getObjectInputStream(m_socket.getInputStream());
118     }
119
120     protected ObjectInputStream getObjectInputStream(InputStream inputStream) throws java.io.IOException JavaDoc
121     {
122         return new FastObjectInputStream(new BufferedInputStream(inputStream, BUFFER_SIZE));
123     }
124
125     protected ObjectOutputStream getObjectOutputStream(OutputStream outputStream) throws java.io.IOException JavaDoc
126     {
127         return new FastObjectOutputStream(new BufferedOutputStream(outputStream, BUFFER_SIZE));
128     }
129
130     public void send(Object JavaDoc obj) throws java.io.IOException JavaDoc
131     {
132         if (m_out != null)
133         {
134             try
135             {
136                 m_out.writeObject(obj);
137             }
138             finally
139             {
140                 m_out.flush();
141                 m_out.reset();
142             }
143         }
144     }
145     
146     public Object JavaDoc receive(int timeout) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc, org.myoodb.exception.TimeoutException
147     {
148         if (m_in == null)
149         {
150             throw new java.io.IOException JavaDoc("connection has been closed");
151         }
152
153         if (timeout != -1)
154         {
155             m_timer = new Timer(m_in, timeout);
156             m_timer.start();
157         }
158
159         Object JavaDoc result = null;
160
161         try
162         {
163             result = m_in.readObject();
164         }
165         catch (java.io.IOException JavaDoc e)
166         {
167             if ((timeout != -1) && (m_timer.hasExpired() == true))
168             {
169                 throw new org.myoodb.exception.TimeoutException("Receive timed out");
170             }
171         }
172
173         if (timeout != -1)
174         {
175             m_timer.halt();
176             m_timer = null;
177         }
178
179         return result;
180     }
181     
182     public synchronized void close() throws java.io.IOException JavaDoc
183     {
184         if (m_socket != null)
185         {
186             m_in = null;
187             m_out = null;
188
189             m_socket.close();
190             m_socket = null;
191         }
192     }
193
194     public boolean isConnected()
195     {
196         return (m_socket != null);
197     }
198
199     public Socket getSocket()
200     {
201         return m_socket;
202     }
203
204     public int hashCode()
205     {
206         return (m_identifier != null) ? m_identifier.hashCode() : super.hashCode();
207     }
208
209     public boolean equals(Object JavaDoc obj)
210     {
211         return (m_identifier != null) ? m_identifier.equals(obj) : super.equals(obj);
212     }
213
214     public int compareTo(Object JavaDoc obj)
215     {
216         return (m_identifier != null) && (obj instanceof AbstractSocket) ? m_identifier.compareTo(((AbstractSocket) obj).m_identifier) : -1;
217     }
218 }
219
Popular Tags