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.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   * The default {@link IoSessionDataStructureFactory} implementation
33   * that creates a new {@link HashMap}-based {@link IoSessionAttributeMap}
34   * instance and a new synchronized {@link CircularQueue} instance per
35   * {@link IoSession}.
36   * 
37   * @author The Apache MINA Project (dev@mina.apache.org)
38   * @version $Rev: 601679 $, $Date: 2007-12-06 03:13:21 -0700 (Thu, 06 Dec 2007) $
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 }