summaryrefslogtreecommitdiffstats
path: root/container-di/src/main/java/com/yahoo/container/di/config/RestApiContext.java
blob: 7b5f85778c687254021500cc36f6f059421c9990 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.di.config;

import com.google.common.collect.ImmutableSet;
import com.google.inject.Inject;
import com.google.inject.Key;
import com.yahoo.component.ComponentId;
import org.osgi.framework.Version;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Only for internal JDisc use.
 *
 * @author gjoranv
 */
public class RestApiContext {

    private final List<BundleInfo> bundles = new ArrayList<>();
    private final List<Injectable> injectableComponents = new ArrayList<>();

    public final JerseyBundlesConfig bundlesConfig;
    public final JerseyInjectionConfig injectionConfig;

    @Inject
    public RestApiContext(JerseyBundlesConfig bundlesConfig, JerseyInjectionConfig injectionConfig) {
        this.bundlesConfig = bundlesConfig;
        this.injectionConfig = injectionConfig;
    }

    public List<BundleInfo> getBundles() {
        return Collections.unmodifiableList(bundles);
    }

    public void addBundle(BundleInfo bundle) {
        bundles.add(bundle);
    }

    public List<Injectable> getInjectableComponents() {
        return Collections.unmodifiableList(injectableComponents);
    }

    public void addInjectableComponent(Key<?> key, ComponentId id, Object component) {
        injectableComponents.add(new Injectable(key, id, component));
    }

    public static class Injectable {
        public final Key<?> key;
        public final ComponentId id;
        public final Object instance;

        public Injectable(Key<?> key, ComponentId id, Object instance) {
            this.key = key;
            this.id = id;
            this.instance = instance;
        }
        @Override
        public String toString() {
            return id.toString();
        }
    }

    public static class BundleInfo {
        public final String symbolicName;
        public final Version version;
        public final String fileLocation;
        public final URL webInfUrl;
        public final ClassLoader classLoader;

        private Set<String> classEntries;

        public BundleInfo(String symbolicName, Version version, String fileLocation, URL webInfUrl, ClassLoader classLoader) {
            this.symbolicName = symbolicName;
            this.version = version;
            this.fileLocation = fileLocation;
            this.webInfUrl = webInfUrl;
            this.classLoader = classLoader;
        }

        @Override
        public String toString() {
            return symbolicName + ":" + version;
        }

        public void setClassEntries(Collection<String> entries) {
            this.classEntries = ImmutableSet.copyOf(entries);
        }

        public Set<String> getClassEntries() {
            return classEntries;
        }
    }
}