
cdc.perfs.api.RuntimeManager Maven / Gradle / Ivy
package cdc.perfs.api;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import cdc.util.lang.InstallationException;
/**
* Main API used to create sources, probes and measures.
*
* @author Damien Carbonne
*
*/
public final class RuntimeManager {
private static final Logger LOGGER = LogManager.getLogger(RuntimeManager.class);
private static RuntimeService service;
private RuntimeManager() {
}
private static RuntimeService getRuntimeService() {
if (service == null) {
try {
@SuppressWarnings("unchecked")
final Class extends RuntimeService> cls = (Class extends RuntimeService>) Class.forName("cdc.perfs.runtime.RuntimeEnvironment");
final Method m = cls.getMethod("getInstance");
service = (RuntimeService) m.invoke(null);
} catch (final ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
LOGGER.catching(e);
throw new InstallationException("Failed to retrieve RuntimeService implementation", e);
}
}
return service;
}
/**
* Returns the shared source that has a given name.
*
* If a source with that name already exists, then it returns it.
* Otherwise, creates a new Source and returns it.
*
* @param name Name of the source.
* @return The shared source that is named name.
*/
public static Source getSource(String name) {
return getRuntimeService().getSource(name);
}
/**
* Returns the source associated to a class.
*
* Equivalent to getSource(klass.getCanonicalName())
*
* @param cls The class whose name will be associated to source.
* @return getSource(cls.getCanonicalName())
*/
public static Source getSource(Class> cls) {
return getRuntimeService().getSource(cls);
}
/**
* Creates a probe that can be used to create measures at a given level and
* associated to a source.
*
* @param source The source for which a probe must be created.
* @param level The level of generated measures.
* @return An appropriate probe (matching source and level).
*/
public static RuntimeProbe createProbe(Source source,
MeasureLevel level) {
return getRuntimeService().createProbe(source, level);
}
/**
* Creates a probe at the MINOR level.
*
* @param source The source for which a probe must be created.
* @return An appropriate probe (matching source and MINOR level).
*/
public static RuntimeProbe createProbe(Source source) {
return getRuntimeService().createProbe(source);
}
public static interface RuntimeService {
/**
* Returns the shared source that has a given name.
*
* If a source with that name already exists, then it returns it.
* Otherwise, creates a new Source and returns it.
*
* @param name Name of the source.
* @return The shared source that is named name.
*/
public Source getSource(String name);
/**
* Returns the source associated to a class.
*
* Equivalent to getSource(klass.getCanonicalName())
*
* @param cls The class whose name will be associated to source.
* @return getSource(cls.getCanonicalName())
*/
public default Source getSource(Class> cls) {
return getSource(cls.getCanonicalName());
}
/**
* Creates a probe that can be used to create measures at a given level and
* associated to a source.
*
* @param source The source for which a probe must be created.
* @param level The level of generated measures.
* @return An appropriate probe (matching source and level).
*/
public RuntimeProbe createProbe(Source source,
MeasureLevel level);
/**
* Creates a probe at the MINOR level.
*
* @param source The source for which a probe must be created.
* @return An appropriate probe (matching source and MINOR level).
*/
public RuntimeProbe createProbe(Source source);
}
}