All Downloads are FREE. Search and download functionalities are using the official Maven repository.

IceMX.ObserverFactory Maven / Gradle / Ivy

Go to download

Ice is a comprehensive RPC framework that helps you build distributed applications with minimal effort using familiar object-oriented idioms

The newest version!
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

package IceMX;

import IceInternal.MetricsMap;

public class ObserverFactory>
{
    public
    ObserverFactory(IceInternal.MetricsAdminI metrics, String name, Class cl)
    {
        _metrics = metrics;
        _name = name;
        _class = cl;
        _metrics.registerMap(name, _class, new Runnable()
            {
                @Override
                public void
                run()
                {
                    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.Entry>(_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 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;
}