You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by mw...@apache.org on 2007/11/15 04:53:08 UTC

svn commit: r595198 - in /mina/sandbox/mwebb/mina: ./ src/main/java/org/apache/mina/util/app/ src/test/java/org/apache/mina/util/app/

Author: mwebb
Date: Wed Nov 14 19:53:07 2007
New Revision: 595198

URL: http://svn.apache.org/viewvc?rev=595198&view=rev
Log:
updated the pom.xml to include the SLF4J libraries. 

Initial addition of the MinaApplication system.

Added:
    mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/
    mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/AbstractMinaApplication.java
    mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaApplication.java
    mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaApplicationFactory.java
    mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaServer.java
    mina/sandbox/mwebb/mina/src/test/java/org/apache/mina/util/app/
    mina/sandbox/mwebb/mina/src/test/java/org/apache/mina/util/app/Tester.java
Modified:
    mina/sandbox/mwebb/mina/pom.xml

Modified: mina/sandbox/mwebb/mina/pom.xml
URL: http://svn.apache.org/viewvc/mina/sandbox/mwebb/mina/pom.xml?rev=595198&r1=595197&r2=595198&view=diff
==============================================================================
--- mina/sandbox/mwebb/mina/pom.xml (original)
+++ mina/sandbox/mwebb/mina/pom.xml Wed Nov 14 19:53:07 2007
@@ -77,6 +77,13 @@
 			<version>2.0.0-M1-SNAPSHOT</version>
 		</dependency>
 
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-simple</artifactId>
+			<version>1.0</version>
+			<scope>compile</scope>
+		</dependency>
+
 	</dependencies>
 
 	<build>

Added: mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/AbstractMinaApplication.java
URL: http://svn.apache.org/viewvc/mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/AbstractMinaApplication.java?rev=595198&view=auto
==============================================================================
--- mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/AbstractMinaApplication.java (added)
+++ mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/AbstractMinaApplication.java Wed Nov 14 19:53:07 2007
@@ -0,0 +1,60 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+
+package org.apache.mina.util.app;
+
+import org.apache.mina.common.IoHandlerAdapter;
+
+/**
+ * TODO AbstractMinaApplication.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public abstract class AbstractMinaApplication implements MinaApplication
+{
+    protected static final int TCP = 1;
+    protected static final int UDP = 2;
+    
+    private int port;
+    private IoHandlerAdapter handler;
+    
+    public AbstractMinaApplication( int port, IoHandlerAdapter handler )
+    {
+        this.port = port;
+        this.handler = handler;
+    }
+    
+    /* (non-Javadoc)
+     * @see org.apache.mina.util.app.MinaApplication#getHandler()
+     */
+    public IoHandlerAdapter getHandler()
+    {
+        return handler;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.mina.util.app.MinaApplication#getPort()
+     */
+    public int getPort()
+    {
+        return port;
+    }
+}
\ No newline at end of file

Added: mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaApplication.java
URL: http://svn.apache.org/viewvc/mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaApplication.java?rev=595198&view=auto
==============================================================================
--- mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaApplication.java (added)
+++ mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaApplication.java Wed Nov 14 19:53:07 2007
@@ -0,0 +1,49 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+
+package org.apache.mina.util.app;
+
+import org.apache.mina.common.IoFilter;
+import org.apache.mina.common.IoHandlerAdapter;
+
+/**
+ * TODO MinaServer.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public interface MinaApplication
+{
+    public void startup() throws Exception;
+    
+    public void shutdown() throws Exception;
+    
+    public boolean isRunning();
+    
+    public int getPort();
+    
+    public IoHandlerAdapter getHandler();
+    
+    public void addFilterFirst( String name, IoFilter filter );
+    
+    public void addFilterLast( String name, IoFilter filter );
+    
+    public void addLogging();
+}

Added: mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaApplicationFactory.java
URL: http://svn.apache.org/viewvc/mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaApplicationFactory.java?rev=595198&view=auto
==============================================================================
--- mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaApplicationFactory.java (added)
+++ mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaApplicationFactory.java Wed Nov 14 19:53:07 2007
@@ -0,0 +1,131 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+
+package org.apache.mina.util.app;
+
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.IoHandlerAdapter;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.IoSessionLogger;
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
+
+/**
+ * Factory class that creates various instances of {@link MinaApplication}.  The 
+ * methods found in this class will generate different types of MINA-based servers
+ * for use in quick prototyping and simple testing.
+ * 
+ * This class is not designed to produce production-quality systems.  These 
+ * {@link MinaApplication} objects can be further configured in order to build 
+ * more robust applications.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class MinaApplicationFactory
+{
+    /**
+     * Creates a simple TCP based server which uses no filtering.  
+     *
+     * @param port
+     *  The port that this server will listen on
+     * @param handler
+     *  The {@link IoHandlerAdapter} that will process incoming messages
+     * @return
+     *  A {@link MinaApplication} object
+     */
+    public static MinaApplication createSimpleTcpDataServer( int port, IoHandlerAdapter handler ){
+        
+        MinaServer minaServer = new MinaServer( port, handler, AbstractMinaApplication.TCP );
+        
+        return minaServer;
+    }
+    
+    /**
+     * Creates a simple TCP based server which utilizes the {@link TextLineCodecFactory}
+     *
+     * @param port
+     *  The port that the server will listen on
+     * @param handler
+     *  The {@link IoHandlerAdapter} that the server will use to service incoming connections
+     * @return
+     *  A {@link MinaApplication} object
+     */
+    public static MinaApplication createSimpleTcpTextServer( int port, IoHandlerAdapter handler ){
+        
+        MinaServer minaServer = new MinaServer( port, handler, AbstractMinaApplication.TCP );
+        
+        minaServer.addFilterFirst( "codec", new ProtocolCodecFilter( new TextLineCodecFactory() ) );
+        
+        return minaServer;
+    }
+    
+    /**
+     * This method creates a simple TCP based server that just prints out a message when
+     * a method gets called in the {@link IoHandlerAdapter}.  
+     *
+     * @param port
+     *  The port that this {@link MinaApplication} will listen on
+     * @return
+     *  A {@link MinaApplication} object
+     */
+    public static MinaApplication createLoggingTcpServer( int port ){
+        
+        MinaServer minaServer = new MinaServer( port, new IoHandlerAdapter(){
+
+            public void exceptionCaught( IoSession session, Throwable cause ) throws Exception
+            {
+                IoSessionLogger.getLogger( session ).info( cause );
+            }
+
+            public void messageReceived( IoSession session, Object message ) throws Exception
+            {
+                IoSessionLogger.getLogger( session ).info( "Message Received" );
+            }
+
+            public void messageSent( IoSession session, Object message ) throws Exception
+            {
+                IoSessionLogger.getLogger( session ).info( "Message Sent" );
+            }
+
+            public void sessionClosed( IoSession session ) throws Exception
+            {
+                IoSessionLogger.getLogger( session ).info( "Session Closed" );
+            }
+
+            public void sessionCreated( IoSession session ) throws Exception
+            {
+                IoSessionLogger.getLogger( session ).info( "Session Created" );
+            }
+
+            public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
+            {
+                IoSessionLogger.getLogger( session ).info( "Session Idle" );
+            }
+
+            public void sessionOpened( IoSession session ) throws Exception
+            {
+                IoSessionLogger.getLogger( session ).info( "Session Opened" );
+            }
+        }, AbstractMinaApplication.TCP);
+        
+        return minaServer;
+    }
+}

Added: mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaServer.java
URL: http://svn.apache.org/viewvc/mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaServer.java?rev=595198&view=auto
==============================================================================
--- mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaServer.java (added)
+++ mina/sandbox/mwebb/mina/src/main/java/org/apache/mina/util/app/MinaServer.java Wed Nov 14 19:53:07 2007
@@ -0,0 +1,108 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+
+package org.apache.mina.util.app;
+
+import java.net.InetSocketAddress;
+
+import org.apache.mina.common.IoAcceptor;
+import org.apache.mina.common.IoFilter;
+import org.apache.mina.common.IoHandlerAdapter;
+import org.apache.mina.filter.logging.LoggingFilter;
+import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
+import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
+
+/**
+ * TODO MinaServer.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class MinaServer extends AbstractMinaApplication
+{
+    private IoAcceptor acceptor;
+    
+    public MinaServer( int port, IoHandlerAdapter handler, int networkType )
+    {
+        super( port, handler );
+        
+        switch( networkType ){
+            case TCP:
+                acceptor = new NioSocketAcceptor();
+                break;
+            case UDP:
+                acceptor = new NioDatagramAcceptor();
+                break;
+            default:
+                throw new IllegalArgumentException("Invalid NetworkProtocol type");
+        }
+        
+        acceptor.setLocalAddress( new InetSocketAddress(getPort()) );
+        acceptor.setHandler( getHandler() );
+    }
+    
+    /* (non-Javadoc)
+     * @see org.apache.mina.util.app.MinaApplication#addFilterFirst(java.lang.String, org.apache.mina.common.IoFilter)
+     */
+    public void addFilterFirst( String name, IoFilter filter )
+    {
+        acceptor.getFilterChain().addFirst( name, filter );
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.mina.util.app.MinaApplication#addFilterLast(java.lang.String, org.apache.mina.common.IoFilter)
+     */
+    public void addFilterLast( String name, IoFilter filter )
+    {
+        acceptor.getFilterChain().addLast( name, filter );
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.mina.util.app.MinaApplication#isRunning()
+     */
+    public boolean isRunning()
+    {
+        return acceptor.isActive();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.mina.util.app.MinaApplication#shutdown()
+     */
+    public void shutdown() throws Exception
+    {
+        acceptor.unbind();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.mina.util.app.MinaApplication#startup()
+     */
+    public void startup() throws Exception
+    {
+        acceptor.bind();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.mina.util.app.MinaApplication#addLogging()
+     */
+    public void addLogging()
+    {
+        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
+    }
+}

Added: mina/sandbox/mwebb/mina/src/test/java/org/apache/mina/util/app/Tester.java
URL: http://svn.apache.org/viewvc/mina/sandbox/mwebb/mina/src/test/java/org/apache/mina/util/app/Tester.java?rev=595198&view=auto
==============================================================================
--- mina/sandbox/mwebb/mina/src/test/java/org/apache/mina/util/app/Tester.java (added)
+++ mina/sandbox/mwebb/mina/src/test/java/org/apache/mina/util/app/Tester.java Wed Nov 14 19:53:07 2007
@@ -0,0 +1,13 @@
+
+package org.apache.mina.util.app;
+
+import org.apache.mina.common.IoHandlerAdapter;
+
+public class Tester
+{
+    public static void main( String[] args ) throws Exception
+    {
+        MinaApplication app = MinaApplicationFactory.createSimpleTcpTextServer( 9999, new IoHandlerAdapter() );
+        app.startup();
+    }
+}