This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
/
EquinoxJPMSSupport.java
300 lines (268 loc) · 11.5 KB
/
EquinoxJPMSSupport.java
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 osgi.jpms.internal.layer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleDescriptor.Exports;
import java.lang.module.ModuleDescriptor.Provides;
import java.lang.module.ModuleDescriptor.Requires;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import org.eclipse.osgi.container.ModuleContainerAdaptor.ModuleEvent;
import org.eclipse.osgi.container.ModuleRevisionBuilder;
import org.eclipse.osgi.internal.hookregistry.ActivatorHookFactory;
import org.eclipse.osgi.internal.hookregistry.HookConfigurator;
import org.eclipse.osgi.internal.hookregistry.HookRegistry;
import org.eclipse.osgi.internal.hookregistry.StorageHookFactory;
import org.eclipse.osgi.internal.hookregistry.StorageHookFactory.StorageHook;
import org.eclipse.osgi.storage.BundleInfo.Generation;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.Version;
import org.osgi.framework.namespace.BundleNamespace;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.framework.namespace.PackageNamespace;
import org.osgi.framework.wiring.FrameworkWiring;
import org.osgi.resource.Namespace;
import osgi.jpms.internal.layer.EquinoxJPMSSupport.EquinoxJPMSStorageHook;
public class EquinoxJPMSSupport extends StorageHookFactory<Object, Object, EquinoxJPMSStorageHook> implements HookConfigurator, ActivatorHookFactory, BundleActivator {
// A storage hook is needed only to allow us to adaptModuleRevisionBuilder
public class EquinoxJPMSStorageHook extends StorageHook<Object, Object> {
public EquinoxJPMSStorageHook(Generation generation) {
super(generation, EquinoxJPMSSupport.class);
}
@Override
public void initialize(Dictionary<String, String> manifest) throws BundleException {
// nothing
}
@Override
public void load(Object loadContext, DataInputStream is) throws IOException {
// nothing
}
@Override
public void save(Object saveContext, DataOutputStream os) throws IOException {
// nothing
}
@Override
public ModuleRevisionBuilder adaptModuleRevisionBuilder(ModuleEvent operation, org.eclipse.osgi.container.Module origin,
ModuleRevisionBuilder builder) {
if (builder.getSymbolicName() == null) {
// only do this if this doesn't have a bsn
File f = getGeneration().getContent();
try {
ModuleFinder finder = ModuleFinder.of(f.toPath());
Set<ModuleReference> found = finder.findAll();
if (found.size() == 1) {
// we only pay attention if we find exactly 1 module
return createBuilder(found.iterator().next());
}
} catch (Exception e) {
// TODO should log
e.printStackTrace();
}
}
// fall back to default
return super.adaptModuleRevisionBuilder(operation, origin, builder);
}
private ModuleRevisionBuilder createBuilder(ModuleReference ref) {
ModuleDescriptor desc = ref.descriptor();
ModuleRevisionBuilder builder = new ModuleRevisionBuilder();
builder.setSymbolicName(desc.name());
Version version = desc.version().map((v) -> {
try {
return Version.valueOf(v.toString());
} catch (IllegalArgumentException e) {
return Version.emptyVersion;
}
}).orElse(Version.emptyVersion);
builder.setVersion(version);
// add bundle and identity capabilities, do not create host capability for JPMS
builder.addCapability(
BundleNamespace.BUNDLE_NAMESPACE,
Map.of(),
Map.of(
BundleNamespace.BUNDLE_NAMESPACE, desc.name(),
BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, version));
builder.addCapability(
IdentityNamespace.IDENTITY_NAMESPACE,
Map.of(),
Map.of(
IdentityNamespace.IDENTITY_NAMESPACE, desc.name(),
IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE, version));
for(Exports exports : desc.exports()) {
// TODO map targets to x-friends directive.
builder.addCapability(
PackageNamespace.PACKAGE_NAMESPACE,
Map.of(),
Map.of(PackageNamespace.PACKAGE_NAMESPACE, exports.source()));
}
for(Provides provides : desc.provides()) {
builder.addCapability(
JpmsServiceNamespace.JPMS_SERVICE_NAMESPACE,
Map.of(),
Map.of(
JpmsServiceNamespace.JPMS_SERVICE_NAMESPACE, provides.service(),
JpmsServiceNamespace.CAPABILITY_PROVIDES_WITH, provides.providers()));
}
for (Requires requires : desc.requires()) {
Map<String, String> directives = new HashMap<>();
// determine the resolution value based on the STATIC modifier
String resolution = requires.modifiers().contains(Requires.Modifier.STATIC) ? Namespace.RESOLUTION_OPTIONAL : Namespace.RESOLUTION_MANDATORY;
directives.put(Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE, resolution);
// determine the visibility value based on the TRANSITIVE modifier
String visibility = requires.modifiers().contains(Requires.Modifier.TRANSITIVE) ? BundleNamespace.VISIBILITY_REEXPORT : BundleNamespace.VISIBILITY_PRIVATE;
directives.put(BundleNamespace.REQUIREMENT_VISIBILITY_DIRECTIVE, visibility);
// create a bundle filter based on the requires name
directives.put(Namespace.REQUIREMENT_FILTER_DIRECTIVE, "(" + BundleNamespace.BUNDLE_NAMESPACE + "=" + requires.name() + ")");
builder.addRequirement(BundleNamespace.BUNDLE_NAMESPACE, directives, Collections.emptyMap());
}
for(String uses : desc.uses()) {
builder.addRequirement(JpmsServiceNamespace.JPMS_SERVICE_NAMESPACE,
Map.of(Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE, Namespace.RESOLUTION_OPTIONAL,
Namespace.REQUIREMENT_FILTER_DIRECTIVE, "(" + JpmsServiceNamespace.JPMS_SERVICE_NAMESPACE + "=" + uses + ")"),
Map.of(JpmsServiceNamespace.JPMS_SERVICE_NAMESPACE, uses));
}
return builder;
}
}
private static final String bootModuleLocationPrefix = "jpmsBootModule:";
@Override
public void addHooks(HookRegistry hookRegistry) {
hookRegistry.addStorageHookFactory(this);
hookRegistry.addActivatorHookFactory(this);
}
@Override
public int getStorageVersion() {
return 0;
}
@Override
protected EquinoxJPMSStorageHook createStorageHook(Generation generation) {
return new EquinoxJPMSStorageHook(generation);
}
@Override
public BundleActivator createActivator() {
return this;
}
@Override
public void start(BundleContext context) throws Exception {
// keep track of the boot modules that should be installed
Set<String> bootModuleLocations = new HashSet<>();
// make sure all boot modules are installed
Set<Module> bootModules = new TreeSet<Module>((m1, m2) ->{return m1.getName().compareTo(m2.getName());});
bootModules.addAll(ModuleLayer.boot().modules());
for (Module module : bootModules) {
bootModuleLocations.add(installBootModule(module, context));
}
Set<Bundle> refresh = new HashSet<>();
for (Bundle b : context.getBundles()) {
String bLoc = b.getLocation();
if (bLoc.startsWith(bootModuleLocationPrefix) && !bootModuleLocations.contains(b.getLocation())) {
// something changed in VM configuration since last start;
// must uninstall this boot module
b.uninstall();
refresh.add(b);
}
}
if (!refresh.isEmpty()) {
CountDownLatch latch = new CountDownLatch(1);
context.getBundle(Constants.SYSTEM_BUNDLE_LOCATION).adapt(FrameworkWiring.class).refreshBundles(refresh, (e) -> latch.countDown());
latch.await(10, TimeUnit.SECONDS);
}
}
private String installBootModule(Module module, BundleContext context) throws IOException, BundleException {
String bootLocation = bootModuleLocationPrefix + module.getName();
if (context.getBundle(bootLocation) == null) {
Manifest m = new Manifest();
Attributes mainAttrs = m.getMainAttributes();
mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
mainAttrs.putValue(Constants.BUNDLE_MANIFESTVERSION, "2");
mainAttrs.putValue(Constants.BUNDLE_SYMBOLICNAME, module.getName() + "; " + LayerFactoryImpl.BOOT_JPMS_MODULE + "=true");
mainAttrs.putValue(Constants.BUNDLE_VERSION, module.getDescriptor().version().map((v) -> {
String s = v.toString();
int indexDash = s.indexOf('-');
if (indexDash >= 0) {
s = s.substring(0, indexDash);
}
return s;
}).orElse("0.0.0"));
StringBuilder exportPackages = new StringBuilder();
for (Exports exports : module.getDescriptor().exports()) {
if (exports.targets().isEmpty() && !exports.source().startsWith("java.")) {
if (exportPackages.length() > 0) {
exportPackages.append(", ");
}
exportPackages.append(exports.source());
exportPackages.append("; mandatory:=").append(LayerFactoryImpl.BOOT_JPMS_MODULE);
exportPackages.append("; ").append(LayerFactoryImpl.BOOT_JPMS_MODULE).append("=true");
}
}
if (exportPackages.length() > 0) {
mainAttrs.putValue(Constants.EXPORT_PACKAGE, exportPackages.toString());
}
StringBuilder provideCapability = new StringBuilder();
for(Provides provides : module.getDescriptor().provides()) {
if (provideCapability.length() > 0) {
provideCapability.append(", ");
}
provideCapability.append(JpmsServiceNamespace.JPMS_SERVICE_NAMESPACE).append("; ");
provideCapability.append(JpmsServiceNamespace.JPMS_SERVICE_NAMESPACE).append("=").append(provides.service()).append("; ");
provideCapability.append(JpmsServiceNamespace.CAPABILITY_PROVIDES_WITH).append(":List<String>").append("=\"");
for (String provider : provides.providers()) {
provideCapability.append(provider).append(',');
}
provideCapability.setLength(provideCapability.length() - 1);
provideCapability.append('\"');
provideCapability.append("; ").append(LayerFactoryImpl.BOOT_JPMS_MODULE).append("=true");
}
if (provideCapability.length() > 0) {
mainAttrs.putValue(Constants.PROVIDE_CAPABILITY, provideCapability.toString());
}
mainAttrs.putValue(Constants.REQUIRE_BUNDLE, Constants.SYSTEM_BUNDLE_SYMBOLICNAME);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
JarOutputStream jarOut = new JarOutputStream(bytes, m);
jarOut.close();
context.installBundle(bootLocation, new ByteArrayInputStream(bytes.toByteArray()));
}
return bootLocation;
}
@Override
public void stop(BundleContext context) throws Exception {
// nothing
}
}