aboutsummaryrefslogtreecommitdiffstats
path: root/zkfacade/src/main/java/com/yahoo/vespa/curator/NodeCacheWrapper.java
blob: 9c72c17cbc02658c82f22193bd2ffb867d5b6508 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;

import java.io.IOException;

/**
 * A file cache backed by a curator node cache.
 *
 * @author bratseth
 */
class NodeCacheWrapper implements Curator.FileCache {

    private final NodeCache wrapped;

    public NodeCacheWrapper(CuratorFramework curatorFramework, String path, boolean dataIsCompressed) {
        wrapped = new NodeCache(curatorFramework, path, dataIsCompressed);
    }

    @Override
    public void start() {
        try {
            wrapped.start(true);
        } catch (Exception e) {
            throw new IllegalStateException("Could not start the Curator cache", e);
        }
    }

    @Override
    public void addListener(NodeCacheListener listener) {
        wrapped.getListenable().addListener(listener);

    }

    @Override
    public ChildData getCurrentData() {
        return wrapped.getCurrentData();
    }

    @Override
    public void close() {
        try {
            wrapped.close();
        } catch (IOException e) {
            throw new RuntimeException("Exception closing curator cache", e);
        }
    }

}