com.zeroc.IceMX.ObserverFactory Maven / Gradle / Ivy
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
package com.zeroc.IceMX;
import com.zeroc.IceInternal.MetricsMap;
public class ObserverFactory>
{
public
ObserverFactory(com.zeroc.IceInternal.MetricsAdminI metrics, String name, Class cl)
{
_metrics = metrics;
_name = name;
_class = cl;
_metrics.registerMap(name, _class, () -> { update(); });
}
public void
destroy()
{
if(_metrics != null)
{
_metrics.unregisterMap(_name);
}
}
public O
getObserver(MetricsHelper helper, Class cl)
{
return getObserver(helper, null, cl);
}
@SuppressWarnings("unchecked")
public synchronized O
getObserver(MetricsHelper helper, Object observer, Class cl)
{
O old = null;
try
{
old = (O)observer;
}
catch(ClassCastException ex)
{
}
java.util.List.Entry> metricsObjects = null;
for(MetricsMap m : _maps)
{
MetricsMap.Entry e = m.getMatching(helper, old != null ? old.getEntry(m) : null);
if(e != null)
{
if(metricsObjects == null)
{
metricsObjects = new java.util.ArrayList<>(_maps.size());
}
metricsObjects.add(e);
}
}
if(metricsObjects == null)
{
if(old != null)
{
old.detach();
}
return null;
}
O obsv;
try
{
obsv = cl.getDeclaredConstructor().newInstance();
}
catch(Exception ex)
{
assert(false);
return null;
}
obsv.init(helper, metricsObjects, old);
return obsv;
}
public void
registerSubMap(String subMap, Class cl, java.lang.reflect.Field field)
{
_metrics.registerSubMap(_name, subMap, cl, field);
}
public boolean
isEnabled()
{
return _enabled;
}
public void
update()
{
Runnable updater;
synchronized(this)
{
_maps.clear();
for(MetricsMap m : _metrics.getMaps(_name, _class))
{
_maps.add(m);
}
_enabled = !_maps.isEmpty();
updater = _updater;
}
if(updater != null)
{
updater.run();
}
}
public synchronized void
setUpdater(Runnable updater)
{
_updater = updater;
}
private final com.zeroc.IceInternal.MetricsAdminI _metrics;
private final String _name;
private final Class _class;
private java.util.List> _maps = new java.util.ArrayList<>();
private volatile boolean _enabled;
private Runnable _updater;
}