001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.transport.nhttp;
020:
021: import java.io.IOException;
022: import java.net.SocketAddress;
023: import java.nio.ByteBuffer;
024: import java.nio.channels.ByteChannel;
025: import java.nio.channels.SelectionKey;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.http.nio.reactor.IOSession;
030: import org.apache.http.nio.reactor.SessionBufferStatus;
031:
032: /**
033: * Decorator class intended to transparently extend an {@link IOSession}
034: * with basic event logging capabilities using Commons Logging.
035: */
036: public class LoggingIOSession implements IOSession {
037:
038: private static int COUNT = 0;
039:
040: private final Log log;
041: private final IOSession session;
042: private final ByteChannel channel;
043: private final int id;
044:
045: public LoggingIOSession(final IOSession session) {
046: super ();
047: if (session == null) {
048: throw new IllegalArgumentException(
049: "I/O session may not be null");
050: }
051: this .session = session;
052: this .channel = new LoggingByteChannel();
053: this .id = ++COUNT;
054: this .log = LogFactory.getLog(session.getClass());
055: }
056:
057: public ByteChannel channel() {
058: return this .channel;
059: }
060:
061: public SocketAddress getLocalAddress() {
062: return this .session.getLocalAddress();
063: }
064:
065: public SocketAddress getRemoteAddress() {
066: return this .session.getRemoteAddress();
067: }
068:
069: public int getEventMask() {
070: return this .session.getEventMask();
071: }
072:
073: private static String formatOps(int ops) {
074: StringBuffer buffer = new StringBuffer(6);
075: buffer.append('[');
076: if ((ops & SelectionKey.OP_READ) > 0) {
077: buffer.append('r');
078: }
079: if ((ops & SelectionKey.OP_WRITE) > 0) {
080: buffer.append('w');
081: }
082: if ((ops & SelectionKey.OP_ACCEPT) > 0) {
083: buffer.append('a');
084: }
085: if ((ops & SelectionKey.OP_CONNECT) > 0) {
086: buffer.append('c');
087: }
088: buffer.append(']');
089: return buffer.toString();
090: }
091:
092: public void setEventMask(int ops) {
093: if (this .log.isDebugEnabled()) {
094: this .log.debug("I/O session " + this .id + " "
095: + this .session + ": Set event mask "
096: + formatOps(ops));
097: }
098: this .session.setEventMask(ops);
099: }
100:
101: public void setEvent(int op) {
102: if (this .log.isDebugEnabled()) {
103: this .log.debug("I/O session " + this .id + " "
104: + this .session + ": Set event " + formatOps(op));
105: }
106: this .session.setEvent(op);
107: }
108:
109: public void clearEvent(int op) {
110: if (this .log.isDebugEnabled()) {
111: this .log.debug("I/O session " + this .id + " "
112: + this .session + ": Clear event " + formatOps(op));
113: }
114: this .session.clearEvent(op);
115: }
116:
117: public void close() {
118: if (this .log.isDebugEnabled()) {
119: this .log.debug("I/O session " + this .id + " "
120: + this .session + ": Close");
121: }
122: this .session.close();
123: }
124:
125: public boolean isClosed() {
126: return this .session.isClosed();
127: }
128:
129: public void shutdown() {
130: if (this .log.isDebugEnabled()) {
131: this .log.debug("I/O session " + this .id + " "
132: + this .session + ": Shutdown");
133: }
134: this .session.shutdown();
135: }
136:
137: public int getSocketTimeout() {
138: return this .session.getSocketTimeout();
139: }
140:
141: public void setSocketTimeout(int timeout) {
142: if (this .log.isDebugEnabled()) {
143: this .log.debug("I/O session " + this .id + " "
144: + this .session + ": Set timeout " + timeout);
145: }
146: this .session.setSocketTimeout(timeout);
147: }
148:
149: public void setBufferStatus(final SessionBufferStatus status) {
150: this .session.setBufferStatus(status);
151: }
152:
153: public boolean hasBufferedInput() {
154: return this .session.hasBufferedInput();
155: }
156:
157: public boolean hasBufferedOutput() {
158: return this .session.hasBufferedOutput();
159: }
160:
161: public Object getAttribute(final String name) {
162: return this .session.getAttribute(name);
163: }
164:
165: public void setAttribute(final String name, final Object obj) {
166: if (this .log.isDebugEnabled()) {
167: this .log.debug("I/O session " + this .id + " "
168: + this .session + ": Set attribute " + name);
169: }
170: this .session.setAttribute(name, obj);
171: }
172:
173: public Object removeAttribute(final String name) {
174: if (this .log.isDebugEnabled()) {
175: this .log.debug("I/O session " + this .id + " "
176: + this .session + ": Remove attribute " + name);
177: }
178: return this .session.removeAttribute(name);
179: }
180:
181: class LoggingByteChannel implements ByteChannel {
182:
183: public int read(final ByteBuffer dst) throws IOException {
184: int bytesRead = session.channel().read(dst);
185: if (log.isDebugEnabled()) {
186: log.debug("I/O session " + id + " " + session + ": "
187: + bytesRead + " bytes read");
188: }
189: return bytesRead;
190: }
191:
192: public int write(final ByteBuffer src) throws IOException {
193: int byteWritten = session.channel().write(src);
194: if (log.isDebugEnabled()) {
195: log.debug("I/O session " + id + " " + session + ": "
196: + byteWritten + " bytes written");
197: }
198: return byteWritten;
199: }
200:
201: public void close() throws IOException {
202: if (log.isDebugEnabled()) {
203: log.debug("I/O session " + id + " " + session
204: + ": Channel close");
205: }
206: session.channel().close();
207: }
208:
209: public boolean isOpen() {
210: return session.channel().isOpen();
211: }
212:
213: }
214:
215: }
|