aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/component/Component.java
blob: 7176ee0580ceb2108eb847f754eb71ff3d1fa52e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.component;

import com.yahoo.collections.Pair;
import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.config.model.producer.AnyConfigProducer;
import com.yahoo.config.model.producer.TreeConfigProducer;
import com.yahoo.osgi.provider.model.ComponentModel;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @author gjoranv
 * @author Tony Vaagenes
 */
public class Component<CCHILD extends AnyConfigProducer, MODEL extends ComponentModel>
        extends TreeConfigProducer<CCHILD> implements Comparable<Component<?, ?>> {

    public final MODEL model;
    final Set<Pair<String, Component>> injectedComponents = new LinkedHashSet<>();

    public Component(MODEL model) {
        super(model.getComponentId().stringValue());
        this.model = model;
    }

    /** Returns a component that uses its class name as id. */
    public static Component<?,?> fromClassAndBundle(Class<?> clazz, String bundle) {
        return new Component<>(new ComponentModel(clazz.getName(), null, bundle));
    }

    public ComponentId getGlobalComponentId() {
        return model.getComponentId();
    }

    public ComponentId getComponentId() {
        return model.getComponentId();
    }

    public ComponentSpecification getClassId() {
        return model.getClassId();
    }

    public void inject(Component component) {
        injectForName("", component);
    }

    public void injectForName(String name, Component component) {
        injectedComponents.add(new Pair<>(name, component));
    }

    public void addComponent(CCHILD child) {
        addChild(child);
    }

    /** For testing only */
    public Set<String> getInjectedComponentIds() {
        Set<String> injectedIds = new HashSet<>();
        for (Pair<String, Component> injected : injectedComponents) {
            injectedIds.add(injected.getSecond().getSubId());
        }
        return injectedIds;
    }

    @Override
    public int compareTo(Component<?, ?> other) {
        return getComponentId().compareTo(other.getComponentId());
    }

    @Override
    public String toString() {
        return "component " + getClassId() +
               (getClassId().toString().equals(getComponentId().toString()) ? "" : ": " + getComponentId());
    }

}