001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.handler.multiton;
021:
022: import org.apache.mina.common.AttributeKey;
023: import org.apache.mina.common.IdleStatus;
024: import org.apache.mina.common.IoHandler;
025: import org.apache.mina.common.IoSession;
026:
027: /**
028: * An {@link IoHandler} implementation which delegates all requests to
029: * {@link SingleSessionIoHandler}s. A {@link SingleSessionIoHandlerFactory}
030: * is used to create a new {@link SingleSessionIoHandler} for each newly
031: * created session.
032: *
033: * @author The Apache MINA Project (dev@mina.apache.org)
034: * @version $Rev: 580762 $, $Date: 2007-09-30 12:33:00 -0600 (Sun, 30 Sep 2007) $
035: */
036: public class SingleSessionIoHandlerDelegate implements IoHandler {
037: /**
038: * The key used to store the {@link SingleSessionIoHandler} as a session
039: * attribute.
040: */
041: public static final AttributeKey HANDLER = new AttributeKey(
042: SingleSessionIoHandlerDelegate.class, "handler");
043:
044: /**
045: * The {@link SingleSessionIoHandlerFactory} used to create new
046: * {@link SingleSessionIoHandler}s.
047: */
048: private final SingleSessionIoHandlerFactory factory;
049:
050: /**
051: * Creates a new instance that uses the passed in
052: * {@link SingleSessionIoHandlerFactory} to create new
053: * {@link SingleSessionIoHandler}s.
054: *
055: * @param factory the factory for {@link SingleSessionIoHandler}s
056: */
057: public SingleSessionIoHandlerDelegate(
058: SingleSessionIoHandlerFactory factory) {
059: if (factory == null) {
060: throw new NullPointerException("factory");
061: }
062: this .factory = factory;
063: }
064:
065: /**
066: * Returns the {@link SingleSessionIoHandlerFactory} that is used to create a new
067: * {@link SingleSessionIoHandler} instance.
068: */
069: public SingleSessionIoHandlerFactory getFactory() {
070: return factory;
071: }
072:
073: /**
074: * Creates a new instance with the factory passed to the constructor of
075: * this class. The created handler is stored as a session
076: * attribute named {@link #HANDLER}.
077: *
078: * @see org.apache.mina.common.IoHandler#sessionCreated(org.apache.mina.common.IoSession)
079: */
080: public void sessionCreated(IoSession session) throws Exception {
081: SingleSessionIoHandler handler = factory.getHandler(session);
082: session.setAttribute(HANDLER, handler);
083: handler.sessionCreated();
084: }
085:
086: /**
087: * Delegates the method call to the
088: * {@link SingleSessionIoHandler#sessionOpened()} method of the handler
089: * assigned to this session.
090: */
091: public void sessionOpened(IoSession session) throws Exception {
092: SingleSessionIoHandler handler = (SingleSessionIoHandler) session
093: .getAttribute(HANDLER);
094: handler.sessionOpened();
095: }
096:
097: /**
098: * Delegates the method call to the
099: * {@link SingleSessionIoHandler#sessionClosed()} method of the handler
100: * assigned to this session.
101: */
102: public void sessionClosed(IoSession session) throws Exception {
103: SingleSessionIoHandler handler = (SingleSessionIoHandler) session
104: .getAttribute(HANDLER);
105: handler.sessionClosed();
106: }
107:
108: /**
109: * Delegates the method call to the
110: * {@link SingleSessionIoHandler#sessionIdle(IdleStatus)} method of the
111: * handler assigned to this session.
112: */
113: public void sessionIdle(IoSession session, IdleStatus status)
114: throws Exception {
115: SingleSessionIoHandler handler = (SingleSessionIoHandler) session
116: .getAttribute(HANDLER);
117: handler.sessionIdle(status);
118: }
119:
120: /**
121: * Delegates the method call to the
122: * {@link SingleSessionIoHandler#exceptionCaught(Throwable)} method of the
123: * handler assigned to this session.
124: */
125: public void exceptionCaught(IoSession session, Throwable cause)
126: throws Exception {
127: SingleSessionIoHandler handler = (SingleSessionIoHandler) session
128: .getAttribute(HANDLER);
129: handler.exceptionCaught(cause);
130: }
131:
132: /**
133: * Delegates the method call to the
134: * {@link SingleSessionIoHandler#messageReceived(Object)} method of the
135: * handler assigned to this session.
136: */
137: public void messageReceived(IoSession session, Object message)
138: throws Exception {
139: SingleSessionIoHandler handler = (SingleSessionIoHandler) session
140: .getAttribute(HANDLER);
141: handler.messageReceived(message);
142: }
143:
144: /**
145: * Delegates the method call to the
146: * {@link SingleSessionIoHandler#messageSent(Object)} method of the handler
147: * assigned to this session.
148: */
149: public void messageSent(IoSession session, Object message)
150: throws Exception {
151: SingleSessionIoHandler handler = (SingleSessionIoHandler) session
152: .getAttribute(HANDLER);
153: handler.messageSent(message);
154: }
155: }
|