KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > common > ssl > IONormalizer


1 package com.ubermq.jms.common.ssl;
2
3 import java.net.*;
4 import java.nio.channels.*;
5 import java.nio.*;
6 import java.io.*;
7
8 /**
9  * For now, it is necessary to map SSL operations onto NIO via pipes.
10  * This is undesirable for a lot of reasons, but there is really not
11  * much we can do here until Sun comes up with a way to do this
12  * from the JDK.<P>
13  *
14  * This could also be useful perhaps to allow HTTP or other protocol
15  * tunneling that is implemented in the JDK. <P>
16  *
17  * @since 2.1
18  */

19 public class IONormalizer
20 {
21     private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(IONormalizer.class);
22     
23     private static final int BUFFER_SIZE = 4096;
24
25     /**
26      * Normalizes I/O from java.io to/from java.nio paradigms.
27      * This method will set up the necessary threads and resources
28      * to read and write data using a pipe on one end and a socket
29      * on the other.
30      *
31      * @param fromsocket a Pipe representing traffic
32      * going to the socket
33      *
34      * @param tosocket a Pipe representing traffic
35      * coming from the socket
36      *
37      * @param s a Socket, which is two-way
38      * by definition.
39      *
40      * @throws IOException if a failure occurs
41      */

42     public static void normalize(final Pipe fromsocket,
43                                  final Pipe tosocket,
44                                  final Socket s)
45         throws IOException
46     {
47         // get streams
48
final InputStream in = s.getInputStream();
49         final OutputStream out = s.getOutputStream();
50
51         // from the pipes to the socket. we need to be
52
// able to interrupt this thread so that it will shutdown
53
// the pipes when necessary.
54
final Thread JavaDoc writer = new Thread JavaDoc(new Runnable JavaDoc()
55                                          {
56                     public void run()
57                     {
58                         try
59                         {
60                             ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE);
61
62                             while(!Thread.interrupted())
63                             {
64                                 int n = tosocket.source().read(buf);
65                                 if (n == -1)
66                                     break;
67                                 else
68                                 {
69                                     out.write(buf.array(), 0, n);
70                                     buf.clear();
71                                 }
72                             }
73                         }
74                         catch (IOException e)
75                         {
76                             ; // do nothing
77
}
78                     }
79                 }, "IONormalizer to-socket");
80
81         // now spawn a thread to handle bringing bytes
82
// from the socket
83
Thread JavaDoc reader = new Thread JavaDoc(new Runnable JavaDoc()
84                                    {
85                     public void run()
86                     {
87                         try
88                         {
89                             byte[] buf = new byte[BUFFER_SIZE];
90
91                             while(!Thread.interrupted())
92                             {
93                                 int n = in.read(buf);
94                                 if (n == -1)
95                                 {
96                                     break;
97                                 }
98                                 else
99                                 {
100                                     int len = 0;
101                                     while(len < n)
102                                     {
103                                         len += fromsocket.sink().write(ByteBuffer.wrap(buf, len, n-len));
104                                     }
105                                 }
106                             }
107                         }
108                         catch (SocketException e)
109                         {
110                             // don;t print here. it's fine that the cxn was
111
// reset.
112
}
113                         catch (IOException e)
114                         {
115                             log.error("", e);
116                         }
117                         finally
118                         {
119                             try
120                             {
121                                 writer.interrupt();
122                                 tosocket.sink().close();
123                                 writer.join();
124                             }
125                             catch (Exception JavaDoc e)
126                             {
127                                 log.error("", e);
128                             }
129
130                             // close everything
131
try
132                             {
133                                 fromsocket.sink().close();
134                             }
135                             catch (IOException e)
136                             {
137                                 log.error("", e);
138                             }
139
140                             try
141                             {
142                                 s.close();
143                             }
144                             catch (IOException e)
145                             {
146                                 log.error("", e);
147                             }
148                         }
149                     }
150                 }, "IONormalizer from-socket");
151
152
153         // start em
154
reader.setDaemon(true);
155         writer.setDaemon(true);
156         reader.start();
157         writer.start();
158     }
159 }
160
Popular Tags