aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/SlobrokPolicy.java
blob: 5d653a956883722679e28f7d94ffc1a47c05efdb (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi.messagebus.protocol;

import com.yahoo.jrt.slobrok.api.IMirror;
import com.yahoo.jrt.slobrok.api.Mirror;
import com.yahoo.messagebus.routing.RoutingContext;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Abstract class for policies that allow you to specify which slobrok to use for the routing.
 */
public abstract class SlobrokPolicy implements DocumentProtocolRoutingPolicy {

    private boolean firstTry = true;

    protected List<Mirror.Entry> lookup(RoutingContext context, String pattern) {
        IMirror mirror1 =  context.getMirror();

        List<Mirror.Entry> arr = mirror1.lookup(pattern);

        if (arr.isEmpty()) {
            synchronized(this)  {
                if (firstTry) {
                    try {
                        int count = 0;
                        while (arr.isEmpty() && count < 100) {
                            Thread.sleep(50);
                            arr = mirror1.lookup(pattern);
                            count++;
                        }
                    } catch (InterruptedException e) {
                    }
                    firstTry = false;
                }
            }
        }

        return arr;
    }

    public static Map<String, String> parse(String param) {
        Map<String, String> map = new TreeMap<>();

        if (param != null) {
            String[] p = param.split(";");
            for (String s : p) {
                String[] keyValue = s.split("=");

                if (keyValue.length == 1) {
                    map.put(keyValue[0], "true");
                } else if (keyValue.length == 2) {
                    map.put(keyValue[0], keyValue[1]);
                }
            }
        }

        return map;
    }

}