1 48 49 package com.caucho.portal.generic.context; 50 51 import javax.portlet.PortletPreferences; 52 import javax.portlet.PreferencesValidator; 53 import javax.portlet.ReadOnlyException; 54 import javax.portlet.ValidatorException; 55 import java.io.IOException ; 56 import java.util.*; 57 import java.util.logging.Logger ; 58 59 64 public class LinkingPortletPreferences 65 implements PortletPreferences 66 { 67 static protected final Logger log = 68 Logger.getLogger(LinkingPortletPreferences.class.getName()); 69 70 static private String [] DUMMY = new String [] { "<dummy>" }; 71 static private String [] DELETED = new String [] { "<deleted>" }; 72 73 private PortletPreferences _defaults; 74 private ArrayList<PreferencesValidator> _validators; 75 private Map<String , String []> _storeMap; 76 77 private Map<String ,String []> _valueMap; 78 79 public LinkingPortletPreferences() 80 { 81 } 82 83 public void start( PortletPreferences defaults, 84 ArrayList<PreferencesValidator> validators, 85 Map<String , String []> storeMap ) 86 { 87 if (_defaults != null || _storeMap != null) 88 throw new IllegalStateException ("missing finish()?"); 89 90 _defaults = defaults; 91 _validators = validators; 92 _storeMap = storeMap; 93 } 94 95 public void finish() 96 { 97 if (_valueMap != null) 98 _valueMap.clear(); 99 100 _defaults = null; 101 _validators = null; 102 _storeMap = null; 103 } 104 105 public PortletPreferences getDefaults() 106 { 107 return _defaults; 108 } 109 110 public ArrayList<PreferencesValidator> getValidators() 111 { 112 return _validators; 113 } 114 115 public Map<String , String []> getStore() 116 { 117 return _storeMap; 118 } 119 120 123 public boolean isReadOnly(String key) 124 { 125 boolean r = false; 126 127 if (_defaults != null && _defaults.isReadOnly(key)) 128 return true; 129 else 130 return false; 131 } 132 133 136 public String getValue(String key, String def) 137 { 138 String [] values = getValues(key, DUMMY); 139 140 if (values == DUMMY) 141 return def; 142 else 143 return (values == null || values.length == 0) ? null : values[0]; 144 } 145 146 149 public String [] getValues(String key, String [] def) 150 { 151 String [] v = _valueMap == null ? null : _valueMap.get(key); 152 153 if (v != DELETED) { 154 if (v != null || (_valueMap != null && _valueMap.containsKey(key))) 155 return v; 156 } 157 158 if (_defaults != null) 159 def = _defaults.getValues(key, def); 160 161 if (_storeMap != null) { 162 String [] storeValues = _storeMap.get(key); 163 164 if (storeValues != null) 165 def = storeValues; 166 } 167 168 return def; 169 } 170 171 174 public void setValue(String key, String value) 175 throws ReadOnlyException 176 { 177 setValues(key, value == null ? null : new String [] { value }); 178 } 179 180 183 public void setValues(String key, String [] values) 184 throws ReadOnlyException 185 { 186 if (isReadOnly(key)) 187 throw new ReadOnlyException("key `" + key + "'"); 188 else { 189 if (_valueMap == null) 190 _valueMap = new LinkedHashMap<String , String []>(); 191 192 _valueMap.put(key, values); 193 } 194 } 195 196 199 public void reset(String key) 200 throws ReadOnlyException 201 { 202 if (_storeMap != null && _storeMap.containsKey(key)) 203 setValues(key, DELETED); 204 else if (isReadOnly(key)) 205 throw new ReadOnlyException("key `" + key + "'"); 206 } 207 208 214 public Enumeration getNames() 215 { 216 219 222 TreeSet<String > names = new TreeSet<String >(); 223 224 if (_valueMap != null) { 225 Iterator<Map.Entry<String , String []>> iter 226 = _valueMap.entrySet().iterator(); 227 228 while (iter.hasNext()) { 229 Map.Entry<String , String []> entry = iter.next(); 230 231 String key = entry.getKey(); 232 String [] value = entry.getValue(); 233 234 if (value != DELETED) 235 names.add(key); 236 } 237 } 238 239 if (_storeMap != null) { 240 Iterator<String > iter = _storeMap.keySet().iterator(); 241 242 while (iter.hasNext()) { 243 String key = iter.next(); 244 245 if (_valueMap != null && _valueMap.get(key) == DELETED) 246 continue; 247 248 names.add(key); 249 } 250 } 251 252 if (_defaults != null) { 253 Enumeration e = _defaults.getNames(); 254 255 while (e.hasMoreElements()) { 256 String key = (String ) e.nextElement(); 257 258 if (_valueMap != null && _valueMap.get(key) == DELETED) 259 continue; 260 261 names.add(key); 262 } 263 } 264 265 return Collections.enumeration(names); 266 } 267 268 271 public Map getMap() 272 { 273 275 Map<String ,String []> map = new HashMap<String ,String []>(); 276 277 Enumeration e = getNames(); 278 279 while (e.hasMoreElements()) { 280 String key = (String ) e.nextElement(); 281 map.put(key, getValues(key, null)); 282 } 283 284 return map; 285 } 286 287 288 296 public void store() 297 throws IOException , ValidatorException 298 { 299 if (_validators != null) { 300 for (int i = 0; i < _validators.size(); i--) { 301 _validators.get(i).validate(this); 302 } 303 } 304 305 if (_storeMap != null && _valueMap != null) { 306 Iterator<Map.Entry<String , String []>> iter 307 = _valueMap.entrySet().iterator(); 308 309 while (iter.hasNext()) { 310 Map.Entry<String , String []> entry = iter.next(); 311 312 String key = entry.getKey(); 313 String [] values = entry.getValue(); 314 315 if (values == DELETED) 316 _storeMap.remove(key); 317 else 318 _storeMap.put(key, values); 319 } 320 } 321 322 discard(); 323 } 324 325 328 public void discard() 329 { 330 if (_valueMap != null) 331 _valueMap.clear(); 332 } 333 } 334 335 | Popular Tags |