1 19 20 package com.maverick.http; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.net.InetAddress ; 28 import java.net.Socket ; 29 import java.net.UnknownHostException ; 30 import java.text.MessageFormat ; 31 32 36 public class SocketWithLayeredTransport extends Socket { 37 38 39 40 LayeredTransport head = null; 41 42 org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(SocketWithLayeredTransport.class); 44 46 public SocketWithLayeredTransport() { 47 } 48 49 public SocketWithLayeredTransport(String hostname, int port) throws UnknownHostException , IOException { 50 super(hostname, port); 51 } 52 53 public SocketWithLayeredTransport(InetAddress address, int port, InetAddress localAddr, int localPort) throws IOException { 54 super(address, port, localAddr, localPort); 55 } 56 57 public SocketWithLayeredTransport(InetAddress address, int port) throws IOException { 58 super(address, port); 59 } 60 61 public synchronized void pushTransport(Object obj) throws IOException { 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 getInputStream() throws IOException { 71 if (head == null) 72 return getRawInputStream(); 73 else 74 return head.getInputStream(); 75 } 76 77 public synchronized OutputStream getOutputStream() throws IOException { 78 if (head == null) 79 return getRawOutputStream(); 80 else 81 return head.getOutputStream(); 82 } 83 84 protected InputStream getRawInputStream() throws IOException { 85 return super.getInputStream(); 86 } 87 88 protected OutputStream getRawOutputStream() throws IOException { 89 return super.getOutputStream(); 90 } 91 92 public void close() throws IOException { 93 if (head != null) 94 head.close(); 95 try { 96 super.close(); 97 } catch (Throwable t) { 98 } 99 } 100 101 public static String getExceptionMessageChain(Throwable t) { 102 StringBuffer buf = new StringBuffer (); 103 while (t != null) { 104 if (buf.length() > 0 && !buf.toString().endsWith(".")) { buf.append(". "); } 107 if (t.getMessage() != null) { 108 buf.append(t.getMessage().trim()); 109 } 110 try { 111 Method m = t.getClass().getMethod("getCause", (Class []) null); t = (Throwable ) m.invoke(t, (Object []) null); 113 } catch (Throwable ex) { 114 } 115 } 116 return buf.toString(); 117 } 118 119 class LayeredTransport { 120 121 Object transport; 122 Object source; 123 InputStream in, rawIn; 124 OutputStream out, rawOut; 125 Method close; 126 Method rawClose; 127 128 LayeredTransport(Object transport, Object source) throws IOException { 129 130 try { 131 132 Method m = source.getClass().getMethod("getInputStream", (Class []) null); rawIn = (InputStream ) m.invoke(source, (Object []) null); 135 136 m = source.getClass().getMethod("getOutputStream", (Class []) null); rawOut = (OutputStream ) m.invoke(source, (Object []) null); 138 139 rawClose = source.getClass().getMethod("close", (Class []) null); 141 m = transport.getClass().getMethod("initialize", new Class [] { InputStream .class, OutputStream .class }); m.invoke(transport, new Object [] { rawIn, rawOut }); 144 145 m = transport.getClass().getMethod("getInputStream", (Class []) null); in = (InputStream ) m.invoke(transport, (Object []) null); 147 148 m = transport.getClass().getMethod("getOutputStream", (Class []) null); out = (OutputStream ) m.invoke(transport, (Object []) null); 150 151 close = transport.getClass().getMethod("close", (Class []) null); 153 this.source = source; 155 this.transport = transport; 156 } catch (InvocationTargetException ite) { 157 Throwable t = ite.getTargetException(); 158 159 if (t != null && t instanceof IOException ) { 160 throw (IOException ) t; 161 } else { 162 throw new IOException (MessageFormat.format(Messages.getString("SocketWithLayeredTransport.failedToLayerTransport"), new Object [] { t == null ? ite.getMessage() : getExceptionMessageChain(t) })); } 164 165 } catch (Throwable ex) { 166 log.info(Messages.getString("SocketWithLayeredTransport.failedToCreateLayeredSocket"), ex); throw new IOException (MessageFormat.format(Messages.getString("SocketWithLayeredTransport.failedToLayerTransport"), new Object [] { ex.getMessage() })); } 171 } 172 173 public InputStream getInputStream() throws IOException { 174 return in; 175 } 176 177 public OutputStream getOutputStream() throws IOException { 178 return out; 179 } 180 181 public void close() throws IOException { 182 try { 183 close.invoke(transport, (Object []) null); 184 } catch (InvocationTargetException ex) { 185 } catch (Throwable ex) { 186 log.info(Messages.getString("SocketWithLayeredTransport.failedToClose"), ex); } 190 } 191 } 192 } 193 | Popular Tags |