1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.util.socketstatistics;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.net.InetAddress;
23 import java.net.SocketAddress;
24 import java.net.SocketException;
25 import java.net.SocketImpl;
26
27
28
29
30
31
32
33
34
35 public class SocketImplLogger extends SocketImpl {
36
37
38
39
40 private final ReflectiveDelegator m_delegator;
41
42
43
44
45 private final ConnectionStatistics m_constats;
46
47
48
49
50
51 public SocketImplLogger() {
52 this.m_delegator = new ReflectiveDelegator(this, SocketImpl.class, "java.net.SocksSocketImpl");
53 this.m_constats = SocketStatistics.addNewConStats();
54 }
55
56
57
58
59 @Override
60 protected void accept(SocketImpl s) throws IOException {
61 m_delegator.invoke(s);
62 }
63
64
65
66
67 @Override
68 protected int available() throws IOException {
69 Integer i = (Integer) m_delegator.invoke();
70 return i.intValue();
71 }
72
73
74
75
76 @Override
77 protected void bind(InetAddress host, int port) throws IOException {
78 m_delegator.invoke(host, port);
79 m_constats.setRemoteAdress((InetAddress) m_delegator.delegateTo("getInetAddress").invoke());
80 m_constats.setRemotePort((Integer) m_delegator.delegateTo("getPort").invoke());
81 m_constats.setLocalPort((Integer) m_delegator.delegateTo("getLocalPort").invoke());
82 }
83
84
85
86
87 @Override
88 protected void close() throws IOException {
89 m_constats.setDestroyed();
90 m_delegator.invoke();
91 }
92
93
94
95
96 @Override
97 protected void connect(String host, int port) throws IOException {
98 try {
99 m_delegator.invoke(host, port);
100 } catch (DelegationException e) {
101 if (e.getCause() instanceof IOException) {
102 throw (IOException) e.getCause();
103 } else {
104 throw e;
105 }
106 }
107 m_constats.setRemoteAdress((InetAddress) m_delegator.delegateTo("getInetAddress").invoke());
108 m_constats.setRemotePort((Integer) m_delegator.delegateTo("getPort").invoke());
109 m_constats.setLocalPort((Integer) m_delegator.delegateTo("getLocalPort").invoke());
110 }
111
112
113
114
115 @Override
116 protected void connect(InetAddress address, int port) throws IOException {
117 try {
118 m_delegator.invoke(address, port);
119 } catch (DelegationException e) {
120 if (e.getCause() instanceof IOException) {
121 throw (IOException) e.getCause();
122 } else {
123 throw e;
124 }
125 }
126 m_constats.setRemoteAdress((InetAddress) m_delegator.delegateTo("getInetAddress").invoke());
127 m_constats.setRemotePort((Integer) m_delegator.delegateTo("getPort").invoke());
128 m_constats.setLocalPort((Integer) m_delegator.delegateTo("getLocalPort").invoke());
129 }
130
131
132
133
134 @Override
135 protected void connect(SocketAddress address, int timeout) throws IOException {
136 try {
137 m_delegator.invoke(address, port);
138 } catch (DelegationException e) {
139 if (e.getCause() instanceof IOException) {
140 throw (IOException) e.getCause();
141 } else {
142 throw e;
143 }
144 }
145 m_constats.setRemoteAdress((InetAddress) m_delegator.delegateTo("getInetAddress").invoke());
146 m_constats.setRemotePort((Integer) m_delegator.delegateTo("getPort").invoke());
147 m_constats.setLocalPort((Integer) m_delegator.delegateTo("getLocalPort").invoke());
148 }
149
150
151
152
153 @Override
154 protected void create(boolean stream) throws IOException {
155 m_delegator.invoke(stream);
156 }
157
158
159
160
161 @Override
162 protected InputStream getInputStream() throws IOException {
163 InputStream real = m_delegator.invoke();
164 m_constats.setRemoteAdress((InetAddress) m_delegator.delegateTo("getInetAddress").invoke());
165 m_constats.setRemotePort((Integer) m_delegator.delegateTo("getPort").invoke());
166 m_constats.setLocalPort((Integer) m_delegator.delegateTo("getLocalPort").invoke());
167 return new InputStreamLogger(real, m_constats);
168 }
169
170
171
172
173 @Override
174 protected OutputStream getOutputStream() throws IOException {
175 OutputStream real = m_delegator.invoke();
176 m_constats.setRemoteAdress((InetAddress) m_delegator.delegateTo("getInetAddress").invoke());
177 m_constats.setRemotePort((Integer) m_delegator.delegateTo("getPort").invoke());
178 m_constats.setLocalPort((Integer) m_delegator.delegateTo("getLocalPort").invoke());
179 return new OutputStreamLogger(real, m_constats);
180 }
181
182
183
184
185 @Override
186 protected void listen(int backlog) throws IOException {
187 m_delegator.invoke(backlog);
188 m_constats.setRemoteAdress((InetAddress) m_delegator.delegateTo("getInetAddress").invoke());
189 m_constats.setRemotePort((Integer) m_delegator.delegateTo("getPort").invoke());
190 m_constats.setLocalPort((Integer) m_delegator.delegateTo("getLocalPort").invoke());
191 }
192
193
194
195
196 @Override
197 protected void sendUrgentData(int data) throws IOException {
198 m_delegator.invoke();
199 }
200
201
202
203
204 @Override
205 public Object getOption(int optID) throws SocketException {
206 return m_delegator.invoke(optID);
207 }
208
209
210
211
212 @Override
213 public void setOption(int optID, Object value) throws SocketException {
214 m_delegator.invoke(optID, value);
215 }
216
217 }