Search in sources :

Example 1 with StellarFunction

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);
}
Also used : BaseStellarFunction(org.apache.metron.stellar.dsl.BaseStellarFunction) StellarFunction(org.apache.metron.stellar.dsl.StellarFunction) Test(org.junit.Test)

Example 2 with StellarFunction

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;
}
Also used : StellarFunctionInfo(org.apache.metron.stellar.dsl.StellarFunctionInfo) StellarFunction(org.apache.metron.stellar.dsl.StellarFunction) Stellar(org.apache.metron.stellar.dsl.Stellar)

Example 3 with StellarFunction

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;
}
Also used : StellarFunction(org.apache.metron.stellar.dsl.StellarFunction) FilterBuilder(org.reflections.util.FilterBuilder) VFSClassLoader(org.apache.commons.vfs2.impl.VFSClassLoader) HashSet(java.util.HashSet)

Aggregations

StellarFunction (org.apache.metron.stellar.dsl.StellarFunction)3 HashSet (java.util.HashSet)1 VFSClassLoader (org.apache.commons.vfs2.impl.VFSClassLoader)1 BaseStellarFunction (org.apache.metron.stellar.dsl.BaseStellarFunction)1 Stellar (org.apache.metron.stellar.dsl.Stellar)1 StellarFunctionInfo (org.apache.metron.stellar.dsl.StellarFunctionInfo)1 Test (org.junit.Test)1 FilterBuilder (org.reflections.util.FilterBuilder)1