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