summaryrefslogtreecommitdiffstats
path: root/config/src/main/java/com/yahoo/config/subscription/ConfigGetter.java
blob: 0f25084c6d008d464a8b2b03b55d81c951e105e5 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.subscription;


import com.yahoo.config.ConfigInstance;

/**
 * This is a simple config getter that retrieves a config with a given class and configId through a
 * simple method call. No subscription is retained when the config has been returned to the client.
 *
 * This class is mainly targeted to unit tests that do not want the extra complexity incurred by setting
 * up their own subscriber. Another use-case is clients that get config, do a task, and exit, e.g.
 * command-line tools.
 *
 * @author gjoranv
 */
public class ConfigGetter<T extends ConfigInstance> {

    private final Class<T> clazz;
    private final ConfigSource source;

    /**
     * Creates a ConfigGetter for class <code>clazz</code>
     *
     * @param clazz a config class
     */
    public ConfigGetter(Class<T> clazz) {
        this(null, clazz);
    }

    /**
     * Creates a ConfigGetter for class <code>clazz</code> with the specified
     * {@link ConfigSource}.
     *
     * @param source a {@link ConfigSource}
     * @param clazz  a config class
     */
    public ConfigGetter(ConfigSource source, Class<T> clazz) {
        this.clazz = clazz;
        this.source = source;
    }

    /**
     * Returns an instance of the config class specified in the constructor.
     *
     * @param configId a config id to use when getting the config
     * @return an instance of a config class
     */
    public synchronized T getConfig(String configId) {
        ConfigSubscriber subscriber;
        ConfigHandle<T> h;
        if (source == null) {
            subscriber = new ConfigSubscriber();
        } else {
            subscriber = new ConfigSubscriber(source);
        }
        h = subscriber.subscribe(clazz, configId);
        subscriber.nextConfig();
        T ret = h.getConfig();
        subscriber.close();
        return ret;
    }

    /**
     * Creates a ConfigGetter instance and returns an instance of the config class <code>c</code>.
     *
     * @param c        a config class
     * @param configId a config id to use when getting the config
     * @return an instance of a config class
     */
    public static <T extends ConfigInstance> T getConfig(Class<T> c, String configId) {
        ConfigGetter<T> getter = new ConfigGetter<T>(c);
        return getter.getConfig(configId);
    }

    /**
     * Creates a ConfigGetter instance and returns an instance of the config class <code>c</code>.
     *
     * @param c        a config class
     * @param configId a config id to use when getting the config
     * @param source   a {@link ConfigSource}
     * @return an instance of a config class
     */
    public static <T extends ConfigInstance> T getConfig(Class<T> c, String configId, ConfigSource source) {
        ConfigGetter<T> getter = new ConfigGetter<T>(source, c);
        return getter.getConfig(configId);
    }
}