aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/main/java/com/yahoo/documentapi/messagebus/protocol/RoutingPolicyRepository.java
blob: 9e295be3a18ea577efab4f422bd17949523b3789 (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
// Copyright Yahoo. 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.messagebus.routing.RoutingPolicy;
import java.util.logging.Level;

import java.util.Map;
import java.util.logging.Logger;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Simon Thoresen Hult
 */
class RoutingPolicyRepository {

    private static final Logger log = Logger.getLogger(RoutingPolicyRepository.class.getName());
    private final Map<String, RoutingPolicyFactory> factories = new ConcurrentHashMap<String, RoutingPolicyFactory>();

    RoutingPolicyRepository() {
    }

    /**
     * Registers a routing policy factory for a given name.
     *
     * @param name    The name of the factory to register.
     * @param factory The factory to register.
     */
    void putFactory(String name, RoutingPolicyFactory factory) {
        factories.put(name, factory);
    }

    /**
     * Returns the routing policy factory for a given name.
     *
     * @param name The name of the factory to return.
     * @return The routing policy factory matching the criteria, or null.
     */
    private RoutingPolicyFactory getFactory(String name) {
        return factories.get(name);
    }

    /**
     * Creates and returns a routing policy using the named factory and the given parameter.
     *
     * @param name  The name of the factory to use.
     * @param param The parameter to pass to the factory.
     * @return The created policy.
     */
    RoutingPolicy createPolicy(String name, String param) {
        RoutingPolicyFactory factory = getFactory(name);
        if (factory == null) {
            log.log(Level.SEVERE, "No routing policy factory found for name '" + name + "'.");
            return null;
        }
        DocumentProtocolRoutingPolicy ret = factory.createPolicy(param);

        if (ret == null) {
            log.log(Level.SEVERE, "Routing policy factory " + factory.getClass().getName() + " failed to create a " +
                    "routing policy for parameter '" + name + "'.");
            return null;
        }

        return ret;
    }
}