use of org.apache.metron.stellar.dsl.StellarFunction in project metron by apache.
the class SimpleFunctionResolverTest method testApply.
/**
* The function resolver should be able to instantiate an instance of the function's implementation.
*/
@Test
public void testApply() {
resolver.withClass(IAmAFunction.class);
final String functionName = "namespace_function";
StellarFunction fn = resolver.apply(functionName);
Assert.assertTrue(fn instanceof IAmAFunction);
}
use of org.apache.metron.stellar.dsl.StellarFunction in project metron by apache.
the class BaseFunctionResolver method resolveFunction.
/**
* Resolves a Stellar function from a given class.
* @param clazz The class.
*/
public static StellarFunctionInfo resolveFunction(Class<? extends StellarFunction> clazz) {
StellarFunctionInfo info = null;
// the class must be annotated
if (clazz.isAnnotationPresent(Stellar.class)) {
Stellar annotation = clazz.getAnnotation(Stellar.class);
String fullyQualifiedName = getNameFromAnnotation(annotation);
StellarFunction function = createFunction(clazz);
if (fullyQualifiedName != null && function != null) {
info = new StellarFunctionInfo(annotation.description(), fullyQualifiedName, annotation.params(), annotation.returns(), function);
}
}
return info;
}
use of org.apache.metron.stellar.dsl.StellarFunction in project metron by apache.
the class ClasspathFunctionResolver method resolvables.
/**
* Returns a set of classes that should undergo further interrogation for resolution
* (aka discovery) of Stellar functions.
*/
@Override
public Set<Class<? extends StellarFunction>> resolvables() {
ClassLoader[] cls = null;
if (this.classLoaders.size() == 0) {
LOG.warn("Using System classloader");
cls = new ClassLoader[] { getClass().getClassLoader() };
} else {
cls = new ClassLoader[this.classLoaders.size()];
for (int i = 0; i < this.classLoaders.size(); ++i) {
ClassLoader cl = this.classLoaders.get(i);
LOG.debug("Using classloader: " + cl.getClass().getCanonicalName());
cls[i] = cl;
}
}
FilterBuilder filterBuilder = new FilterBuilder();
excludes.forEach(excl -> {
if (excl != null) {
filterBuilder.exclude(excl);
}
});
includes.forEach(incl -> {
if (incl != null) {
filterBuilder.include(incl);
}
});
Set<String> classes = new HashSet<>();
Set<Class<? extends StellarFunction>> ret = new HashSet<>();
for (ClassLoader cl : cls) {
for (Class<?> c : ClassIndex.getAnnotated(Stellar.class, cl)) {
LOG.debug("{}: Found class: {}", cl.getClass().getCanonicalName(), c.getCanonicalName());
boolean isAssignable = StellarFunction.class.isAssignableFrom(c);
boolean isFiltered = filterBuilder.apply(c.getCanonicalName());
if (isAssignable && isFiltered) {
String className = c.getName();
if (!classes.contains(className)) {
LOG.debug("{}: Added class: {}", cl.getClass().getCanonicalName(), className);
ret.add((Class<? extends StellarFunction>) c);
classes.add(className);
}
}
}
}
return ret;
}
Aggregations