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   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
39   */
40  @Deprecated
41  public class SingleSessionIoHandlerDelegate implements IoHandler {
42      /**
43       * The key used to store the {@link SingleSessionIoHandler} as a session
44       * attribute.
45       */
46      public static final AttributeKey HANDLER = new AttributeKey(SingleSessionIoHandlerDelegate.class, "handler");
47  
48      /**
49       * The {@link SingleSessionIoHandlerFactory} used to create new
50       * {@link SingleSessionIoHandler}s.
51       */
52      private final SingleSessionIoHandlerFactory factory;
53  
54      /**
55       * Creates a new instance that uses the passed in
56       * {@link SingleSessionIoHandlerFactory} to create new
57       * {@link SingleSessionIoHandler}s.
58       *
59       * @param factory  the factory for {@link SingleSessionIoHandler}s
60       */
61      public SingleSessionIoHandlerDelegate(SingleSessionIoHandlerFactory factory) {
62          if (factory == null) {
63              throw new IllegalArgumentException("factory");
64          }
65          this.factory = factory;
66      }
67  
68      /**
69       * @return the {@link SingleSessionIoHandlerFactory} that is used to create a new
70       * {@link SingleSessionIoHandler} instance.
71       */
72      public SingleSessionIoHandlerFactory getFactory() {
73          return factory;
74      }
75  
76      /**
77       * Creates a new instance with the factory passed to the constructor of
78       * this class.  The created handler is stored as a session
79       * attribute named {@link #HANDLER}.
80       * 
81       * @see org.apache.mina.core.service.IoHandler#sessionCreated(org.apache.mina.core.session.IoSession)
82       * 
83       * {@inheritDoc}
84       */
85      public void sessionCreated(IoSession session) throws Exception {
86          SingleSessionIoHandler handler = factory.getHandler(session);
87          session.setAttribute(HANDLER, handler);
88          handler.sessionCreated();
89      }
90  
91      /**
92       * Delegates the method call to the
93       * {@link SingleSessionIoHandler#sessionOpened()} method of the handler
94       * assigned to this session.
95       * 
96       * {@inheritDoc}
97       */
98      public void sessionOpened(IoSession session) throws Exception {
99          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 }