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 org.jboss.netty.util.internal.StringUtil;
19  
20  import java.util.Map;
21  import java.util.Set;
22  import java.util.TreeMap;
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      public Set<Integer> getIDs() {
33          return getIds();
34      }
35  
36      public Set<Integer> getIds() {
37          return settingsMap.keySet();
38      }
39  
40      public boolean isSet(int id) {
41          Integer key = id;
42          return settingsMap.containsKey(key);
43      }
44  
45      public int getValue(int id) {
46          Integer key = id;
47          if (settingsMap.containsKey(key)) {
48              return settingsMap.get(key).getValue();
49          } else {
50              return -1;
51          }
52      }
53  
54      public void setValue(int id, int value) {
55          setValue(id, value, false, false);
56      }
57  
58      public void setValue(int id, int value, boolean persistValue, boolean persisted) {
59          if (id <= 0 || id > SpdyCodecUtil.SPDY_SETTINGS_MAX_ID) {
60              throw new IllegalArgumentException("Setting id is not valid: " + id);
61          }
62          Integer key = id;
63          if (settingsMap.containsKey(key)) {
64              Setting setting = settingsMap.get(key);
65              setting.setValue(value);
66              setting.setPersist(persistValue);
67              setting.setPersisted(persisted);
68          } else {
69              settingsMap.put(key, new Setting(value, persistValue, persisted));
70          }
71      }
72  
73      public void removeValue(int id) {
74          Integer key = id;
75          if (settingsMap.containsKey(key)) {
76              settingsMap.remove(key);
77          }
78      }
79  
80      public boolean persistValue(int id) {
81          return isPersistValue(id);
82      }
83  
84      public boolean isPersistValue(int id) {
85          Integer key = id;
86          if (settingsMap.containsKey(key)) {
87              return settingsMap.get(key).isPersist();
88          } else {
89              return false;
90          }
91      }
92  
93      public void setPersistValue(int id, boolean persistValue) {
94          Integer key = id;
95          if (settingsMap.containsKey(key)) {
96              settingsMap.get(key).setPersist(persistValue);
97          }
98      }
99  
100     public boolean isPersisted(int id) {
101         Integer key = id;
102         if (settingsMap.containsKey(key)) {
103             return settingsMap.get(key).isPersisted();
104         } else {
105             return false;
106         }
107     }
108 
109     public void setPersisted(int id, boolean persisted) {
110         Integer key = id;
111         if (settingsMap.containsKey(key)) {
112             settingsMap.get(key).setPersisted(persisted);
113         }
114     }
115 
116     public boolean clearPreviouslyPersistedSettings() {
117         return clear;
118     }
119 
120     public void setClearPreviouslyPersistedSettings(boolean clear) {
121         this.clear = clear;
122     }
123 
124     private Set<Map.Entry<Integer, Setting>> getSettings() {
125         return settingsMap.entrySet();
126     }
127 
128     private void appendSettings(StringBuilder buf) {
129         for (Map.Entry<Integer, Setting> e: getSettings()) {
130             Setting setting = e.getValue();
131             buf.append("--> ");
132             buf.append(e.getKey().toString());
133             buf.append(':');
134             buf.append(setting.getValue());
135             buf.append(" (persist value: ");
136             buf.append(setting.isPersist());
137             buf.append("; persisted: ");
138             buf.append(setting.isPersisted());
139             buf.append(')');
140             buf.append(StringUtil.NEWLINE);
141         }
142     }
143 
144     @Override
145     public String toString() {
146         StringBuilder buf = new StringBuilder();
147         buf.append(getClass().getSimpleName());
148         buf.append(StringUtil.NEWLINE);
149         appendSettings(buf);
150         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
151         return buf.toString();
152     }
153 
154     private static final class Setting {
155 
156         private int value;
157         private boolean persist;
158         private boolean persisted;
159 
160         Setting(int value, boolean persist, boolean persisted) {
161             this.value = value;
162             this.persist = persist;
163             this.persisted = persisted;
164         }
165 
166         int getValue() {
167             return value;
168         }
169 
170         void setValue(int value) {
171             this.value = value;
172         }
173 
174         boolean isPersist() {
175             return persist;
176         }
177 
178         void setPersist(boolean persist) {
179             this.persist = persist;
180         }
181 
182         boolean isPersisted() {
183             return persisted;
184         }
185 
186         void setPersisted(boolean persisted) {
187             this.persisted = persisted;
188         }
189     }
190 }