/
OSGiResourceExtender.xtend
147 lines (118 loc) · 4.07 KB
/
OSGiResourceExtender.xtend
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
package de.modularco.vaadin.integration.impl
import java.io.IOException
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.osgi.framework.Bundle
import org.osgi.framework.BundleActivator
import org.osgi.framework.BundleContext
import org.osgi.framework.BundleEvent
import org.osgi.service.http.HttpContext
import org.osgi.service.http.HttpService
import org.osgi.util.tracker.BundleTracker
import static extension de.modularco.vaadin.integration.impl.Helper.*
class OSGiResourceExtender implements BundleActivator{
BundleTracker resTracker
BundleTracker themeTracker
BundleTracker widgetsetTracker
override start(BundleContext context) throws Exception {
resTracker = new OSGiVaadinResourceTracker(context, 'OSGiVaadin-Resource')
resTracker.open
themeTracker = new OSGiVaadinThemeTracker(context, 'OSGiVaadin-Theme')
themeTracker.open
widgetsetTracker = new OSGiVaadinWidgetsetTracker(context, 'OSGiVaadin-Widgetset')
widgetsetTracker.open
}
override stop(BundleContext context) throws Exception {
resTracker.close
resTracker = null
themeTracker.close
themeTracker = null
widgetsetTracker.close
widgetsetTracker = null
}
}
abstract class OSGiVaadinTracker extends BundleTracker{
String header
new(BundleContext context, String header) {
super(context, Bundle.ACTIVE, null)
this.header = header
}
override addingBundle(Bundle bundle, BundleEvent event) {
var resPath = bundle.getHeaders().get(header) as String
if(resPath !== null){
// the path usually looks like /zzz/VAADIN/xxx, where xxx is the resource
val resourceSegments = resPath.split('VAADIN/')
if(resourceSegments.size != 2){
throw new IllegalArgumentException('The OSGIVaadin-Resource must be a String that contains VAADIN/ exactly once!')
}
// we need the path after VAADIN
val res = resourceSegments.get(1)
// create Vaadin compliant alias and path
val alias = getVaadinAlias(res)
val path = getVaadinPath(res)
// get a HttpService from Framework
val httpRef = bundle.bundleContext.getServiceReference(HttpService.name)
if(httpRef === null){
throw new IllegalStateException('OSGi runtime does not contain HttpService!')
}
val http = bundle.bundleContext.getService(httpRef) as HttpService
// register resource with specialized HttpContext
http.registerResources(alias, path, new OSGiVaadinResourceHttpContext(resPath, bundle))
}
return bundle
}
override removedBundle(Bundle bundle, BundleEvent event, Object object) {
}
protected abstract def String getVaadinPath(String path)
protected abstract def String getVaadinAlias(String path)
}
class OSGiVaadinResourceTracker extends OSGiVaadinTracker{
new(BundleContext context, String header) {
super(context, header)
}
override protected getVaadinPath(String path) {
getRootResourcePath(path);
}
override protected getVaadinAlias(String path) {
getRootResourceAlias(path, VAADIN_VERSION_PREFIX);
}
}
class OSGiVaadinThemeTracker extends OSGiVaadinTracker{
new(BundleContext context, String header) {
super(context, header)
}
override protected getVaadinPath(String path) {
getThemePath(path);
}
override protected getVaadinAlias(String path) {
getThemeAlias(path, VAADIN_VERSION_PREFIX);
}
}
class OSGiVaadinWidgetsetTracker extends OSGiVaadinTracker{
new(BundleContext context, String header) {
super(context, header)
}
override protected getVaadinPath(String path) {
getWidgetsetPath(path);
}
override protected getVaadinAlias(String path) {
getWidgetsetAlias(path, VAADIN_VERSION_PREFIX);
}
}
class OSGiVaadinResourceHttpContext implements HttpContext{
String basePath
Bundle bundle
new(String basePath, Bundle bundle){
this.basePath = basePath
this.bundle = bundle
}
override getMimeType(String name) {
return null
}
override getResource(String name) {
return bundle.getResource(basePath + name)
}
override handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
return true
}
}