1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.jcr.util;
35
36 import info.magnolia.context.MgnlContext;
37 import info.magnolia.jcr.wrapper.DelegateSessionWrapper;
38
39 import javax.jcr.Node;
40 import javax.jcr.RepositoryException;
41 import javax.jcr.Session;
42
43 import org.apache.commons.lang.StringUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48
49
50
51
52 public class SessionUtil {
53
54 private final static Logger log = LoggerFactory.getLogger(SessionUtil.class);
55
56 public static boolean hasSameUnderlyingSession(Session first, Session second) {
57 return unwrap(first).equals(unwrap(second));
58 }
59
60 public static Session unwrap(Session session) {
61 return session instanceof DelegateSessionWrapper ? ((DelegateSessionWrapper) session).unwrap() : session;
62 }
63
64
65
66
67 public static Node getNode(String repository, String path) {
68 Node res = null;
69 Session session;
70 if (StringUtils.isBlank(repository) || StringUtils.isBlank(path)) {
71 log.debug("getNode returns null because either nodePath: '" + path + "' or repository: '" + repository
72 + "' is empty");
73 return res;
74 }
75 try {
76 session = MgnlContext.getJCRSession(repository);
77 if (session != null) {
78 res = session.getNode(path);
79 }
80 } catch (RepositoryException e) {
81 log.error("Exeption during node Search for nodePath: '" + path + "' in repository: '" + repository + "'", e);
82 }
83 return res;
84 }
85
86
87
88
89 public static Node getNodeByIdentifier(String repository, String id) {
90 Node res = null;
91 Session session;
92 if (StringUtils.isBlank(repository) || StringUtils.isBlank(id)) {
93 log.debug("getNode returns null because either identifier: '" + id + "' or repository: '" + repository
94 + "' is empty");
95 return res;
96 }
97 try {
98 session = MgnlContext.getJCRSession(repository);
99 if (session != null) {
100 res = session.getNodeByIdentifier(id);
101 }
102 } catch (RepositoryException e) {
103 log.error("Exeption during node Search by identifier: '" + id + "' in repository: '" + repository + "'", e);
104 }
105 return res;
106 }
107 }