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       * Returns 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      public void sessionCreated(IoSession session) throws Exception {
84          SingleSessionIoHandler handler = factory.getHandler(session);
85          session.setAttribute(HANDLER, handler);
86          handler.sessionCreated();
87      }
88  
89      /**
90       * Delegates the method call to the
91       * {@link SingleSessionIoHandler#sessionOpened()} method of the handler
92       * assigned to this session.
93       */
94      public void sessionOpened(IoSession session) throws Exception {
95          SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
96          handler.sessionOpened();
97      }
98  
99      /**
100      * Delegates the method call to the
101      * {@link SingleSessionIoHandler#sessionClosed()} method of the handler
102      * assigned to this session.
103      */
104     public void sessionClosed(IoSession session) throws Exception {
105         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
106         handler.sessionClosed();
107     }
108 
109     /**
110      * Delegates the method call to the
111      * {@link SingleSessionIoHandler#sessionIdle(IdleStatus)} method of the
112      * handler assigned to this session.
113      */
114     public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
115         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
116         handler.sessionIdle(status);
117     }
118 
119     /**
120      * Delegates the method call to the
121      * {@link SingleSessionIoHandler#exceptionCaught(Throwable)} method of the
122      * handler assigned to this session.
123      */
124     public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
125         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
126         handler.exceptionCaught(cause);
127     }
128 
129     /**
130      * Delegates the method call to the
131      * {@link SingleSessionIoHandler#messageReceived(Object)} method of the
132      * handler assigned to this session.
133      */
134     public void messageReceived(IoSession session, Object message) throws Exception {
135         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
136         handler.messageReceived(message);
137     }
138 
139     /**
140      * Delegates the method call to the
141      * {@link SingleSessionIoHandler#messageSent(Object)} method of the handler
142      * assigned to this session.
143      */
144     public void messageSent(IoSession session, Object message) throws Exception {
145         SingleSessionIoHandler handler = (SingleSessionIoHandler) session.getAttribute(HANDLER);
146         handler.messageSent(message);
147     }
148 }