Example usage for org.apache.lucene.expressions.js JavascriptCompiler compile

List of usage examples for org.apache.lucene.expressions.js JavascriptCompiler compile

Introduction

In this page you can find the example usage for org.apache.lucene.expressions.js JavascriptCompiler compile.

Prototype

public static Expression compile(String sourceText, Map<String, Method> functions, ClassLoader parent)
        throws ParseException 

Source Link

Document

Compiles the given expression with the supplied custom functions.

Usage

From source file:com.o19s.es.ltr.utils.Scripting.java

License:Apache License

public static Object compile(String scriptSource) {
    // classloader created here
    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(new SpecialPermission());
    }//from  ww  w .j  a  v a2 s . c  o m
    return AccessController.doPrivileged(new PrivilegedAction<Expression>() {
        @Override
        public Expression run() {
            try {
                // snapshot our context here, we check on behalf of the expression
                AccessControlContext engineContext = AccessController.getContext();
                ClassLoader loader = getClass().getClassLoader();
                if (sm != null) {
                    loader = new ClassLoader(loader) {
                        @Override
                        protected Class<?> loadClass(String name, boolean resolve)
                                throws ClassNotFoundException {
                            try {
                                engineContext.checkPermission(new ClassPermission(name));
                            } catch (SecurityException e) {
                                throw new ClassNotFoundException(name, e);
                            }
                            return super.loadClass(name, resolve);
                        }
                    };
                }
                // NOTE: validation is delayed to allow runtime vars, and we don't have access to per index stuff here
                return JavascriptCompiler.compile(scriptSource, JavascriptCompiler.DEFAULT_FUNCTIONS, loader);
            } catch (ParseException e) {
                throw convertToScriptException("compile error", scriptSource, scriptSource, e);
            }
        }
    });
}

From source file:org.elasticsearch.script.expression.ExpressionScriptEngine.java

License:Apache License

@Override
public <T> T compile(String scriptName, String scriptSource, ScriptContext<T> context,
        Map<String, String> params) {
    // classloader created here
    final SecurityManager sm = System.getSecurityManager();
    SpecialPermission.check();/*from  ww w.  j  a va 2 s . co m*/
    Expression expr = AccessController.doPrivileged(new PrivilegedAction<Expression>() {
        @Override
        public Expression run() {
            try {
                // snapshot our context here, we check on behalf of the expression
                AccessControlContext engineContext = AccessController.getContext();
                ClassLoader loader = getClass().getClassLoader();
                if (sm != null) {
                    loader = new ClassLoader(loader) {
                        @Override
                        protected Class<?> loadClass(String name, boolean resolve)
                                throws ClassNotFoundException {
                            try {
                                engineContext.checkPermission(new ClassPermission(name));
                            } catch (SecurityException e) {
                                throw new ClassNotFoundException(name, e);
                            }
                            return super.loadClass(name, resolve);
                        }
                    };
                }
                // NOTE: validation is delayed to allow runtime vars, and we don't have access to per index stuff here
                return JavascriptCompiler.compile(scriptSource, JavascriptCompiler.DEFAULT_FUNCTIONS, loader);
            } catch (ParseException e) {
                throw convertToScriptException("compile error", scriptSource, scriptSource, e);
            }
        }
    });
    if (context.instanceClazz.equals(SearchScript.class)) {
        SearchScript.Factory factory = (p, lookup) -> newSearchScript(expr, lookup, p);
        return context.factoryClazz.cast(factory);
    } else if (context.instanceClazz.equals(ExecutableScript.class)) {
        ExecutableScript.Factory factory = (p) -> new ExpressionExecutableScript(expr, p);
        return context.factoryClazz.cast(factory);
    } else if (context.instanceClazz.equals(FilterScript.class)) {
        FilterScript.Factory factory = (p, lookup) -> newFilterScript(expr, lookup, p);
        return context.factoryClazz.cast(factory);
    }
    throw new IllegalArgumentException(
            "expression engine does not know how to handle script context [" + context.name + "]");
}

From source file:org.elasticsearch.script.expression.ExpressionScriptEngineService.java

License:Apache License

@Override
public Object compile(final String script, Map<String, String> params) {
    // classloader created here
    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(new SpecialPermission());
    }// www. jav a  2  s.com
    return AccessController.doPrivileged(new PrivilegedAction<Expression>() {
        @Override
        public Expression run() {
            try {
                // snapshot our context here, we check on behalf of the expression
                final AccessControlContext engineContext = AccessController.getContext();
                ClassLoader loader = getClass().getClassLoader();
                if (sm != null) {
                    loader = new ClassLoader(loader) {
                        @Override
                        protected Class<?> loadClass(String name, boolean resolve)
                                throws ClassNotFoundException {
                            try {
                                engineContext.checkPermission(new ClassPermission(name));
                            } catch (SecurityException e) {
                                throw new ClassNotFoundException(name, e);
                            }
                            return super.loadClass(name, resolve);
                        }
                    };
                }
                // NOTE: validation is delayed to allow runtime vars, and we don't have access to per index stuff here
                return JavascriptCompiler.compile(script, JavascriptCompiler.DEFAULT_FUNCTIONS, loader);
            } catch (ParseException e) {
                throw new ScriptException("Failed to parse expression: " + script, e);
            }
        }
    });
}