001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.common;
021:
022: import java.util.Collections;
023: import java.util.HashMap;
024: import java.util.HashSet;
025: import java.util.Map;
026: import java.util.Queue;
027: import java.util.Set;
028:
029: import org.apache.mina.util.CircularQueue;
030:
031: /**
032: * The default {@link IoSessionDataStructureFactory} implementation
033: * that creates a new {@link HashMap}-based {@link IoSessionAttributeMap}
034: * instance and a new synchronized {@link CircularQueue} instance per
035: * {@link IoSession}.
036: *
037: * @author The Apache MINA Project (dev@mina.apache.org)
038: * @version $Rev: 601679 $, $Date: 2007-12-06 03:13:21 -0700 (Thu, 06 Dec 2007) $
039: */
040: public class DefaultIoSessionDataStructureFactory implements
041: IoSessionDataStructureFactory {
042:
043: public IoSessionAttributeMap getAttributeMap(IoSession session)
044: throws Exception {
045: return new DefaultIoSessionAttributeMap();
046: }
047:
048: public WriteRequestQueue getWriteRequestQueue(IoSession session)
049: throws Exception {
050: return new DefaultWriteRequestQueue();
051: }
052:
053: private static class DefaultIoSessionAttributeMap implements
054: IoSessionAttributeMap {
055:
056: private final Map<Object, Object> attributes = Collections
057: .synchronizedMap(new HashMap<Object, Object>(4));
058:
059: public Object getAttribute(IoSession session, Object key,
060: Object defaultValue) {
061: if (key == null) {
062: throw new NullPointerException("key");
063: }
064:
065: Object answer = attributes.get(key);
066: if (answer == null) {
067: return defaultValue;
068: } else {
069: return answer;
070: }
071: }
072:
073: public Object setAttribute(IoSession session, Object key,
074: Object value) {
075: if (key == null) {
076: throw new NullPointerException("key");
077: }
078:
079: if (value == null) {
080: return attributes.remove(key);
081: } else {
082: return attributes.put(key, value);
083: }
084: }
085:
086: public Object setAttributeIfAbsent(IoSession session,
087: Object key, Object value) {
088: if (key == null) {
089: throw new NullPointerException("key");
090: }
091:
092: if (value == null) {
093: return null;
094: }
095:
096: Object oldValue;
097: synchronized (attributes) {
098: oldValue = attributes.get(key);
099: if (oldValue == null) {
100: attributes.put(key, value);
101: }
102: }
103: return oldValue;
104: }
105:
106: public Object removeAttribute(IoSession session, Object key) {
107: if (key == null) {
108: throw new NullPointerException("key");
109: }
110:
111: return attributes.remove(key);
112: }
113:
114: public boolean removeAttribute(IoSession session, Object key,
115: Object value) {
116: if (key == null) {
117: throw new NullPointerException("key");
118: }
119:
120: if (value == null) {
121: return false;
122: }
123:
124: synchronized (attributes) {
125: if (value.equals(attributes.get(key))) {
126: attributes.remove(key);
127: return true;
128: }
129: }
130:
131: return false;
132: }
133:
134: public boolean replaceAttribute(IoSession session, Object key,
135: Object oldValue, Object newValue) {
136: synchronized (attributes) {
137: Object actualOldValue = attributes.get(key);
138: if (actualOldValue == null) {
139: return false;
140: }
141:
142: if (actualOldValue.equals(oldValue)) {
143: attributes.put(key, newValue);
144: return true;
145: } else {
146: return false;
147: }
148: }
149: }
150:
151: public boolean containsAttribute(IoSession session, Object key) {
152: return attributes.containsKey(key);
153: }
154:
155: public Set<Object> getAttributeKeys(IoSession session) {
156: synchronized (attributes) {
157: return new HashSet<Object>(attributes.keySet());
158: }
159: }
160:
161: public void dispose(IoSession session) throws Exception {
162: }
163: }
164:
165: private static class DefaultWriteRequestQueue implements
166: WriteRequestQueue {
167:
168: private final Queue<WriteRequest> q = new CircularQueue<WriteRequest>(
169: 16);
170:
171: public void dispose(IoSession session) {
172: }
173:
174: public void clear(IoSession session) {
175: q.clear();
176: }
177:
178: public synchronized boolean isEmpty(IoSession session) {
179: return q.isEmpty();
180: }
181:
182: public synchronized void offer(IoSession session,
183: WriteRequest writeRequest) {
184: q.offer(writeRequest);
185: }
186:
187: public synchronized WriteRequest poll(IoSession session) {
188: return q.poll();
189: }
190:
191: @Override
192: public String toString() {
193: return q.toString();
194: }
195: }
196: }
|