directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r165587 - in /directory/network/trunk/src: examples/org/apache/mina/examples/echoserver/ examples/org/apache/mina/examples/reverser/ java/org/apache/mina/io/filter/ java/org/apache/mina/protocol/filter/
Date Mon, 02 May 2005 06:44:04 GMT
Author: trustin
Date: Sun May  1 23:44:03 2005
New Revision: 165587

URL: http://svn.apache.org/viewcvs?rev=165587&view=rev
Log:
* Added a LoggingFilter to examples
* Fixed: LoggingFilter didn't work
* Added: SessionLog (extracted from LoggingFilter)
* Removed unused codes

Modified:
    directory/network/trunk/src/examples/org/apache/mina/examples/echoserver/Main.java
    directory/network/trunk/src/examples/org/apache/mina/examples/reverser/Main.java
    directory/network/trunk/src/examples/org/apache/mina/examples/reverser/ReverseProtocolHandler.java
    directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java
    directory/network/trunk/src/java/org/apache/mina/io/filter/SSLFilter.java
    directory/network/trunk/src/java/org/apache/mina/protocol/filter/LoggingFilter.java

Modified: directory/network/trunk/src/examples/org/apache/mina/examples/echoserver/Main.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/echoserver/Main.java?rev=165587&r1=165586&r2=165587&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/echoserver/Main.java (original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/echoserver/Main.java Sun
May  1 23:44:03 2005
@@ -21,6 +21,7 @@
 import org.apache.mina.common.TransportType;
 import org.apache.mina.examples.echoserver.ssl.BogusSSLContextFactory;
 import org.apache.mina.io.IoAcceptor;
+import org.apache.mina.io.filter.LoggingFilter;
 import org.apache.mina.io.filter.SSLFilter;
 import org.apache.mina.registry.Service;
 import org.apache.mina.registry.ServiceRegistry;
@@ -49,7 +50,9 @@
         {
             addSSLSupport( registry );
         }
-
+        
+        addLogger( registry );
+        
         // Bind
         Service service = new Service( "echo", TransportType.SOCKET, PORT );
         registry.bind( service, new EchoProtocolHandler() );
@@ -60,10 +63,17 @@
     private static void addSSLSupport( ServiceRegistry registry )
         throws Exception
     {
-        System.out.println( "SSL is enabled." );
         SSLFilter sslFilter =
             new SSLFilter( BogusSSLContextFactory.getInstance( true ) );
         IoAcceptor acceptor = registry.getIoAcceptor( TransportType.SOCKET );
         acceptor.getFilterChain().addLast( "sslFilter", sslFilter );
+        System.out.println( "SSL ON" );
+    }
+    
+    private static void addLogger( ServiceRegistry registry )
+    {
+        IoAcceptor acceptor = registry.getIoAcceptor( TransportType.SOCKET );
+        acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
+        System.out.println( "Logging ON" );
     }
 }

Modified: directory/network/trunk/src/examples/org/apache/mina/examples/reverser/Main.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/reverser/Main.java?rev=165587&r1=165586&r2=165587&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/reverser/Main.java (original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/reverser/Main.java Sun May
 1 23:44:03 2005
@@ -19,6 +19,8 @@
 package org.apache.mina.examples.reverser;
 
 import org.apache.mina.common.TransportType;
+import org.apache.mina.protocol.ProtocolAcceptor;
+import org.apache.mina.protocol.filter.LoggingFilter;
 import org.apache.mina.registry.Service;
 import org.apache.mina.registry.ServiceRegistry;
 import org.apache.mina.registry.SimpleServiceRegistry;
@@ -38,10 +40,18 @@
     {
         ServiceRegistry registry = new SimpleServiceRegistry();
 
+        addLogger( registry );
         // Bind
         Service service = new Service( "reverse", TransportType.SOCKET, PORT );
         registry.bind( service, new ReverseProtocolProvider() );
 
         System.out.println( "Listening on port " + PORT );
+    }
+
+    private static void addLogger( ServiceRegistry registry )
+    {
+        ProtocolAcceptor acceptor = registry.getProtocolAcceptor( TransportType.SOCKET );
+        acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
+        System.out.println( "Logging ON" );
     }
 }

Modified: directory/network/trunk/src/examples/org/apache/mina/examples/reverser/ReverseProtocolHandler.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/reverser/ReverseProtocolHandler.java?rev=165587&r1=165586&r2=165587&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/reverser/ReverseProtocolHandler.java
(original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/reverser/ReverseProtocolHandler.java
Sun May  1 23:44:03 2005
@@ -32,17 +32,12 @@
 {
     public void exceptionCaught( ProtocolSession session, Throwable cause )
     {
-        System.out.println( session.getRemoteAddress() + " EXCEPTION" );
-        cause.printStackTrace( System.out );
-
         // Close connection when unexpected exception is caught.
         session.close();
     }
 
     public void messageReceived( ProtocolSession session, Object message )
     {
-        System.out.println( session.getRemoteAddress() + " RCVD: " + message );
-
         // Reverse reveiced string
         String str = message.toString();
         StringBuffer buf = new StringBuffer( str.length() );

Modified: directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java?rev=165587&r1=165586&r2=165587&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/filter/LoggingFilter.java Sun May
 1 23:44:03 2005
@@ -24,126 +24,74 @@
 import org.apache.mina.common.ByteBuffer;
 import org.apache.mina.common.IdleStatus;
 import org.apache.mina.io.IoFilter;
-import org.apache.mina.io.IoHandler;
 import org.apache.mina.io.IoSession;
+import org.apache.mina.util.SessionLog;
 
 /**
  * Logs all MINA I/O events to {@link Logger}.
- * <p>
- * Call {@link #getLogger(IoSession)}, {@link #log(IoSession, String)}, and
- * {@link #log(IoSession, String, Throwable)} to log protocol-specific messages.
- * <p>
- * Set {@link #PREFIX}, {@link #LOGGER}, {@link #LEVEL} session attributes
- * to override prefix string, logger, and log level.
- * (See {@link IoHandler#sessionCreated(IoSession)}))
  * 
  * @author The Apache Directory Project (dev@directory.apache.org)
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
+ * 
+ * @see SessionLog
  */
 public class LoggingFilter implements IoFilter
 {
     /**
      * Session attribute key: prefix string
      */
-    public static final String PREFIX = LoggingFilter.class.getName() + ".prefix";
+    public static final String PREFIX = SessionLog.PREFIX;
 
     /**
      * Session attribute key: {@link Logger}
      */
-    public static final String LOGGER = LoggingFilter.class.getName() + ".logger";
+    public static final String LOGGER = SessionLog.LOGGER;
     
     /**
      * Session attribute key: {@link Level}
      */
-    public static final String LEVEL = LoggingFilter.class.getName() + ".level";
+    public static final String LEVEL = SessionLog.LEVEL;
     
-    public static Logger getLogger( IoSession session )
-    {
-        
-        Logger log = (Logger) session.getAttribute( LOGGER );
-        if( log == null )
-        {
-            log = Logger.getLogger( session.getHandler().getClass().getName() );
-            String prefix = ( String ) session.getAttribute( PREFIX );
-            if( prefix != null )
-            {
-                prefix = "[" + session.getRemoteAddress() + "] ";
-                session.setAttribute( PREFIX, prefix );
-            }
-            
-            Level level = ( Level ) session.getAttribute( LEVEL );
-            if( level != null )
-            {
-                level = Level.INFO;
-                session.setAttribute( LEVEL, level );
-            }
-                
-            session.setAttribute( LOGGER, log );
-        }
-        
-        return log;
-    }
-
-    public static void log( IoSession session, String message )
-    {
-        Logger log = getLogger( session );
-        Level level = ( Level ) session.getAttribute( LEVEL );
-        if( log.isLoggable( level ) )
-        {
-            log.log( level, message );
-        }
-    }
-
-    public static void log( IoSession session, String message, Throwable cause )
-    {
-        Logger log = getLogger( session );
-        Level level = ( Level ) session.getAttribute( LEVEL );
-        if( log.isLoggable( level ) )
-        {
-            log.log( level, message, cause );
-        }
-    }
-
     public void sessionOpened( NextFilter nextFilter, IoSession session )
     {
-        log( session, "OPENED" );
+        SessionLog.log( session, "OPENED" );
         nextFilter.sessionOpened( session );
     }
 
     public void sessionClosed( NextFilter nextFilter, IoSession session )
     {
-        log( session, "CLOSED" );
+        SessionLog.log( session, "CLOSED" );
         nextFilter.sessionClosed( session );
     }
 
     public void sessionIdle( NextFilter nextFilter, IoSession session, IdleStatus status
)
     {
-        log( session, "IDLE: " + status );
+        SessionLog.log( session, "IDLE: " + status );
         nextFilter.sessionIdle( session, status );
     }
 
     public void exceptionCaught( NextFilter nextFilter, IoSession session, Throwable cause
)
     {
-        log( session, "EXCEPTION:", cause );
+        SessionLog.log( session, "EXCEPTION:", cause );
         nextFilter.exceptionCaught( session, cause );
     }
 
     public void dataRead( NextFilter nextFilter, IoSession session, ByteBuffer buf)
     {
-        log( session, "READ: " + buf.getHexDump() );
+        SessionLog.log( session, "READ: " + buf.getHexDump() );
         nextFilter.dataRead( session, buf );
     }
 
     public void dataWritten( NextFilter nextFilter, IoSession session, Object marker)
     {
-        log( session, "WRITTEN: " + marker );
+        SessionLog.log( session, "WRITTEN: " + marker );
         nextFilter.dataWritten( session, marker );
     }
 
     public void filterWrite( NextFilter nextFilter, IoSession session, ByteBuffer buf, Object
marker)
     {
-        log( session, "WRITE:" + marker + ", " + buf.getHexDump() );
+        SessionLog.log( session, "WRITE:" + marker + ", " + buf.getHexDump() );
         nextFilter.filterWrite( session, buf, marker );
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/io/filter/SSLFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/filter/SSLFilter.java?rev=165587&r1=165586&r2=165587&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/filter/SSLFilter.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/filter/SSLFilter.java Sun May  1 23:44:03
2005
@@ -18,9 +18,6 @@
  */
 package org.apache.mina.io.filter;
 
-import java.lang.reflect.Method;
-import java.text.DateFormat;
-import java.util.Date;
 import java.util.IdentityHashMap;
 import java.util.Map;
 import java.util.logging.Level;
@@ -32,9 +29,8 @@
 import javax.net.ssl.SSLHandshakeException;
 
 import org.apache.mina.common.ByteBuffer;
-import org.apache.mina.common.DefaultExceptionMonitor;
-import org.apache.mina.io.IoHandler;
 import org.apache.mina.io.IoFilterAdapter;
+import org.apache.mina.io.IoHandler;
 import org.apache.mina.io.IoSession;
 
 /**
@@ -522,127 +518,6 @@
         synchronized( sslSessionHandlerMap )
         {
             sslSessionHandlerMap.remove( session );
-        }
-    }
-
-    /**
-     * An interface that users can log debug messages from an {@link SSLFilter}.
-     * 
-     * @author Trustin Lee (trustin@apache.org)
-     * @version $Rev$, $Date$
-     */
-    public static interface Debug
-    {
-        /**
-         * This will print out the messages to Commons-Logging or stdout.
-         */
-        static final Debug ON = new DebugOn();
-
-        /**
-         * This will suppress debug messages.
-         */
-        static final Debug OFF = new DebugOff();
-
-        /**
-         * Prints out the specified debug messages.
-         */
-        void print( SSLFilter filter, String message );
-    }
-
-    private static class DebugOn implements Debug
-    {
-        private static final Object log;
-
-        private static final Method debugMethod;
-
-        static
-        {
-            Object tempLog = null;
-            Method tempDebugMethod = null;
-
-            try
-            {
-                Class logCls = Class
-                        .forName( "org.apache.commons.logging.Log" );
-                Class logFactoryCls = Class
-                        .forName( "org.apache.commons.logging.LogFactory" );
-                Method getLogMethod = logFactoryCls.getMethod( "getLog",
-                        new Class[] { String.class } );
-                tempLog = getLogMethod.invoke( null,
-                        new Object[] { DefaultExceptionMonitor.class
-                                .getPackage().getName() } );
-                tempDebugMethod = logCls.getMethod( "debug",
-                        new Class[] { Object.class } );
-            }
-            catch( Exception e )
-            {
-                tempLog = null;
-                tempDebugMethod = null;
-            }
-
-            log = tempLog;
-            debugMethod = tempDebugMethod;
-        }
-
-        private final DateFormat df = DateFormat.getDateTimeInstance(
-                DateFormat.MEDIUM, DateFormat.MEDIUM );
-
-        private final Date date = new Date();
-
-        public void print( SSLFilter filter, String message )
-        {
-            if( filter.isUseClientMode() )
-            {
-                message = "[CLIENT] " + message;
-            }
-            else
-            {
-                message = "[SERVER] " + message;
-            }
-
-            if( log == null )
-            {
-                logToStdOut( message );
-            }
-            else
-            {
-                logToCommonsLogging( message );
-            }
-        }
-
-        private void logToCommonsLogging( String message )
-        {
-            try
-            {
-                debugMethod.invoke( log, new Object[] { message } );
-            }
-            catch( Exception e )
-            {
-                logToStdOut( message );
-            }
-        }
-
-        private void logToStdOut( String message )
-        {
-            synchronized( System.out )
-            {
-                date.setTime( System.currentTimeMillis() );
-
-                System.out.print( '[' );
-                System.out.print( df.format( date ) );
-                System.out.print( "] [" );
-                System.out.print( Thread.currentThread().getName() );
-                System.out.print( "] " );
-                System.out.println( message );
-            }
-        }
-    }
-
-    private static class DebugOff implements Debug
-    {
-        public void print( SSLFilter filter, String message )
-        {
-            // do nothing
         }
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/filter/LoggingFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/filter/LoggingFilter.java?rev=165587&r1=165586&r2=165587&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/filter/LoggingFilter.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/filter/LoggingFilter.java Sun
May  1 23:44:03 2005
@@ -23,126 +23,74 @@
 
 import org.apache.mina.common.IdleStatus;
 import org.apache.mina.protocol.ProtocolFilter;
-import org.apache.mina.protocol.ProtocolHandler;
 import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.util.SessionLog;
 
 /**
  * Logs all MINA protocol events to {@link Logger}.
- * <p>
- * Call {@link #getLogger(ProtocolSession)}, {@link #log(ProtocolSession, String)}, and
- * {@link #log(ProtocolSession, String, Throwable)} to log protocol-specific messages.
- * <p>
- * Set {@link #PREFIX}, {@link #LOGGER}, {@link #LEVEL} session attributes
- * to override prefix string, logger, and log level in
- * {@link ProtocolHandler#sessionCreated(ProtocolSession)}.
  * 
  * @author The Apache Directory Project (dev@directory.apache.org)
  * @author Trustin Lee (trustin@apache.org)
  * @version $Rev$, $Date$
+ * 
+ * @see SessionLog
  */
 public class LoggingFilter implements ProtocolFilter
 {
     /**
      * Session attribute key: prefix string
      */
-    public static final String PREFIX = LoggingFilter.class.getName() + ".prefix";
+    public static final String PREFIX = SessionLog.PREFIX;
 
     /**
      * Session attribute key: {@link Logger}
      */
-    public static final String LOGGER = LoggingFilter.class.getName() + ".logger";
+    public static final String LOGGER = SessionLog.LOGGER;
     
     /**
      * Session attribute key: {@link Level}
      */
-    public static final String LEVEL = LoggingFilter.class.getName() + ".level";
+    public static final String LEVEL = SessionLog.LEVEL;
     
-    public static Logger getLogger( ProtocolSession session )
-    {
-        
-        Logger log = (Logger) session.getAttribute( LOGGER );
-        if( log == null )
-        {
-            log = Logger.getLogger( session.getHandler().getClass().getName() );
-            String prefix = ( String ) session.getAttribute( PREFIX );
-            if( prefix != null )
-            {
-                prefix = "[" + session.getRemoteAddress() + "] ";
-                session.setAttribute( PREFIX, prefix );
-            }
-            
-            Level level = ( Level ) session.getAttribute( LEVEL );
-            if( level != null )
-            {
-                level = Level.INFO;
-                session.setAttribute( LEVEL, level );
-            }
-                
-            session.setAttribute( LOGGER, log );
-        }
-        
-        return log;
-    }
-
-    public static void log( ProtocolSession session, String message )
-    {
-        Logger log = getLogger( session );
-        Level level = ( Level ) session.getAttribute( LEVEL );
-        if( log.isLoggable( level ) )
-        {
-            log.log( level, message );
-        }
-    }
-
-    public static void log( ProtocolSession session, String message, Throwable cause )
-    {
-        Logger log = getLogger( session );
-        Level level = ( Level ) session.getAttribute( LEVEL );
-        if( log.isLoggable( level ) )
-        {
-            log.log( level, message, cause );
-        }
-    }
-
     public void sessionOpened( NextFilter nextFilter, ProtocolSession session )
     {
-        log( session, "OPENED" );
+        SessionLog.log( session, "OPENED" );
         nextFilter.sessionOpened( session );
     }
 
     public void sessionClosed( NextFilter nextFilter, ProtocolSession session )
     {
-        log( session, "CLOSED" );
+        SessionLog.log( session, "CLOSED" );
         nextFilter.sessionClosed( session );
     }
 
     public void sessionIdle( NextFilter nextFilter, ProtocolSession session, IdleStatus status
)
     {
-        log( session, "IDLE: " + status );
+        SessionLog.log( session, "IDLE: " + status );
         nextFilter.sessionIdle( session, status );
     }
 
     public void exceptionCaught( NextFilter nextFilter, ProtocolSession session, Throwable
cause )
     {
-        log( session, "EXCEPTION:", cause );
+        SessionLog.log( session, "EXCEPTION:", cause );
         nextFilter.exceptionCaught( session, cause );
     }
 
     public void messageReceived( NextFilter nextFilter, ProtocolSession session, Object message
)
     {
-        log( session, "RECEIVED: " + message );
+        SessionLog.log( session, "RECEIVED: " + message );
         nextFilter.messageReceived( session, message );
     }
 
     public void messageSent( NextFilter nextFilter, ProtocolSession session, Object message
)
     {
-        log( session, "SENT: " + message );
+        SessionLog.log( session, "SENT: " + message );
         nextFilter.messageSent( session, message );
     }
 
     public void filterWrite( NextFilter nextFilter, ProtocolSession session, Object message)
     {
-        log( session, "WRITE: " + message );
+        SessionLog.log( session, "WRITE: " + message );
         nextFilter.filterWrite( session, message );
     }
 }



Mime
View raw message