Activator.java
6.17 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.Servlet
* javax.servlet.ServletException
* org.apache.sling.auth.core.AuthenticationSupport
* org.apache.sling.commons.mime.MimeTypeService
* org.osgi.framework.BundleActivator
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceReference
* org.osgi.framework.ServiceRegistration
* org.osgi.service.http.HttpContext
* org.osgi.service.http.HttpService
* org.osgi.service.http.NamespaceException
* org.osgi.util.tracker.ServiceTracker
* org.osgi.util.tracker.ServiceTrackerCustomizer
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.crx.delite.impl;
import com.day.crx.delite.impl.AuthHttpContext;
import com.day.crx.delite.impl.MainServlet;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.sling.auth.core.AuthenticationSupport;
import org.apache.sling.commons.mime.MimeTypeService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Activator
implements BundleActivator {
private static final String PAR_AUTH_REQ = "sling.auth.requirements";
private final Logger logger;
private ServiceTracker httpServiceTracker;
private ServiceTracker authenticationTracker;
private ServiceTracker mimetypeTracker;
private MainServlet mainServlet;
private final AuthHttpContext authContext;
public Activator() {
this.logger = LoggerFactory.getLogger(this.getClass());
this.authContext = new AuthHttpContext();
}
public void start(BundleContext context) throws Exception {
context.registerService("java.lang.Object", new Object(), (Dictionary)new Hashtable<String, Object>(){});
this.mainServlet = new MainServlet(context);
this.httpServiceTracker = new SimpleServiceTracker<HttpService>(context, HttpService.class, new ServiceListener<HttpService>(){
@Override
public void addedService(HttpService httpService) {
try {
httpService.registerServlet("/crx/de", (Servlet)Activator.this.mainServlet, null, (HttpContext)Activator.this.authContext);
Activator.this.logger.info("Registered servlet at {}", (Object)"/crx/de");
}
catch (ServletException e) {
Activator.this.logger.error("Unable to register servlet at /crx/de", (Throwable)e);
}
catch (NamespaceException e) {
Activator.this.logger.error("Unable to register servlet at /crx/de", (Throwable)e);
}
}
@Override
public void removedService(HttpService httpService) {
try {
Activator.this.logger.info("Unregistering servlet from {}", (Object)"/crx/de");
httpService.unregister("/crx/de");
}
catch (Exception ignore) {
// empty catch block
}
}
});
this.authenticationTracker = new SimpleServiceTracker<AuthenticationSupport>(context, AuthenticationSupport.class, new ServiceListener<AuthenticationSupport>(){
@Override
public void addedService(AuthenticationSupport service) {
Activator.this.authContext.setAuthenticationSupport(service);
}
@Override
public void removedService(AuthenticationSupport service) {
Activator.this.authContext.setAuthenticationSupport(null);
}
});
this.mimetypeTracker = new SimpleServiceTracker<MimeTypeService>(context, MimeTypeService.class, new ServiceListener<MimeTypeService>(){
@Override
public void addedService(MimeTypeService service) {
Activator.this.mainServlet.setMimeTypeService(service);
}
@Override
public void removedService(MimeTypeService service) {
Activator.this.mainServlet.setMimeTypeService(null);
}
});
this.httpServiceTracker.open();
this.authenticationTracker.open();
this.mimetypeTracker.open();
}
public void stop(BundleContext context) {
if (this.httpServiceTracker != null) {
this.httpServiceTracker.close();
this.httpServiceTracker = null;
}
if (this.authenticationTracker != null) {
this.authenticationTracker.close();
this.authenticationTracker = null;
}
if (this.mimetypeTracker != null) {
this.mimetypeTracker.close();
this.mimetypeTracker = null;
}
if (this.mainServlet != null) {
this.mainServlet = null;
}
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public static class SimpleServiceTracker<T>
extends ServiceTracker<T, T> {
private final ServiceListener<T> listener;
public SimpleServiceTracker(BundleContext context, Class<T> clazz, ServiceListener<T> listener) {
super(context, clazz, null);
this.listener = listener;
}
public T addingService(ServiceReference<T> reference) {
Object service = ServiceTracker.super.addingService(reference);
this.listener.addedService(service);
return (T)service;
}
public void removedService(ServiceReference<T> reference, T service) {
this.listener.removedService(service);
ServiceTracker.super.removedService(reference, service);
}
}
public static interface ServiceListener<T> {
public void addedService(T var1);
public void removedService(T var1);
}
}