summaryrefslogtreecommitdiffstats
path: root/logserver/src/main/java/com/yahoo/logserver/handlers/replicator/ReplicatorPlugin.java
blob: 03bc66c8262e81ccfc4a9580ca5866b490118d67 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.logserver.handlers.replicator;

import java.io.IOException;
import java.util.logging.Logger;

import com.yahoo.log.LogLevel;
import com.yahoo.logserver.Server;
import com.yahoo.plugin.Config;
import com.yahoo.plugin.Plugin;

public class ReplicatorPlugin implements Plugin
{
    private static final String DEFAULT_PORT = "19083";
    private static final Logger log =
            Logger.getLogger(ReplicatorPlugin.class.getName());
    private Replicator replicator;
    private final Server server = Server.getInstance();

    public String getPluginName () {
        return "replicator";
    }

    /**
     * Initialize the replicator plugin
     */
    public void initPlugin (Config config) {

        if (replicator != null) {
            throw new IllegalStateException(
                    "plugin already initialized: " + getPluginName());
        }
        int listenPort = config.getInt("port", DEFAULT_PORT);
        String threadName = config.get("thread", getPluginName());
        try {
            replicator = new Replicator(listenPort);
        }
        catch (IOException e) {
            log.log(LogLevel.WARNING, "init failed: " + e);
            return;
        }
        server.registerLogHandler(replicator, threadName);
    }

    /**
     * Shut down the replicator plugin.
     */
    public void shutdownPlugin () {

        if (replicator == null) {
            throw new IllegalStateException(
                    "plugin not initialized: " + getPluginName());
        }
        server.unregisterLogHandler(replicator);
        replicator = null;
    }

}