1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.service.server;
14
15 import org.abstracthorizon.danube.adapter.AdapterFactory;
16 import org.abstracthorizon.danube.adapter.AdapterManager;
17 import org.abstracthorizon.danube.connection.Connection;
18
19 import java.io.InputStream;
20 import java.io.OutputStream;
21 import java.net.Socket;
22 import java.nio.channels.Channel;
23 import java.nio.channels.Channels;
24 import java.nio.channels.SocketChannel;
25
26
27
28
29
30
31
32
33
34 public class SocketChannelConnection implements Connection, AdapterFactory {
35
36
37 protected static final Class<?>[] ADAPTING_CLASSES =
38 new Class[]{
39 SocketChannelConnection.class,
40 SocketChannel.class,
41 Channel.class,
42 InputStream.class,
43 OutputStream.class,
44 Socket.class};
45
46
47 protected SocketChannel socketChannel;
48
49
50 protected InputStream cachedInputStream;
51
52
53 protected OutputStream cachedOutputStream;
54
55
56
57
58
59 public SocketChannelConnection(SocketChannel socketChannel) {
60 this.socketChannel = socketChannel;
61 }
62
63
64
65
66
67 public SocketChannelConnection(AdapterManager adapterManager, SocketChannel socketChannel) {
68 this.socketChannel = socketChannel;
69 }
70
71
72
73
74
75 public SocketChannel getSocketChannel() {
76 return socketChannel;
77 }
78
79
80
81
82
83 public Socket getSocket() {
84 return socketChannel.socket();
85 }
86
87
88
89
90
91 public InputStream getInputStream() {
92 if (cachedInputStream == null) {
93 cachedInputStream = Channels.newInputStream(socketChannel);
94 }
95 return cachedInputStream;
96 }
97
98
99
100
101
102 public OutputStream getOutputStream() {
103 if (cachedOutputStream == null) {
104 cachedOutputStream = Channels.newOutputStream(socketChannel);
105 }
106 return cachedOutputStream;
107 }
108
109
110
111
112 public void close() {
113 try {
114 socketChannel.close();
115
116 Socket socket = socketChannel.socket();
117
118
119
120 socket.setSoTimeout(10);
121 synchronized (socket) {
122 socket.notifyAll();
123 }
124 } catch (Exception ignore) {
125
126 ignore.printStackTrace();
127 }
128
129 }
130
131
132
133
134
135 public boolean isClosed() {
136
137 return !socketChannel.isOpen();
138 }
139
140
141
142
143
144
145 public String toString() {
146 return "SocketConnectionImpl[" + socketChannel + "]";
147 }
148
149
150
151
152
153
154
155 public <T> T adapt(T object, Class<T> cls) {
156 if (object == this) {
157 return (T)adapt(cls);
158 } else {
159 return null;
160 }
161 }
162
163 @SuppressWarnings("unchecked")
164 public <T> T adapt(Class<T> cls) {
165 if (cls == SocketChannelConnection.class) {
166 return (T)this;
167 } else if (cls == Socket.class) {
168 return (T)getSocket();
169 } else if (cls == OutputStream.class) {
170 return (T)getOutputStream();
171 } else if (cls == InputStream.class) {
172 return (T)getInputStream();
173 } else if (cls == SocketChannel.class) {
174 return (T)getSocketChannel();
175 } else if (cls == Channel.class) {
176 return (T)getSocketChannel();
177 }
178 return null;
179 }
180
181
182
183
184
185 @SuppressWarnings("unchecked")
186 public <T> Class<T>[] getAdaptingClasses(T object) {
187 return (Class<T>[])ADAPTING_CLASSES;
188 }
189 }