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 */
020package org.apache.mina.handler.multiton;
021
022import org.apache.mina.core.service.IoHandler;
023import org.apache.mina.core.session.AttributeKey;
024import org.apache.mina.core.session.IdleStatus;
025import org.apache.mina.core.session.IoSession;
026
027/**
028 * An {@link IoHandler} implementation which delegates all requests to
029 * {@link SingleSessionIoHandler}s.  A {@link SingleSessionIoHandlerFactory}
030 * is used to create a new {@link SingleSessionIoHandler} for each newly
031 * created session.
032 *
033 * WARNING : This {@link IoHandler} implementation may be easier to understand and
034 * thus to use but the user should be aware that creating one handler by session
035 * will lower scalability if building an high performance server. This should only
036 * be used with very specific needs in mind.
037 * 
038 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
039 */
040@Deprecated
041public class SingleSessionIoHandlerDelegate implements IoHandler {
042    /**
043     * The key used to store the {@link SingleSessionIoHandler} as a session
044     * attribute.
045     */
046    public static final AttributeKey HANDLER = new AttributeKey(SingleSessionIoHandlerDelegate.class, "handler");
047
048    /**
049     * The {@link SingleSessionIoHandlerFactory} used to create new
050     * {@link SingleSessionIoHandler}s.
051     */
052    private final SingleSessionIoHandlerFactory factory;
053
054    /**
055     * Creates a new instance that uses the passed in
056     * {@link SingleSessionIoHandlerFactory} to create new
057     * {@link SingleSessionIoHandler}s.
058     *
059     * @param factory  the factory for {@link SingleSessionIoHandler}s
060     */
061    public SingleSessionIoHandlerDelegate(SingleSessionIoHandlerFactory factory) {
062        if (factory == null) {
063            throw new IllegalArgumentException("factory");
064        }
065        this.factory = factory;
066    }
067
068    /**
069     * @return the {@link SingleSessionIoHandlerFactory} that is used to create a new
070     * {@link SingleSessionIoHandler} instance.
071     */
072    public SingleSessionIoHandlerFactory getFactory() {
073        return factory;
074    }
075
076    /**
077     * Creates a new instance with the factory passed to the constructor of
078     * this class.  The created handler is stored as a session
079     * attribute named {@link #HANDLER}.
080     * 
081     * @see org.apache.mina.core.service.IoHandler#sessionCreated(org.apache.mina.core.session.IoSession)
082     * 
083     * {@inheritDoc}
084     */
085    public void sessionCreated(IoSession session) throws Exception {
086        SingleSessionIoHandler handler = factory.getHandler(session);
087        session.setAttribute(HANDLER, handler);
088        handler.sessionCreated();
089    }
090
091    /**
092     * Delegates the method call to the
093     * {@link SingleSessionIoHandler#sessionOpened()} method of the handler
094     * assigned to this session.
095     * 
096     * {@inheritDoc}
097     */
098    public void sessionOpened(IoSession session) throws Exception {
099        SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
100        handler.sessionOpened();
101    }
102
103    /**
104     * Delegates the method call to the
105     * {@link SingleSessionIoHandler#sessionClosed()} method of the handler
106     * assigned to this session.
107     * 
108     * {@inheritDoc}
109     */
110    public void sessionClosed(IoSession session) throws Exception {
111        SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
112        handler.sessionClosed();
113    }
114
115    /**
116     * Delegates the method call to the
117     * {@link SingleSessionIoHandler#sessionIdle(IdleStatus)} method of the
118     * handler assigned to this session.
119     * 
120     * {@inheritDoc}
121     */
122    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
123        SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
124        handler.sessionIdle(status);
125    }
126
127    /**
128     * Delegates the method call to the
129     * {@link SingleSessionIoHandler#exceptionCaught(Throwable)} method of the
130     * handler assigned to this session.
131     * 
132     * {@inheritDoc}
133     */
134    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
135        SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
136        handler.exceptionCaught(cause);
137    }
138
139    /**
140     * Delegates the method call to the
141     * {@link SingleSessionIoHandler#messageReceived(Object)} method of the
142     * handler assigned to this session.
143     * 
144     * {@inheritDoc}
145     */
146    public void messageReceived(IoSession session, Object message) throws Exception {
147        SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
148        handler.messageReceived(message);
149    }
150
151    /**
152     * Delegates the method call to the
153     * {@link SingleSessionIoHandler#messageSent(Object)} method of the handler
154     * assigned to this session.
155     * 
156     * {@inheritDoc}
157     */
158    public void messageSent(IoSession session, Object message) throws Exception {
159        SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
160        handler.messageSent(message);
161    }
162
163    /**
164     * {@inheritDoc}
165     */
166    public void inputClosed(IoSession session) throws Exception {
167        SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
168        handler.inputClosed(session);
169    }
170}