View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the License. You may obtain a copy of the License at:
    7    *
    8    *   http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.handler.codec.spdy;
    17
    18  import java.util.Map;
    19  import java.util.Set;
    20  import java.util.TreeMap;
    21
    22  import org.jboss.netty.util.internal.StringUtil;
    23
    24  /**
    25   * The default {@link SpdySettingsFrame} implementation.
    26   */
    27  public class DefaultSpdySettingsFrame implements SpdySettingsFrame {
    28
    29      private boolean clear;
    30      private final Map<Integer, Setting> settingsMap = new TreeMap<Integer, Setting>();
    31
    32      /**
    33       * Creates a new instance.
    34       */
    35      public DefaultSpdySettingsFrame() {
    36      }
    37
    38      public Set<Integer> getIDs() {
    39          return getIds();
    40      }
    41
    42      public Set<Integer> getIds() {
    43          return settingsMap.keySet();
    44      }
    45
    46      public boolean isSet(int ID) {
    47          Integer key = ID;
    48          return settingsMap.containsKey(key);
    49      }
    50
    51      public int getValue(int ID) {
    52          Integer key = ID;
    53          if (settingsMap.containsKey(key)) {
    54              return settingsMap.get(key).getValue();
    55          } else {
    56              return -1;
    57          }
    58      }
    59
    60      public void setValue(int ID, int value) {
    61          setValue(ID, value, false, false);
    62      }
    63
    64      public void setValue(int ID, int value, boolean persistValue, boolean persisted) {
    65          if (ID <= 0 || ID > SpdyCodecUtil.SPDY_SETTINGS_MAX_ID) {
    66              throw new IllegalArgumentException("Setting ID is not valid: " + ID);
    67          }
    68          Integer key = ID;
    69          if (settingsMap.containsKey(key)) {
    70              Setting setting = settingsMap.get(key);
    71              setting.setValue(value);
    72              setting.setPersist(persistValue);
    73              setting.setPersisted(persisted);
    74          } else {
    75              settingsMap.put(key, new Setting(value, persistValue, persisted));
    76          }
    77      }
    78
    79      public void removeValue(int ID) {
    80          Integer key = ID;
    81          if (settingsMap.containsKey(key)) {
    82              settingsMap.remove(key);
    83          }
    84      }
    85
    86      public boolean persistValue(int ID) {
    87          return isPersistValue(ID);
    88      }
    89
    90      public boolean isPersistValue(int ID) {
    91          Integer key = ID;
    92          if (settingsMap.containsKey(key)) {
    93              return settingsMap.get(key).isPersist();
    94          } else {
    95              return false;
    96          }
    97      }
    98
    99      public void setPersistValue(int ID, boolean persistValue) {
    100         Integer key = ID;
    101         if (settingsMap.containsKey(key)) {
    102             settingsMap.get(key).setPersist(persistValue);
    103         }
    104     }
    105
    106     public boolean isPersisted(int ID) {
    107         Integer key = ID;
    108         if (settingsMap.containsKey(key)) {
    109             return settingsMap.get(key).isPersisted();
    110         } else {
    111             return false;
    112         }
    113     }
    114
    115     public void setPersisted(int ID, boolean persisted) {
    116         Integer key = ID;
    117         if (settingsMap.containsKey(key)) {
    118             settingsMap.get(key).setPersisted(persisted);
    119         }
    120     }
    121
    122     public boolean clearPreviouslyPersistedSettings() {
    123         return clear;
    124     }
    125
    126     public void setClearPreviouslyPersistedSettings(boolean clear) {
    127         this.clear = clear;
    128     }
    129
    130     private Set<Map.Entry<Integer, Setting>> getSettings() {
    131         return settingsMap.entrySet();
    132     }
    133
    134     private void appendSettings(StringBuilder buf) {
    135         for (Map.Entry<Integer, Setting> e: getSettings()) {
    136             Setting setting = e.getValue();
    137             buf.append("--> ");
    138             buf.append(e.getKey().toString());
    139             buf.append(":");
    140             buf.append(setting.getValue());
    141             buf.append(" (persist value: ");
    142             buf.append(setting.isPersist());
    143             buf.append("; persisted: ");
    144             buf.append(setting.isPersisted());
    145             buf.append(')');
    146             buf.append(StringUtil.NEWLINE);
    147         }
    148     }
    149
    150     @Override
    151     public String toString() {
    152         StringBuilder buf = new StringBuilder();
    153         buf.append(getClass().getSimpleName());
    154         buf.append(StringUtil.NEWLINE);
    155         appendSettings(buf);
    156         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
    157         return buf.toString();
    158     }
    159
    160     private static final class Setting {
    161
    162         private int value;
    163         private boolean persist;
    164         private boolean persisted;
    165
    166         Setting(int value, boolean persist, boolean persisted) {
    167             this.value = value;
    168             this.persist = persist;
    169             this.persisted = persisted;
    170         }
    171
    172         int getValue() {
    173             return value;
    174         }
    175
    176         void setValue(int value) {
    177             this.value = value;
    178         }
    179
    180         boolean isPersist() {
    181             return persist;
    182         }
    183
    184         void setPersist(boolean persist) {
    185             this.persist = persist;
    186         }
    187
    188         boolean isPersisted() {
    189             return persisted;
    190         }
    191
    192         void setPersisted(boolean persisted) {
    193             this.persisted = persisted;
    194         }
    195     }
    196 }