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  
22  import ch.elca.el4j.util.socketstatistics.genericlogger.GenericLogFactory;
23  import ch.elca.el4j.util.socketstatistics.genericlogger.GenericLogger;
24  
25  
26  /**
27   * Implementation of InputStream with modification for logging.
28   *
29   * @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/InputStreamLogger.java $
30   *
31   * @author Jonas Hauenstein (JHN)
32   */
33  public class InputStreamLogger extends InputStream {
34  
35  	/**
36  	 * Reference to original OutputStream.
37  	 */
38  	private InputStream m_is;
39  
40  	/**
41  	 * Reference to ConnectionStatistics for logging.
42  	 */
43  	private ConnectionStatistics m_cs;
44  
45  	/**
46  	 * Generic Logger.
47  	 */
48  	private final GenericLogger m_logger = GenericLogFactory.getLogger(SocketStatistics.class);
49  
50  	/**
51  	 * Constructor.
52  	 * 
53  	 * @param is
54  	 *            original InputStream used for reading
55  	 * @param cs
56  	 *            ConnectionStatistics used for logging
57  	 */
58  	public InputStreamLogger(InputStream is, ConnectionStatistics cs) {
59  		this.m_is = is;
60  		this.m_cs = cs;
61  	}
62  
63  	/**
64  	 * Modified version of read which also logs traffic to assigned ConnectionStatistics. {@inheritDoc}
65  	 */
66  	public int read() throws IOException {
67  		int result = m_is.read();
68  		if (result != -1) {
69  			m_cs.addbytesrecv(1);
70  			if (m_logger.isLogEnabled("debug")) {
71  				m_logger.log("debug", "SocketID: " + m_cs.getSocketID() + " reads " + (char) result);
72  			}
73  		}
74  		return result;
75  	}
76  
77  	/**
78  	 * Modified version of read which also logs traffic to assigned ConnectionStatistics. {@inheritDoc}
79  	 */
80  	public int read(byte[] b, int off, int len) throws IOException {
81  		int length = m_is.read(b, off, len);
82  		if (length != -1) {
83  			m_cs.addbytesrecv(length);
84  			if (m_logger.isLogEnabled("debug")) {
85  				// create String from response
86  				StringBuilder sb = new StringBuilder("SocketID: " + m_cs.getSocketID() + " reads ");
87  				for (int i = off; i < off + length; i++) {
88  					sb.append((char) b[i]);
89  				}
90  				m_logger.log("debug", sb.toString());
91  			}
92  		}
93  		return length;
94  	}
95  
96  }