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