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
42 public class DefaultIoSessionDataStructureFactory implements
43 IoSessionDataStructureFactory {
44
45 public IoSessionAttributeMap getAttributeMap(IoSession session)
46 throws Exception {
47 return new DefaultIoSessionAttributeMap();
48 }
49
50 public WriteRequestQueue getWriteRequestQueue(IoSession session)
51 throws Exception {
52 return new DefaultWriteRequestQueue();
53 }
54
55 private static class DefaultIoSessionAttributeMap implements IoSessionAttributeMap {
56
57 private final Map<Object, Object> attributes =
58 Collections.synchronizedMap(new HashMap<Object, Object>(4));
59
60 public Object getAttribute(IoSession session, Object key, Object defaultValue) {
61 if (key == null) {
62 throw new NullPointerException("key");
63 }
64
65 Object answer = attributes.get(key);
66 if (answer == null) {
67 return defaultValue;
68 } else {
69 return answer;
70 }
71 }
72
73 public Object setAttribute(IoSession session, Object key, Object value) {
74 if (key == null) {
75 throw new NullPointerException("key");
76 }
77
78 if (value == null) {
79 return attributes.remove(key);
80 } else {
81 return attributes.put(key, value);
82 }
83 }
84
85 public Object setAttributeIfAbsent(IoSession session, Object key, Object value) {
86 if (key == null) {
87 throw new NullPointerException("key");
88 }
89
90 if (value == null) {
91 return null;
92 }
93
94 Object oldValue;
95 synchronized (attributes) {
96 oldValue = attributes.get(key);
97 if (oldValue == null) {
98 attributes.put(key, value);
99 }
100 }
101 return oldValue;
102 }
103
104 public Object removeAttribute(IoSession session, Object key) {
105 if (key == null) {
106 throw new NullPointerException("key");
107 }
108
109 return attributes.remove(key);
110 }
111
112 public boolean removeAttribute(IoSession session, Object key, Object value) {
113 if (key == null) {
114 throw new NullPointerException("key");
115 }
116
117 if (value == null) {
118 return false;
119 }
120
121 synchronized (attributes) {
122 if (value.equals(attributes.get(key))) {
123 attributes.remove(key);
124 return true;
125 }
126 }
127
128 return false;
129 }
130
131 public boolean replaceAttribute(IoSession session, Object key, Object oldValue, Object newValue) {
132 synchronized (attributes) {
133 Object actualOldValue = attributes.get(key);
134 if (actualOldValue == null) {
135 return false;
136 }
137
138 if (actualOldValue.equals(oldValue)) {
139 attributes.put(key, newValue);
140 return true;
141 } else {
142 return false;
143 }
144 }
145 }
146
147 public boolean containsAttribute(IoSession session, Object key) {
148 return attributes.containsKey(key);
149 }
150
151 public Set<Object> getAttributeKeys(IoSession session) {
152 synchronized (attributes) {
153 return new HashSet<Object>(attributes.keySet());
154 }
155 }
156
157 public void dispose(IoSession session) throws Exception {
158 }
159 }
160
161 private static class DefaultWriteRequestQueue implements WriteRequestQueue {
162
163 private final Queue<WriteRequest> q = new CircularQueue<WriteRequest>(16);
164
165
166
167
168 public void dispose(IoSession session) {
169 }
170
171
172
173
174 public void clear(IoSession session) {
175 q.clear();
176 }
177
178
179
180
181 public synchronized boolean isEmpty(IoSession session) {
182 return q.isEmpty();
183 }
184
185
186
187
188 public synchronized void offer(IoSession session, WriteRequest writeRequest) {
189 q.offer(writeRequest);
190 }
191
192
193
194
195 public synchronized WriteRequest poll(IoSession session) {
196 return q.poll();
197 }
198
199 @Override
200 public String toString() {
201 return q.toString();
202 }
203 }
204 }