~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/org/jboss/remoting/transport/socket/ClientSocketWrapper.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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.
6
 
*
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.
11
 
*
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.
16
 
*
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.
21
 
*/
22
 
 
23
 
package org.jboss.remoting.transport.socket;
24
 
 
25
 
import java.io.IOException;
26
 
import java.io.InputStream;
27
 
import java.io.OutputStream;
28
 
import java.net.Socket;
29
 
import java.util.Map;
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;
36
 
 
37
 
/**
38
 
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
39
 
 */
40
 
public class ClientSocketWrapper extends SocketWrapper implements OpenConnectionChecker
41
 
{
42
 
   // Constants ------------------------------------------------------------------------------------
43
 
 
44
 
   private static final Logger log = Logger.getLogger(ClientSocketWrapper.class);
45
 
 
46
 
   // Static ---------------------------------------------------------------------------------------
47
 
 
48
 
   private static boolean trace = log.isTraceEnabled();
49
 
 
50
 
   // Attributes -----------------------------------------------------------------------------------
51
 
 
52
 
   private InputStream in;
53
 
   private OutputStream out;
54
 
   private int writeTimeout = -1;
55
 
 
56
 
   // Constructors ---------------------------------------------------------------------------------
57
 
 
58
 
   public ClientSocketWrapper(Socket socket) throws IOException
59
 
   {
60
 
      super(socket);
61
 
      createStreams(socket, null);
62
 
   }
63
 
 
64
 
   public ClientSocketWrapper(Socket socket, Map metadata, Integer timeout) throws Exception
65
 
   {
66
 
      super(socket, timeout);
67
 
      createStreams(socket, metadata);
68
 
   }
69
 
 
70
 
   // SocketWrapper overrides ----------------------------------------------------------------------
71
 
 
72
 
   public OutputStream getOutputStream()
73
 
   {
74
 
      return out;
75
 
   }
76
 
 
77
 
   public InputStream getInputStream()
78
 
   {
79
 
      return in;
80
 
   }
81
 
 
82
 
   public int getWriteTimeout()
83
 
   {
84
 
      return writeTimeout;
85
 
   }
86
 
 
87
 
   public void setWriteTimeout(int writeTimeout)
88
 
   {
89
 
      this.writeTimeout = writeTimeout;
90
 
   }
91
 
 
92
 
   public void checkConnection() throws IOException
93
 
   {
94
 
      // Test to see if socket is alive by send ACK message
95
 
      final byte ACK = 1;
96
 
      
97
 
//      out.reset();
98
 
//      out.writeByte(ACK);
99
 
//      out.flush();
100
 
//      in.readByte();
101
 
 
102
 
      out.write(ACK);
103
 
      out.flush();
104
 
      int i = in.read();
105
 
      if (trace) { log.trace(this + " got " + i + " while checking connection"); }
106
 
   }
107
 
   
108
 
   // OpenConnectionChecker implementation ---------------------------------------------------------
109
 
   
110
 
   public void checkOpenConnection() throws IOException
111
 
   {
112
 
      if (trace) log.trace("checking open connection");
113
 
      if (in.available() > 1)
114
 
      {
115
 
         log.trace("remote endpoint has closed");
116
 
         throw new IOException("remote endpoint has closed");
117
 
      }
118
 
   }
119
 
 
120
 
   // Public ---------------------------------------------------------------------------------------
121
 
 
122
 
   public String toString()
123
 
   {
124
 
      Socket socket = getSocket();
125
 
      return "ClientSocketWrapper[" + socket + "." +
126
 
         Integer.toHexString(System.identityHashCode(socket)) + "]";
127
 
   }
128
 
 
129
 
   // Package protected ----------------------------------------------------------------------------
130
 
 
131
 
   // Protected ------------------------------------------------------------------------------------
132
 
 
133
 
   protected void createStreams(Socket socket, Map metadata) throws IOException
134
 
   {
135
 
 
136
 
      String serializationType = "java"; // hardcoding to default to java serialization
137
 
 
138
 
      if(metadata != null)
139
 
      {
140
 
         String serializationTypeParam = (String) metadata.get(InvokerLocator.SERIALIZATIONTYPE);
141
 
         if(serializationTypeParam == null || serializationTypeParam.length() == 0)
142
 
         {
143
 
            serializationTypeParam = (String) metadata.get(InvokerLocator.SERIALIZATIONTYPE_CASED);
144
 
         }
145
 
         if(serializationTypeParam != null && serializationTypeParam.length() > 0)
146
 
         {
147
 
            serializationType = serializationTypeParam;
148
 
         }
149
 
      }
150
 
 
151
 
      Marshaller marshaller = null;
152
 
      UnMarshaller unmarshaller = null;
153
 
      int tempTimeout = -1;
154
 
      int savedTimeout = getTimeout();
155
 
      
156
 
      if (metadata != null)
157
 
      {
158
 
         marshaller = (Marshaller) metadata.get(MARSHALLER);
159
 
         unmarshaller = (UnMarshaller) metadata.get(UNMARSHALLER);
160
 
         Object o = metadata.get(TEMP_TIMEOUT);
161
 
         if (o instanceof Integer)
162
 
         {
163
 
            tempTimeout = ((Integer) o).intValue();
164
 
            if (tempTimeout != -1)
165
 
            {
166
 
               socket.setSoTimeout(tempTimeout);
167
 
               log.trace("set temp timeout to: " + tempTimeout);
168
 
            }
169
 
         }
170
 
         o = metadata.get(WRITE_TIMEOUT);
171
 
         if (o instanceof Integer)
172
 
         {
173
 
            writeTimeout = ((Integer) o).intValue();
174
 
            if (writeTimeout != -1)
175
 
            {
176
 
               log.trace("set writeTimeout to: " + writeTimeout);
177
 
            }
178
 
         }
179
 
      }
180
 
      
181
 
      out = createOutputStream(serializationType, socket, marshaller);
182
 
      in = createInputStream(serializationType, socket, unmarshaller);
183
 
      setTimeout(savedTimeout);
184
 
      log.trace("reset timeout: " + savedTimeout);
185
 
   }
186
 
 
187
 
   protected InputStream createInputStream(String serializationType, Socket socket, UnMarshaller unmarshaller)
188
 
         throws IOException
189
 
   {
190
 
      if (trace) { log.trace(this + " getting input stream from " + socket + ", " + unmarshaller); }
191
 
      
192
 
      if (unmarshaller == null)
193
 
         log.warn("got null unmarshaller");
194
 
      
195
 
      InputStream is = socket.getInputStream();
196
 
      if (unmarshaller instanceof PreferredStreamUnMarshaller)
197
 
      {
198
 
         PreferredStreamUnMarshaller psum = (PreferredStreamUnMarshaller) unmarshaller;
199
 
         is = psum.getMarshallingStream(is);
200
 
      }
201
 
      
202
 
      return is;
203
 
   }
204
 
 
205
 
   protected OutputStream createOutputStream(String serializationType, Socket socket, Marshaller marshaller)
206
 
         throws IOException
207
 
   {
208
 
      if (trace) { log.trace(this + " getting output stream from " + socket + ", " + marshaller); }
209
 
      
210
 
      
211
 
      if (marshaller == null)
212
 
         log.warn("got null marshaller");
213
 
      
214
 
      OutputStream os = socket.getOutputStream();
215
 
      if (writeTimeout > 0)
216
 
      {
217
 
         os = new TimedOutputStream(os, writeTimeout);
218
 
      }
219
 
      
220
 
      if (marshaller instanceof PreferredStreamMarshaller)
221
 
      {
222
 
         PreferredStreamMarshaller psm = (PreferredStreamMarshaller) marshaller;
223
 
         os = psm.getMarshallingStream(os);
224
 
      }
225
 
      
226
 
      return os;
227
 
   }
228
 
 
229
 
   // Private --------------------------------------------------------------------------------------
230
 
 
231
 
   // Inner classes --------------------------------------------------------------------------------
232
 
 
233
 
}