KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > usermanager > UserProperties


1 package org.jahia.services.usermanager;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Properties JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.io.Serializable JavaDoc;
9
10 /**
11  * This class stores user properties, which are different from regular
12  * java.util.Properties because they can also store information about read-only
13  * state as well as the original provider of the properties.
14  * @author Serge Huber
15  */

16 public class UserProperties implements Serializable JavaDoc {
17
18     private Map JavaDoc properties = new HashMap JavaDoc();
19
20     public UserProperties () {
21     }
22
23     /**
24      * Copy constructor. All properties copied from will be marked as read-write
25      * Be very careful when using this method or you will loose readOnly tagging
26      * @param properties Properties
27      * @param readOnly specifies whether the copied properties should be marked
28      * as read-only or not.
29      */

30     public UserProperties(Properties JavaDoc properties, boolean readOnly) {
31         Enumeration JavaDoc sourceNameEnum = properties.propertyNames();
32         while (sourceNameEnum.hasMoreElements()) {
33             String JavaDoc curSourceName = (String JavaDoc) 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 JavaDoc entryIter = copy.properties.entrySet().iterator();
41         while (entryIter.hasNext()) {
42             Map.Entry JavaDoc curEntry = (Map.Entry JavaDoc) entryIter.next();
43             UserProperty curUserProperty = (UserProperty) curEntry.getValue();
44             properties.put(curUserProperty.getName(), curUserProperty.clone());
45         }
46     }
47
48     public Object JavaDoc 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 JavaDoc name) {
58         return (UserProperty) properties.get(name);
59     }
60
61     public void setUserProperty(String JavaDoc name, UserProperty value) {
62         properties.put(name, value);
63     }
64
65     public UserProperty removeUserProperty(String JavaDoc name) {
66         return (UserProperty) properties.remove(name);
67     }
68
69     /**
70      * Tests if a property is read-only.
71      * Warning : this method also returns false if the property doesn't exist !
72      * @param name String
73      * @return boolean
74      */

75     public boolean isReadOnly(String JavaDoc 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 JavaDoc propertyNameIterator() {
84         return properties.keySet().iterator();
85     }
86
87     public Properties JavaDoc getProperties() {
88         Properties JavaDoc propertiesCopy = new Properties JavaDoc();
89         Iterator JavaDoc entryIter = properties.entrySet().iterator();
90         while (entryIter.hasNext()) {
91             Map.Entry JavaDoc curEntry = (Map.Entry JavaDoc) entryIter.next();
92             UserProperty curUserProperty = (UserProperty) curEntry.getValue();
93             propertiesCopy.put(curUserProperty.getName(), curUserProperty.getValue());
94         }
95         return propertiesCopy;
96     }
97
98     public String JavaDoc getProperty(String JavaDoc 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 JavaDoc name, String JavaDoc 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 JavaDoc removeProperty(String JavaDoc 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