summaryrefslogtreecommitdiffstats
path: root/logserver/src/main/java/com/yahoo/logserver/formatter/LogFormatterManager.java
blob: 1d0d29adb9e0ecbca73a55a221d0b660946cedf2 (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
66
67
68
69
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/*
 * $Id$
 *
 */

package com.yahoo.logserver.formatter;

import java.util.HashMap;
import java.util.Map;

/**
 * This singleton class implements a central registry of LogFormatter
 * instances.
 *
 * @author Bjorn Borud
 */
public class LogFormatterManager {
    private static final LogFormatterManager instance;

    static {
        instance = new LogFormatterManager();
        instance.addLogFormatterInternal("system.textformatter", new TextFormatter());
        instance.addLogFormatterInternal("system.nullformatter", new NullFormatter());
    }

    private final Map<String, LogFormatter> logFormatters = new HashMap<String, LogFormatter>();

    private LogFormatterManager() {}

    /**
     * LogFormatter lookup function
     *
     * @param name The name of the LogFormatter to be looked up.
     * @return Returns the LogFormatter associated with this name or
     * <code>null</code> if not found.
     */
    public static LogFormatter getLogFormatter(String name) {
        synchronized (instance.logFormatters) {
            return instance.logFormatters.get(name);
        }
    }

    /**
     * Get the names of the defined formatters.
     *
     * @return Returns an array containing the names of formatters that
     * have been registered.
     */
    public static String[] getFormatterNames() {
        synchronized (instance.logFormatters) {
            String[] formatterNames = new String[instance.logFormatters.keySet().size()];
            instance.logFormatters.keySet().toArray(formatterNames);
            return formatterNames;
        }
    }

    /**
     * Internal method which takes care of the job of adding
     * LogFormatter mappings but doesn't perform any of the checks
     * performed by the public method for adding mappings.
     */
    private void addLogFormatterInternal(String name, LogFormatter logFormatter) {
        synchronized (logFormatters) {
            logFormatters.put(name, logFormatter);
        }
    }

}