aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/main/java/com/yahoo/config/subscription/impl/JarConfigSubscription.java
blob: a75e1d0b97674b01be958b34f613b7789399a98a (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 Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.subscription.impl;

import com.yahoo.config.ConfigInstance;
import com.yahoo.config.ConfigurationRuntimeException;
import com.yahoo.config.subscription.CfgConfigPayloadBuilder;
import com.yahoo.config.subscription.ConfigInterruptedException;
import com.yahoo.io.IOUtils;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.config.ConfigPayload;
import com.yahoo.vespa.config.PayloadChecksums;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

/**
 * Subscription to use when config id is jar:.../foo.jar[!/pathInJar/]
 *
 * @author Vegard Havdal
 * @author gjoranv
 */
public class JarConfigSubscription<T extends ConfigInstance> extends ConfigSubscription<T> {

    private final String jarName;
    private final String path;
    private ZipEntry zipEntry = null;

    // jar:configs/app.jar!/configs/
    JarConfigSubscription(ConfigKey<T> key, String jarName, String path) {
        super(key);
        this.jarName = jarName;
        this.path = path;
    }

    @Override
    public boolean nextConfig(long timeout) {
        if (checkReloaded()) {
            // Not supporting changing the payload for jar
            return true;
        }
        if (zipEntry == null) {
            // First time polled
            JarFile jarFile;
            try {
                jarFile = new JarFile(jarName);
            } catch (IOException e) {
                throw new IllegalArgumentException(e);
            }
            zipEntry = getEntry(jarFile, path);
            if (zipEntry == null)
                throw new IllegalArgumentException("Config '" + key.getName() + "' not found in '" + jarName + "!/" + path + "'.");
            T config;
            try {
                ConfigPayload payload = new CfgConfigPayloadBuilder().deserialize(Arrays.asList(IOUtils.readAll(new InputStreamReader(jarFile.getInputStream(zipEntry), StandardCharsets.UTF_8)).split("\n")));
                config = payload.toInstance(configClass, key.getConfigId());
            } catch (UnsupportedEncodingException e) {
                throw new IllegalStateException(e);
            } catch (IOException e) {
                throw new ConfigurationRuntimeException(e);
            }
            setConfig(0L, false, config, PayloadChecksums.empty());
            try {
                jarFile.close();
            } catch (IOException e) {
                throw new ConfigurationRuntimeException(e);
            }
            return true;
        }
        // TODO: Should wait and detect changes
        try {
            Thread.sleep(timeout);
        } catch (InterruptedException e) {
            throw new ConfigInterruptedException(e);
        }
        return false;
    }

    /**
     * Returns the entry corresponding to the ConfigInstance's defName in the given directory in
     * the given jar file.
     * The file's existence is checked elsewhere.
     */
    private ZipEntry getEntry(JarFile jarFile, String dir) {
        if (!dir.endsWith("/")) {
            dir = dir + '/';
        }
        return jarFile.getEntry(dir + getConfigFilename(key));
    }

    @Override
    public boolean subscribe(long timeout) {
        return true;
    }

}