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.statemachine.context;
21
22 import org.apache.mina.common.IoSession;
23
24 /**
25 * MINA specific {@link StateContextLookup} which uses an {@link IoSession}
26 * attribute to store the {@link StateContextLookup}.
27 *
28 * @author The Apache MINA Project (dev@mina.apache.org)
29 * @version $Rev: 589477 $, $Date: 2007-10-28 21:19:58 -0600 (Sun, 28 Oct 2007) $
30 */
31 public class IoSessionStateContextLookup extends AbstractStateContextLookup {
32 /**
33 * The name of the {@link IoSession} attribute used to store the
34 * {@link StateContext} object.
35 */
36 public static final String STATE_CONTEXT =
37 IoSessionStateContextLookup.class.getName() + ".stateContext";
38
39 /**
40 * Creates a new instance using a {@link DefaultStateContextFactory} to
41 * create {@link StateContext} objects for new {@link IoSession}s.
42 */
43 public IoSessionStateContextLookup() {
44 this(new DefaultStateContextFactory());
45 }
46
47 /**
48 * Creates a new instance using the specified {@link StateContextFactory} to
49 * create {@link StateContext} objects for new {@link IoSession}s.
50 *
51 * @param contextFactory the {@link StateContextFactory}.
52 */
53 public IoSessionStateContextLookup(StateContextFactory contextFactory) {
54 super(contextFactory);
55 }
56
57 protected StateContext lookup(Object eventArg) {
58 IoSession session = (IoSession) eventArg;
59 return (StateContext) session.getAttribute(STATE_CONTEXT);
60 }
61
62 protected void store(Object eventArg, StateContext context) {
63 IoSession session = (IoSession) eventArg;
64 session.setAttribute(STATE_CONTEXT, context);
65 }
66
67 protected boolean supports(Class<?> c) {
68 return IoSession.class.isAssignableFrom(c);
69 }
70 }