View Javadoc

1   /*
2    *  Copyright 2006 Simon Raess
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.beep4j.transport;
17  
18  import java.io.PrintWriter;
19  import java.io.StringWriter;
20  import java.net.SocketAddress;
21  import java.nio.ByteBuffer;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import net.sf.beep4j.internal.util.Assert;
27  import net.sf.beep4j.internal.util.HexDump;
28  
29  /**
30   * Logging interceptor for methods of TransportContext.
31   * 
32   * @author Simon Raess
33   */
34  public class LoggingTransportContext implements TransportContext {
35  
36  	private static final Logger LOG = LoggerFactory.getLogger("net.sf.beep4j.transport");
37  	
38  	private static final Logger DATA_LOG = LoggerFactory.getLogger("net.sf.beep4j.transport.DATA");
39  	
40  	private final TransportContext target;
41  		
42  	public LoggingTransportContext(TransportContext target) {
43  		Assert.notNull("target", target);
44  		this.target = target;
45  	}
46  
47  	public void connectionEstablished(SocketAddress address) {
48  		if (LOG.isDebugEnabled()) {
49  			LOG.debug("connection established to " + address);
50  		}
51  		target.connectionEstablished(address);
52  	}
53  
54  	public void messageReceived(ByteBuffer buffer) {
55  		if (LOG.isDebugEnabled()) {
56  			LOG.debug("message received");
57  			DATA_LOG.debug(HexDump.dump(buffer));
58  		}
59  		target.messageReceived(buffer);
60  	}
61  
62  	public void exceptionCaught(Throwable cause) {
63  		LOG.warn("exception caught from transport:");
64  		StringWriter writer = new StringWriter();
65  		cause.printStackTrace(new PrintWriter(writer));
66  		LOG.warn(writer.toString());
67  		target.exceptionCaught(cause);
68  	}
69  
70  	public void connectionClosed() {
71  		if (LOG.isDebugEnabled()) {
72  			LOG.debug("connection closed");
73  		}
74  		target.connectionClosed();
75  	}
76  
77  }