]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.jcr/src/org/argeo/jcr/Jcr.java
Use Argeo TP Core v2.1.25 and Argeo TP Extras v2.1.13.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / Jcr.java
1 package org.argeo.jcr;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import javax.jcr.ItemNotFoundException;
8 import javax.jcr.Node;
9 import javax.jcr.NodeIterator;
10 import javax.jcr.Property;
11 import javax.jcr.PropertyType;
12 import javax.jcr.Repository;
13 import javax.jcr.RepositoryException;
14 import javax.jcr.Session;
15 import javax.jcr.Value;
16
17 /**
18 * Utility class whose purpose is to make using JCR less verbose by
19 * systematically using unchecked exceptions and returning <code>null</code>
20 * when something is not found. This is especially useful when writing user
21 * interfaces (such as with SWT) where listeners and callbacks expect unchecked
22 * exceptions. Loosely inspired by Java's <code>Files</code> singleton.
23 */
24 public class Jcr {
25
26 /**
27 * @see Node#isNodeType(String)
28 * @throws IllegalStateException caused by {@link RepositoryException}
29 */
30 public static boolean isNodeType(Node node, String nodeTypeName) {
31 try {
32 return node.isNodeType(nodeTypeName);
33 } catch (RepositoryException e) {
34 throw new IllegalStateException("Cannot get whether " + node + " is of type " + nodeTypeName, e);
35 }
36 }
37
38 /**
39 * @see Node#hasNodes()
40 * @throws IllegalStateException caused by {@link RepositoryException}
41 */
42 public static boolean hasNodes(Node node) {
43 try {
44 return node.hasNodes();
45 } catch (RepositoryException e) {
46 throw new IllegalStateException("Cannot get whether " + node + " has children.", e);
47 }
48 }
49
50 /**
51 * @see Node#getParent()
52 * @throws IllegalStateException caused by {@link RepositoryException}
53 */
54 public static Node getParent(Node node) {
55 try {
56 return isRoot(node) ? null : node.getParent();
57 } catch (RepositoryException e) {
58 throw new IllegalStateException("Cannot get parent of " + node, e);
59 }
60 }
61
62 /**
63 * Whether this node is the root node.
64 *
65 * @throws IllegalStateException caused by {@link RepositoryException}
66 */
67 public static boolean isRoot(Node node) {
68 try {
69 return node.getDepth() == 0;
70 } catch (RepositoryException e) {
71 throw new IllegalStateException("Cannot get depth of " + node, e);
72 }
73 }
74
75 /**
76 * @see Node#getPath()
77 * @throws IllegalStateException caused by {@link RepositoryException}
78 */
79 public static String getPath(Node node) {
80 try {
81 return node.getPath();
82 } catch (RepositoryException e) {
83 throw new IllegalStateException("Cannot get path of " + node, e);
84 }
85 }
86
87 /**
88 * @see Node#getIdentifier()
89 * @throws IllegalStateException caused by {@link RepositoryException}
90 */
91 public static String getIdentifier(Node node) {
92 try {
93 return node.getIdentifier();
94 } catch (RepositoryException e) {
95 throw new IllegalStateException("Cannot get identifier of " + node, e);
96 }
97 }
98
99 /**
100 * @see Node#getName()
101 * @throws IllegalStateException caused by {@link RepositoryException}
102 */
103 public static String getName(Node node) {
104 try {
105 return node.getName();
106 } catch (RepositoryException e) {
107 throw new IllegalStateException("Cannot get name of " + node, e);
108 }
109 }
110
111 /** Accesses a {@link NodeIterator} as an {@link Iterable}. */
112 @SuppressWarnings("unchecked")
113 public static Iterable<Node> iterate(NodeIterator nodeIterator) {
114 return new Iterable<Node>() {
115
116 @Override
117 public Iterator<Node> iterator() {
118 return nodeIterator;
119 }
120 };
121 }
122
123 /**
124 * @return the children as an {@link Iterable} for use in for-each llops.
125 * @see Node#getNodes()
126 * @throws IllegalStateException caused by {@link RepositoryException}
127 */
128 public static Iterable<Node> nodes(Node node) {
129 try {
130 return iterate(node.getNodes());
131 } catch (RepositoryException e) {
132 throw new IllegalStateException("Cannot get children of " + node, e);
133 }
134 }
135
136 /**
137 * @return the children as a (possibly empty) {@link List}.
138 * @see Node#getNodes()
139 * @throws IllegalStateException caused by {@link RepositoryException}
140 */
141 public static List<Node> getNodes(Node node) {
142 List<Node> nodes = new ArrayList<>();
143 try {
144 if (node.hasNodes()) {
145 NodeIterator nit = node.getNodes();
146 while (nit.hasNext())
147 nodes.add(nit.nextNode());
148 return nodes;
149 } else
150 return nodes;
151 } catch (RepositoryException e) {
152 throw new IllegalStateException("Cannot get children of " + node, e);
153 }
154 }
155
156 /**
157 * @return the child or <code>null</node> if not found
158 * @see Node#getNode(String)
159 * @throws IllegalStateException caused by {@link RepositoryException}
160 */
161 public static Node getNode(Node node, String child) {
162 try {
163 if (node.hasNode(child))
164 return node.getNode(child);
165 else
166 return null;
167 } catch (RepositoryException e) {
168 throw new IllegalStateException("Cannot get child of " + node, e);
169 }
170 }
171
172 /**
173 * @return the node at this path or <code>null</node> if not found
174 * @see Session#getNode(String)
175 * @throws IllegalStateException caused by {@link RepositoryException}
176 */
177 public static Node getNode(Session session, String path) {
178 try {
179 if (session.nodeExists(path))
180 return session.getNode(path);
181 else
182 return null;
183 } catch (RepositoryException e) {
184 throw new IllegalStateException("Cannot get node " + path, e);
185 }
186 }
187
188 /**
189 * @return the node with htis id or <code>null</node> if not found
190 * @see Session#getNodeByIdentifier(String)
191 * @throws IllegalStateException caused by {@link RepositoryException}
192 */
193 public static Node getNodeById(Session session, String id) {
194 try {
195 return session.getNodeByIdentifier(id);
196 } catch (ItemNotFoundException e) {
197 return null;
198 } catch (RepositoryException e) {
199 throw new IllegalStateException("Cannot get node with id " + id, e);
200 }
201 }
202
203 /**
204 * Get property as {@link String}.
205 *
206 * @return the value of
207 * {@link Node#getProperty(String)}.{@link Property#getString()} or
208 * <code>null</code> if the property does not exist.
209 * @throws IllegalStateException caused by {@link RepositoryException}
210 */
211 public static String get(Node node, String property) {
212 return get(node, property, null);
213 }
214
215 /**
216 * Get property as a {@link String}.
217 *
218 * @return the value of
219 * {@link Node#getProperty(String)}.{@link Property#getString()} or
220 * <code>defaultValue</code> if the property does not exist.
221 * @throws IllegalStateException caused by {@link RepositoryException}
222 */
223 public static String get(Node node, String property, String defaultValue) {
224 try {
225 if (node.hasProperty(property))
226 return node.getProperty(property).getString();
227 else
228 return defaultValue;
229 } catch (RepositoryException e) {
230 throw new IllegalStateException("Cannot retrieve property " + property + " from " + node);
231 }
232 }
233
234 /**
235 * Get property as a {@link Value}.
236 *
237 * @return {@link Node#getProperty(String)} or <code>null</code> if the property
238 * does not exist.
239 * @throws IllegalStateException caused by {@link RepositoryException}
240 */
241 public static Value getValue(Node node, String property) {
242 try {
243 if (node.hasProperty(property))
244 return node.getProperty(property).getValue();
245 else
246 return null;
247 } catch (RepositoryException e) {
248 throw new IllegalStateException("Cannot retrieve property " + property + " from " + node);
249 }
250 }
251
252 /**
253 * Get property doing a best effort to cast it as the target object.
254 *
255 * @return the value of {@link Node#getProperty(String)} or
256 * <code>defaultValue</code> if the property does not exist.
257 * @throws IllegalArgumentException if the value could not be cast
258 * @throws IllegalStateException in case of unexpected
259 * {@link RepositoryException}
260 */
261 @SuppressWarnings("unchecked")
262 public static <T> T getAs(Node node, String property, T defaultValue) {
263 try {
264 if (node.hasProperty(property)) {
265 Property p = node.getProperty(property);
266 try {
267 switch (p.getType()) {
268 case PropertyType.STRING:
269 return (T) node.getProperty(property).getString();
270 case PropertyType.DOUBLE:
271 return (T) (Double) node.getProperty(property).getDouble();
272 case PropertyType.LONG:
273 return (T) (Long) node.getProperty(property).getLong();
274 case PropertyType.BOOLEAN:
275 return (T) (Boolean) node.getProperty(property).getBoolean();
276 case PropertyType.DATE:
277 return (T) node.getProperty(property).getDate();
278 default:
279 return (T) node.getProperty(property).getString();
280 }
281 } catch (ClassCastException e) {
282 throw new IllegalArgumentException(
283 "Cannot cast property of type " + PropertyType.nameFromValue(p.getType()), e);
284 }
285 } else {
286 return defaultValue;
287 }
288 } catch (RepositoryException e) {
289 throw new IllegalStateException("Cannot retrieve property " + property + " from " + node);
290 }
291 }
292
293 /** Login to a JCR repository. */
294 public static Session login(Repository repository, String workspace) {
295 try {
296 return repository.login(workspace);
297 } catch (RepositoryException e) {
298 throw new IllegalArgumentException("Cannot login to repository", e);
299 }
300 }
301
302 /** Safely and silently logs out a session. */
303 public static void logout(Session session) {
304 try {
305 if (session != null)
306 if (session.isLive())
307 session.logout();
308 } catch (Exception e) {
309 // silent
310 }
311 }
312
313 /** Singleton. */
314 private Jcr() {
315
316 }
317 }