2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software 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 GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.remoting.transport.socket;
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.io.OutputStream;
28
import java.net.Socket;
30
import org.jboss.remoting.InvokerLocator;
31
import org.jboss.remoting.marshal.Marshaller;
32
import org.jboss.remoting.marshal.PreferredStreamMarshaller;
33
import org.jboss.remoting.marshal.PreferredStreamUnMarshaller;
34
import org.jboss.remoting.marshal.UnMarshaller;
35
import org.jboss.logging.Logger;
38
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
40
public class ClientSocketWrapper extends SocketWrapper implements OpenConnectionChecker
42
// Constants ------------------------------------------------------------------------------------
44
private static final Logger log = Logger.getLogger(ClientSocketWrapper.class);
46
// Static ---------------------------------------------------------------------------------------
48
private static boolean trace = log.isTraceEnabled();
50
// Attributes -----------------------------------------------------------------------------------
52
private InputStream in;
53
private OutputStream out;
54
private int writeTimeout = -1;
56
// Constructors ---------------------------------------------------------------------------------
58
public ClientSocketWrapper(Socket socket) throws IOException
61
createStreams(socket, null);
64
public ClientSocketWrapper(Socket socket, Map metadata, Integer timeout) throws Exception
66
super(socket, timeout);
67
createStreams(socket, metadata);
70
// SocketWrapper overrides ----------------------------------------------------------------------
72
public OutputStream getOutputStream()
77
public InputStream getInputStream()
82
public int getWriteTimeout()
87
public void setWriteTimeout(int writeTimeout)
89
this.writeTimeout = writeTimeout;
92
public void checkConnection() throws IOException
94
// Test to see if socket is alive by send ACK message
98
// out.writeByte(ACK);
105
if (trace) { log.trace(this + " got " + i + " while checking connection"); }
108
// OpenConnectionChecker implementation ---------------------------------------------------------
110
public void checkOpenConnection() throws IOException
112
if (trace) log.trace("checking open connection");
113
if (in.available() > 1)
115
log.trace("remote endpoint has closed");
116
throw new IOException("remote endpoint has closed");
120
// Public ---------------------------------------------------------------------------------------
122
public String toString()
124
Socket socket = getSocket();
125
return "ClientSocketWrapper[" + socket + "." +
126
Integer.toHexString(System.identityHashCode(socket)) + "]";
129
// Package protected ----------------------------------------------------------------------------
131
// Protected ------------------------------------------------------------------------------------
133
protected void createStreams(Socket socket, Map metadata) throws IOException
136
String serializationType = "java"; // hardcoding to default to java serialization
140
String serializationTypeParam = (String) metadata.get(InvokerLocator.SERIALIZATIONTYPE);
141
if(serializationTypeParam == null || serializationTypeParam.length() == 0)
143
serializationTypeParam = (String) metadata.get(InvokerLocator.SERIALIZATIONTYPE_CASED);
145
if(serializationTypeParam != null && serializationTypeParam.length() > 0)
147
serializationType = serializationTypeParam;
151
Marshaller marshaller = null;
152
UnMarshaller unmarshaller = null;
153
int tempTimeout = -1;
154
int savedTimeout = getTimeout();
156
if (metadata != null)
158
marshaller = (Marshaller) metadata.get(MARSHALLER);
159
unmarshaller = (UnMarshaller) metadata.get(UNMARSHALLER);
160
Object o = metadata.get(TEMP_TIMEOUT);
161
if (o instanceof Integer)
163
tempTimeout = ((Integer) o).intValue();
164
if (tempTimeout != -1)
166
socket.setSoTimeout(tempTimeout);
167
log.trace("set temp timeout to: " + tempTimeout);
170
o = metadata.get(WRITE_TIMEOUT);
171
if (o instanceof Integer)
173
writeTimeout = ((Integer) o).intValue();
174
if (writeTimeout != -1)
176
log.trace("set writeTimeout to: " + writeTimeout);
181
out = createOutputStream(serializationType, socket, marshaller);
182
in = createInputStream(serializationType, socket, unmarshaller);
183
setTimeout(savedTimeout);
184
log.trace("reset timeout: " + savedTimeout);
187
protected InputStream createInputStream(String serializationType, Socket socket, UnMarshaller unmarshaller)
190
if (trace) { log.trace(this + " getting input stream from " + socket + ", " + unmarshaller); }
192
if (unmarshaller == null)
193
log.warn("got null unmarshaller");
195
InputStream is = socket.getInputStream();
196
if (unmarshaller instanceof PreferredStreamUnMarshaller)
198
PreferredStreamUnMarshaller psum = (PreferredStreamUnMarshaller) unmarshaller;
199
is = psum.getMarshallingStream(is);
205
protected OutputStream createOutputStream(String serializationType, Socket socket, Marshaller marshaller)
208
if (trace) { log.trace(this + " getting output stream from " + socket + ", " + marshaller); }
211
if (marshaller == null)
212
log.warn("got null marshaller");
214
OutputStream os = socket.getOutputStream();
215
if (writeTimeout > 0)
217
os = new TimedOutputStream(os, writeTimeout);
220
if (marshaller instanceof PreferredStreamMarshaller)
222
PreferredStreamMarshaller psm = (PreferredStreamMarshaller) marshaller;
223
os = psm.getMarshallingStream(os);
229
// Private --------------------------------------------------------------------------------------
231
// Inner classes --------------------------------------------------------------------------------