KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > SocketWithLayeredTransport


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.net.InetAddress JavaDoc;
28 import java.net.Socket JavaDoc;
29 import java.net.UnknownHostException JavaDoc;
30 import java.text.MessageFormat JavaDoc;
31
32 /**
33  *
34  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
35  */

36 public class SocketWithLayeredTransport extends Socket JavaDoc {
37
38
39
40     LayeredTransport head = null;
41
42     // #ifdef DEBUG
43
org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(SocketWithLayeredTransport.class);
44     // #endif
45

46     public SocketWithLayeredTransport() {
47     }
48     
49     public SocketWithLayeredTransport(String JavaDoc hostname, int port) throws UnknownHostException JavaDoc, IOException JavaDoc {
50         super(hostname, port);
51     }
52     
53     public SocketWithLayeredTransport(InetAddress JavaDoc address, int port, InetAddress JavaDoc localAddr, int localPort) throws IOException JavaDoc {
54         super(address, port, localAddr, localPort);
55     }
56
57     public SocketWithLayeredTransport(InetAddress JavaDoc address, int port) throws IOException JavaDoc {
58         super(address, port);
59     }
60     
61     public synchronized void pushTransport(Object JavaDoc obj) throws IOException JavaDoc {
62         if(obj!=null) {
63             if (head == null)
64                 head = new LayeredTransport(obj, this);
65             else
66                 head = new LayeredTransport(obj, head);
67         }
68     }
69
70     public synchronized InputStream JavaDoc getInputStream() throws IOException JavaDoc {
71         if (head == null)
72             return getRawInputStream();
73         else
74             return head.getInputStream();
75     }
76
77     public synchronized OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
78         if (head == null)
79             return getRawOutputStream();
80         else
81             return head.getOutputStream();
82     }
83     
84     protected InputStream JavaDoc getRawInputStream() throws IOException JavaDoc {
85         return super.getInputStream();
86     }
87     
88     protected OutputStream JavaDoc getRawOutputStream() throws IOException JavaDoc {
89         return super.getOutputStream();
90     }
91
92     public void close() throws IOException JavaDoc {
93         if (head != null)
94             head.close();
95         try {
96             super.close();
97         } catch (Throwable JavaDoc t) {
98         }
99     }
100
101     public static String JavaDoc getExceptionMessageChain(Throwable JavaDoc t) {
102         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
103         while (t != null) {
104             if (buf.length() > 0 && !buf.toString().endsWith(".")) { //$NON-NLS-1$
105
buf.append(". "); //$NON-NLS-1$
106
}
107             if (t.getMessage() != null) {
108                 buf.append(t.getMessage().trim());
109             }
110             try {
111                 Method JavaDoc m = t.getClass().getMethod("getCause", (Class JavaDoc[]) null); //$NON-NLS-1$
112
t = (Throwable JavaDoc) m.invoke(t, (Object JavaDoc[]) null);
113             } catch (Throwable JavaDoc ex) {
114             }
115         }
116         return buf.toString();
117     }
118
119     class LayeredTransport {
120
121         Object JavaDoc transport;
122         Object JavaDoc source;
123         InputStream JavaDoc in, rawIn;
124         OutputStream JavaDoc out, rawOut;
125         Method JavaDoc close;
126         Method JavaDoc rawClose;
127
128         LayeredTransport(Object JavaDoc transport, Object JavaDoc source) throws IOException JavaDoc {
129
130             try {
131
132                 // Get the source objects
133
Method JavaDoc m = source.getClass().getMethod("getInputStream", (Class JavaDoc[]) null); //$NON-NLS-1$
134
rawIn = (InputStream JavaDoc) m.invoke(source, (Object JavaDoc[]) null);
135
136                 m = source.getClass().getMethod("getOutputStream", (Class JavaDoc[]) null); //$NON-NLS-1$
137
rawOut = (OutputStream JavaDoc) m.invoke(source, (Object JavaDoc[]) null);
138
139                 rawClose = source.getClass().getMethod("close", (Class JavaDoc[]) null); //$NON-NLS-1$
140

141                 // Get the transport objects
142
m = transport.getClass().getMethod("initialize", new Class JavaDoc[] { InputStream JavaDoc.class, OutputStream JavaDoc.class }); //$NON-NLS-1$
143
m.invoke(transport, new Object JavaDoc[] { rawIn, rawOut });
144
145                 m = transport.getClass().getMethod("getInputStream", (Class JavaDoc[]) null); //$NON-NLS-1$
146
in = (InputStream JavaDoc) m.invoke(transport, (Object JavaDoc[]) null);
147
148                 m = transport.getClass().getMethod("getOutputStream", (Class JavaDoc[]) null); //$NON-NLS-1$
149
out = (OutputStream JavaDoc) m.invoke(transport, (Object JavaDoc[]) null);
150
151                 close = transport.getClass().getMethod("close", (Class JavaDoc[]) null); //$NON-NLS-1$
152

153                 // Everything ok
154
this.source = source;
155                 this.transport = transport;
156             } catch (InvocationTargetException JavaDoc ite) {
157                 Throwable JavaDoc t = ite.getTargetException();
158
159                 if (t != null && t instanceof IOException JavaDoc) {
160                     throw (IOException JavaDoc) t;
161                 } else {
162                     throw new IOException JavaDoc(MessageFormat.format(Messages.getString("SocketWithLayeredTransport.failedToLayerTransport"), new Object JavaDoc[] { t == null ? ite.getMessage() : getExceptionMessageChain(t) })); //$NON-NLS-1$
163
}
164
165             } catch (Throwable JavaDoc ex) {
166                 // #ifdef DEBUG
167
log.info(Messages.getString("SocketWithLayeredTransport.failedToCreateLayeredSocket"), ex); //$NON-NLS-1$
168
// #endif
169
throw new IOException JavaDoc(MessageFormat.format(Messages.getString("SocketWithLayeredTransport.failedToLayerTransport"), new Object JavaDoc[] { ex.getMessage() })); //$NON-NLS-1$
170
}
171         }
172
173         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
174             return in;
175         }
176
177         public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
178             return out;
179         }
180
181         public void close() throws IOException JavaDoc {
182             try {
183                 close.invoke(transport, (Object JavaDoc[]) null);
184             } catch (InvocationTargetException JavaDoc ex) {
185             } catch (Throwable JavaDoc ex) {
186                 // #ifdef DEBUG
187
log.info(Messages.getString("SocketWithLayeredTransport.failedToClose"), ex); //$NON-NLS-1$
188
// #endif
189
}
190         }
191     }
192 }
193
Popular Tags