1 9 package org.jboss.portal.core.impl.preferences; 10 11 import java.util.HashMap ; 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import org.apache.log4j.Logger; 18 import org.jboss.portal.common.FQN; 19 import org.jboss.portal.common.value.Value; 20 import org.jboss.portal.server.plugins.preferences.Preference; 21 import org.jboss.portal.server.plugins.preferences.PreferenceSet; 22 23 30 public class MappedPreferenceSet 31 implements PreferenceSet 32 { 33 34 35 private static Logger log = Logger.getLogger(MappedPreferenceSet.class); 36 37 private Integer id; 38 private MappedPreferenceSet parent; 39 private Set children; 40 private String name; 41 private FQN fqn; 42 protected Map map; 43 44 47 public MappedPreferenceSet() 48 { 49 this.children = new HashSet (); 50 this.id = null; 51 this.name = null; 52 this.parent = null; 53 this.fqn = null; 54 this.map = null; 55 } 56 57 public MappedPreferenceSet(String name) 58 { 59 this.children = new HashSet (); 60 this.id = null; 61 this.name = name; 62 this.parent = null; 63 this.fqn = new FQN(); 64 this.map = new HashMap (); 65 } 66 67 72 public Integer getID() 73 { 74 return id; 75 } 76 77 80 protected void setID(Integer id) 81 { 82 this.id = id; 83 } 84 85 91 public String getName() 92 { 93 return name; 94 } 95 96 99 protected void setName(String name) 100 { 101 this.name = name; 102 } 103 104 109 public MappedPreferenceSet getParent() 110 { 111 return parent; 112 } 113 114 public void setParent(MappedPreferenceSet parent) 115 { 116 this.parent = parent; 117 } 118 119 public void addChild(MappedPreferenceSet child) 120 { 121 children.add(child); 122 child.setParent(this); 123 } 124 125 135 public Set getChildren() 136 { 137 return children; 138 } 139 140 public void setChildren(Set children) 141 { 142 this.children = children; 143 } 144 145 public MappedPreferenceSet getDescendant(FQN fqn) 146 { 147 boolean debug = log.isDebugEnabled(); 149 MappedPreferenceSet current = this; 150 151 for (int i = 0;i < fqn.size();i++) 153 { 154 String name = fqn.getName(i).toString(); 156 if (debug) 157 { 158 log.debug("Using for " + this.id + "/" + this.name + " name (" + i + "," + name + ")"); 159 } 160 161 MappedPreferenceSet next = null; 163 for (Iterator j = current.getChildren().iterator();j.hasNext();) 164 { 165 MappedPreferenceSet child = (MappedPreferenceSet)j.next(); 166 String childName = child.getName(); 167 if (name.equals(childName)) 168 { 169 next = child; 170 if (debug) 171 { 172 log.debug("Found for " + this.id + "/" + this.name + " child " + childName); 173 } 174 break; 175 } 176 } 177 178 if (next == null) 180 { 181 if (debug) 182 { 183 log.debug("Creating child for " + this.id + "/" + this.name); 184 } 185 next = newChild(name); 186 current.addChild(next); 187 } 188 189 current = next; 190 } 191 192 return current; 193 } 194 195 public FQN getFQN() 196 { 197 if (fqn == null) 198 { 199 if (parent == null) 200 { 201 this.fqn = new FQN(); 202 } 203 else 204 { 205 this.fqn = new FQN(parent.getFQN(), name); 206 } 207 } 208 return fqn; 209 } 210 211 223 public Map getContent() 224 { 225 return map; 226 } 227 228 public void setContent(Map content) 229 { 230 this.map = content; 231 } 232 233 235 public Set keySet() 236 { 237 return map.keySet(); 238 } 239 240 public Preference getPreference(String key) 241 { 242 if (key == null) 243 { 244 throw new IllegalArgumentException ("key must not be null"); 245 } 246 return (Preference)map.get(key); 247 } 248 249 public Value getValue(String key) 250 { 251 if (key == null) 252 { 253 throw new IllegalArgumentException ("key must not be null"); 254 } 255 Preference preference = getPreference(key); 256 if (preference != null) 257 { 258 return preference.getValue(); 259 } 260 else 261 { 262 return null; 263 } 264 } 265 266 public void setValue(String key, Value value) 267 { 268 if (key == null) 269 { 270 throw new IllegalArgumentException ("key must not be null"); 271 } 272 if (value == null) 273 { 274 map.remove(key); 275 } 276 else 277 { 278 MappedPreference pref = (MappedPreference)map.get(key); 279 if (pref != null) 280 { 281 pref.setValue(value); 282 } 283 else 284 { 285 map.put(key, newPreference(key, value, false)); 286 } 287 } 288 } 289 290 public boolean isReadOnly(String key) 291 { 292 if (key == null) 293 { 294 throw new IllegalArgumentException ("key must not be null"); 295 } 296 return false; 297 } 298 299 public void setReadOnly(String key, boolean readOnly) 300 { 301 throw new UnsupportedOperationException ("The mapping does not support the read only flag"); 302 } 303 304 306 309 protected MappedPreferenceSet newChild(String name) 310 { 311 return new MappedPreferenceSet(name); 312 } 313 314 317 protected Preference newPreference(String name, Value value, boolean readOnly) 318 { 319 MappedPreference preference = new MappedPreference(name); 320 preference.setValue(value); 321 return preference; 322 } 323 } 324 | Popular Tags |