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.core.session;
21  
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Queue;
25  import java.util.Set;
26  import java.util.concurrent.ConcurrentHashMap;
27  import java.util.concurrent.ConcurrentLinkedQueue;
28  
29  import org.apache.mina.core.write.WriteRequest;
30  import org.apache.mina.core.write.WriteRequestQueue;
31  
32  /**
33   * The default {@link IoSessionDataStructureFactory} implementation
34   * that creates a new {@link HashMap}-based {@link IoSessionAttributeMap}
35   * instance and a new synchronized {@link ConcurrentLinkedQueue} instance per
36   * {@link IoSession}.
37   * 
38   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
39   */
40  public class DefaultIoSessionDataStructureFactory implements IoSessionDataStructureFactory {
41      /**
42       * {@inheritDoc}
43       */
44      @Override
45      public IoSessionAttributeMap getAttributeMap(IoSession session) throws Exception {
46          return new DefaultIoSessionAttributeMap();
47      }
48  
49      /**
50       * {@inheritDoc}
51       */
52      @Override
53      public WriteRequestQueue getWriteRequestQueue(IoSession session) throws Exception {
54          return new DefaultWriteRequestQueue();
55      }
56  
57      private static class DefaultIoSessionAttributeMap implements IoSessionAttributeMap {
58          private final ConcurrentHashMap<Object, Object> attributes = new ConcurrentHashMap<>(4);
59  
60          /**
61           * Default constructor
62           */
63          public DefaultIoSessionAttributeMap() {
64              super();
65          }
66  
67          /**
68           * {@inheritDoc}
69           */
70          @Override
71          public Object getAttribute(IoSession session, Object key, Object defaultValue) {
72              if (key == null) {
73                  throw new IllegalArgumentException("key");
74              }
75  
76              if (defaultValue == null) {
77                  return attributes.get(key);
78              }
79  
80              Object object = attributes.putIfAbsent(key, defaultValue);
81  
82              if (object == null) {
83                  return defaultValue;
84              } else {
85                  return object;
86              }
87          }
88  
89          /**
90           * {@inheritDoc}
91           */
92          @Override
93          public Object setAttribute(IoSession session, Object key, Object value) {
94              if (key == null) {
95                  throw new IllegalArgumentException("key");
96              }
97  
98              if (value == null) {
99                  return attributes.remove(key);
100             }
101 
102             return attributes.put(key, value);
103         }
104 
105         /**
106          * {@inheritDoc}
107          */
108         @Override
109         public Object setAttributeIfAbsent(IoSession session, Object key, Object value) {
110             if (key == null) {
111                 throw new IllegalArgumentException("key");
112             }
113 
114             if (value == null) {
115                 return null;
116             }
117 
118             return attributes.putIfAbsent(key, value);
119         }
120 
121         /**
122          * {@inheritDoc}
123          */
124         @Override
125         public Object removeAttribute(IoSession session, Object key) {
126             if (key == null) {
127                 throw new IllegalArgumentException("key");
128             }
129 
130             return attributes.remove(key);
131         }
132 
133         /**
134          * {@inheritDoc}
135          */
136         @Override
137         public boolean removeAttribute(IoSession session, Object key, Object value) {
138             if (key == null) {
139                 throw new IllegalArgumentException("key");
140             }
141 
142             if (value == null) {
143                 return false;
144             }
145 
146             try {
147                 return attributes.remove(key, value);
148             } catch (NullPointerException e) {
149                 return false;
150             }
151         }
152 
153         /**
154          * {@inheritDoc}
155          */
156         @Override
157         public boolean replaceAttribute(IoSession session, Object key, Object oldValue, Object newValue) {
158             try {
159                 return attributes.replace(key, oldValue, newValue);
160             } catch (NullPointerException e) {
161             }
162 
163             return false;
164         }
165 
166         /**
167          * {@inheritDoc}
168          */
169         @Override
170         public boolean containsAttribute(IoSession session, Object key) {
171             return attributes.containsKey(key);
172         }
173 
174         /**
175          * {@inheritDoc}
176          */
177         @Override
178         public Set<Object> getAttributeKeys(IoSession session) {
179             synchronized (attributes) {
180                 return new HashSet<>(attributes.keySet());
181             }
182         }
183 
184         /**
185          * {@inheritDoc}
186          */
187         @Override
188         public void dispose(IoSession session) throws Exception {
189             // Do nothing
190         }
191     }
192 
193     private static class DefaultWriteRequestQueue implements WriteRequestQueue {
194         /** A queue to store incoming write requests */
195         private final Queue<WriteRequest> q = new ConcurrentLinkedQueue<>();
196 
197         /**
198          * {@inheritDoc}
199          */
200         @Override
201         public void dispose(IoSession session) {
202             // Do nothing
203         }
204 
205         /**
206          * {@inheritDoc}
207          */
208         @Override
209         public void clear(IoSession session) {
210             q.clear();
211         }
212 
213         /**
214          * {@inheritDoc}
215          */
216         @Override
217         public boolean isEmpty(IoSession session) {
218             return q.isEmpty();
219         }
220 
221         /**
222          * {@inheritDoc}
223          */
224         @Override
225         public void offer(IoSession session, WriteRequest writeRequest) {
226             q.offer(writeRequest);
227         }
228 
229         /**
230          * {@inheritDoc}
231          */
232         @Override
233         public WriteRequest poll(IoSession session) {
234             WriteRequest answer = q.poll();
235 
236             if (answer == AbstractIoSession.CLOSE_REQUEST) {
237                 session.closeNow();
238                 dispose(session);
239                 answer = null;
240             }
241 
242             return answer;
243         }
244 
245         /**
246          * {@inheritDoc}
247          */
248         @Override
249         public String toString() {
250             return q.toString();
251         }
252 
253         /**
254          * {@inheritDoc}
255          */
256         @Override
257         public int size() {
258             return q.size();
259         }
260     }
261 }