Search in sources :

Example 1 with OScriptManager

use of com.orientechnologies.orient.core.command.script.OScriptManager in project orientdb by orientechnologies.

the class OClassTrigger method executeFunction.

private RESULT executeFunction(final ODocument iDocument, final OFunction func) {
    if (func == null)
        return RESULT.RECORD_NOT_CHANGED;
    final OScriptManager scriptManager = Orient.instance().getScriptManager();
    final OPartitionedObjectPool.PoolEntry<ScriptEngine> entry = scriptManager.acquireDatabaseEngine(database.getName(), func.getLanguage());
    final ScriptEngine scriptEngine = entry.object;
    try {
        final Bindings binding = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
        scriptManager.bind(binding, (ODatabaseDocumentTx) database, null, null);
        binding.put("doc", iDocument);
        String result = null;
        try {
            if (func.getLanguage() == null)
                throw new OConfigurationException("Database function '" + func.getName() + "' has no language");
            final String funcStr = scriptManager.getFunctionDefinition(func);
            if (funcStr != null) {
                try {
                    scriptEngine.eval(funcStr);
                } catch (ScriptException e) {
                    scriptManager.throwErrorMessage(e, funcStr);
                }
            }
            if (scriptEngine instanceof Invocable) {
                final Invocable invocableEngine = (Invocable) scriptEngine;
                Object[] EMPTY = OCommonConst.EMPTY_OBJECT_ARRAY;
                result = (String) invocableEngine.invokeFunction(func.getName(), EMPTY);
            }
        } catch (ScriptException e) {
            throw OException.wrapException(new OCommandScriptException("Error on execution of the script", func.getName(), e.getColumnNumber()), e);
        } catch (NoSuchMethodException e) {
            throw OException.wrapException(new OCommandScriptException("Error on execution of the script", func.getName(), 0), e);
        } catch (OCommandScriptException e) {
            // PASS THROUGH
            throw e;
        } finally {
            scriptManager.unbind(binding, null, null);
        }
        if (result == null) {
            return RESULT.RECORD_NOT_CHANGED;
        }
        return RESULT.valueOf(result);
    } finally {
        scriptManager.releaseDatabaseEngine(func.getLanguage(), database.getName(), entry);
    }
}
Also used : OCommandScriptException(com.orientechnologies.orient.core.command.script.OCommandScriptException) OScriptManager(com.orientechnologies.orient.core.command.script.OScriptManager) OCommandScriptException(com.orientechnologies.orient.core.command.script.OCommandScriptException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) OPartitionedObjectPool(com.orientechnologies.common.concur.resource.OPartitionedObjectPool)

Example 2 with OScriptManager

use of com.orientechnologies.orient.core.command.script.OScriptManager in project orientdb by orientechnologies.

the class OServerCommandGetSupportedLanguages method execute.

@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    String[] urlParts = checkSyntax(iRequest.url, 2, "Syntax error: supportedLanguages/<database>");
    iRequest.data.commandInfo = "Returns the supported languages";
    ODatabaseDocument db = null;
    try {
        db = getProfiledDatabaseInstance(iRequest);
        ODocument result = new ODocument();
        Set<String> languages = new HashSet<String>();
        OScriptManager scriptManager = Orient.instance().getScriptManager();
        for (String language : scriptManager.getSupportedLanguages()) {
            if (scriptManager.getFormatters() != null && scriptManager.getFormatters().get(language) != null) {
                languages.add(language);
            }
        }
        result.field("languages", languages);
        iResponse.writeRecord(result);
    } finally {
        if (db != null)
            db.close();
    }
    return false;
}
Also used : OScriptManager(com.orientechnologies.orient.core.command.script.OScriptManager) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) HashSet(java.util.HashSet)

Aggregations

OScriptManager (com.orientechnologies.orient.core.command.script.OScriptManager)2 OPartitionedObjectPool (com.orientechnologies.common.concur.resource.OPartitionedObjectPool)1 OCommandScriptException (com.orientechnologies.orient.core.command.script.OCommandScriptException)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 HashSet (java.util.HashSet)1