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

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

/**
 *
 * The mode the config proxy can be running with.
 *
 * 'default' mode is requesting config from server, serving from cache only when known config
 * and no new config having been sent from server. When in 'memorycache' mode, there is no connection
 * to a config source, the proxy serves from memory cache only.
 *
 * @author hmusum
 */
class Mode {
    private final ModeName mode;

    enum ModeName {
        DEFAULT, MEMORYCACHE
    }

    Mode(ModeName modeName) {
        mode = modeName;
    }

    Mode(String modeString) {
        switch (modeString.toLowerCase()) {
            case "default" :
                mode = ModeName.DEFAULT;
                break;
            case "memorycache" :
                mode = ModeName.MEMORYCACHE;
                break;
            default:
                throw new IllegalArgumentException("Unrecognized mode '" + modeString + "' supplied. Legal modes are '" + Mode.modes() + "'");
        }
    }

    ModeName getMode() {
        return mode;
    }

    boolean isDefault() {
        return mode.equals(ModeName.DEFAULT);
    }

    boolean requiresConfigSource() {
        return mode.equals(ModeName.DEFAULT);
    }

    static Set<String> modes() {
        Set<String> modes = new HashSet<>();
        for (ModeName mode : ModeName.values()) {
            modes.add(mode.name().toLowerCase());
        }
        return modes;
    }

    String name() {
        return mode.name().toLowerCase();
    }

    @Override
    public String toString() {
        return mode.name().toLowerCase();
    }
}