BackupMerger.java
8.47 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.sling.jcr.resource.JcrResourceUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.upgrades.backup.impl;
import com.adobe.cq.upgrades.backup.BackupConfig;
import com.adobe.cq.upgrades.backup.UpgradeBackupMerger;
import com.adobe.cq.upgrades.backup.impl.U;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.jcr.resource.JcrResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BackupMerger {
private static final String MIXIN_REP_ACCESS_CONTROLLABLE = "rep:AccessControllable";
private static final String PATH_REP_POLICY = "/rep:policy";
private final Logger log;
private final String backupRootPath;
private final UpgradeBackupMerger.ProgressInfo progressInfo;
private final BackupConfig backupConfig;
public BackupMerger(String backupRootPath, BackupConfig backupConfig, UpgradeBackupMerger.ProgressInfo progressInfo) {
this.log = LoggerFactory.getLogger(this.getClass());
this.backupRootPath = backupRootPath;
this.backupConfig = backupConfig;
this.progressInfo = progressInfo;
}
private void safeMove(Session s, String src, String dest) {
try {
Node parent;
String parentPath;
if (dest.endsWith("/rep:policy") && (parent = s.getNode(parentPath = dest.substring(0, dest.length() - "/rep:policy".length()))).canAddMixin("rep:AccessControllable")) {
parent.addMixin("rep:AccessControllable");
}
s.move(src, dest);
}
catch (Exception e) {
this.log.warn("Failed to move {} to {}, source node won't be merged", new Object[]{src, dest, e});
}
}
protected void setProgressInfo(String message) {
this.progressInfo.info(message);
this.log.info(message);
}
public void execute(Session sess) throws Exception {
if (!sess.nodeExists(this.backupRootPath)) {
this.setProgressInfo(this.backupRootPath + " does not exist, no content to merge");
return;
}
ArrayList<Node> matchingNodes = new ArrayList<Node>();
Node mainRoot = sess.getNode(this.backupRootPath);
NodeIterator it = mainRoot.getNodes();
while (it.hasNext()) {
Node child = it.nextNode();
if (!child.hasNode("to-process")) continue;
matchingNodes.add(child.getNode("to-process"));
}
if (matchingNodes.size() == 0) {
this.log.info("No backup node matching {} found under {}, no content to upgrade", (Object)"to-process", (Object)mainRoot.getPath());
return;
}
if (matchingNodes.size() > 1) {
Collections.sort(matchingNodes, new Comparator<Node>(){
@Override
public int compare(Node x, Node y) {
return y.toString().compareTo(x.toString());
}
});
}
Node backupRoot = (Node)matchingNodes.get(0);
String overwrittenRootPath = backupRoot.getParent().getPath() + "/" + "overwritten";
String leftoversRootPath = backupRoot.getParent().getPath() + "/" + "leftovers";
NodeIterator it2 = backupRoot.getNodes();
while (it2.hasNext()) {
Node n = it2.nextNode();
String restoreRootPath = n.getPath().substring(backupRoot.getPath().length());
this.restore(n, restoreRootPath, overwrittenRootPath, leftoversRootPath);
}
}
private void restore(Node backupRoot, String restoreRootPath, String overwrittenPath, String leftoversPath) throws RepositoryException {
Session sess = backupRoot.getSession();
Node restoreRoot = sess.getNode(restoreRootPath);
this.setProgressInfo("Moving " + backupRoot.getPath() + " back under " + restoreRoot.getPath());
AtomicInteger overwrittenCount = new AtomicInteger();
JcrResourceUtil.createPath((String)overwrittenPath, (String)"nt:unstructured", (String)"nt:unstructured", (Session)sess, (boolean)false);
this.moveOverwritesBack(backupRoot, backupRoot, restoreRoot, overwrittenPath, overwrittenCount);
this.log.info("{} nodes restored in overwrite mode", (Object)overwrittenCount.get());
AtomicInteger counter = new AtomicInteger();
this.moveChildrenBack(backupRoot, backupRoot, restoreRoot, counter);
sess.save();
this.setProgressInfo(counter + " backup nodes processed");
String dest = leftoversPath + restoreRoot.getPath();
JcrResourceUtil.createPath((String)U.getParentPath(dest, 1), (String)"nt:unstructured", (String)"nt:unstructured", (Session)sess, (boolean)false);
this.safeMove(sess, backupRoot.getPath(), dest);
this.setProgressInfo("Remaining backup nodes moved under " + dest);
sess.save();
}
private void moveOverwritesBack(Node src, Node backupRoot, Node destRoot, String overwrittenPath, AtomicInteger counter) throws RepositoryException {
Set<String> pathsToOverwrite = this.backupConfig.getPathsToOverwrite();
String destPath = this.getDestPath(src, backupRoot, destRoot);
if (destPath != null && pathsToOverwrite.contains(destPath)) {
String saveOverwrittenPath = overwrittenPath + destPath;
Session s = src.getSession();
if (s.nodeExists(destPath)) {
this.log.info("Moving {} under {} - will be overwritten by backed up content", (Object)destPath, (Object)saveOverwrittenPath);
String destFolder = U.getParentPath(saveOverwrittenPath, 1);
JcrResourceUtil.createPath((String)destFolder, (String)"nt:unstructured", (String)"nt:unstructured", (Session)s, (boolean)false);
this.safeMove(s, destPath, saveOverwrittenPath);
s.save();
}
this.log.info("Moving {} under {} - overwrites old content", (Object)src.getPath(), (Object)destPath);
String destParentPath = U.getParentPath(destPath, 1);
if (!s.nodeExists(destParentPath)) {
this.log.info("Destination parent {} does not exist, creating it as nt:unstructured", (Object)destParentPath);
JcrResourceUtil.createPath((String)destParentPath, (String)"nt:unstructured", (String)"nt:unstructured", (Session)s, (boolean)false);
}
this.safeMove(s, src.getPath(), destPath);
s.save();
counter.incrementAndGet();
} else {
NodeIterator it = src.getNodes();
while (it.hasNext()) {
this.moveOverwritesBack(it.nextNode(), backupRoot, destRoot, overwrittenPath, counter);
}
}
}
private void moveChildrenBack(Node src, Node backupRoot, Node destRoot, AtomicInteger counter) throws RepositoryException {
NodeIterator it = src.getNodes();
while (it.hasNext()) {
Node child = it.nextNode();
String destPath = this.getDestPath(child, backupRoot, destRoot);
counter.incrementAndGet();
if (child.getSession().nodeExists(destPath)) {
this.log.info("Destination {} exists, backup node ignored: {}", (Object)destPath, (Object)child.getPath());
this.moveChildrenBack(child, backupRoot, destRoot, counter);
} else {
this.log.info("Moving {} back to {}", (Object)child.getPath(), (Object)destPath);
this.safeMove(child.getSession(), child.getPath(), destPath);
}
if (counter.get() % 10 != 0) continue;
src.getSession().save();
this.setProgressInfo(counter + " backup nodes processed");
}
}
private String getDestPath(Node src, Node backupRoot, Node destRoot) throws RepositoryException {
try {
return destRoot.getPath() + src.getPath().substring(backupRoot.getPath().length());
}
catch (StringIndexOutOfBoundsException oob) {
this.log.warn("Cannot compute destination path for {} with backup root {} and destination root {}", new Object[]{src.getPath(), backupRoot.getPath(), destRoot.getPath()});
return null;
}
}
}