1 9 package org.jboss.portal.portlet.impl; 10 11 import java.io.IOException ; 12 import java.util.Arrays ; 13 import java.util.Collection ; 14 import java.util.Enumeration ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 import javax.portlet.PortletPreferences; 21 import javax.portlet.PreferencesValidator; 22 import javax.portlet.ReadOnlyException; 23 import javax.portlet.ValidatorException; 24 25 import org.apache.log4j.Logger; 26 import org.jboss.portal.common.util.Tools; 27 import org.jboss.portal.common.value.NullValue; 28 import org.jboss.portal.common.value.StringValue; 29 import org.jboss.portal.common.value.StringValues; 30 import org.jboss.portal.common.value.Value; 31 import org.jboss.portal.server.plugins.preferences.MergeStrategy; 32 import org.jboss.portal.server.plugins.preferences.Preference; 33 import org.jboss.portal.server.plugins.preferences.PreferenceSet; 34 import org.jboss.portal.server.plugins.preferences.SimpleMergeStrategy; 35 36 40 public class PortletPreferencesImpl implements PortletPreferences 41 { 42 43 44 protected static final Logger log = Logger.getLogger(PortletPreferencesImpl.class); 45 46 47 public static final int ACTION = 1; 48 49 50 public static final int RENDER = 0; 51 52 protected final PreferenceSet[] sets1; 53 protected final PreferenceSet[] sets2; 54 55 protected final PreferenceSet system; 56 protected final PreferenceSet user; 57 protected final PreferencesValidator validator; 58 protected final int mode; 59 protected final MergeStrategy strategy; 60 61 62 protected final Map updates; 63 64 public PortletPreferencesImpl( 65 PreferenceSet system, 66 PreferenceSet user, 67 PreferencesValidator validator, 68 int mode) 69 { 70 this.sets1 = new PreferenceSet[]{system,user}; 71 this.sets2 = new PreferenceSet[]{system}; 72 this.validator = validator; 73 this.mode = mode; 74 this.system = system; 75 this.user = user; 76 this.strategy = SimpleMergeStrategy.getSingleton(); 77 78 this.updates = new HashMap (); 79 } 80 81 public Map getMap() 82 { 83 return new MyMap(strategy, updates, sets1); 84 } 85 86 public Enumeration getNames() 87 { 88 Set keySet = strategy.getKeySet(sets1); 89 return Tools.toEnumeration(keySet.iterator()); 90 } 91 92 private Value getValue(String key) 93 { 94 Value value = null; 95 Update update = (Update)updates.get(key); 96 if (update != null) 97 { 98 if (update.value != null) 99 { 100 value = update.value; 101 } 102 else 103 { 104 Preference preference = strategy.getPreference(sets2, key); 105 if (preference != null) 106 { 107 value = preference.getValue(); 108 } 109 } 110 } 111 else 112 { 113 Preference preference = strategy.getPreference(sets1, key); 114 if (preference != null) 115 { 116 value = preference.getValue(); 117 } 118 } 119 return value; 120 } 121 122 public String getValue(String key, String def) throws IllegalArgumentException 123 { 124 if (key == null) 125 { 126 throw new IllegalArgumentException ("key must not be null"); 127 } 128 Value value = getValue(key); 129 if (value != null) 130 { 131 return value.asString(); 132 } 133 else 134 { 135 return def; 136 } 137 } 138 139 public String [] getValues(String key, String [] def) throws IllegalArgumentException 140 { 141 if (key == null) 142 { 143 throw new IllegalArgumentException ("key must not be null"); 144 } 145 Value value = getValue(key); 146 if (value != null) 147 { 148 return value.asStringArray(); 149 } 150 else 151 { 152 return def; 153 } 154 } 155 156 public boolean isReadOnly(String key) throws IllegalArgumentException , IllegalArgumentException 157 { 158 if (key == null) 159 { 160 throw new IllegalArgumentException ("key must not be null"); 161 } 162 return strategy.isReadOnly(sets2, key); 163 } 164 165 public void reset(String key) throws IllegalArgumentException , ReadOnlyException 166 { 167 if (key == null) 168 { 169 throw new IllegalArgumentException ("key must not be null"); 170 } 171 if (isReadOnly(key)) 172 { 173 throw new ReadOnlyException("Key " + key + " cannot be written"); 174 } 175 Update update = (Update)updates.get(key); 176 if (update == null) 177 { 178 updates.put(key, new Update(null)); 179 } 180 else 181 { 182 update.value = null; 183 } 184 } 185 186 public void setValue(String key, String value) throws IllegalArgumentException , ReadOnlyException 187 { 188 if (key == null) 189 { 190 throw new IllegalArgumentException ("key must not be null"); 191 } 192 if (isReadOnly(key)) 193 { 194 throw new ReadOnlyException("Key " + key + " cannot be written"); 195 } 196 Value value_ = null; 197 if (value == null) 198 { 199 value_ = NullValue.INSTANCE; 200 } 201 else 202 { 203 value_ = new StringValue(value); 204 } 205 Update update = (Update)updates.get(key); 206 if (update == null) 207 { 208 updates.put(key, new Update(value_)); 209 } 210 else 211 { 212 update.value = value_; 213 } 214 } 215 216 public void setValues(String key, String [] values) throws IllegalArgumentException , ReadOnlyException 217 { 218 if (key == null) 219 { 220 throw new IllegalArgumentException ("key must not be null"); 221 } 222 if (isReadOnly(key)) 223 { 224 throw new ReadOnlyException("Key " + key + " cannot be written"); 225 } 226 Value value_ = null; 227 if (values == null) 228 { 229 value_ = NullValue.INSTANCE; 230 } 231 else 232 { 233 value_ = new StringValues(values); 234 } 235 Update update = (Update)updates.get(key); 236 if (update == null) 237 { 238 updates.put(key, new Update(value_)); 239 } 240 else 241 { 242 update.value = value_; 243 } 244 } 245 246 public void store() throws IOException , ValidatorException 247 { 248 if (mode != ACTION) 250 { 251 throw new IllegalStateException ("Store must be called within the scope of an action request"); 252 } 253 254 if (validator != null) 256 { 257 validator.validate(this); 258 } 259 260 for (Iterator i = updates.entrySet().iterator();i.hasNext();) 262 { 263 Map.Entry entry = (Map.Entry )i.next(); 264 String key = (String )entry.getKey(); 265 Update update = (Update)entry.getValue(); 266 user.setValue(key, update.value); 267 } 268 269 updates.clear(); 271 } 272 273 private static class MyMap extends HashMap 274 { 275 276 279 private MyMap(MergeStrategy strategy, Map updates, PreferenceSet[] sets) 280 { 281 super(10); 283 284 Set keys = strategy.getKeySet(sets); 286 for (Iterator i = keys.iterator();i.hasNext();) 287 { 288 String key = (String )i.next(); 289 Preference preference = strategy.getPreference(sets, key); 290 String [] value = preference.getValue().asStringArray(); 291 String [] clone = new String [value.length]; 292 System.arraycopy(value, 0, clone, 0, value.length); 293 super.put(key, clone); 294 } 295 296 for (Iterator i = updates.entrySet().iterator(); i.hasNext();) 298 { 299 Map.Entry entry = (Map.Entry )i.next(); 300 String name = (String )entry.getKey(); 301 Update update = (Update)entry.getValue(); 302 303 if (update.value == null) 304 { 305 super.remove(name); 306 } 307 else 308 { 309 String [] value = update.value.asStringArray(); 310 String [] clone = new String [value.length]; 311 System.arraycopy(value, 0, clone, 0, value.length); 312 super.put(name, clone); 313 } 314 } 315 } 316 317 public boolean containsValue(Object value) 318 { 319 if (value instanceof String []) 320 { 321 String [] strings = (String [])value; 322 for (Iterator i = super.values().iterator();i.hasNext();) 323 { 324 String [] other = (String [])i.next(); 325 if (Arrays.equals(strings, other)) 326 { 327 return true; 328 } 329 } 330 } 331 return false; 332 } 333 334 public Object get(Object key) 335 { 336 String [] strings = (String [])super.get(key); return strings; 338 } 339 340 public Collection values() 341 { 342 Collection values = super.values(); return values; 344 } 345 346 public Set entrySet() 347 { 348 return super.entrySet(); 349 } 350 351 356 public Object put(Object key, Object value) 357 { 358 return null; 359 } 360 361 366 public Object remove(Object key) 367 { 368 return null; 369 } 370 371 374 public void putAll(Map t) 375 { 376 } 377 378 381 public void clear() 382 { 383 } 384 } 385 386 protected static class Update 387 { 388 private Value value; 389 public Update(Value value) 390 { 391 this.value = value; 392 } 393 } 394 } 395 | Popular Tags |