ConfigurationUnbinder.java
3.49 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.launchpad.api.StartupHandler
* org.osgi.framework.Bundle
* org.osgi.framework.BundleContext
* org.osgi.service.cm.Configuration
* org.osgi.service.cm.ConfigurationAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.compat.impl.config;
import java.io.IOException;
import java.util.Dictionary;
import java.util.HashSet;
import java.util.LinkedList;
import org.apache.sling.launchpad.api.StartupHandler;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConfigurationUnbinder {
private static String PROP_UPDATE = "compat.upgrade.configs.update";
private final Logger log;
public ConfigurationUnbinder(final BundleContext bundleContext, final ConfigurationAdmin ca, final StartupHandler startupHandler) {
this.log = LoggerFactory.getLogger(this.getClass());
final boolean doUpdate = "true".equalsIgnoreCase(bundleContext.getProperty(PROP_UPDATE));
startupHandler.waitWithStartup(true);
Thread t = new Thread(){
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void run() {
try {
ConfigurationUnbinder.this.unbindAndUpdateAllConfigurations(bundleContext, ca, doUpdate);
}
finally {
startupHandler.waitWithStartup(false);
}
}
};
t.setName("Compat Configuration Unbinder");
t.setDaemon(false);
t.start();
}
void unbindAndUpdateAllConfigurations(BundleContext bundleContext, ConfigurationAdmin ca, boolean doUpdate) {
Bundle[] bundles = bundleContext.getBundles();
HashSet<String> ignoreLocations = new HashSet<String>();
for (Bundle b : bundles) {
if (b.getState() != 32 && b.getState() != 8) continue;
ignoreLocations.add(b.getLocation());
}
Configuration[] allConfigs = null;
try {
allConfigs = ca.listConfigurations(null);
}
catch (Exception e) {
this.log.error("Unable to list all configurations.", (Throwable)e);
}
if (allConfigs != null) {
LinkedList<Configuration> toUpdate = new LinkedList<Configuration>();
for (Configuration cfg : allConfigs) {
String location = cfg.getBundleLocation();
if (location == null || location.length() <= 0 || ignoreLocations.contains(location)) continue;
this.log.info("Unbinding Configuration {} from {}", (Object)cfg.getPid(), (Object)location);
cfg.setBundleLocation(null);
toUpdate.add(cfg);
}
if (doUpdate) {
for (Configuration cfg2 : toUpdate) {
this.log.info("Updating Configuration {} to re-activate it", (Object)cfg2.getPid());
Dictionary props = cfg2.getProperties();
try {
cfg2.update(props);
}
catch (IOException e) {
this.log.warn("Error during configuration update of " + cfg2.getPid(), (Throwable)e);
}
}
}
}
}
}