View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.handler.multiton;
21  
22  import org.apache.mina.core.service.IoHandler;
23  import org.apache.mina.core.session.AttributeKey;
24  import org.apache.mina.core.session.IdleStatus;
25  import org.apache.mina.core.session.IoSession;
26  import org.apache.mina.filter.FilterEvent;
27  
28  /**
29   * An {@link IoHandler} implementation which delegates all requests to
30   * {@link SingleSessionIoHandler}s.  A {@link SingleSessionIoHandlerFactory}
31   * is used to create a new {@link SingleSessionIoHandler} for each newly
32   * created session.
33   *
34   * WARNING : This {@link IoHandler} implementation may be easier to understand and
35   * thus to use but the user should be aware that creating one handler by session
36   * will lower scalability if building an high performance server. This should only
37   * be used with very specific needs in mind.
38   * 
39   * @deprecated This class is deprecated
40   * 
41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
42   */
43  @Deprecated
44  public class SingleSessionIoHandlerDelegate implements IoHandler {
45      /**
46       * The key used to store the {@link SingleSessionIoHandler} as a session
47       * attribute.
48       */
49      public static final AttributeKeyributeKey.html#AttributeKey">AttributeKey HANDLER = new AttributeKey(SingleSessionIoHandlerDelegate.class, "handler");
50  
51      /**
52       * The {@link SingleSessionIoHandlerFactory} used to create new
53       * {@link SingleSessionIoHandler}s.
54       */
55      private final SingleSessionIoHandlerFactory factory;
56  
57      /**
58       * Creates a new instance that uses the passed in
59       * {@link SingleSessionIoHandlerFactory} to create new
60       * {@link SingleSessionIoHandler}s.
61       *
62       * @param factory  the factory for {@link SingleSessionIoHandler}s
63       */
64      public SingleSessionIoHandlerDelegate(SingleSessionIoHandlerFactory factory) {
65          if (factory == null) {
66              throw new IllegalArgumentException("factory");
67          }
68          this.factory = factory;
69      }
70  
71      /**
72       * @return the {@link SingleSessionIoHandlerFactory} that is used to create a new
73       * {@link SingleSessionIoHandler} instance.
74       */
75      public SingleSessionIoHandlerFactory getFactory() {
76          return factory;
77      }
78  
79      /**
80       * Creates a new instance with the factory passed to the constructor of
81       * this class.  The created handler is stored as a session
82       * attribute named {@link #HANDLER}.
83       * 
84       * @see org.apache.mina.core.service.IoHandler#sessionCreated(org.apache.mina.core.session.IoSession)
85       */
86      @Override
87      public void sessionCreated(IoSession session) throws Exception {
88          SingleSessionIoHandler handler = factory.getHandler(session);
89          session.setAttribute(HANDLER, handler);
90          handler.sessionCreated();
91      }
92  
93      /**
94       * Delegates the method call to the
95       * {@link SingleSessionIoHandler#sessionOpened()} method of the handler
96       * assigned to this session.
97       * 
98       * {@inheritDoc}
99       */
100     @Override
101     public void sessionOpened(IoSession session) throws Exception {
102         SingleSessionIoHandlerg/apache/mina/handler/multiton/SingleSessionIoHandler.html#SingleSessionIoHandler">SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
103         handler.sessionOpened();
104     }
105 
106     /**
107      * Delegates the method call to the
108      * {@link SingleSessionIoHandler#sessionClosed()} method of the handler
109      * assigned to this session.
110      * 
111      * {@inheritDoc}
112      */
113     @Override
114     public void sessionClosed(IoSession session) throws Exception {
115         SingleSessionIoHandlerg/apache/mina/handler/multiton/SingleSessionIoHandler.html#SingleSessionIoHandler">SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
116         handler.sessionClosed();
117     }
118 
119     /**
120      * Delegates the method call to the
121      * {@link SingleSessionIoHandler#sessionIdle(IdleStatus)} method of the
122      * handler assigned to this session.
123      * 
124      * {@inheritDoc}
125      */
126     @Override
127     public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
128         SingleSessionIoHandlerg/apache/mina/handler/multiton/SingleSessionIoHandler.html#SingleSessionIoHandler">SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
129         handler.sessionIdle(status);
130     }
131 
132     /**
133      * Delegates the method call to the
134      * {@link SingleSessionIoHandler#exceptionCaught(Throwable)} method of the
135      * handler assigned to this session.
136      * 
137      * {@inheritDoc}
138      */
139     @Override
140     public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
141         SingleSessionIoHandlerg/apache/mina/handler/multiton/SingleSessionIoHandler.html#SingleSessionIoHandler">SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
142         handler.exceptionCaught(cause);
143     }
144 
145     /**
146      * Delegates the method call to the
147      * {@link SingleSessionIoHandler#messageReceived(Object)} method of the
148      * handler assigned to this session.
149      * 
150      * {@inheritDoc}
151      */
152     @Override
153     public void messageReceived(IoSession session, Object message) throws Exception {
154         SingleSessionIoHandlerg/apache/mina/handler/multiton/SingleSessionIoHandler.html#SingleSessionIoHandler">SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
155         handler.messageReceived(message);
156     }
157 
158     /**
159      * Delegates the method call to the
160      * {@link SingleSessionIoHandler#messageSent(Object)} method of the handler
161      * assigned to this session.
162      * 
163      * {@inheritDoc}
164      */
165     @Override
166     public void messageSent(IoSession session, Object message) throws Exception {
167         SingleSessionIoHandlerg/apache/mina/handler/multiton/SingleSessionIoHandler.html#SingleSessionIoHandler">SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
168         handler.messageSent(message);
169     }
170 
171     /**
172      * {@inheritDoc}
173      */
174     @Override
175     public void inputClosed(IoSession session) throws Exception {
176         SingleSessionIoHandlerg/apache/mina/handler/multiton/SingleSessionIoHandler.html#SingleSessionIoHandler">SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
177         handler.inputClosed(session);
178     }
179 
180     /**
181      * Delegates the method call to the
182      * {@link SingleSessionIoHandler#event(FilterEvent)} method of the handler
183      * assigned to this session.
184      * 
185      * {@inheritDoc}
186      */
187     @Override
188     public void event(IoSession session, FilterEvent event) throws Exception {
189         SingleSessionIoHandlerg/apache/mina/handler/multiton/SingleSessionIoHandler.html#SingleSessionIoHandler">SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
190         handler.event(event);
191     }
192 }