StatusNode.java
3.18 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.compat.codeupgrade.impl;
import com.day.cq.commons.jcr.JcrUtil;
import java.util.Calendar;
import java.util.Date;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class StatusNode {
private final Logger log;
private final String statusNodePath;
private final String statusPropertyName;
private final String statusPropertyPath;
public StatusNode(String path, String propertyName) {
this.log = LoggerFactory.getLogger(this.getClass());
this.statusNodePath = path;
this.statusPropertyName = propertyName;
this.statusPropertyPath = path + "/" + propertyName;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void recordUpgrade(SlingRepository repository) throws RepositoryException {
Session s = repository.loginService("compat-codeupgrade", null);
try {
Node n = null;
if (s.itemExists(this.statusNodePath)) {
Item i = s.getItem(this.statusNodePath);
if (!i.isNode()) {
throw new IllegalStateException(i.getPath() + " is not a Node");
}
n = (Node)i;
} else {
n = JcrUtil.createPath((String)this.statusNodePath, (String)"sling:Folder", (Session)s);
}
Calendar now = Calendar.getInstance();
n.setProperty(this.statusPropertyName, now);
s.save();
this.log.info("Upgrade recorded, {} set to {}", (Object)this.statusPropertyPath, (Object)now.getTime());
}
finally {
s.logout();
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public boolean upgradeNeeded(SlingRepository repository) {
Session s = null;
boolean result = false;
try {
s = repository.loginService("compat-codeupgrade", null);
boolean exists = s.itemExists(this.statusPropertyPath);
if (this.log.isInfoEnabled()) {
if (exists) {
this.log.info("{} exists, upgrade already executed", (Object)this.statusPropertyPath);
} else {
this.log.info("{} does not exist, upgrade needed", (Object)this.statusPropertyPath);
}
}
result = !exists;
}
catch (Exception e) {
this.log.warn("RepositoryException in upgradeNeeded(" + this.statusPropertyPath + ")", (Throwable)e);
}
finally {
s.logout();
}
return result;
}
public String getStatusPropertyPath() {
return this.statusPropertyPath;
}
}