View Javadoc

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