Package com.orientechnologies.orient.core.command.script

Examples of com.orientechnologies.orient.core.command.script.OScriptManager


    OLogManager.instance().warn(this, "execute : " + this.toString() + " at " + sdf.format(date));
    ODatabaseRecordThreadLocal.INSTANCE.set(db);
    this.document.field(PROP_STATUS, SCHEDULER_STATUS.RUNNING);
    this.document.field(PROP_STARTTIME, System.currentTimeMillis());
    this.document.save();
    OScriptManager scriptManager = null;
    Bindings binding = null;
    try {
      if (this.function == null)
        return;
      if (db != null && !(db instanceof ODatabaseRecordTx))
        db = db.getUnderlying();
      scriptManager = Orient.instance().getScriptManager();
      final ScriptEngine scriptEngine = scriptManager.getEngine(this.function.getLanguage());
      binding = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);

      for (OScriptInjection i : scriptManager.getInjections())
        i.bind(binding);
      binding.put("doc", this.document);
      if (db != null)
        binding.put("db", new OScriptDocumentDatabaseWrapper((ODatabaseRecordTx) db));
      binding.put("orient", new OScriptOrientWrapper(db));
      if (iArgs != null) {
        for (Entry<Object, Object> a : iArgs.entrySet()) {
          binding.put(a.getKey().toString(), a.getValue());
        }
        binding.put("params", iArgs.values().toArray());
      } else {
        binding.put("params", new Object[0]);
      }

      if (this.function.getLanguage() == null)
        throw new OConfigurationException("Database function '" + this.function.getName() + "' has no language");
      final String funcStr = scriptManager.getFunctionDefinition(this.function);
      if (funcStr != null) {
        try {
          scriptEngine.eval(funcStr);
        } catch (ScriptException e) {
          scriptManager.getErrorMessage(e, funcStr);
        }
      }
      if (scriptEngine instanceof Invocable) {
        final Invocable invocableEngine = (Invocable) scriptEngine;
        Object[] args = null;
        if (iArgs != null) {
          args = new Object[iArgs.size()];
          int i = 0;
          for (Entry<Object, Object> arg : iArgs.entrySet())
            args[i++] = arg.getValue();
        }
        invocableEngine.invokeFunction(this.function.getName(), args);
      }
    } catch (ScriptException e) {
      throw new OCommandScriptException("Error on execution of the script", this.function.getName(), e.getColumnNumber(), e);
    } catch (NoSuchMethodException e) {
      throw new OCommandScriptException("Error on execution of the script", this.function.getName(), 0, e);
    } catch (OCommandScriptException e) {
      throw e;
    } catch (Exception ex) {
      throw new OCommandScriptException("Unknown Exception", this.function.getName(), 0, ex);
    } finally {
      if (scriptManager != null && binding != null)
        scriptManager.unbind(binding);
      OLogManager.instance().warn(this, "Job : " + this.toString() + " Finished!");
      isRunning = false;
      this.document.field(PROP_STATUS, SCHEDULER_STATUS.WAITING);
      this.document.save();
    }
View Full Code Here


    ODatabaseRecordInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
    if (db != null && !(db instanceof ODatabaseRecordTx))
      db = db.getUnderlying();
    // final OFunction f = db.getMetadata().getFunctionLibrary().getFunction(funcName);
    final OScriptManager scriptManager = Orient.instance().getScriptManager();
    final ScriptEngine scriptEngine = scriptManager.getEngine(func.getLanguage());
    // final ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("javascript");
    final Bindings binding = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
    // final Bindings binding = scriptEngine.createBindings();

    for (OScriptInjection i : scriptManager.getInjections())
      i.bind(binding);
    binding.put("doc", iDocument);
    if (db != null) {
      binding.put("db", new OScriptDocumentDatabaseWrapper((ODatabaseRecordTx) db));
      binding.put("orient", new OScriptOrientWrapper(db));
    } else
      binding.put("orient", new OScriptOrientWrapper());

    // scriptEngine.setBindings(binding, ScriptContext.ENGINE_SCOPE);

    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.getErrorMessage(e, funcStr);
        }
      }
      if (scriptEngine instanceof Invocable) {
        final Invocable invocableEngine = (Invocable) scriptEngine;
        Object[] EMPTY = new Object[0];
        result = (String) invocableEngine.invokeFunction(func.getName(), EMPTY);
      }
    } catch (ScriptException e) {
      throw new OCommandScriptException("Error on execution of the script", func.getName(), e.getColumnNumber(), e);
    } catch (NoSuchMethodException e) {
      throw new OCommandScriptException("Error on execution of the script", func.getName(), 0, e);
    } catch (OCommandScriptException e) {
      // PASS THROUGH
      throw e;

    } finally {
      scriptManager.unbind(binding);
    }
    if (result == null) {
      return RESULT.RECORD_NOT_CHANGED;
    }
    return RESULT.valueOf(result);// result;
View Full Code Here

      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);
View Full Code Here

TOP

Related Classes of com.orientechnologies.orient.core.command.script.OScriptManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.