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.core.session;
    21
    22  import java.util.HashMap;
    23  import java.util.HashSet;
    24  import java.util.Queue;
    25  import java.util.Set;
    26  import java.util.concurrent.ConcurrentHashMap;
    27  import java.util.concurrent.ConcurrentLinkedQueue;
    28
    29  import org.apache.mina.core.write.WriteRequest;
    30  import org.apache.mina.core.write.WriteRequestQueue;
    31
    32  /**
    33   * The default {@link IoSessionDataStructureFactory} implementation
    34   * that creates a new {@link HashMap}-based {@link IoSessionAttributeMap}
    35   * instance and a new synchronized {@link ConcurrentLinkedQueue} instance per
    36   * {@link IoSession}.
    37   *
    38   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    39   */
    40  public class DefaultIoSessionDataStructureFactory implements IoSessionDataStructureFactory {
    41
    42      public IoSessionAttributeMap getAttributeMap(IoSession session) throws Exception {
    43          return new DefaultIoSessionAttributeMap();
    44      }
    45
    46      public WriteRequestQueue getWriteRequestQueue(IoSession session) throws Exception {
    47          return new DefaultWriteRequestQueue();
    48      }
    49
    50      private static class DefaultIoSessionAttributeMap implements IoSessionAttributeMap {
    51          private final ConcurrentHashMap<Object, Object> attributes = new ConcurrentHashMap<Object, Object>(4);
    52
    53          /**
    54           * Default constructor
    55           */
    56          public DefaultIoSessionAttributeMap() {
    57              super();
    58          }
    59
    60          /**
    61           * {@inheritDoc}
    62           */
    63          public Object getAttribute(IoSession session, Object key, Object defaultValue) {
    64              if (key == null) {
    65                  throw new IllegalArgumentException("key");
    66              }
    67
    68              if (defaultValue == null) {
    69                  return attributes.get(key);
    70              }
    71
    72              Object object = attributes.putIfAbsent(key, defaultValue);
    73
    74              if (object == null) {
    75                  return defaultValue;
    76              } else {
    77                  return object;
    78              }
    79          }
    80
    81          /**
    82           * {@inheritDoc}
    83           */
    84          public Object setAttribute(IoSession session, Object key, Object value) {
    85              if (key == null) {
    86                  throw new IllegalArgumentException("key");
    87              }
    88
    89              if (value == null) {
    90                  return attributes.remove(key);
    91              }
    92
    93              return attributes.put(key, value);
    94          }
    95
    96          /**
    97           * {@inheritDoc}
    98           */
    99          public Object setAttributeIfAbsent(IoSession session, Object key, Object value) {
    100             if (key == null) {
    101                 throw new IllegalArgumentException("key");
    102             }
    103
    104             if (value == null) {
    105                 return null;
    106             }
    107
    108             return attributes.putIfAbsent(key, value);
    109         }
    110
    111         /**
    112          * {@inheritDoc}
    113          */
    114         public Object removeAttribute(IoSession session, Object key) {
    115             if (key == null) {
    116                 throw new IllegalArgumentException("key");
    117             }
    118
    119             return attributes.remove(key);
    120         }
    121
    122         /**
    123          * {@inheritDoc}
    124          */
    125         public boolean removeAttribute(IoSession session, Object key, Object value) {
    126             if (key == null) {
    127                 throw new IllegalArgumentException("key");
    128             }
    129
    130             if (value == null) {
    131                 return false;
    132             }
    133
    134             try {
    135                 return attributes.remove(key, value);
    136             } catch (NullPointerException e) {
    137                 return false;
    138             }
    139         }
    140
    141         /**
    142          * {@inheritDoc}
    143          */
    144         public boolean replaceAttribute(IoSession session, Object key, Object oldValue, Object newValue) {
    145             try {
    146                 return attributes.replace(key, oldValue, newValue);
    147             } catch (NullPointerException e) {
    148             }
    149
    150             return false;
    151         }
    152
    153         /**
    154          * {@inheritDoc}
    155          */
    156         public boolean containsAttribute(IoSession session, Object key) {
    157             return attributes.containsKey(key);
    158         }
    159
    160         /**
    161          * {@inheritDoc}
    162          */
    163         public Set<Object> getAttributeKeys(IoSession session) {
    164             synchronized (attributes) {
    165                 return new HashSet<Object>(attributes.keySet());
    166             }
    167         }
    168
    169         /**
    170          * {@inheritDoc}
    171          */
    172         public void dispose(IoSession session) throws Exception {
    173             // Do nothing
    174         }
    175     }
    176
    177     private static class DefaultWriteRequestQueue implements WriteRequestQueue {
    178         /** A queue to store incoming write requests */
    179         private final Queue<WriteRequest> q = new ConcurrentLinkedQueue<WriteRequest>();
    180
    181         /**
    182          * Default constructor
    183          */
    184         public DefaultWriteRequestQueue() {
    185             super();
    186         }
    187
    188         /**
    189          * {@inheritDoc}
    190          */
    191         public void dispose(IoSession session) {
    192             // Do nothing
    193         }
    194
    195         /**
    196          * {@inheritDoc}
    197          */
    198         public void clear(IoSession session) {
    199             q.clear();
    200         }
    201
    202         /**
    203          * {@inheritDoc}
    204          */
    205         public synchronized boolean isEmpty(IoSession session) {
    206             return q.isEmpty();
    207         }
    208
    209         /**
    210          * {@inheritDoc}
    211          */
    212         public synchronized void offer(IoSession session, WriteRequest writeRequest) {
    213             q.offer(writeRequest);
    214         }
    215
    216         /**
    217          * {@inheritDoc}
    218          */
    219         public synchronized WriteRequest poll(IoSession session) {
    220             return q.poll();
    221         }
    222
    223         @Override
    224         public String toString() {
    225             return q.toString();
    226         }
    227
    228         /**
    229          * {@inheritDoc}
    230          */
    231         public int size() {
    232             return q.size();
    233         }
    234     }
    235 }