aboutsummaryrefslogtreecommitdiffstats
path: root/logserver/src/main/java/com/yahoo/logserver/filter/LogFilterManager.java
blob: 94905acfa18fb6aebc97d1a33c2af5bf22025bfc (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.logserver.filter;

import com.yahoo.log.LogLevel;

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

/**
 * The LogFilterManager keeps track of associations between
 * LogFilter names and instances, so that access to filters
 * is truly global.  It also manages the LogFilter namespace
 * to ensure that system-defined filters are not tampered with.
 *
 * @author Bjorn Borud
 */
@SuppressWarnings("deprecation")
public class LogFilterManager {
    private static final LogFilterManager instance;

    static {
        instance = new LogFilterManager();

        LevelFilter allEvents = new LevelFilter();
        allEvents.addLevel(LogLevel.EVENT);
        instance.addLogFilterInternal("system.allevents", allEvents);
        instance.addLogFilterInternal("system.all", new NullFilter());
        instance.addLogFilterInternal("system.mute", MuteFilter.getInstance());
    }

    private final Map<String, LogFilter> filters = new HashMap<String, LogFilter>();

    private LogFilterManager() {
    }

    /**
     * Public interface for adding a name-logfilter mapping. If
     * there exists a mapping already the old mapping is replaced
     * with the new mapping.
     * <p>
     * If the name is within the namespace reserved for internal
     * built-in filters it will throw an exception
     */
    public static void addLogFilter(String name, LogFilter filter) {
        if (filter == null) {
            throw new NullPointerException("filter cannot be null");
        }

        if (name == null) {
            throw new NullPointerException("name cannot be null");
        }

        String n = name.toLowerCase();

        if (n.startsWith("system.")) {
            throw new IllegalArgumentException("'system' namespace is reserved");
        }

        instance.addLogFilterInternal(n, filter);
    }

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


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

    /**
     * Internal method which takes care of the job of adding
     * LogFilter mappings but doesn't perform any of the checks
     * performed by the public method for adding mappings.
     */
    private void addLogFilterInternal(String name, LogFilter filter) {
        filters.put(name, filter);
    }
}