Activator.java
3.75 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.osgi.framework.Bundle
* org.osgi.framework.BundleActivator
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceEvent
* org.osgi.framework.ServiceListener
* org.osgi.framework.ServiceReference
* org.osgi.service.cm.Configuration
* org.osgi.service.cm.ConfigurationAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.auth.impl;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Activator
implements BundleActivator,
ServiceListener {
private BundleContext ctx;
private final Logger log;
public Activator() {
this.log = LoggerFactory.getLogger(this.getClass());
}
public void start(BundleContext bc) throws Exception {
ConfigurationAdmin ca;
this.ctx = bc;
boolean done = false;
ServiceReference ref = bc.getServiceReference(ConfigurationAdmin.class.getName());
if (ref != null && (ca = (ConfigurationAdmin)bc.getService(ref)) != null) {
this.log.info("ConfigurationAdmin available at startup, trying to rebind config");
this.rebindConfig(ca);
done = true;
}
if (!done) {
this.log.info("ConfigurationAdmin not available at startup, will rebind when it is registered");
bc.addServiceListener((ServiceListener)this);
}
}
public void stop(BundleContext bc) throws Exception {
bc.removeServiceListener((ServiceListener)this);
this.ctx = null;
}
public void serviceChanged(ServiceEvent e) {
ServiceReference ref;
Object o;
if (e.getType() == 1 && this.ctx != null && (o = this.ctx.getService(ref = e.getServiceReference())) instanceof ConfigurationAdmin) {
this.log.info("ConfigurationAdmin registered, trying to rebind config");
this.rebindConfig((ConfigurationAdmin)o);
this.log.info("Deactivating ServiceListener");
this.ctx.removeServiceListener((ServiceListener)this);
}
}
protected void rebindConfig(ConfigurationAdmin ca) {
try {
String pid1 = "com.day.cq.wcm.foundation.impl.HTTPAuthHandler";
String filter = "(service.pid=com.day.cq.wcm.foundation.impl.HTTPAuthHandler)";
Configuration[] cfgs = ca.listConfigurations("(service.pid=com.day.cq.wcm.foundation.impl.HTTPAuthHandler)");
if (cfgs != null) {
for (Configuration cfg : cfgs) {
String myLocation = this.ctx.getBundle().getLocation();
if (cfg.getBundleLocation() == null) {
this.log.info("Config {} is not bound, no changes needed", (Object)cfg.getPid());
continue;
}
if (cfg.getBundleLocation().equals(myLocation)) {
this.log.info("Config {} already bound to this bundle, no changes needed", (Object)cfg.getPid());
continue;
}
cfg.setBundleLocation(null);
cfg.update();
this.log.info("Config {} unbound and updated", (Object)cfg.getPid());
}
} else {
this.log.info("No configuration to rebind");
}
}
catch (Exception e) {
this.log.error("Exception in rebindConfig", (Throwable)e);
}
}
}