001 /* 002 * Copyright 2010 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package com.hs.mail.imap.server; 017 018 import java.io.PrintStream; 019 020 import org.jboss.netty.buffer.ChannelBuffer; 021 import org.jboss.netty.channel.ChannelDownstreamHandler; 022 import org.jboss.netty.channel.ChannelEvent; 023 import org.jboss.netty.channel.ChannelHandlerContext; 024 import org.jboss.netty.channel.ChannelPipelineCoverage; 025 import org.jboss.netty.channel.ChannelUpstreamHandler; 026 import org.jboss.netty.channel.MessageEvent; 027 028 /** 029 * A ChannelHandler that logs all protocol streams. 030 * 031 * @author Won Chul Doh 032 * @since Jul 7, 2010 033 */ 034 035 @ChannelPipelineCoverage("all") 036 public class DebuggingHandler implements ChannelUpstreamHandler, 037 ChannelDownstreamHandler { 038 039 private PrintStream out; // debug output stream 040 041 public DebuggingHandler() { 042 } 043 044 /** 045 * Set the stream to be used for debugging output. If <code>out</code> is 046 * null, <code>System.out</code> will be used. 047 * 048 * @param out 049 * the PrintStream to use for debugging output 050 */ 051 public synchronized void setDebugOut(PrintStream out) { 052 this.out = out; 053 } 054 055 /** 056 * Returns the stream to be used for debugging output. If no stream has been 057 * set, <code>System.out</code> is returned. 058 * 059 * @return the PrintStream to use for debugging output. 060 */ 061 public synchronized PrintStream getDebugOut() { 062 if (out == null) 063 return System.out; 064 else 065 return out; 066 } 067 068 private String toString(ChannelBuffer buffer) { 069 byte[] dst = new byte[buffer.readableBytes()]; 070 buffer.getBytes(buffer.readerIndex(), dst); 071 return new String(dst); 072 } 073 074 /** 075 * Logs the specified event to the {@link out} returned by 076 * {@link #getDebugOut()}. 077 */ 078 public void debug(ChannelEvent e) { 079 if (e instanceof MessageEvent) { 080 Object msg = ((MessageEvent) e).getMessage(); 081 if (msg instanceof ChannelBuffer) { 082 getDebugOut().print(toString((ChannelBuffer) msg)); 083 } else { 084 getDebugOut().print(msg); 085 } 086 } 087 } 088 089 public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) 090 throws Exception { 091 if (e != null) { 092 debug(e); 093 ctx.sendUpstream(e); 094 } 095 } 096 097 public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) 098 throws Exception { 099 if (e != null) { 100 debug(e); 101 ctx.sendDownstream(e); 102 } 103 } 104 105 }