View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2009 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
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   * SocketImpl for use with SocketStatistics. Generates and returns modified InputStreams / OutputStreams with support
29   * logging. In other cases, it behaves just like the original java.net class
30   *
31   * @svnLink $Revision: 3929 $;$Date: 2009-09-25 16:38:41 +0200 (Fr, 25. Sep 2009) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/socketstatistics/src/main/java/ch/elca/el4j/util/socketstatistics/SocketImplLogger.java $
32   *
33   * @author Jonas Hauenstein (JHN)
34   */
35  public class SocketImplLogger extends SocketImpl {
36  
37  	/**
38  	 * Delegator used for calls to java.net.SocksSocketImpl.
39  	 */
40  	private final ReflectiveDelegator m_delegator;
41  
42  	/**
43  	 * Reference to the corresponding ConnectionStatistics.
44  	 */
45  	private final ConnectionStatistics m_constats;
46  
47  	/**
48  	 * Constructor. creates a ReflectiveDelegator for delegation of method calls to java.net.SocksSocketImpl and a new
49  	 * ConnectionStatistics inside SocketStatistics
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  	 * {@inheritDoc}
58  	 */
59  	@Override
60  	protected void accept(SocketImpl s) throws IOException {
61  		m_delegator.invoke(s);
62  	}
63  
64  	/**
65  	 * {@inheritDoc}
66  	 */
67  	@Override
68  	protected int available() throws IOException {
69  		Integer i = (Integer) m_delegator.invoke();
70  		return i.intValue();
71  	}
72  
73  	/**
74  	 * {@inheritDoc}
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  	 * Modified version of close(). Sets the the destroyed date in ConnectionStatistics {@inheritDoc}
86  	 */
87  	@Override
88  	protected void close() throws IOException {
89  		m_constats.setDestroyed();
90  		m_delegator.invoke();
91  	}
92  
93  	/**
94  	 * {@inheritDoc}
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 	 * {@inheritDoc}
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 	 * {@inheritDoc}
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 	 * {@inheritDoc}
152 	 */
153 	@Override
154 	protected void create(boolean stream) throws IOException {
155 		m_delegator.invoke(stream);
156 	}
157 
158 	/**
159 	 * Modified version of getInputStream(). Returns an InputStreamLogger for logging of socket traffic {@inheritDoc}
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 	 * Modified version of getOutputStream(). Returns an OutputStreamLogger for logging of socket traffic {@inheritDoc}
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 	 * {@inheritDoc}
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 	 * {@inheritDoc}
195 	 */
196 	@Override
197 	protected void sendUrgentData(int data) throws IOException {
198 		m_delegator.invoke();
199 	}
200 
201 	/**
202 	 * {@inheritDoc}
203 	 */
204 	@Override
205 	public Object getOption(int optID) throws SocketException {
206 		return m_delegator.invoke(optID);
207 	}
208 
209 	/**
210 	 * {@inheritDoc}
211 	 */
212 	@Override
213 	public void setOption(int optID, Object value) throws SocketException {
214 		m_delegator.invoke(optID, value);
215 	}
216 
217 }