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  
27  /**
28   * An {@link IoHandler} implementation which delegates all requests to
29   * {@link SingleSessionIoHandler}s.  A {@link SingleSessionIoHandlerFactory}
30   * is used to create a new {@link SingleSessionIoHandler} for each newly
31   * created session.
32   *
33   * WARNING : This {@link IoHandler} implementation may be easier to understand and
34   * thus to use but the user should be aware that creating one handler by session
35   * will lower scalability if building an high performance server. This should only
36   * be used with very specific needs in mind.
37   * 
38   * @deprecated This class is deprecated
39   * 
40   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
41   */
42  @Deprecated
43  public class SingleSessionIoHandlerDelegate implements IoHandler {
44      /**
45       * The key used to store the {@link SingleSessionIoHandler} as a session
46       * attribute.
47       */
48      public static final AttributeKey HANDLER = new AttributeKey(SingleSessionIoHandlerDelegate.class, "handler");
49  
50      /**
51       * The {@link SingleSessionIoHandlerFactory} used to create new
52       * {@link SingleSessionIoHandler}s.
53       */
54      private final SingleSessionIoHandlerFactory factory;
55  
56      /**
57       * Creates a new instance that uses the passed in
58       * {@link SingleSessionIoHandlerFactory} to create new
59       * {@link SingleSessionIoHandler}s.
60       *
61       * @param factory  the factory for {@link SingleSessionIoHandler}s
62       */
63      public SingleSessionIoHandlerDelegate(SingleSessionIoHandlerFactory factory) {
64          if (factory == null) {
65              throw new IllegalArgumentException("factory");
66          }
67          this.factory = factory;
68      }
69  
70      /**
71       * @return the {@link SingleSessionIoHandlerFactory} that is used to create a new
72       * {@link SingleSessionIoHandler} instance.
73       */
74      public SingleSessionIoHandlerFactory getFactory() {
75          return factory;
76      }
77  
78      /**
79       * Creates a new instance with the factory passed to the constructor of
80       * this class.  The created handler is stored as a session
81       * attribute named {@link #HANDLER}.
82       * 
83       * @see org.apache.mina.core.service.IoHandler#sessionCreated(org.apache.mina.core.session.IoSession)
84       */
85      @Override
86      public void sessionCreated(IoSession session) throws Exception {
87          SingleSessionIoHandler handler = factory.getHandler(session);
88          session.setAttribute(HANDLER, handler);
89          handler.sessionCreated();
90      }
91  
92      /**
93       * Delegates the method call to the
94       * {@link SingleSessionIoHandler#sessionOpened()} method of the handler
95       * assigned to this session.
96       * 
97       * {@inheritDoc}
98       */
99      @Override
100     public void sessionOpened(IoSession session) throws Exception {
101         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
102         handler.sessionOpened();
103     }
104 
105     /**
106      * Delegates the method call to the
107      * {@link SingleSessionIoHandler#sessionClosed()} method of the handler
108      * assigned to this session.
109      * 
110      * {@inheritDoc}
111      */
112     @Override
113     public void sessionClosed(IoSession session) throws Exception {
114         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
115         handler.sessionClosed();
116     }
117 
118     /**
119      * Delegates the method call to the
120      * {@link SingleSessionIoHandler#sessionIdle(IdleStatus)} method of the
121      * handler assigned to this session.
122      * 
123      * {@inheritDoc}
124      */
125     @Override
126     public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
127         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
128         handler.sessionIdle(status);
129     }
130 
131     /**
132      * Delegates the method call to the
133      * {@link SingleSessionIoHandler#exceptionCaught(Throwable)} method of the
134      * handler assigned to this session.
135      * 
136      * {@inheritDoc}
137      */
138     @Override
139     public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
140         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
141         handler.exceptionCaught(cause);
142     }
143 
144     /**
145      * Delegates the method call to the
146      * {@link SingleSessionIoHandler#messageReceived(Object)} method of the
147      * handler assigned to this session.
148      * 
149      * {@inheritDoc}
150      */
151     @Override
152     public void messageReceived(IoSession session, Object message) throws Exception {
153         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
154         handler.messageReceived(message);
155     }
156 
157     /**
158      * Delegates the method call to the
159      * {@link SingleSessionIoHandler#messageSent(Object)} method of the handler
160      * assigned to this session.
161      * 
162      * {@inheritDoc}
163      */
164     @Override
165     public void messageSent(IoSession session, Object message) throws Exception {
166         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
167         handler.messageSent(message);
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public void inputClosed(IoSession session) throws Exception {
175         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
176         handler.inputClosed(session);
177     }
178 }