BackupConfigImpl.java
5.88 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFormatException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.upgrades.backup.impl;
import com.adobe.cq.upgrades.backup.BackupConfig;
import com.day.cq.commons.jcr.JcrUtil;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
public class BackupConfigImpl
implements BackupConfig {
private final Logger log;
private Set<String> pathsToBackup;
private Set<String> pathsToOverwrite;
static final int CONFIG_VERSION_NUMBER = 61000;
static final String CONFIG_BACKUP_PATH = "/var/upgrade/PreUpgradeBackup/configuration" + "-old-" + 61000;
static final String DEFAULT_CONFIG_BACKUP_PATH = "/var/upgrade/PreUpgradeBackup" + "/default-config-" + 61000;
public BackupConfigImpl() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Override
public synchronized void readConfig(Session s) throws RepositoryException {
this.pathsToBackup = new HashSet<String>();
this.pathsToOverwrite = new HashSet<String>();
String configSavePath = "/var/upgrade/PreUpgradeBackup/configuration";
boolean isAlreadySaved = false;
if (s.nodeExists("/var/upgrade/PreUpgradeBackup/configuration")) {
Node configRoot = s.getNode("/var/upgrade/PreUpgradeBackup/configuration");
try {
if (configRoot.hasProperty("ignoreDefaultConfig") && configRoot.getProperty("ignoreDefaultConfig").getBoolean()) {
configSavePath = DEFAULT_CONFIG_BACKUP_PATH;
if (s.itemExists(configSavePath)) {
isAlreadySaved = true;
}
} else if (s.itemExists(CONFIG_BACKUP_PATH)) {
isAlreadySaved = true;
} else {
this.backupConfig(s, CONFIG_BACKUP_PATH);
}
}
catch (ValueFormatException ex) {
this.log.warn("Found property {} but could not cast to boolean.", (Object)"ignoreDefaultConfig");
}
}
if (!isAlreadySaved) {
this.createDefaultConfig(s, configSavePath);
}
if (!s.nodeExists("/var/upgrade/PreUpgradeBackup/configuration")) {
throw new IllegalStateException("Config node not found: /var/upgrade/PreUpgradeBackup/configuration");
}
this.log.info("Only paths found under {} will be backed up", (Object)"/var/upgrade/PreUpgradeBackup/configuration");
Node n = s.getNode("/var/upgrade/PreUpgradeBackup/configuration");
this.readConfig(n, n);
}
private static String configPath(String rootPath, String path) {
return rootPath + path;
}
private void createDefaultConfig(Session s, String configSavePath) throws RepositoryException {
this.log.info("Creating default configuration at {}", (Object)configSavePath);
JcrUtil.createPath(BackupConfigImpl.configPath(configSavePath, "/etc/tags"), "nt:unstructured", s);
JcrUtil.createPath(BackupConfigImpl.configPath(configSavePath, "/etc/segmentation"), "nt:unstructured", s);
JcrUtil.createPath(BackupConfigImpl.configPath(configSavePath, "/etc/cloudsettings/default"), "nt:unstructured", s);
Node cs = JcrUtil.createPath(BackupConfigImpl.configPath(configSavePath, "/etc/cloudservices"), "nt:unstructured", s);
cs.setProperty("overwritePaths", new String[]{"/etc/cloudservices/proxy"});
s.save();
}
private void backupConfig(Session s, String destinationPath) throws RepositoryException {
s.move("/var/upgrade/PreUpgradeBackup/configuration", destinationPath);
s.save();
}
@Override
public Set<String> getPathsToBackup() {
return Collections.unmodifiableSet(this.pathsToBackup);
}
@Override
public Set<String> getPathsToOverwrite() {
return Collections.unmodifiableSet(this.pathsToOverwrite);
}
private void readConfig(Node base, Node current) throws RepositoryException {
if (current.hasNodes()) {
NodeIterator it = current.getNodes();
while (it.hasNext()) {
this.readConfig(base, it.nextNode());
}
} else {
String path = current.getPath().substring(base.getPath().length());
if (path.length() == 0) {
return;
}
this.pathsToBackup.add(path);
this.log.info("{} added to paths to backup due to config node {}", (Object)path, (Object)current.getPath());
if (current.hasProperty("overwritePaths")) {
Property p = current.getProperty("overwritePaths");
if (p.isMultiple()) {
Value[] vs;
for (Value v : vs = p.getValues()) {
String s = v.getString();
this.pathsToOverwrite.add(s);
this.log.info("path to overwrite '{}' set for {}", (Object)s, (Object)path);
}
} else {
String s = p.getValue().getString();
this.pathsToOverwrite.add(s);
this.log.info("path to overwrite '{}' set for {}", (Object)s, (Object)path);
}
}
}
}
}