LockUtil.java
6.62 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.workflow.WorkflowSession
* com.day.cq.workflow.exec.WorkflowData
* javax.jcr.ItemNotFoundException
* javax.jcr.Node
* javax.jcr.PathNotFoundException
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.lock.Lock
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.workflow.impl.process;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkflowData;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.lock.Lock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LockUtil {
private static final Logger log = LoggerFactory.getLogger(LockUtil.class);
private static final String TYPE_JCR_PATH = "JCR_PATH";
private static final String TYPE_JCR_UUID = "JCR_UUID";
private static LockUtil INSTANCE = null;
private LockUtil() {
}
public static LockUtil getInstance() {
if (INSTANCE == null) {
INSTANCE = new LockUtil();
}
return INSTANCE;
}
public void lock(WorkflowData workflowData, WorkflowSession session) {
try {
if (this.isLockable(workflowData, session)) {
if (this.isJCRPathType(workflowData)) {
this.lockJCRPathPayload(workflowData, session);
} else if (this.isUUIDType(workflowData)) {
this.lockUUIDPayload(workflowData, session);
}
}
}
catch (RepositoryException e) {
log.warn(e.getMessage());
}
}
public void unlock(WorkflowData workflowData, WorkflowSession session) {
try {
if (this.isUnlockable(workflowData, session)) {
if (this.isJCRPathType(workflowData)) {
this.unlockJCRPathPayload(workflowData, session);
} else if (this.isUUIDType(workflowData)) {
this.unlockUUIDPayload(workflowData, session);
}
}
}
catch (RepositoryException e) {
log.warn(e.getMessage());
}
}
private void unlockUUIDPayload(WorkflowData workflowData, WorkflowSession session) throws RepositoryException {
Node node = this.getNodeByUUID((String)workflowData.getPayload(), session);
this.unlockNode(node);
}
private void unlockJCRPathPayload(WorkflowData workflowData, WorkflowSession session) throws RepositoryException {
Node node = this.getNodeByPath((String)workflowData.getPayload(), session);
this.unlockNode(node);
}
private void lockUUIDPayload(WorkflowData workflowData, WorkflowSession session) throws RepositoryException {
Node node = this.getNodeByUUID((String)workflowData.getPayload(), session);
this.lockNode(node);
}
private void lockJCRPathPayload(WorkflowData workflowData, WorkflowSession session) throws RepositoryException {
Node node = this.getNodeByPath((String)workflowData.getPayload(), session);
this.lockNode(node);
}
private boolean isUUIDType(WorkflowData workflowData) {
return workflowData.getPayloadType().equals("JCR_UUID");
}
private boolean isJCRPathType(WorkflowData workflowData) {
return workflowData.getPayloadType().equals("JCR_PATH");
}
private boolean isLockable(WorkflowData workflowData, WorkflowSession session) {
try {
if (workflowData.getPayloadType().equals("JCR_PATH")) {
Node node = this.getNodeByPath((String)workflowData.getPayload(), session);
return !this.isLocked(node);
}
if (workflowData.getPayloadType().equals("JCR_UUID")) {
Node node = this.getNodeByUUID((String)workflowData.getPayload(), session);
return !this.isLocked(node);
}
return false;
}
catch (PathNotFoundException e) {
log.debug(e.getMessage());
return false;
}
catch (ItemNotFoundException e) {
log.debug(e.getMessage());
return false;
}
catch (RepositoryException e) {
log.warn(e.getMessage());
return false;
}
}
private boolean isUnlockable(WorkflowData workflowData, WorkflowSession session) {
try {
if (workflowData.getPayloadType().equals("JCR_PATH")) {
Node node = this.getNodeByPath((String)workflowData.getPayload(), session);
return this.isLocked(node);
}
if (workflowData.getPayloadType().equals("JCR_UUID")) {
Node node = this.getNodeByUUID((String)workflowData.getPayload(), session);
return this.isLocked(node);
}
return false;
}
catch (PathNotFoundException e) {
log.debug(e.getMessage());
return false;
}
catch (ItemNotFoundException e) {
log.debug(e.getMessage());
return false;
}
catch (RepositoryException e) {
log.warn(e.getMessage());
return false;
}
}
private Node getNodeByUUID(String uuid, WorkflowSession session) throws RepositoryException {
Node node = session.getSession().getNodeByIdentifier(uuid);
return node;
}
private Node getNodeByPath(String path, WorkflowSession session) throws RepositoryException {
return session.getSession().getNode(path);
}
private boolean isLocked(Node node) throws RepositoryException {
if (this.hasContent(node)) {
return node.getNode("jcr:content").isLocked();
}
return false;
}
private void unlockNode(Node node) throws RepositoryException {
if (this.hasContent(node)) {
node.getNode("jcr:content").unlock();
log.debug("unlocked node: " + node.getPath());
}
}
private void lockNode(Node node) throws RepositoryException {
if (this.hasContent(node)) {
Node content = node.getNode("jcr:content");
if (content.canAddMixin("mix:lockable")) {
content.addMixin("mix:lockable");
content.save();
}
content.lock(true, false);
log.debug("locked node: " + node.getPath());
}
}
private boolean hasContent(Node node) {
try {
return node.hasNode("jcr:content");
}
catch (RepositoryException e) {
return false;
}
}
}