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 NullPointerException("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
96                  .getAttribute(HANDLER);
97          handler.sessionOpened();
98      }
99  
100     /**
101      * Delegates the method call to the
102      * {@link SingleSessionIoHandler#sessionClosed()} method of the handler
103      * assigned to this session.
104      */
105     public void sessionClosed(IoSession session) throws Exception {
106         SingleSessionIoHandler handler = (SingleSessionIoHandler) session
107                 .getAttribute(HANDLER);
108         handler.sessionClosed();
109     }
110 
111     /**
112      * Delegates the method call to the
113      * {@link SingleSessionIoHandler#sessionIdle(IdleStatus)} method of the
114      * handler assigned to this session.
115      */
116     public void sessionIdle(IoSession session, IdleStatus status)
117             throws Exception {
118         SingleSessionIoHandler handler = (SingleSessionIoHandler) session
119                 .getAttribute(HANDLER);
120         handler.sessionIdle(status);
121     }
122 
123     /**
124      * Delegates the method call to the
125      * {@link SingleSessionIoHandler#exceptionCaught(Throwable)} method of the
126      * handler assigned to this session.
127      */
128     public void exceptionCaught(IoSession session, Throwable cause)
129             throws Exception {
130         SingleSessionIoHandler handler = (SingleSessionIoHandler) session
131                 .getAttribute(HANDLER);
132         handler.exceptionCaught(cause);
133     }
134 
135     /**
136      * Delegates the method call to the
137      * {@link SingleSessionIoHandler#messageReceived(Object)} method of the
138      * handler assigned to this session.
139      */
140     public void messageReceived(IoSession session, Object message)
141             throws Exception {
142         SingleSessionIoHandler handler = (SingleSessionIoHandler) session
143                 .getAttribute(HANDLER);
144         handler.messageReceived(message);
145     }
146 
147     /**
148      * Delegates the method call to the
149      * {@link SingleSessionIoHandler#messageSent(Object)} method of the handler
150      * assigned to this session.
151      */
152     public void messageSent(IoSession session, Object message) throws Exception {
153         SingleSessionIoHandler handler = (SingleSessionIoHandler) session
154                 .getAttribute(HANDLER);
155         handler.messageSent(message);
156     }
157 }