summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/MetricsViewI.java
blob: 2f535e234f3e99db431ba6421226838912831d80 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package IceInternal;

public class MetricsViewI
{
    MetricsViewI(String name)
    {
        _name = name;
    }

    public boolean
    addOrUpdateMap(Ice.Properties properties, String mapName, MetricsAdminI.MetricsMapFactory<?> factory,
                   Ice.Logger logger)
    {
        //
        // Add maps to views configured with the given map.
        //
        String viewPrefix = "IceMX.Metrics." + _name + ".";
        String mapsPrefix = viewPrefix + "Map.";
        java.util.Map<String, String> mapsProps = properties.getPropertiesForPrefix(mapsPrefix);

        String mapPrefix;
        java.util.Map<String, String> mapProps = new java.util.HashMap<String, String>();
        if(!mapsProps.isEmpty())
        {
            mapPrefix = mapsPrefix + mapName + ".";
            mapProps = properties.getPropertiesForPrefix(mapPrefix);
            if(mapProps.isEmpty())
            {
                // This map isn't configured for this view.
                return _maps.remove(mapName) != null;
            }
        }
        else
        {
            mapPrefix = viewPrefix;
            mapProps = properties.getPropertiesForPrefix(mapPrefix);
        }

        if(properties.getPropertyAsInt(mapPrefix + "Disabled") > 0)
        {
            // This map is disabled for this view.
            return _maps.remove(mapName) != null;
        }

        MetricsMap<?> m = _maps.get(mapName);
        if(m != null && m.getProperties().equals(mapProps))
        {
            return false; // The map configuration didn't change, no need to re-create.
        }

        try
        {
            _maps.put(mapName, factory.create(mapPrefix, properties));
        }
        catch(Exception ex)
        {
            logger.warning("unexpected exception while creating metrics map:\n" + ex);
            _maps.remove(mapName);
        }
        return true;
    }

    public boolean
    removeMap(String mapName)
    {
        return _maps.remove(mapName) != null;
    }

    public java.util.Map<String, IceMX.Metrics[]>
    getMetrics()
    {
        java.util.Map<String, IceMX.Metrics[]> metrics = new java.util.HashMap<String, IceMX.Metrics[]>();
        for(java.util.Map.Entry<String, MetricsMap<?>> e : _maps.entrySet())
        {
            metrics.put(e.getKey(), e.getValue().getMetrics());
        }
        return metrics;
    }

    public IceMX.MetricsFailures[]
    getFailures(String mapName)
    {
        MetricsMap<?> m = _maps.get(mapName);
        if(m != null)
        {
            return m.getFailures();
        }
        return null;
    }

    public IceMX.MetricsFailures
    getFailures(String mapName, String id)
    {
        MetricsMap<?> m = _maps.get(mapName);
        if(m != null)
        {
            return m.getFailures(id);
        }
        return null;
    }

    public java.util.Collection<String>
    getMaps()
    {
        return _maps.keySet();
    }

    @SuppressWarnings("unchecked")
    public <T extends IceMX.Metrics> MetricsMap<T>
    getMap(String mapName, Class<T> cl)
    {
        return (MetricsMap<T>)_maps.get(mapName);
    }

    final private String _name;
    final private java.util.Map<String, MetricsMap<?>> _maps = new java.util.HashMap<String, MetricsMap<?>>();
};