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.common.IdleStatus; 23 import org.apache.mina.common.IoHandler; 24 import org.apache.mina.common.IoSession; 25 26 /** 27 * An {@link IoHandler} implementation which delegates all requests to 28 * {@link SingleSessionIoHandler}s. A {@link SingleSessionIoHandlerFactory} 29 * is used to create a new {@link SingleSessionIoHandler} for each newly 30 * created session. 31 * 32 * @author The Apache Directory Project (mina-dev@directory.apache.org) 33 * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $ 34 */ 35 public class SingleSessionIoHandlerDelegate implements IoHandler { 36 /** 37 * The key used to store the {@link SingleSessionIoHandler} as a session 38 * attribute. 39 */ 40 public static final String HANDLER = SingleSessionIoHandlerDelegate.class 41 .getName() 42 + ".handler"; 43 44 /** 45 * The {@link SingleSessionIoHandlerFactory} used to create new 46 * {@link SingleSessionIoHandler}s. 47 */ 48 private final SingleSessionIoHandlerFactory factory; 49 50 /** 51 * Creates a new instance that uses the passed in 52 * {@link SingleSessionIoHandlerFactory} to create new 53 * {@link SingleSessionIoHandler}s. 54 * 55 * @param factory the factory for {@link SingleSessionIoHandler}s 56 */ 57 public SingleSessionIoHandlerDelegate(SingleSessionIoHandlerFactory factory) { 58 if (factory == null) { 59 throw new NullPointerException("factory"); 60 } 61 this.factory = factory; 62 } 63 64 /** 65 * Creates a new instance with the factory passed to the constructor of 66 * this class. The created handler is stored as a session 67 * attribute named {@link #HANDLER}. 68 * 69 * @see org.apache.mina.common.IoHandler#sessionCreated(org.apache.mina.common.IoSession) 70 */ 71 public void sessionCreated(IoSession session) throws Exception { 72 SingleSessionIoHandler handler = factory.getHandler(session); 73 session.setAttribute(HANDLER, handler); 74 handler.sessionCreated(); 75 } 76 77 /** 78 * Delegates the method call to the 79 * {@link SingleSessionIoHandler#sessionOpened()} method of the handler 80 * assigned to this session. 81 */ 82 public void sessionOpened(IoSession session) throws Exception { 83 SingleSessionIoHandler handler = (SingleSessionIoHandler) session 84 .getAttribute(HANDLER); 85 handler.sessionOpened(); 86 } 87 88 /** 89 * Delegates the method call to the 90 * {@link SingleSessionIoHandler#sessionClosed()} method of the handler 91 * assigned to this session. 92 */ 93 public void sessionClosed(IoSession session) throws Exception { 94 SingleSessionIoHandler handler = (SingleSessionIoHandler) session 95 .getAttribute(HANDLER); 96 handler.sessionClosed(); 97 } 98 99 /** 100 * Delegates the method call to the 101 * {@link SingleSessionIoHandler#sessionIdle(IdleStatus)} method of the 102 * handler assigned to this session. 103 */ 104 public void sessionIdle(IoSession session, IdleStatus status) 105 throws Exception { 106 SingleSessionIoHandler handler = (SingleSessionIoHandler) session 107 .getAttribute(HANDLER); 108 handler.sessionIdle(status); 109 } 110 111 /** 112 * Delegates the method call to the 113 * {@link SingleSessionIoHandler#exceptionCaught(Throwable)} method of the 114 * handler assigned to this session. 115 */ 116 public void exceptionCaught(IoSession session, Throwable cause) 117 throws Exception { 118 SingleSessionIoHandler handler = (SingleSessionIoHandler) session 119 .getAttribute(HANDLER); 120 handler.exceptionCaught(cause); 121 } 122 123 /** 124 * Delegates the method call to the 125 * {@link SingleSessionIoHandler#messageReceived(Object)} method of the 126 * handler assigned to this session. 127 */ 128 public void messageReceived(IoSession session, Object message) 129 throws Exception { 130 SingleSessionIoHandler handler = (SingleSessionIoHandler) session 131 .getAttribute(HANDLER); 132 handler.messageReceived(message); 133 } 134 135 /** 136 * Delegates the method call to the 137 * {@link SingleSessionIoHandler#messageSent(Object)} method of the handler 138 * assigned to this session. 139 */ 140 public void messageSent(IoSession session, Object message) throws Exception { 141 SingleSessionIoHandler handler = (SingleSessionIoHandler) session 142 .getAttribute(HANDLER); 143 handler.messageSent(message); 144 } 145 }