Activator.java
4.23 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
/*
* 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.BundleException
* org.osgi.framework.ServiceReference
* org.osgi.service.packageadmin.PackageAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.compat.commonsauth.impl;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.packageadmin.PackageAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Activator
implements BundleActivator,
Runnable {
private static final String COMMONS_AUTH_NAME = "org.apache.sling.commons.auth";
private static final String FILE_BUNDLE_CHECKED = "commons.auth.checked";
private final Logger log;
private BundleContext bundleContext;
private Bundle[] bundlesToRemove;
public Activator() {
this.log = LoggerFactory.getLogger(this.getClass());
}
public void start(BundleContext context) {
if (!this.isCommonsAuthChecked(context)) {
Bundle[] bundles = context.getBundles();
ArrayList<Bundle> touninstall = new ArrayList<Bundle>();
for (Bundle bundle : bundles) {
if (!"org.apache.sling.commons.auth".equals(bundle.getSymbolicName())) continue;
touninstall.add(bundle);
}
if (!touninstall.isEmpty()) {
this.log.info("Compat-Commons-Auth: Preparing to remove {} bundles", (Object)touninstall.size());
this.bundlesToRemove = touninstall.toArray((T[])new Bundle[touninstall.size()]);
this.bundleContext = context;
this.startThread();
} else {
this.setCommonsAuthChecked(context);
}
}
}
public void stop(BundleContext context) {
}
private boolean isCommonsAuthChecked(BundleContext context) {
File ckFile = context.getDataFile("commons.auth.checked");
return ckFile != null && ckFile.exists();
}
private void setCommonsAuthChecked(BundleContext context) {
File ckFile = context.getDataFile("commons.auth.checked");
if (ckFile != null) {
try {
ckFile.createNewFile();
}
catch (IOException ioe) {
this.log.warn("Compat-Commons-Auth: Failed to create check file " + ckFile.getAbsolutePath(), (Throwable)ioe);
}
}
}
private void startThread() {
this.log.info("Compat-Commons-Auth: Starting thread to uninstall bundle(s)");
Thread worker = new Thread((Runnable)this, "Commons Auth Uninstaller");
worker.setDaemon(true);
worker.start();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void run() {
try {
Thread.sleep(5000);
}
catch (InterruptedException ie) {
// empty catch block
}
Bundle[] touninstall = this.bundlesToRemove;
this.bundlesToRemove = null;
for (Bundle bundle : touninstall) {
try {
this.log.info("Compat-Commons-Auth: Uninstalling bundle {}", (Object)bundle);
bundle.uninstall();
continue;
}
catch (BundleException be) {
// empty catch block
}
}
ServiceReference sr = this.bundleContext.getServiceReference(PackageAdmin.class.getName());
if (sr != null) {
PackageAdmin pa = (PackageAdmin)this.bundleContext.getService(sr);
try {
this.log.info("Compat-Commons-Auth: Refreshing packages for {} bundles", (Object)touninstall.length);
pa.refreshPackages(touninstall);
}
finally {
this.bundleContext.ungetService(sr);
}
}
this.setCommonsAuthChecked(this.bundleContext);
this.log.info("Compat-Commons-Auth: Done uninstalling bundles");
}
}