01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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
32: AbstractStateContextLookup {
33: /**
34: * The name of the {@link IoSession} attribute used to store the
35: * {@link StateContext} object.
36: */
37: public static final String STATE_CONTEXT = IoSessionStateContextLookup.class
38: .getName()
39: + ".stateContext";
40:
41: /**
42: * Creates a new instance using a {@link DefaultStateContextFactory} to
43: * create {@link StateContext} objects for new {@link IoSession}s.
44: */
45: public IoSessionStateContextLookup() {
46: this (new DefaultStateContextFactory());
47: }
48:
49: /**
50: * Creates a new instance using the specified {@link StateContextFactory} to
51: * create {@link StateContext} objects for new {@link IoSession}s.
52: *
53: * @param contextFactory the {@link StateContextFactory}.
54: */
55: public IoSessionStateContextLookup(
56: StateContextFactory contextFactory) {
57: super (contextFactory);
58: }
59:
60: protected StateContext lookup(Object eventArg) {
61: IoSession session = (IoSession) eventArg;
62: return (StateContext) session.getAttribute(STATE_CONTEXT);
63: }
64:
65: protected void store(Object eventArg, StateContext context) {
66: IoSession session = (IoSession) eventArg;
67: session.setAttribute(STATE_CONTEXT, context);
68: }
69:
70: protected boolean supports(Class<?> c) {
71: return IoSession.class.isAssignableFrom(c);
72: }
73: }
|