directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r330420 - in /directory/network/trunk/src/java/org/apache/mina: filter/SSLFilter.java filter/support/SSLHandler.java util/SessionLog.java
Date Thu, 03 Nov 2005 02:37:24 GMT
Author: trustin
Date: Wed Nov  2 18:37:20 2005
New Revision: 330420

URL: http://svn.apache.org/viewcvs?rev=330420&view=rev
Log:
* Now most log messages are forwarded to SessionLog and ExceptionMonitor, so you can simply
replace these two classes if you don't want to use SLF4J
** Added SessionLog.isXXXXEnabled(IoSession)
** Made SessionLog.getLogger() private
** SSLFilter and SSLHandler now uses SessionLog instead of using SLF4J directly.


Modified:
    directory/network/trunk/src/java/org/apache/mina/filter/SSLFilter.java
    directory/network/trunk/src/java/org/apache/mina/filter/support/SSLHandler.java
    directory/network/trunk/src/java/org/apache/mina/util/SessionLog.java

Modified: directory/network/trunk/src/java/org/apache/mina/filter/SSLFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/filter/SSLFilter.java?rev=330420&r1=330419&r2=330420&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/filter/SSLFilter.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/filter/SSLFilter.java Wed Nov  2 18:37:20
2005
@@ -33,8 +33,7 @@
 import org.apache.mina.common.IoSession;
 import org.apache.mina.common.WriteFuture;
 import org.apache.mina.filter.support.SSLHandler;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.mina.util.SessionLog;
 
 /**
  * An SSL filter that encrypts and decrypts the data exchanged in the session.
@@ -42,7 +41,7 @@
  * Java version 5 or above is mandatory to use this filter. And please note that
  * this filter only works for TCP/IP connections.
  * <p>
- * This filter logs debug information using {@link Logger}.
+ * This filter logs debug information using {@link SessionLog}.
  * 
  * <h2>Implementing StartTLS</h2>
  * <p>
@@ -117,8 +116,6 @@
     public static final SSLFilterMessage SESSION_UNSECURED = new SSLFilterMessage( "SESSION_UNSECURED"
);
     
     private static final String SSL_HANDLER = SSLFilter.class.getName() + ".SSLHandler";
-    
-    private static final Logger log = LoggerFactory.getLogger( SSLFilter.class );
 
     private IoFilterChain parent;
     
@@ -379,9 +376,9 @@
                 return;
             }
     
-            if( log.isDebugEnabled() )
+            if( SessionLog.isDebugEnabled( session ) )
             {
-                log.debug( session + " Closed: " + getSSLSessionHandler( session ) );
+                SessionLog.debug( session, " Closed: " + getSSLSessionHandler( session )
);
             }
             
             // release resources
@@ -417,9 +414,9 @@
             sslHandler = createSSLSessionHandler( nextFilter, session );
             if( sslHandler != null )
             {
-                if( log.isDebugEnabled() )
+                if( SessionLog.isDebugEnabled( session ) )
                 {
-                    log.debug( session + " Data Read: " + sslHandler + " (" + buf+ ')' );
+                    SessionLog.debug( session, " Data Read: " + sslHandler + " (" + buf+
')' );
                 }
 
                 try
@@ -434,10 +431,10 @@
                     {
                         if( sslHandler.isOutboundDone() )
                         {
-                            if( log.isDebugEnabled() )
+                            if( SessionLog.isDebugEnabled( session ) )
                             {
-                                log.debug(
-                                         session + " SSL Session closed." );
+                                SessionLog.debug(
+                                        session, " SSL Session closed." );
                             }
                             
                             releaseSSLSessionHandler( session );
@@ -512,17 +509,17 @@
         
             SSLHandler handler = createSSLSessionHandler( nextFilter, session );
 
-            if( log.isDebugEnabled() )
+            if( SessionLog.isDebugEnabled( session ) )
             {
-                log.debug( session + " Filtered Write: " + handler );
+                SessionLog.debug( session, " Filtered Write: " + handler );
             }
 
             if( handler.isWritingEncryptedData() )
             {
                 // data already encrypted; simply return buffer
-                if( log.isDebugEnabled() )
+                if( SessionLog.isDebugEnabled( session ) )
                 {
-                    log.debug( session + "   already encrypted: " + buf );
+                    SessionLog.debug( session, "   already encrypted: " + buf );
                 }
                 nextFilter.filterWrite( session, writeRequest );
                 return;
@@ -531,9 +528,9 @@
             if( handler.isInitialHandshakeComplete() )
             {
                 // SSL encrypt
-                if( log.isDebugEnabled() )
+                if( SessionLog.isDebugEnabled( session ) )
                 {
-                    log.debug( session + " encrypt: " + buf );
+                    SessionLog.debug( session, " encrypt: " + buf );
                 }
                 
                 int pos = buf.position();
@@ -542,9 +539,9 @@
                 ByteBuffer encryptedBuffer = new EncryptedBuffer(
                         SSLHandler.copy( handler.getOutNetBuffer() ), buf );
 
-                if( log.isDebugEnabled() )
+                if( SessionLog.isDebugEnabled( session ) )
                 {
-                    log.debug( session + " encrypted buf: " + encryptedBuffer);
+                    SessionLog.debug( session, " encrypted buf: " + encryptedBuffer);
                 }
                 nextFilter.filterWrite( session, new WriteRequest( encryptedBuffer, writeRequest.getFuture()
) );
                 return;
@@ -553,16 +550,16 @@
             {
                 if( !session.isConnected() )
                 {
-                    if( log.isDebugEnabled() )
+                    if( SessionLog.isDebugEnabled( session ) )
                     {
-                        log.debug( session + " Write request on closed session." );
+                        SessionLog.debug( session, " Write request on closed session." );
                     }
                 }
                 else
                 {
-                    if( log.isDebugEnabled() )
+                    if( SessionLog.isDebugEnabled( session ) )
                     {
-                        log.debug( session + " Handshaking is not complete yet. Buffering
write request." );
+                        SessionLog.debug( session, " Handshaking is not complete yet. Buffering
write request." );
                     }
                     handler.scheduleWrite( nextFilter, writeRequest );
                 }
@@ -643,16 +640,16 @@
             return;
         }
 
-        if( log.isDebugEnabled() )
+        if( SessionLog.isDebugEnabled( session ) )
         {
-            log.debug( session + " appBuffer: " + sslHandler.getAppBuffer() );
+            SessionLog.debug( session, " appBuffer: " + sslHandler.getAppBuffer() );
         }
 
         // forward read app data
         ByteBuffer readBuffer = SSLHandler.copy( sslHandler.getAppBuffer() );
-        if( log.isDebugEnabled() )
+        if( SessionLog.isDebugEnabled( session ) )
         {
-            log.debug( session + " app data read: " + readBuffer + " (" + readBuffer.getHexDump()
+ ')' );
+            SessionLog.debug( session, " app data read: " + readBuffer + " (" + readBuffer.getHexDump()
+ ')' );
         }
         nextFilter.messageReceived( session, readBuffer );
     }

Modified: directory/network/trunk/src/java/org/apache/mina/filter/support/SSLHandler.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/filter/support/SSLHandler.java?rev=330420&r1=330419&r2=330420&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/filter/support/SSLHandler.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/filter/support/SSLHandler.java Wed Nov
 2 18:37:20 2005
@@ -33,8 +33,7 @@
 import org.apache.mina.common.IoFilter.WriteRequest;
 import org.apache.mina.filter.SSLFilter;
 import org.apache.mina.util.Queue;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.mina.util.SessionLog;
 
 /**
  * A helper class using the SSLEngine API to decrypt/encrypt data.
@@ -49,8 +48,6 @@
  */
 public class SSLHandler
 {
-    private static final Logger log = LoggerFactory.getLogger( SSLFilter.class );
-
     private final SSLFilter parent;
     private final IoSession session;
     private final Queue scheduledWrites = new Queue();
@@ -190,9 +187,9 @@
         
         while( ( scheduledWrite = ( ScheduledWrite ) scheduledWrites.pop() ) != null )
         {
-            if( log.isDebugEnabled() )
+            if( SessionLog.isDebugEnabled( session ) )
             {
-                log.debug( session + " Flushing buffered write request: " + scheduledWrite.writeRequest
);
+                SessionLog.debug( session, " Flushing buffered write request: " + scheduledWrite.writeRequest
);
             }
             parent.filterWrite( scheduledWrite.nextFilter, session, scheduledWrite.writeRequest
);
         }
@@ -216,11 +213,11 @@
             appBuffer = SSLByteBufferPool.expandBuffer( appBuffer, inNetBuffer.capacity()
* 2);
             appBuffer.position( 0 );
             appBuffer.limit( 0 );
-            if( log.isDebugEnabled() )
+            if( SessionLog.isDebugEnabled( session ) )
             {
-                log.debug( session + 
+                SessionLog.debug( session, 
                                     " expanded inNetBuffer:" + inNetBuffer );
-                log.debug( session + 
+                SessionLog.debug( session, 
                                     " expanded appBuffer:" + appBuffer );
             }
         }
@@ -291,14 +288,14 @@
                 // Note: there is no way to know the exact size required, but enrypted data
                 // shouln't need to be larger than twice the source data size?
                 outNetBuffer = SSLByteBufferPool.expandBuffer( outNetBuffer, src.capacity()
* 2 );
-                if ( log.isDebugEnabled() ) {
-                    log.debug( session + " expanded outNetBuffer:" + outNetBuffer );
+                if ( SessionLog.isDebugEnabled( session ) ) {
+                    SessionLog.debug( session, " expanded outNetBuffer:" + outNetBuffer );
                 }
             }
 
             result = sslEngine.wrap( src, outNetBuffer );
-            if ( log.isDebugEnabled() ) {
-                log.debug( session + " Wrap res:" + result );
+            if ( SessionLog.isDebugEnabled( session ) ) {
+                SessionLog.debug( session, " Wrap res:" + result );
             }
 
             if ( result.getStatus() == SSLEngineResult.Status.OK ) {
@@ -369,8 +366,8 @@
 
         if( appBuffer.hasRemaining() )
         {
-             if ( log.isDebugEnabled() ) {
-                 log.debug( session + " Error: appBuffer not empty!" );
+             if ( SessionLog.isDebugEnabled( session ) ) {
+                 SessionLog.debug( session, " Error: appBuffer not empty!" );
              }
             //still app data in buffer!?
             throw new IllegalStateException();
@@ -402,9 +399,9 @@
      */
     public void handshake( NextFilter nextFilter ) throws SSLException
     {
-        if( log.isDebugEnabled() )
+        if( SessionLog.isDebugEnabled( session ) )
         {
-            log.debug( session + " doHandshake()" );
+            SessionLog.debug( session, " doHandshake()" );
         }
         
         if( initialHandshakeComplete )
@@ -417,11 +414,11 @@
             if( initialHandshakeStatus == SSLEngineResult.HandshakeStatus.FINISHED )
             {
                 session.setAttribute( SSLFilter.SSL_SESSION, sslEngine.getSession() );
-                if( log.isDebugEnabled() )
+                if( SessionLog.isDebugEnabled( session ) )
                 {
                     SSLSession sslSession = sslEngine.getSession();
-                    log.debug( session + "  initialHandshakeStatus=FINISHED" );
-                    log.debug( session + "  sslSession CipherSuite used " + sslSession.getCipherSuite()
);
+                    SessionLog.debug( session, "  initialHandshakeStatus=FINISHED" );
+                    SessionLog.debug( session, "  sslSession CipherSuite used " + sslSession.getCipherSuite()
);
                 }
                 initialHandshakeComplete = true;
                 if( session.containsAttribute( SSLFilter.USE_NOTIFICATION ) )
@@ -432,19 +429,18 @@
             }
             else if( initialHandshakeStatus == SSLEngineResult.HandshakeStatus.NEED_TASK
)
             {
-                if( log.isDebugEnabled() )
+                if( SessionLog.isDebugEnabled( session ) )
                 {
-                    log.debug( session + "  initialHandshakeStatus=NEED_TASK" );
+                    SessionLog.debug( session, "  initialHandshakeStatus=NEED_TASK" );
                 }
                 initialHandshakeStatus = doTasks();
             }
             else if( initialHandshakeStatus == SSLEngineResult.HandshakeStatus.NEED_UNWRAP
)
             {
                 // we need more data read
-                if( log.isDebugEnabled() )
+                if( SessionLog.isDebugEnabled( session ) )
                 {
-                    log.debug( session +
-                             "  initialHandshakeStatus=NEED_UNWRAP" );
+                    SessionLog.debug( session, "  initialHandshakeStatus=NEED_UNWRAP" );
                 }
                 SSLEngineResult.Status status = unwrapHandshake();
                 if( ( initialHandshakeStatus != SSLEngineResult.HandshakeStatus.FINISHED

@@ -457,25 +453,25 @@
             }
             else if( initialHandshakeStatus == SSLEngineResult.HandshakeStatus.NEED_WRAP
)
             {
-                if( log.isDebugEnabled() )
+                if( SessionLog.isDebugEnabled( session ) )
                 {
-                    log.debug( session + "  initialHandshakeStatus=NEED_WRAP" );
+                    SessionLog.debug( session, "  initialHandshakeStatus=NEED_WRAP" );
                 }
                 // First make sure that the out buffer is completely empty. Since we
                 // cannot call wrap with data left on the buffer
                 if( outNetBuffer.hasRemaining() )
                 {
-                    if( log.isDebugEnabled() )
+                    if( SessionLog.isDebugEnabled( session ) )
                     {
-                        log.debug( session + "  Still data in out buffer!" );
+                        SessionLog.debug( session, "  Still data in out buffer!" );
                     }
                     break;
                 }
                 outNetBuffer.clear();
                 SSLEngineResult result = sslEngine.wrap( hsBB, outNetBuffer );
-                if( log.isDebugEnabled() )
+                if( SessionLog.isDebugEnabled( session ) )
                 {
-                    log.debug( session + " Wrap res:" + result );
+                    SessionLog.debug( session, " Wrap res:" + result );
                 }
 
                 outNetBuffer.flip();
@@ -511,14 +507,14 @@
         
         try
         {
-            if( log.isDebugEnabled() )
+            if( SessionLog.isDebugEnabled( session ) )
             {
-                log.debug( session + " write outNetBuffer: " + getOutNetBuffer() );
+                SessionLog.debug( session, " write outNetBuffer: " + getOutNetBuffer() );
             }
             org.apache.mina.common.ByteBuffer writeBuffer = copy( getOutNetBuffer() );
-            if( log.isDebugEnabled() )
+            if( SessionLog.isDebugEnabled( session ) )
             {
-                log.debug( session + " session write: " + writeBuffer );
+                SessionLog.debug( session, " session write: " + writeBuffer );
             }
             //debug("outNetBuffer (after copy): {0}", sslHandler.getOutNetBuffer());
             
@@ -541,9 +537,9 @@
                 }
                 if( getOutNetBuffer().hasRemaining() )
                 {
-                    if( log.isDebugEnabled() )
+                    if( SessionLog.isDebugEnabled( session ) )
                     {
-                        log.debug( session + " write outNetBuffer2: " + getOutNetBuffer()
);
+                        SessionLog.debug( session, " write outNetBuffer2: " + getOutNetBuffer()
);
                     }
                     org.apache.mina.common.ByteBuffer writeBuffer2 = copy( getOutNetBuffer()
);
                     writeFuture = new WriteFuture();
@@ -569,9 +565,9 @@
     
     private SSLEngineResult.Status unwrap() throws SSLException
     {
-        if( log.isDebugEnabled() )
+        if( SessionLog.isDebugEnabled( session ) )
         {
-            log.debug( session + " unwrap()" );
+            SessionLog.debug( session, " unwrap()" );
         }
         // Prepare the application buffer to receive decrypted data
         appBuffer.clear();
@@ -582,15 +578,15 @@
         SSLEngineResult res;
         do
         {
-            if( log.isDebugEnabled() )
+            if( SessionLog.isDebugEnabled( session ) )
             {
-                log.debug( session + "   inNetBuffer: " + inNetBuffer );
-                log.debug( session + "   appBuffer: " + appBuffer );
+                SessionLog.debug( session, "   inNetBuffer: " + inNetBuffer );
+                SessionLog.debug( session, "   appBuffer: " + appBuffer );
             }
             res = sslEngine.unwrap( inNetBuffer, appBuffer );
-            if( log.isDebugEnabled() )
+            if( SessionLog.isDebugEnabled( session ) )
             {
-                log.debug( session + " Unwrap res:" + res );
+                SessionLog.debug( session, " Unwrap res:" + res );
             }
         }
         while( res.getStatus() == SSLEngineResult.Status.OK );
@@ -613,9 +609,9 @@
 
     private SSLEngineResult.Status unwrapHandshake() throws SSLException
     {
-        if( log.isDebugEnabled() )
+        if( SessionLog.isDebugEnabled( session ) )
         {
-            log.debug( session + " unwrapHandshake()" );
+            SessionLog.debug( session, " unwrapHandshake()" );
         }
         // Prepare the application buffer to receive decrypted data
         appBuffer.clear();
@@ -626,15 +622,15 @@
         SSLEngineResult res;
         do
         {
-            if( log.isDebugEnabled() )
+            if( SessionLog.isDebugEnabled( session ) )
             {
-                log.debug( session + "   inNetBuffer: " + inNetBuffer );
-                log.debug( session + "   appBuffer: " + appBuffer );
+                SessionLog.debug( session, "   inNetBuffer: " + inNetBuffer );
+                SessionLog.debug( session, "   appBuffer: " + appBuffer );
             }
             res = sslEngine.unwrap( inNetBuffer, appBuffer );
-            if( log.isDebugEnabled() )
+            if( SessionLog.isDebugEnabled( session ) )
             {
-                log.debug( session + " Unwrap res:" + res );
+                SessionLog.debug( session, " Unwrap res:" + res );
             }
 
         }
@@ -650,14 +646,14 @@
 				&& res.getStatus() == SSLEngineResult.Status.OK
 				&& inNetBuffer.hasRemaining()) {
 			do {
-				if (log.isDebugEnabled()) {
-					log.debug( session + "  extra handshake unwrap" );
-                    log.debug( session + "   inNetBuffer: " + inNetBuffer );
-                    log.debug( session + "   appBuffer: " + appBuffer );
+				if (SessionLog.isDebugEnabled( session )) {
+					SessionLog.debug( session, "  extra handshake unwrap" );
+                    SessionLog.debug( session, "   inNetBuffer: " + inNetBuffer );
+                    SessionLog.debug( session, "   appBuffer: " + appBuffer );
 				}
 				res = sslEngine.unwrap(inNetBuffer, appBuffer);
-				if (log.isDebugEnabled()) {
-                    log.debug( session + " Unwrap res:" + res );
+				if (SessionLog.isDebugEnabled( session )) {
+                    SessionLog.debug( session, " Unwrap res:" + res );
 				}
 			} while (res.getStatus() == SSLEngineResult.Status.OK);
 		}
@@ -685,9 +681,9 @@
      */
     private SSLEngineResult.HandshakeStatus doTasks()
     {
-        if( log.isDebugEnabled() )
+        if( SessionLog.isDebugEnabled( session ) )
         {
-            log.debug( session + "   doTasks()" );
+            SessionLog.debug( session, "   doTasks()" );
         }
 
         /*
@@ -697,15 +693,15 @@
         Runnable runnable;
         while( ( runnable = sslEngine.getDelegatedTask() ) != null )
         {
-            if( log.isDebugEnabled() )
+            if( SessionLog.isDebugEnabled( session ) )
             {
-                log.debug( session + "    doTask: " + runnable );
+                SessionLog.debug( session, "    doTask: " + runnable );
             }
             runnable.run();
         }
-        if( log.isDebugEnabled() )
+        if( SessionLog.isDebugEnabled( session ) )
         {
-            log.debug( session + "   doTasks(): "
+            SessionLog.debug( session, "   doTasks(): "
                     + sslEngine.getHandshakeStatus() );
         }
         return sslEngine.getHandshakeStatus();

Modified: directory/network/trunk/src/java/org/apache/mina/util/SessionLog.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/util/SessionLog.java?rev=330420&r1=330419&r2=330420&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/util/SessionLog.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/util/SessionLog.java Wed Nov  2 18:37:20
2005
@@ -43,25 +43,6 @@
      */
     public static final String LOGGER = SessionLog.class.getName() + ".logger";
     
-    public static Logger getLogger( IoSession session )
-    {
-        Logger log = (Logger) session.getAttribute( LOGGER );
-        if( log == null )
-        {
-            log = LoggerFactory.getLogger( getClass( session ) );
-            String prefix = ( String ) session.getAttribute( PREFIX );
-            if( prefix == null )
-            {
-                prefix = "[" + session.getRemoteAddress() + "] ";
-                session.setAttribute( PREFIX, prefix );
-            }
-                
-            session.setAttribute( LOGGER, log );
-        }
-        
-        return log;
-    }
-    
     private static Class getClass( IoSession session )
     {
         return session.getHandler().getClass();
@@ -137,5 +118,44 @@
         {
             log.error( String.valueOf( session.getAttribute( PREFIX ) ) + message, cause
);
         }
+    }
+    
+    public static boolean isDebugEnabled( IoSession session )
+    {
+        return getLogger( session ).isDebugEnabled();
+    }
+    
+    public static boolean isInfoEnabled( IoSession session )
+    {
+        return getLogger( session ).isInfoEnabled();
+    }
+    
+    public static boolean isWarnEnabled( IoSession session )
+    {
+        return getLogger( session ).isWarnEnabled();
+    }
+    
+    public static boolean isErrorEnabled( IoSession session )
+    {
+        return getLogger( session ).isErrorEnabled();
+    }
+
+    private static Logger getLogger( IoSession session )
+    {
+        Logger log = (Logger) session.getAttribute( LOGGER );
+        if( log == null )
+        {
+            log = LoggerFactory.getLogger( getClass( session ) );
+            String prefix = ( String ) session.getAttribute( PREFIX );
+            if( prefix == null )
+            {
+                prefix = "[" + session.getRemoteAddress() + "] ";
+                session.setAttribute( PREFIX, prefix );
+            }
+                
+            session.setAttribute( LOGGER, log );
+        }
+        
+        return log;
     }
 }



Mime
View raw message