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 */ 020package org.apache.mina.statemachine.context; 021 022import org.apache.mina.core.session.IoSession; 023 024/** 025 * MINA specific {@link StateContextLookup} which uses an {@link IoSession} 026 * attribute to store the {@link StateContextLookup}. 027 * 028 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 029 */ 030public class IoSessionStateContextLookup extends AbstractStateContextLookup { 031 /** 032 * The default name of the {@link IoSession} attribute used to store the 033 * {@link StateContext} object. 034 */ 035 public static final String DEFAULT_SESSION_ATTRIBUTE_NAME = IoSessionStateContextLookup.class.getName() 036 + ".stateContext"; 037 038 private final String sessionAttributeName; 039 040 /** 041 * Creates a new instance using a {@link DefaultStateContextFactory} to 042 * create {@link StateContext} objects for new {@link IoSession}s. 043 */ 044 public IoSessionStateContextLookup() { 045 this(new DefaultStateContextFactory(), DEFAULT_SESSION_ATTRIBUTE_NAME); 046 } 047 048 /** 049 * Creates a new instance using a {@link DefaultStateContextFactory} to 050 * create {@link StateContext} objects for new {@link IoSession}s. 051 * 052 * @param sessionAttributeName the name of the {@link IoSession} attribute 053 * used to store the {@link StateContext} object. 054 */ 055 public IoSessionStateContextLookup(String sessionAttributeName) { 056 this(new DefaultStateContextFactory(), sessionAttributeName); 057 } 058 059 /** 060 * Creates a new instance using the specified {@link StateContextFactory} to 061 * create {@link StateContext} objects for new {@link IoSession}s. 062 * 063 * @param contextFactory the {@link StateContextFactory}. 064 */ 065 public IoSessionStateContextLookup(StateContextFactory contextFactory) { 066 this(contextFactory, DEFAULT_SESSION_ATTRIBUTE_NAME); 067 } 068 069 /** 070 * Creates a new instance using the specified {@link StateContextFactory} to 071 * create {@link StateContext} objects for new {@link IoSession}s. 072 * 073 * @param contextFactory the {@link StateContextFactory}. 074 * @param sessionAttributeName the name of the {@link IoSession} attribute 075 * used to store the {@link StateContext} object. 076 */ 077 public IoSessionStateContextLookup(StateContextFactory contextFactory, String sessionAttributeName) { 078 super(contextFactory); 079 this.sessionAttributeName = sessionAttributeName; 080 } 081 082 protected StateContext lookup(Object eventArg) { 083 IoSession session = (IoSession) eventArg; 084 return (StateContext) session.getAttribute(sessionAttributeName); 085 } 086 087 protected void store(Object eventArg, StateContext context) { 088 IoSession session = (IoSession) eventArg; 089 session.setAttribute(sessionAttributeName, context); 090 } 091 092 protected boolean supports(Class<?> c) { 093 return IoSession.class.isAssignableFrom(c); 094 } 095}