aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/main/java/com/yahoo/messagebus/routing/test/CustomPolicyFactory.java
blob: 995dfa8831d7d5eadf31189eef7023dbd23312c2 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.routing.test;

import com.yahoo.messagebus.routing.Route;
import com.yahoo.messagebus.routing.RoutingPolicy;
import com.yahoo.messagebus.test.SimpleProtocol;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Simon Thoresen Hult
 */
public class CustomPolicyFactory implements SimpleProtocol.PolicyFactory {

    private boolean selectOnRetry;
    private final List<Integer> consumableErrors = new ArrayList<Integer>();

    public CustomPolicyFactory() {
        this(true);
    }

    public CustomPolicyFactory(boolean selectOnRetry) {
        this(selectOnRetry, new ArrayList<Integer>());
    }

    public CustomPolicyFactory(boolean selectOnRetry, int consumableError) {
        this(selectOnRetry, Arrays.asList(consumableError));
    }

    public CustomPolicyFactory(boolean selectOnRetry, List<Integer> consumableErrors) {
        this.selectOnRetry = selectOnRetry;
        this.consumableErrors.addAll(consumableErrors);
    }

    public RoutingPolicy create(String param) {
        return new CustomPolicy(selectOnRetry, consumableErrors, parseRoutes(param));
    }

    public static List<Route> parseRoutes(String routes) {
        List<Route> ret = new ArrayList<Route>();
        if (routes != null && !routes.isEmpty()) {
            for (String route : routes.split(",")) {
                Route r = Route.parse(route);
                assert(route.equals(r.toString()));
                ret.add(r);
            }
        }
        return ret;
    }
}