se.l4.vibe.internal.VibeImpl Maven / Gradle / Ivy
package se.l4.vibe.internal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import se.l4.vibe.Vibe;
import se.l4.vibe.VibeBuilder;
import se.l4.vibe.VibeException;
import se.l4.vibe.backend.VibeBackend;
import se.l4.vibe.builder.EventsBuilder;
import se.l4.vibe.builder.ProbeBuilder;
import se.l4.vibe.builder.SamplerBuilder;
import se.l4.vibe.builder.TimerBuilder;
import se.l4.vibe.event.Events;
import se.l4.vibe.internal.builder.EventsBuilderImpl;
import se.l4.vibe.internal.builder.ProbeBuilderImpl;
import se.l4.vibe.internal.builder.SamplerBuilderImpl;
import se.l4.vibe.internal.builder.TimerBuilderImpl;
import se.l4.vibe.internal.time.TimeSampler;
import se.l4.vibe.probes.Probe;
import se.l4.vibe.probes.SampledProbe;
import se.l4.vibe.probes.Sampler;
import se.l4.vibe.timer.Timer;
/**
* Implementation of {@link Vibe}.
*
* @author Andreas Holstenson
*
*/
public class VibeImpl
implements Vibe
{
private final Map instances;
private final BackendImpl backend;
private final TimeSampler sampler;
/**
* Create a new instance.
*
* @param backend
* backend to send all built instances to
* @param sampleInterval
* sampling interval in ms
* @param sampleRetention
* sample retention in ms
*/
public VibeImpl(VibeBackend backend, long sampleInterval)
{
instances = new ConcurrentHashMap();
this.backend = new BackendImpl();
if(backend != null)
{
this.backend.add(backend);
}
sampler = new TimeSampler(sampleInterval);
}
/**
* Start building a new {@link Vibe}.
*
* @return
*/
public static VibeBuilder builder()
{
return new DefaultVibeBuilder();
}
@Override
public ProbeBuilder probe(Probe probe)
{
return new ProbeBuilderImpl(backend, probe);
}
@Override
public SamplerBuilder sample(SampledProbe probe)
{
return new SamplerBuilderImpl(backend, sampler, probe);
}
public TimeSampler sampler()
{
return sampler;
}
@Override
public EventsBuilder events(Class base)
{
return new EventsBuilderImpl(backend);
}
@Override
public TimerBuilder timer()
{
return new TimerBuilderImpl(backend);
}
@Override
public Events getEvents(String path)
{
Object o = instances.get(path);
return o instanceof Events ? (Events) o : null;
}
@Override
public Probe getProbe(String path)
{
Object o = instances.get(path);
return o instanceof Probe ? (Probe) o : null;
}
@Override
public Sampler getTimeSeries(String path)
{
Object o = instances.get(path);
return o instanceof Sampler ? (Sampler) o : null;
}
@Override
public Timer getTimer(String path)
{
Object o = instances.get(path);
return o instanceof Timer ? (Timer) o : null;
}
@Override
public Vibe scope(String path)
{
return new ScopedVibe(this, backend, path);
}
@Override
public void addBackend(VibeBackend backend)
{
this.backend.add(backend);
}
private class BackendImpl
implements VibeBackend
{
private final List backends;
public BackendImpl()
{
backends = new ArrayList();
}
private void checkPathAndAdd(String path, Object o)
{
if(instances.containsKey(path))
{
throw new VibeException("Something has already been registered at " + path + ": " + instances.get(path));
}
instances.put(path, o);
}
@Override
public void export(String path, Events> events)
{
checkPathAndAdd(path, events);
for(VibeBackend vb : backends)
{
vb.export(path, events);
}
}
@Override
public void export(String path, Probe> probe)
{
checkPathAndAdd(path, probe);
for(VibeBackend vb : backends)
{
vb.export(path, probe);
}
}
@Override
public void export(String path, Sampler> series)
{
checkPathAndAdd(path, series);
for(VibeBackend vb : backends)
{
vb.export(path, series);
}
}
@Override
public void export(String path, Timer timer)
{
checkPathAndAdd(path, timer);
for(VibeBackend vb : backends)
{
vb.export(path, timer);
}
}
@Override
public void close()
{
for(VibeBackend vb : backends)
{
vb.close();
}
}
public void add(VibeBackend backend)
{
backends.add(backend);
for(Map.Entry e : instances.entrySet())
{
String path = e.getKey();
Object o = e.getValue();
if(o instanceof Events)
{
backend.export(path, (Events) o);
}
else if(o instanceof Probe)
{
backend.export(path, (Probe) o);
}
else if(o instanceof Sampler)
{
backend.export(path, (Sampler) o);
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy