You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2006/03/23 09:23:57 UTC

svn commit: r388103 - in /directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton: ./ SingleSessionIoHandler.java SingleSessionIoHandlerAdapter.java SingleSessionIoHandlerDelegate.java SingleSessionIoHandlerFactory.java package.html

Author: trustin
Date: Thu Mar 23 00:23:45 2006
New Revision: 388103

URL: http://svn.apache.org/viewcvs?rev=388103&view=rev
Log:
Resolved issue: DIRMINA-187 (Support IoHandler per IoSession (SingleSessionIoHandler))
* Applied Simon's patch.
** classes are moved to org.apache.min.handler.multiton
** fixed some JavaDoc errors

Added:
    directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/
    directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandler.java   (with props)
    directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerAdapter.java   (with props)
    directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerDelegate.java   (with props)
    directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerFactory.java   (with props)
    directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/package.html

Added: directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandler.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandler.java?rev=388103&view=auto
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandler.java (added)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandler.java Thu Mar 23 00:23:45 2006
@@ -0,0 +1,108 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed 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.handler.multiton;
+
+import java.io.IOException;
+
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoSession;
+
+/**
+ * A session handler without an {@link IoSession} parameter for simplicity.
+ * <p>
+ * A {@link SingleSessionIoHandler} is similar to an {@link IoHandler} with
+ * the notable difference that a {@link SingleSessionIoHandler} is used only
+ * by one session at a time. Thus, there is no {@link IoSession} parameter in
+ * the methods of this interface.
+ * </p>
+ * <p>
+ * Because events are passed to the session in order, it is possible to store
+ * conversational state as instance variables in this object.
+ * </p>
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface SingleSessionIoHandler
+{
+
+    /**
+     * Invoked when the session is created. Initialize default socket parameters
+     * and user-defined attributes here.
+     * 
+     * @throws Exception
+     * @see IoHandler#sessionCreated(IoSession)
+     */
+    void sessionCreated() throws Exception;
+
+    /**
+     * Invoked when the connection is opened. This method is not invoked if the
+     * transport type is UDP.
+     * 
+     * @see IoHandler#sessionOpened(IoSession)
+     */
+    void sessionOpened() throws Exception;
+
+    /**
+     * Invoked when the connection is closed. This method is not invoked if the
+     * transport type is UDP.
+     * 
+     * @see IoHandler#sessionClosed(IoSession)
+     */
+    void sessionClosed() throws Exception;
+
+    /**
+     * Invoked when the connection is idle. Refer to {@link IdleStatus}. This
+     * method is not invoked if the transport type is UDP.
+     * 
+     * @param status the type of idleness
+     * @see IoHandler#sessionIdle(IoSession, IdleStatus)
+     */
+    void sessionIdle(IdleStatus status) throws Exception;
+
+    /**
+     * Invoked when any exception is thrown by user {@link IoHandler}
+     * implementation or by MINA. If <code>cause</code> is instanceof
+     * {@link IOException}, MINA will close the connection automatically.
+     * 
+     * @param cause the caught exception
+     * @see IoHandler#exceptionCaught(IoSession, Throwable)
+     */
+    void exceptionCaught(Throwable cause) throws Exception;
+
+    /**
+     * Invoked when protocol message is received. Implement your protocol flow
+     * here.
+     * 
+     * @param message the received message
+     * @see IoHandler#messageReceived(IoSession, Object)
+     */
+    void messageReceived(Object message) throws Exception;
+
+    /**
+     * Invoked when protocol message that user requested by
+     * {@link IoSession#write(Object)} is sent out actually.
+     * 
+     * @param message the sent message
+     * @see IoHandler#messageSent(IoSession, Object)
+     */
+    void messageSent(Object message) throws Exception;
+
+}

Propchange: directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandler.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerAdapter.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerAdapter.java?rev=388103&view=auto
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerAdapter.java (added)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerAdapter.java Thu Mar 23 00:23:45 2006
@@ -0,0 +1,100 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed 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.handler.multiton;
+
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.util.SessionUtil;
+
+/**
+ * Adapter class for implementors of the SingleSessionIoHandler interface. The
+ * session to which the handler is assigned is accessible through the
+ * {@link #getSession()} method.
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class SingleSessionIoHandlerAdapter implements SingleSessionIoHandler
+{
+
+    /**
+     * The session to which the handler is assigned.
+     */
+    private final IoSession session;
+
+    /**
+     * Creates a new SingleSessionIoHandlerAdapter that is assigned to the
+     * passed in session.
+     * 
+     * @param session the session to which the handler is assigned
+     */
+    public SingleSessionIoHandlerAdapter(IoSession session)
+    {
+        if (session == null)
+        {
+            throw new NullPointerException("session");
+        }
+        this.session = session;
+    }
+
+    /**
+     * Retrieves the session to which this handler is assigned.
+     * 
+     * @return the session
+     */
+    protected IoSession getSession() 
+    {
+        return session;
+    }
+
+    public void exceptionCaught(Throwable th) throws Exception 
+    {
+
+    }
+
+    public void messageReceived(Object message) throws Exception 
+    {
+
+    }
+
+    public void messageSent(Object message) throws Exception 
+    {
+
+    }
+
+    public void sessionClosed() throws Exception 
+    {
+        SessionUtil.initialize( getSession() );
+    }
+
+    public void sessionCreated() throws Exception 
+    {
+
+    }
+
+    public void sessionIdle(IdleStatus status) throws Exception 
+    {
+
+    }
+
+    public void sessionOpened() throws Exception 
+    {
+
+    }
+}

Propchange: directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerAdapter.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerDelegate.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerDelegate.java?rev=388103&view=auto
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerDelegate.java (added)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerDelegate.java Thu Mar 23 00:23:45 2006
@@ -0,0 +1,152 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed 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.handler.multiton;
+
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoSession;
+
+/**
+ * An {@link IoHandler} implementation which delegates all requests to
+ * {@link SingleSessionIoHandler}s.  A {@link SingleSessionIoHandlerFactory}
+ * is used to create a new {@link SingleSessionIoHandler} for each newly
+ * created session.
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public class SingleSessionIoHandlerDelegate implements IoHandler
+{
+    /**
+     * The key used to store the {@link SingleSessionIoHandler} as a session
+     * attribute.
+     */
+    public static final String HANDLER =
+        SingleSessionIoHandlerDelegate.class.getName() + ".handler";
+
+    /**
+     * The {@link SingleSessionIoHandlerFactory} used to create new
+     * {@link SingleSessionIoHandler}s.
+     */
+    private final SingleSessionIoHandlerFactory factory;
+
+    /**
+     * Creates a new instance that uses the passed in
+     * SingleSessionIoHandlerFactory to create new {@link SingleSessionIoHandler}s.
+     * 
+     * @param factory  the factory for {@link SingleSessionIoHandler}s
+     */
+    public SingleSessionIoHandlerDelegate( SingleSessionIoHandlerFactory factory )
+    {
+        if (factory == null)
+        {
+            throw new NullPointerException("factory");
+        }
+        this.factory = factory;
+    }
+
+    /**
+     * Creates a new instance with the factory passed to the constructor of
+     * this class.  The created handler is stored as a session
+     * attribute named {@link #HANDLER}.
+     * 
+     * @see org.apache.mina.common.IoHandler#sessionCreated(org.apache.mina.common.IoSession)
+     */
+    public void sessionCreated(IoSession session) throws Exception 
+    {
+        SingleSessionIoHandler handler = factory.getHandler(session);
+        session.setAttribute(HANDLER, handler);
+        handler.sessionCreated();
+    }
+
+    /**
+     * Delegates the method call to the
+     * {@link SingleSessionIoHandler#sessionOpened()} method of the handler
+     * assigned to this session.
+     */
+    public void sessionOpened(IoSession session) throws Exception 
+    {
+        SingleSessionIoHandler handler = (SingleSessionIoHandler) 
+                session.getAttribute(HANDLER);
+        handler.sessionOpened();
+    }
+
+    /**
+     * Delegates the method call to the
+     * {@link SingleSessionIoHandler#sessionClosed()} method of the handler
+     * assigned to this session.
+     */
+    public void sessionClosed(IoSession session) throws Exception 
+    {
+        SingleSessionIoHandler handler = (SingleSessionIoHandler) 
+                session.getAttribute(HANDLER);
+        handler.sessionClosed();
+    }
+
+    /**
+     * Delegates the method call to the
+     * {@link SingleSessionIoHandler#sessionIdle(IdleStatus)} method of the
+     * handler assigned to this session.
+     */
+    public void sessionIdle(IoSession session, IdleStatus status)
+            throws Exception
+    {
+        SingleSessionIoHandler handler = (SingleSessionIoHandler) 
+                session.getAttribute(HANDLER);
+        handler.sessionIdle(status);
+    }
+
+    /**
+     * Delegates the method call to the
+     * {@link SingleSessionIoHandler#exceptionCaught(Throwable)} method of the
+     * handler assigned to this session.
+     */
+    public void exceptionCaught(IoSession session, Throwable cause)
+            throws Exception
+    {
+        SingleSessionIoHandler handler = (SingleSessionIoHandler) 
+                session.getAttribute(HANDLER);
+        handler.exceptionCaught(cause);
+    }
+
+    /**
+     * Delegates the method call to the
+     * {@link SingleSessionIoHandler#messageReceived(Object)} method of the
+     * handler assigned to this session.
+     */
+    public void messageReceived(IoSession session, Object message)
+            throws Exception
+    {
+        SingleSessionIoHandler handler = (SingleSessionIoHandler) 
+                session.getAttribute(HANDLER);
+        handler.messageReceived(message);
+    }
+
+    /**
+     * Delegates the method call to the
+     * {@link SingleSessionIoHandler#messageSent(Object)} method of the handler
+     * assigned to this session.
+     */
+    public void messageSent(IoSession session, Object message) throws Exception
+    {
+        SingleSessionIoHandler handler = (SingleSessionIoHandler) 
+                session.getAttribute(HANDLER);
+        handler.messageSent(message);
+    }
+}

Propchange: directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerDelegate.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerFactory.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerFactory.java?rev=388103&view=auto
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerFactory.java (added)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerFactory.java Thu Mar 23 00:23:45 2006
@@ -0,0 +1,41 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed 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.handler.multiton;
+
+import org.apache.mina.common.IoSession;
+
+/**
+ * A factory that creates {@link SingleSessionIoHandler}
+ * to be used with one particular session.
+ * 
+ * @see SingleSessionIoHandler
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface SingleSessionIoHandlerFactory {
+	
+	/**
+	 * Returns a {@link SingleSessionIoHandler} for the given session.
+	 * 
+	 * @param session the session for which a handler is requested
+	 */
+	SingleSessionIoHandler getHandler(IoSession session);
+	
+}

Propchange: directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/SingleSessionIoHandlerFactory.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Added: directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/package.html
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/package.html?rev=388103&view=auto
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/package.html (added)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/handler/multiton/package.html Thu Mar 23 00:23:45 2006
@@ -0,0 +1,10 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+</head>
+<body>
+Enables creating a handler per session instead of having one handler for many
+sessions, using
+<a href="http://en.wikipedia.org/wiki/Multiton_pattern">Multiton pattern</a>.
+</body>
+</html>
\ No newline at end of file