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
|
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.osgi.servlet;
import java.util.Collections;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.annotation.WebServlet;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
import org.osgi.service.log.LogService;
import com.vaadin.osgi.resources.OsgiVaadinResources;
import com.vaadin.osgi.resources.OsgiVaadinResources.ResourceBundleInactiveException;
import com.vaadin.osgi.resources.VaadinResourceService;
import com.vaadin.server.Constants;
import com.vaadin.server.VaadinServlet;
/**
* This component tracks {@link VaadinServlet} registrations, configures them
* with the appropriate path to the Vaadin static resources and registers a
* {@link Servlet} using the HttpWhiteboard specification.
*
* @author Vaadin Ltd.
*
* @since 8.1
*/
@Component
public class VaadinServletRegistration {
private final Map<ServiceReference<VaadinServlet>, ServiceRegistration<Servlet>> registeredServlets = Collections
.synchronizedMap(new LinkedHashMap<>());
private static final String MISSING_ANNOTATION_MESSAGE_FORMAT = "The property '%s' must be set in a '%s' without the '%s' annotation!";
private static final String URL_PATTERNS_NOT_SET_MESSAGE_FORMAT = "The property '%s' must be set when the 'urlPatterns' attribute is not set!";
private static final String SERVLET_PATTERN = HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
private static final String VAADIN_RESOURCES_PARAM = HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX
+ Constants.PARAMETER_VAADIN_RESOURCES;
private LogService logService;
@Reference(cardinality = ReferenceCardinality.MULTIPLE, service = VaadinServlet.class, policy = ReferencePolicy.DYNAMIC)
void bindVaadinServlet(VaadinServlet servlet, ServiceReference<VaadinServlet> reference)
throws ResourceBundleInactiveException {
log(LogService.LOG_WARNING, "VaadinServlet Registration");
Hashtable<String, Object> properties = getProperties(reference);
WebServlet annotation = servlet.getClass()
.getAnnotation(WebServlet.class);
if (!validateSettings(annotation, properties)) {
return;
}
properties.put(VAADIN_RESOURCES_PARAM, getResourcePath());
if (annotation != null) {
properties.put(
HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_ASYNC_SUPPORTED,
Boolean.toString(annotation.asyncSupported()));
}
// We register the Http Whiteboard servlet using the context of
// the bundle which registered the Vaadin Servlet, not our own
BundleContext bundleContext = reference.getBundle().getBundleContext();
ServiceRegistration<Servlet> servletRegistration = bundleContext
.registerService(Servlet.class, servlet, properties);
registeredServlets.put(reference, servletRegistration);
}
private boolean validateSettings(WebServlet annotation,
Hashtable<String, Object> properties) {
if (!properties.containsKey(SERVLET_PATTERN)) {
if (annotation == null) {
log(LogService.LOG_ERROR,
String.format(MISSING_ANNOTATION_MESSAGE_FORMAT,
SERVLET_PATTERN,
VaadinServlet.class.getSimpleName(),
WebServlet.class.getName()));
return false;
} else if (annotation.urlPatterns().length == 0) {
log(LogService.LOG_ERROR, String.format(
URL_PATTERNS_NOT_SET_MESSAGE_FORMAT, SERVLET_PATTERN));
return false;
}
}
return true;
}
private String getResourcePath() throws ResourceBundleInactiveException {
VaadinResourceService service = OsgiVaadinResources.getService();
return String.format("/%s", service.getResourcePathPrefix());
}
private void log(int level, String message) {
if (logService != null) {
logService.log(level, message);
}
}
void unbindVaadinServlet(ServiceReference<VaadinServlet> reference) {
ServiceRegistration<?> servletRegistration = registeredServlets
.remove(reference);
if (servletRegistration != null) {
try {
servletRegistration.unregister();
} catch (IllegalStateException ise) {
// This service may have already been unregistered
// automatically by the OSGi framework if the
// application bundle is being stopped. This is
// obviously not a problem for us.
}
}
}
@Reference(cardinality = ReferenceCardinality.OPTIONAL)
void setLogService(LogService logService) {
this.logService = logService;
}
void unsetLogService(LogService logService) {
this.logService = null;
}
private Hashtable<String, Object> getProperties(
ServiceReference<VaadinServlet> reference) {
Hashtable<String, Object> properties = new Hashtable<>();
for (String key : reference.getPropertyKeys()) {
properties.put(key, reference.getProperty(key));
}
return properties;
}
}
|