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.core.session.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 */
30 public class IoSessionStateContextLookup extends AbstractStateContextLookup {
31 /**
32 * The default name of the {@link IoSession} attribute used to store the
33 * {@link StateContext} object.
34 */
35 public static final String DEFAULT_SESSION_ATTRIBUTE_NAME =
36 IoSessionStateContextLookup.class.getName() + ".stateContext";
37
38 private final String sessionAttributeName;
39
40 /**
41 * Creates a new instance using a {@link DefaultStateContextFactory} to
42 * create {@link StateContext} objects for new {@link IoSession}s.
43 */
44 public IoSessionStateContextLookup() {
45 this(new DefaultStateContextFactory(), DEFAULT_SESSION_ATTRIBUTE_NAME);
46 }
47
48 /**
49 * Creates a new instance using a {@link DefaultStateContextFactory} to
50 * create {@link StateContext} objects for new {@link IoSession}s.
51 *
52 * @param sessionAttributeName the name of the {@link IoSession} attribute
53 * used to store the {@link StateContext} object.
54 */
55 public IoSessionStateContextLookup(String sessionAttributeName) {
56 this(new DefaultStateContextFactory(), sessionAttributeName);
57 }
58
59 /**
60 * Creates a new instance using the specified {@link StateContextFactory} to
61 * create {@link StateContext} objects for new {@link IoSession}s.
62 *
63 * @param contextFactory the {@link StateContextFactory}.
64 */
65 public IoSessionStateContextLookup(StateContextFactory contextFactory) {
66 this(contextFactory, DEFAULT_SESSION_ATTRIBUTE_NAME);
67 }
68
69 /**
70 * Creates a new instance using the specified {@link StateContextFactory} to
71 * create {@link StateContext} objects for new {@link IoSession}s.
72 *
73 * @param contextFactory the {@link StateContextFactory}.
74 * @param sessionAttributeName the name of the {@link IoSession} attribute
75 * used to store the {@link StateContext} object.
76 */
77 public IoSessionStateContextLookup(StateContextFactory contextFactory, String sessionAttributeName) {
78 super(contextFactory);
79 this.sessionAttributeName = sessionAttributeName;
80 }
81
82 protected StateContext lookup(Object eventArg) {
83 IoSession session = (IoSession) eventArg;
84 return (StateContext) session.getAttribute(sessionAttributeName);
85 }
86
87 protected void store(Object eventArg, StateContext context) {
88 IoSession session = (IoSession) eventArg;
89 session.setAttribute(sessionAttributeName, context);
90 }
91
92 protected boolean supports(Class<?> c) {
93 return IoSession.class.isAssignableFrom(c);
94 }
95 }