1 package org.jahia.services.usermanager; 2 3 import java.util.Map ; 4 import java.util.HashMap ; 5 import java.util.Properties ; 6 import java.util.Iterator ; 7 import java.util.Enumeration ; 8 import java.io.Serializable ; 9 10 16 public class UserProperties implements Serializable { 17 18 private Map properties = new HashMap (); 19 20 public UserProperties () { 21 } 22 23 30 public UserProperties(Properties properties, boolean readOnly) { 31 Enumeration sourceNameEnum = properties.propertyNames(); 32 while (sourceNameEnum.hasMoreElements()) { 33 String curSourceName = (String ) sourceNameEnum.nextElement(); 34 UserProperty curUserProperty = new UserProperty(curSourceName, properties.getProperty(curSourceName), readOnly); 35 this.properties.put(curSourceName, curUserProperty); 36 } 37 } 38 39 protected UserProperties(UserProperties copy) { 40 Iterator entryIter = copy.properties.entrySet().iterator(); 41 while (entryIter.hasNext()) { 42 Map.Entry curEntry = (Map.Entry ) entryIter.next(); 43 UserProperty curUserProperty = (UserProperty) curEntry.getValue(); 44 properties.put(curUserProperty.getName(), curUserProperty.clone()); 45 } 46 } 47 48 public Object clone() { 49 UserProperties clonedProps = new UserProperties(this); 50 return clonedProps; 51 } 52 53 public void putAll(UserProperties ups) { 54 properties.putAll(ups.properties); 55 } 56 57 public UserProperty getUserProperty(String name) { 58 return (UserProperty) properties.get(name); 59 } 60 61 public void setUserProperty(String name, UserProperty value) { 62 properties.put(name, value); 63 } 64 65 public UserProperty removeUserProperty(String name) { 66 return (UserProperty) properties.remove(name); 67 } 68 69 75 public boolean isReadOnly(String name) { 76 UserProperty userProperty = (UserProperty) properties.get(name); 77 if (userProperty == null) { 78 return false; 79 } 80 return userProperty.isReadOnly(); 81 } 82 83 public Iterator propertyNameIterator() { 84 return properties.keySet().iterator(); 85 } 86 87 public Properties getProperties() { 88 Properties propertiesCopy = new Properties (); 89 Iterator entryIter = properties.entrySet().iterator(); 90 while (entryIter.hasNext()) { 91 Map.Entry curEntry = (Map.Entry ) entryIter.next(); 92 UserProperty curUserProperty = (UserProperty) curEntry.getValue(); 93 propertiesCopy.put(curUserProperty.getName(), curUserProperty.getValue()); 94 } 95 return propertiesCopy; 96 } 97 98 public String getProperty(String name) { 99 UserProperty userProperty = (UserProperty) properties.get(name); 100 if (userProperty == null) { 101 return null; 102 } 103 return userProperty.getValue(); 104 } 105 106 public void setProperty(String name, String value) 107 throws UserPropertyReadOnlyException { 108 UserProperty userProperty = (UserProperty) properties.get(name); 109 if (userProperty != null) { 110 if (userProperty.isReadOnly()) { 111 throw new UserPropertyReadOnlyException(userProperty, "Property " + name + " is readonly"); 112 } 113 userProperty.setValue(value); 114 } else { 115 userProperty = new UserProperty(name, value, false); 116 } 117 properties.put(name, userProperty); 118 } 119 120 public String removeProperty(String name) 121 throws UserPropertyReadOnlyException { 122 UserProperty userProperty = (UserProperty) properties.get(name); 123 if (userProperty != null) { 124 if (userProperty.isReadOnly()) { 125 throw new UserPropertyReadOnlyException(userProperty, 126 "Property " + name + " is readonly"); 127 } else { 128 return userProperty.getValue(); 129 } 130 } 131 return null; 132 } 133 } 134 | Popular Tags |