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.filter.util;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.concurrent.ConcurrentHashMap;
26  
27  import org.apache.mina.core.filterchain.IoFilter;
28  import org.apache.mina.core.filterchain.IoFilterAdapter;
29  import org.apache.mina.core.session.IoSession;
30  
31  /**
32   * An {@link IoFilter} that sets initial attributes when a new
33   * {@link IoSession} is created.  By default, the attribute map is empty when
34   * an {@link IoSession} is newly created.  Inserting this filter will make
35   * the pre-configured attributes available after this filter executes the
36   * <tt>sessionCreated</tt> event.
37   *
38   * @author The Apache MINA Project (dev@mina.apache.org)
39   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
40   */
41  public class SessionAttributeInitializingFilter extends IoFilterAdapter {
42      private final Map<String, Object> attributes = new ConcurrentHashMap<String, Object>();
43  
44      /**
45       * Creates a new instance with no default attributes.  You can set
46       * the additional attributes by calling methods such as
47       * {@link #setAttribute(String, Object)} and {@link #setAttributes(Map)}.
48       */
49      public SessionAttributeInitializingFilter() {
50      }
51  
52      /**
53       * Creates a new instance with the specified default attributes.  You can
54       * set the additional attributes by calling methods such as
55       * {@link #setAttribute(String, Object)} and {@link #setAttributes(Map)}.
56       */
57      public SessionAttributeInitializingFilter(
58              Map<String, ? extends Object> attributes) {
59          setAttributes(attributes);
60      }
61  
62      /**
63       * Returns the value of user-defined attribute.
64       *
65       * @param key the key of the attribute
66       * @return <tt>null</tt> if there is no attribute with the specified key
67       */
68      public Object getAttribute(String key) {
69          return attributes.get(key);
70      }
71  
72      /**
73       * Sets a user-defined attribute.
74       *
75       * @param key the key of the attribute
76       * @param value the value of the attribute
77       * @return The old value of the attribute.  <tt>null</tt> if it is new.
78       */
79      public Object setAttribute(String key, Object value) {
80          if (value == null) {
81              return removeAttribute(key);
82          } else {
83              return attributes.put(key, value);
84          }
85      }
86  
87      /**
88       * Sets a user defined attribute without a value.  This is useful when
89       * you just want to put a 'mark' attribute.  Its value is set to
90       * {@link Boolean#TRUE}.
91       *
92       * @param key the key of the attribute
93       * @return The old value of the attribute.  <tt>null</tt> if it is new.
94       */
95      public Object setAttribute(String key) {
96          return attributes.put(key, Boolean.TRUE);
97      }
98  
99      /**
100      * Removes a user-defined attribute with the specified key.
101      *
102      * @return The old value of the attribute.  <tt>null</tt> if not found.
103      */
104     public Object removeAttribute(String key) {
105         return attributes.remove(key);
106     }
107 
108     /**
109      * Returns <tt>true</tt> if this session contains the attribute with
110      * the specified <tt>key</tt>.
111      */
112     boolean containsAttribute(String key) {
113         return attributes.containsKey(key);
114     }
115 
116     /**
117      * Returns the set of keys of all user-defined attributes.
118      */
119     public Set<String> getAttributeKeys() {
120         return attributes.keySet();
121     }
122 
123     /**
124      * Sets the attribute map.  The specified attributes are copied into the
125      * underlying map, so modifying the specified attributes parameter after
126      * the call won't change the internal state.
127      */
128     public void setAttributes(Map<String, ? extends Object> attributes) {
129         if (attributes == null) {
130             attributes = new HashMap<String, Object>();
131         }
132 
133         this.attributes.clear();
134         this.attributes.putAll(attributes);
135     }
136 
137     /**
138      * Puts all pre-configured attributes into the actual session attribute
139      * map and forward the event to the next filter.
140      */
141     @Override
142     public void sessionCreated(NextFilter nextFilter, IoSession session)
143             throws Exception {
144         for (Map.Entry<String, Object> e : attributes.entrySet()) {
145             session.setAttribute(e.getKey(), e.getValue());
146         }
147 
148         nextFilter.sessionCreated(session);
149     }
150 }