KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > TcpStream


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import javax.net.ssl.SSLContext;
32 import javax.net.ssl.SSLSocketFactory;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.OutputStream JavaDoc;
36 import java.net.Socket JavaDoc;
37 import java.net.SocketException JavaDoc;
38
39 /**
40  * Implementation of a TCP stream. Mostly this just forwards the
41  * request to the underlying socket streams.
42  */

43 class TcpStream extends StreamImpl {
44   private Socket JavaDoc _s;
45   private InputStream JavaDoc _is;
46   private OutputStream JavaDoc _os;
47
48   private TcpStream(TcpPath path, long timeout) throws IOException JavaDoc
49   {
50     setPath(path);
51
52     //_s = new Socket(path.getHost(), path.getPort());
53
_s = new Socket JavaDoc();
54     
55     if (timeout > 0)
56       _s.connect(path.getSocketAddress(), (int) timeout);
57     else
58       _s.connect(path.getSocketAddress());
59
60     if (! _s.isConnected())
61       throw new IOException JavaDoc("connection timeout");
62     
63     if (timeout < 0)
64       timeout = 120000;
65
66     _s.setSoTimeout((int) timeout);
67
68     try {
69       if (path instanceof TcpsPath) {
70     SSLContext context = SSLContext.getInstance("TLS");
71
72     javax.net.ssl.TrustManager tm =
73       new javax.net.ssl.X509TrustManager() {
74         public java.security.cert.X509Certificate JavaDoc[]
75           getAcceptedIssuers() {
76           return null;
77         }
78         public void checkClientTrusted(
79                        java.security.cert.X509Certificate JavaDoc[] cert, String JavaDoc foo) {
80         }
81         public void checkServerTrusted(
82                        java.security.cert.X509Certificate JavaDoc[] cert, String JavaDoc foo) {
83         }
84       };
85
86       
87     context.init(null, new javax.net.ssl.TrustManager[] { tm }, null);
88     SSLSocketFactory factory = context.getSocketFactory();
89
90     _s = factory.createSocket(_s, path.getHost(), path.getPort(), true);
91       }
92     } catch (IOException JavaDoc e) {
93       throw e;
94     } catch (RuntimeException JavaDoc e) {
95       throw e;
96     } catch (Exception JavaDoc e) {
97       throw new IOExceptionWrapper(e);
98     }
99     
100     _is = _s.getInputStream();
101     _os = _s.getOutputStream();
102   }
103
104   public void setAttribute(String JavaDoc name, Object JavaDoc value)
105   {
106     if (name.equals("timeout")) {
107       try {
108     if (value instanceof Number JavaDoc)
109       _s.setSoTimeout(((Number JavaDoc) value).intValue());
110     else
111       _s.setSoTimeout(Integer.parseInt(String.valueOf(value)));
112       } catch (SocketException JavaDoc e) {
113       }
114     }
115   }
116
117   static TcpStream openRead(TcpPath path, long timeout) throws IOException JavaDoc
118   {
119     return new TcpStream(path, timeout);
120   }
121
122   static TcpStream openReadWrite(TcpPath path, long timeout) throws IOException JavaDoc
123   {
124     return new TcpStream(path, timeout);
125   }
126
127   public boolean canWrite()
128   {
129     return _os != null;
130   }
131   
132   /**
133    * Writes a buffer to the underlying stream.
134    *
135    * @param buffer the byte array to write.
136    * @param offset the offset into the byte array.
137    * @param length the number of bytes to write.
138    * @param isEnd true when the write is flushing a close.
139    */

140   public void write(byte []buf, int offset, int length, boolean isEnd)
141     throws IOException JavaDoc
142   {
143     if (_os != null)
144       _os.write(buf, offset, length);
145   }
146
147   public boolean canRead()
148   {
149     return _is != null;
150   }
151
152   public int getAvailable() throws IOException JavaDoc
153   {
154     if (_is != null)
155       return _is.available();
156     else
157       return -1;
158   }
159
160   public int read(byte []buf, int offset, int length) throws IOException JavaDoc
161   {
162     if (_is != null)
163       return _is.read(buf, offset, length);
164     else
165       return -1;
166   }
167
168   public void closeWrite() throws IOException JavaDoc
169   {
170     OutputStream JavaDoc os = _os;
171     _os = null;
172     
173     try {
174       if (os != null)
175         _s.shutdownOutput();
176     } finally {
177       if (_is == null) {
178         Socket JavaDoc s = _s;
179         _s = null;
180         
181         if (s != null)
182           s.close();
183       }
184     }
185   }
186
187   public void close() throws IOException JavaDoc
188   {
189     InputStream JavaDoc is = _is;
190     _is = null;
191     
192     OutputStream JavaDoc os = _os;
193     _os = null;
194     
195     Socket JavaDoc s = _s;
196     _s = null;
197
198     try {
199       if (os != null)
200     os.close();
201
202       if (is != null)
203     is.close();
204     } finally {
205       if (s != null)
206     s.close();
207     }
208   }
209 }
210
Popular Tags