1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
36
37
38
39
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
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
164 }
165 }
166
167 private static class DefaultWriteRequestQueue implements WriteRequestQueue {
168
169 private final Queue<WriteRequest> q = new CircularQueue<WriteRequest>(16);
170
171
172
173
174 public DefaultWriteRequestQueue() {
175 super();
176 }
177
178
179
180
181 public void dispose(IoSession session) {
182
183 }
184
185
186
187
188 public void clear(IoSession session) {
189 q.clear();
190 }
191
192
193
194
195 public synchronized boolean isEmpty(IoSession session) {
196 return q.isEmpty();
197 }
198
199
200
201
202 public synchronized void offer(IoSession session, WriteRequest writeRequest) {
203 q.offer(writeRequest);
204 }
205
206
207
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 }