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