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