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   * @author The Apache MINA Project (dev@mina.apache.org)
34   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
35   */
36  public class SingleSessionIoHandlerDelegate implements IoHandler {
37      /**
38       * The key used to store the {@link SingleSessionIoHandler} as a session
39       * attribute.
40       */
41      public static final AttributeKey HANDLER = new AttributeKey(SingleSessionIoHandlerDelegate.class, "handler");
42  
43      /**
44       * The {@link SingleSessionIoHandlerFactory} used to create new
45       * {@link SingleSessionIoHandler}s.
46       */
47      private final SingleSessionIoHandlerFactory factory;
48  
49      /**
50       * Creates a new instance that uses the passed in
51       * {@link SingleSessionIoHandlerFactory} to create new
52       * {@link SingleSessionIoHandler}s.
53       *
54       * @param factory  the factory for {@link SingleSessionIoHandler}s
55       */
56      public SingleSessionIoHandlerDelegate(SingleSessionIoHandlerFactory factory) {
57          if (factory == null) {
58              throw new NullPointerException("factory");
59          }
60          this.factory = factory;
61      }
62  
63      /**
64       * Returns the {@link SingleSessionIoHandlerFactory} that is used to create a new
65       * {@link SingleSessionIoHandler} instance.
66       */
67      public SingleSessionIoHandlerFactory getFactory() {
68          return factory;
69      }
70  
71      /**
72       * Creates a new instance with the factory passed to the constructor of
73       * this class.  The created handler is stored as a session
74       * attribute named {@link #HANDLER}.
75       *
76       * @see org.apache.mina.core.service.IoHandler#sessionCreated(org.apache.mina.core.session.IoSession)
77       */
78      public void sessionCreated(IoSession session) throws Exception {
79          SingleSessionIoHandler handler = factory.getHandler(session);
80          session.setAttribute(HANDLER, handler);
81          handler.sessionCreated();
82      }
83  
84      /**
85       * Delegates the method call to the
86       * {@link SingleSessionIoHandler#sessionOpened()} method of the handler
87       * assigned to this session.
88       */
89      public void sessionOpened(IoSession session) throws Exception {
90          SingleSessionIoHandler handler = (SingleSessionIoHandler) session
91                  .getAttribute(HANDLER);
92          handler.sessionOpened();
93      }
94  
95      /**
96       * Delegates the method call to the
97       * {@link SingleSessionIoHandler#sessionClosed()} method of the handler
98       * assigned to this session.
99       */
100     public void sessionClosed(IoSession session) throws Exception {
101         SingleSessionIoHandler handler = (SingleSessionIoHandler) session
102                 .getAttribute(HANDLER);
103         handler.sessionClosed();
104     }
105 
106     /**
107      * Delegates the method call to the
108      * {@link SingleSessionIoHandler#sessionIdle(IdleStatus)} method of the
109      * handler assigned to this session.
110      */
111     public void sessionIdle(IoSession session, IdleStatus status)
112             throws Exception {
113         SingleSessionIoHandler handler = (SingleSessionIoHandler) session
114                 .getAttribute(HANDLER);
115         handler.sessionIdle(status);
116     }
117 
118     /**
119      * Delegates the method call to the
120      * {@link SingleSessionIoHandler#exceptionCaught(Throwable)} method of the
121      * handler assigned to this session.
122      */
123     public void exceptionCaught(IoSession session, Throwable cause)
124             throws Exception {
125         SingleSessionIoHandler handler = (SingleSessionIoHandler) session
126                 .getAttribute(HANDLER);
127         handler.exceptionCaught(cause);
128     }
129 
130     /**
131      * Delegates the method call to the
132      * {@link SingleSessionIoHandler#messageReceived(Object)} method of the
133      * handler assigned to this session.
134      */
135     public void messageReceived(IoSession session, Object message)
136             throws Exception {
137         SingleSessionIoHandler handler = (SingleSessionIoHandler) session
138                 .getAttribute(HANDLER);
139         handler.messageReceived(message);
140     }
141 
142     /**
143      * Delegates the method call to the
144      * {@link SingleSessionIoHandler#messageSent(Object)} method of the handler
145      * assigned to this session.
146      */
147     public void messageSent(IoSession session, Object message) throws Exception {
148         SingleSessionIoHandler handler = (SingleSessionIoHandler) session
149                 .getAttribute(HANDLER);
150         handler.messageSent(message);
151     }
152 }