summaryrefslogtreecommitdiffstats
path: root/standalone-container/src/main/java/com/yahoo/application/container/impl/ClassLoaderOsgiFramework.java
blob: 48f1efc761592002e42460576c56640c40bcae73 (plain) (blame)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.application.container.impl;

import com.yahoo.container.standalone.StandaloneContainerApplication;
import com.yahoo.jdisc.application.OsgiFramework;
import com.yahoo.jdisc.application.OsgiHeader;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleListener;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceObjects;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.Version;
import org.osgi.framework.wiring.BundleCapability;
import org.osgi.framework.wiring.BundleRequirement;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.framework.wiring.BundleWire;
import org.osgi.framework.wiring.BundleWiring;
import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;
import org.osgi.resource.Wire;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.jar.Attributes;
import java.util.jar.JarFile;

/**
 * A (mock) OSGI implementation which loads classes from the system classpath
 *
 * @author Tony Vaagenes
 * @author ollivir
 */
public final class ClassLoaderOsgiFramework implements OsgiFramework {

    private final BundleContextImpl bundleContextImpl = new BundleContextImpl();
    private final SystemBundleImpl systemBundleImpl = new SystemBundleImpl();
    private final BundleWiringImpl bundleWiringImpl = new BundleWiringImpl();

    private final List<URL> bundleLocations = new ArrayList<>();
    private final List<Bundle> bundleList = new ArrayList<>(List.of(systemBundleImpl));
    private ClassLoader classLoader = null;

    private final AtomicInteger nextBundleId = new AtomicInteger(1);

    @Override
    public List<Bundle> installBundle(String bundleLocation) {
        if (bundleLocation != null && ! bundleLocation.isEmpty()) {
            try {
                URL url = new URL(bundleLocation);
                bundleLocations.add(url);
                bundleList.add(new JarBundleImpl(url));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return bundles();
    }

    private ClassLoader getClassLoader() {
        if (bundleLocations.isEmpty()) {
            return getClass().getClassLoader();
        } else {
            if (classLoader == null) {
                classLoader = new URLClassLoader(bundleLocations.toArray(new URL[0]), getClass().getClassLoader());
            }
            return classLoader;
        }
    }

    @Override
    public void startBundles(List<Bundle> bundles, boolean privileged) {
    }

    @Override
    public void refreshPackages() {
    }

    @Override
    public BundleContext bundleContext() {
        return bundleContextImpl;
    }

    @Override
    public List<Bundle> bundles() {
        return bundleList;
    }

    @Override
    public List<Bundle> getBundles(Bundle requestingBundle) {
        return bundleList;
    }

    @Override
    public void allowDuplicateBundles(Collection<Bundle> bundles) { }

    @Override
    public void start() {
    }

    @Override
    public void stop() {
    }

    private abstract class BundleImpl implements Bundle {
        @Override
        public int getState() {
            return Bundle.ACTIVE;
        }

        @Override
        public void start(int options) {
        }

        @Override
        public void start() {
        }

        @Override
        public void stop(int options) {
        }

        @Override
        public void stop() {
        }

        @Override
        public void update(InputStream input) {
        }

        @Override
        public void update() {
        }

        @Override
        public void uninstall() {
        }

        @Override
        public Dictionary<String, String> getHeaders(String locale) {
            return getHeaders();
        }

        @Override
        public String getSymbolicName() {
            return ClassLoaderOsgiFramework.this.getClass().getName();
        }

        @Override
        public String getLocation() {
            return getSymbolicName();
        }

        @Override
        public ServiceReference<?>[] getRegisteredServices() {
            return new ServiceReference<?>[0];
        }

        @Override
        public ServiceReference<?>[] getServicesInUse() {
            return getRegisteredServices();
        }

        @Override
        public boolean hasPermission(Object permission) {
            return true;
        }

        @Override
        public URL getResource(String name) {
            return getClassLoader().getResource(name);
        }

        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            return getClassLoader().loadClass(name);
        }

        @Override
        public Enumeration<URL> getResources(String name) throws IOException {
            return getClassLoader().getResources(name);
        }

        @Override
        public Enumeration<String> getEntryPaths(String path) {
            throw new UnsupportedOperationException();
        }

        @Override
        public URL getEntry(String path) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Enumeration<URL> findEntries(String path, String filePattern, boolean recurse) {
            throw new UnsupportedOperationException();
        }

        @Override
        public long getLastModified() {
            return 1L;
        }

        @Override
        public BundleContext getBundleContext() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Map<X509Certificate, List<X509Certificate>> getSignerCertificates(int signersType) {
            return Collections.emptyMap();
        }

        @Override
        @SuppressWarnings("unchecked")
        public <T> T adapt(Class<T> clazz) {
            if (clazz.equals(BundleRevision.class)) {
                return (T) new BundleRevisionImpl();
            } else if (clazz.equals(BundleWiring.class)) {
                return (T) new BundleWiringImpl();
            } else {
                return null;
            }
        }

        @Override
        public File getDataFile(String filename) {
            return null;
        }

        @Override
        public int compareTo(Bundle o) {
            return Long.compare(getBundleId(), o.getBundleId());
        }
    }

    private class BundleRevisionImpl implements BundleRevision {
        @Override
        public String getSymbolicName() {
            return this.getClass().getName();
        }

        @Override
        public List<BundleRequirement> getDeclaredRequirements(String p1) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Version getVersion() {
            return Version.emptyVersion;
        }

        @Override
        public BundleWiring getWiring() {
            return bundleWiringImpl;
        }

        @Override
        public List<BundleCapability> getDeclaredCapabilities(String p1) {
            throw new UnsupportedOperationException();
        }

        @Override
        public int getTypes() {
            return 0;
        }

        @Override
        public Bundle getBundle() {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<Capability> getCapabilities(String p1) {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<Requirement> getRequirements(String p1) {
            throw new UnsupportedOperationException();
        }
    }

    private class BundleWiringImpl implements BundleWiring {
        @Override
        public List<URL> findEntries(String p1, String p2, int p3) {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<Wire> getRequiredResourceWires(String p1) {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<Capability> getResourceCapabilities(String p1) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isCurrent() {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<BundleWire> getRequiredWires(String p1) {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<BundleCapability> getCapabilities(String p1) {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<Wire> getProvidedResourceWires(String p1) {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<BundleWire> getProvidedWires(String p1) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BundleRevision getRevision() {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<Requirement> getResourceRequirements(String p1) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isInUse() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Collection<String> listResources(String p1, String p2, int p3) {
            throw new UnsupportedOperationException();
        }

        @Override
        public ClassLoader getClassLoader() {
            return ClassLoaderOsgiFramework.this.getClassLoader();
        }

        @Override
        public List<BundleRequirement> getRequirements(String p1) {
            throw new UnsupportedOperationException();
        }

        @Override
        public BundleRevision getResource() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Bundle getBundle() {
            throw new UnsupportedOperationException();
        }
    }

    private class SystemBundleImpl extends BundleImpl {
        @Override
        public long getBundleId() {
            return 0L;
        }

        @Override
        public Version getVersion() {
            return Version.emptyVersion;
        }

        @Override
        public Dictionary<String, String> getHeaders() {
            Hashtable<String, String> ret = new Hashtable<>();
            ret.put(OsgiHeader.APPLICATION, StandaloneContainerApplication.class.getName());
            return ret;
        }
    }

    private class JarBundleImpl extends BundleImpl {
        private final long bundleId;
        private final Dictionary<String, String> headers;

        JarBundleImpl(URL location) throws IOException {
            this.bundleId = (long) nextBundleId.getAndIncrement();
            this.headers = retrieveHeaders(location);
        }

        @Override
        public long getBundleId() {
            return bundleId;
        }

        @Override
        public Dictionary<String, String> getHeaders() {
            return headers;
        }

        @Override
        public String getSymbolicName() {
            return headers.get("Bundle-SymbolicName");
        }

        @Override
        public Version getVersion() {
            return Version.parseVersion(headers.get("Bundle-Version"));
        }

        private Dictionary<String, String> retrieveHeaders(URL location) throws IOException {
            try (JarFile jarFile = new JarFile(location.getFile())) {
                Attributes attributes = jarFile.getManifest().getMainAttributes();
                Hashtable<String, String> ret = new Hashtable<>();
                attributes.forEach((k, v) -> ret.put(k.toString(), v.toString()));
                return ret;
            }
        }
    }

    private class BundleContextImpl implements BundleContext {
        @Override
        public String getProperty(String key) {
            return null;
        }

        @Override
        public Bundle getBundle() {
            return systemBundleImpl;
        }

        @Override
        public Bundle installBundle(String location, InputStream input) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Bundle installBundle(String location) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Bundle getBundle(long id) {
            return systemBundleImpl;
        }

        @Override
        public Bundle[] getBundles() {
            return new Bundle[] { systemBundleImpl };
        }

        @Override
        public Bundle getBundle(String location) {
            return systemBundleImpl;
        }

        @Override
        public void addServiceListener(ServiceListener listener, String filter) {
        }

        @Override
        public void addServiceListener(ServiceListener listener) {
        }

        @Override
        public void removeServiceListener(ServiceListener listener) {
        }

        @Override
        public void addBundleListener(BundleListener listener) {
        }

        @Override
        public void removeBundleListener(BundleListener listener) {
        }

        @Override
        public void addFrameworkListener(FrameworkListener listener) {
        }

        @Override
        public void removeFrameworkListener(FrameworkListener listener) {
        }

        @Override
        public ServiceRegistration<?> registerService(String[] classes, Object service, Dictionary<String, ?> properties) {
            throw new UnsupportedOperationException();
        }

        @Override
        public ServiceRegistration<?> registerService(String clazz, Object service, Dictionary<String, ?> properties) {
            return null;
        }

        @Override
        public <S> ServiceRegistration<S> registerService(Class<S> clazz, S service, Dictionary<String, ?> properties) {
            throw new UnsupportedOperationException();
        }

        @Override
        public ServiceReference<?>[] getServiceReferences(String clazz, String filter) {
            throw new UnsupportedOperationException();
        }

        @Override
        public ServiceReference<?>[] getAllServiceReferences(String clazz, String filter) {
            throw new UnsupportedOperationException();
        }

        @Override
        public ServiceReference<?> getServiceReference(String clazz) {
            throw new UnsupportedOperationException();
        }

        @Override
        public <S> ServiceReference<S> getServiceReference(Class<S> clazz) {
            throw new UnsupportedOperationException();
        }

        @Override
        public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> clazz, String filter) {
            return new ArrayList<>();
        }

        @Override
        public <S> S getService(ServiceReference<S> reference) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean ungetService(ServiceReference<?> reference) {
            throw new UnsupportedOperationException();
        }

        @Override
        public File getDataFile(String filename) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Filter createFilter(String filter) {
            throw new UnsupportedOperationException();
        }

        @Override
        public <S> ServiceRegistration<S> registerService(Class<S> aClass, ServiceFactory<S> serviceFactory,
                Dictionary<String, ?> dictionary) {
            throw new UnsupportedOperationException();
        }

        @Override
        public <S> ServiceObjects<S> getServiceObjects(ServiceReference<S> serviceReference) {
            throw new UnsupportedOperationException();
        }
    }

}