1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.prefs.impl; |
18 |
|
|
19 |
|
import java.sql.Timestamp; |
20 |
|
import java.util.ArrayList; |
21 |
|
import java.util.Collection; |
22 |
|
import java.util.Iterator; |
23 |
|
import java.util.prefs.AbstractPreferences; |
24 |
|
import java.util.prefs.BackingStoreException; |
25 |
|
import java.util.prefs.Preferences; |
26 |
|
|
27 |
|
import org.apache.commons.logging.Log; |
28 |
|
import org.apache.commons.logging.LogFactory; |
29 |
|
import org.apache.jetspeed.prefs.FailedToCreateNodeException; |
30 |
|
import org.apache.jetspeed.prefs.NodeAlreadyExistsException; |
31 |
|
import org.apache.jetspeed.prefs.NodeDoesNotExistException; |
32 |
|
import org.apache.jetspeed.prefs.PreferencesProvider; |
33 |
|
import org.apache.jetspeed.prefs.om.Node; |
34 |
|
import org.apache.jetspeed.prefs.om.Property; |
35 |
|
import org.apache.jetspeed.prefs.om.impl.PropertyImpl; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
public class PreferencesImpl extends AbstractPreferences |
46 |
|
{ |
47 |
|
|
48 |
|
|
49 |
|
public static final int USER_NODE_TYPE = 0; |
50 |
|
|
51 |
|
|
52 |
|
public static final int SYSTEM_NODE_TYPE = 1; |
53 |
|
|
54 |
|
|
55 |
0 |
private Node node = null; |
56 |
|
|
57 |
|
|
58 |
0 |
private static final Log log = LogFactory.getLog(PreferencesImpl.class); |
59 |
|
|
60 |
|
protected static PreferencesProvider prefsProvider; |
61 |
|
|
62 |
|
static PreferencesImpl systemRoot; |
63 |
|
|
64 |
|
static PreferencesImpl userRoot; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
public PreferencesImpl(PreferencesImpl parent, String nodeName, int nodeType) throws IllegalStateException |
80 |
|
{ |
81 |
0 |
super(parent, nodeName); |
82 |
|
|
83 |
|
try |
84 |
|
{ |
85 |
0 |
if (parent != null) |
86 |
|
{ |
87 |
0 |
this.node = prefsProvider.createNode(parent.getNode(), nodeName, nodeType, class="keyword">this.absolutePath()); |
88 |
|
} |
89 |
|
else |
90 |
|
{ |
91 |
0 |
this.node = prefsProvider.createNode(null, nodeName, nodeType, class="keyword">this.absolutePath()); |
92 |
|
} |
93 |
|
|
94 |
0 |
newNode = true; |
95 |
|
} |
96 |
0 |
catch (FailedToCreateNodeException e) |
97 |
|
{ |
98 |
0 |
IllegalStateException ise = new IllegalStateException("Failed to create new Preferences of type " |
99 |
|
+ nodeType + " for path " + this.absolutePath()); |
100 |
0 |
ise.initCause(e); |
101 |
0 |
throw ise; |
102 |
|
} |
103 |
0 |
catch (NodeAlreadyExistsException e) |
104 |
|
{ |
105 |
|
try |
106 |
|
{ |
107 |
0 |
node = prefsProvider.getNode(this.absolutePath(), nodeType); |
108 |
0 |
newNode = false; |
109 |
|
} |
110 |
0 |
catch (NodeDoesNotExistException e1) |
111 |
|
{ |
112 |
|
|
113 |
0 |
IllegalStateException ise = new IllegalStateException( |
114 |
|
"Unable to create node for Preferences of type " |
115 |
|
+ nodeType |
116 |
|
+ " for path " |
117 |
|
+ this.absolutePath() |
118 |
|
+ ". If you see this exception at this, it more than likely means that the Preferences backing store is corrupt."); |
119 |
0 |
ise.initCause(e1); |
120 |
0 |
throw ise; |
121 |
0 |
} |
122 |
0 |
} |
123 |
|
|
124 |
0 |
} |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
public String[] childrenNamesSpi() throws BackingStoreException |
130 |
|
{ |
131 |
0 |
Collection nodes = prefsProvider.getChildren(getNode()); |
132 |
|
|
133 |
0 |
if (null != nodes) |
134 |
|
{ |
135 |
0 |
ArrayList childrenNames = new ArrayList(nodes.size()); |
136 |
0 |
for (Iterator i = nodes.iterator(); i.hasNext();) |
137 |
|
{ |
138 |
0 |
Node curnode = (Node) i.next(); |
139 |
0 |
childrenNames.add(curnode.getNodeName()); |
140 |
0 |
} |
141 |
0 |
return (String[]) childrenNames.toArray(new String[0]); |
142 |
|
} |
143 |
|
else |
144 |
|
{ |
145 |
|
|
146 |
|
|
147 |
0 |
return new String[0]; |
148 |
|
} |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
public AbstractPreferences childSpi(String name) |
155 |
|
{ |
156 |
0 |
return new PreferencesImpl(this, name, node.getNodeType()); |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
public void flushSpi() throws BackingStoreException |
163 |
|
{ |
164 |
0 |
prefsProvider.storeNode(this.node); |
165 |
0 |
} |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
public String getSpi(String key) |
171 |
|
{ |
172 |
0 |
String value = null; |
173 |
0 |
Collection properties = node.getNodeProperties(); |
174 |
|
|
175 |
0 |
for (Iterator i = properties.iterator(); i.hasNext();) |
176 |
|
{ |
177 |
0 |
Property curProp = (Property) i.next(); |
178 |
0 |
if ((null != curProp) && (class="keyword">null != curProp.getPropertyName()) && (curProp.getPropertyName().equals(key))) |
179 |
|
{ |
180 |
0 |
value = curProp.getPropertyValue(); |
181 |
|
} |
182 |
0 |
} |
183 |
0 |
return value; |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
public String[] keysSpi() |
190 |
|
{ |
191 |
0 |
ArrayList propertyNames = new ArrayList(); |
192 |
|
|
193 |
0 |
Collection propCol = node.getNodeProperties(); |
194 |
0 |
if ((null != propCol) && propCol.size() > 0) |
195 |
|
{ |
196 |
0 |
for (Iterator j = propCol.iterator(); j.hasNext();) |
197 |
|
{ |
198 |
0 |
Property curprop = (Property) j.next(); |
199 |
0 |
if ((null != curprop) && (class="keyword">null != curprop.getPropertyName()) |
200 |
|
&& !propertyNames.contains(curprop.getPropertyName())) |
201 |
|
{ |
202 |
0 |
propertyNames.add(curprop.getPropertyName()); |
203 |
|
} |
204 |
0 |
} |
205 |
|
} |
206 |
|
|
207 |
0 |
return (String[]) propertyNames.toArray(new String[propertyNames.size()]); |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
public void putSpi(String key, String value) |
215 |
|
{ |
216 |
0 |
Collection properties = node.getNodeProperties(); |
217 |
0 |
if (null == properties) |
218 |
|
{ |
219 |
0 |
log.error("Could not retrieve node property: [key: " + key + ", value:" + value + "]"); |
220 |
0 |
return; |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
0 |
boolean propFound = false; |
225 |
0 |
for (Iterator i = properties.iterator(); i.hasNext();) |
226 |
|
{ |
227 |
0 |
Property curProp = (Property) i.next(); |
228 |
0 |
if ((null != curProp) && (class="keyword">null != curProp.getPropertyName()) && curProp.getPropertyName().equals(key)) |
229 |
|
{ |
230 |
0 |
propFound = true; |
231 |
0 |
curProp.setPropertyValue(value); |
232 |
0 |
curProp.setModifiedDate(new Timestamp(System.currentTimeMillis())); |
233 |
0 |
if (log.isDebugEnabled()) |
234 |
|
{ |
235 |
0 |
log.debug("Update existing property: " + curProp.toString()); |
236 |
|
} |
237 |
|
|
238 |
|
break; |
239 |
|
} |
240 |
0 |
} |
241 |
0 |
if (!propFound) |
242 |
|
{ |
243 |
0 |
properties.add(new PropertyImpl(node.getNodeId(), key, value)); |
244 |
|
} |
245 |
|
|
246 |
0 |
prefsProvider.storeNode(node); |
247 |
0 |
} |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
public void removeNodeSpi() throws BackingStoreException |
253 |
|
{ |
254 |
0 |
Node parentNode = null; |
255 |
0 |
Preferences parent = parent(); |
256 |
0 |
if (parent != null && parent instanceof PreferencesImpl) |
257 |
|
{ |
258 |
0 |
parentNode = ((PreferencesImpl) parent).getNode(); |
259 |
|
} |
260 |
0 |
prefsProvider.removeNode(parentNode, node); |
261 |
0 |
} |
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
public void removeSpi(String key) |
267 |
|
{ |
268 |
0 |
Collection properties = node.getNodeProperties(); |
269 |
|
|
270 |
0 |
for (Iterator i = properties.iterator(); i.hasNext();) |
271 |
|
{ |
272 |
0 |
Property curProp = (Property) i.next(); |
273 |
|
|
274 |
0 |
if ((curProp.getPropertyName().equals(key))) |
275 |
|
{ |
276 |
0 |
i.remove(); |
277 |
|
} |
278 |
0 |
} |
279 |
|
|
280 |
0 |
prefsProvider.storeNode(node); |
281 |
0 |
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
public void syncSpi() throws BackingStoreException |
287 |
|
{ |
288 |
0 |
flushSpi(); |
289 |
0 |
} |
290 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
public Node getNode() |
300 |
|
{ |
301 |
0 |
return node; |
302 |
|
} |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
public static void setPreferencesProvider(PreferencesProvider prefsProvider) |
316 |
|
{ |
317 |
0 |
PreferencesImpl.prefsProvider = prefsProvider; |
318 |
0 |
} |
319 |
|
} |