List of usage examples for javax.script Bindings get
public Object get(Object key);
From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java
public static Object[] createRowCopy(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { if (ArgList.length == 1) { try {/*from w w w .ja va2s .c o m*/ int newSize = (int) Math.round((Double) ArgList[0]); Object scmO = actualObject.get("row"); Object[] row = (Object[]) scmO; // TODO AKRETION ensure return RowDataUtil.createResizedCopy(row, newSize); } catch (Exception e) { throw new RuntimeException("Unable to create a row copy: " + Const.CR + e.toString()); } } else { throw new RuntimeException( "The function call createRowCopy requires a single arguments : the new size of the row"); } }
From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java
@SuppressWarnings("unused") public static Object fireToDB(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { Object oRC = new Object(); if (ArgList.length == 2) { try {/*from www . j a v a2 s . c o m*/ Object scmO = actualObject.get("_step_"); Script scm = (Script) scmO; String strDBName = (String) ArgList[0]; String strSQL = (String) ArgList[1]; DatabaseMeta ci = DatabaseMeta.findDatabase(scm.getTransMeta().getDatabases(), strDBName); if (ci == null) { throw new RuntimeException("Database connection not found: " + strDBName); } ci.shareVariablesWith(scm); Database db = new Database(scm, ci); db.setQueryLimit(0); try { if (scm.getTransMeta().isUsingUniqueConnections()) { synchronized (scm.getTrans()) { db.connect(scm.getTrans().getTransactionId(), scm.getPartitionID()); } } else { db.connect(scm.getPartitionID()); } ResultSet rs = db.openQuery(strSQL); ResultSetMetaData resultSetMetaData = rs.getMetaData(); int columnCount = resultSetMetaData.getColumnCount(); if (rs != null) { List<Object[]> list = new ArrayList<Object[]>(); while (rs.next()) { Object[] objRow = new Object[columnCount]; for (int i = 0; i < columnCount; i++) { objRow[i] = rs.getObject(i + 1); } list.add(objRow); } Object[][] resultArr = new Object[list.size()][]; list.toArray(resultArr); db.disconnect(); return resultArr; } } catch (Exception er) { throw new RuntimeException(er.toString()); } } catch (Exception e) { throw new RuntimeException(e.toString()); } } else { throw new RuntimeException("The function call fireToDB requires 2 arguments."); } return oRC; }
From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java
public static String getVariable(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { String sRC = ""; String sArg1 = ""; String sArg2 = ""; if (ArgList.length == 2) { try {/*from w ww.j a v a2 s .com*/ Object scmo = actualObject.get("_step_"); Object scmO = scmo; if (scmO instanceof Script) { Script scm = (Script) scmO; sArg1 = (String) ArgList[0]; sArg2 = (String) ArgList[1]; return scm.getVariable(sArg1, sArg2); } else { // running via the Test button in a dialog sArg2 = (String) ArgList[1]; return sArg2; } } catch (Exception e) { sRC = ""; } } else { throw new RuntimeException("The function call getVariable requires 2 arguments."); } return sRC; }
From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java
public static void writeToLog(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { switch (ArgList.length) { case 1:/* ww w.j av a 2 s . c o m*/ try { if (!isNull(ArgList) && !isUndefined(ArgList)) { Object scmO = actualObject.get("_step_"); Script scm = (Script) scmO; String strMessage = (String) ArgList[0]; scm.logDebug(strMessage); } } catch (Exception e) { // Ignore errors } break; case 2: try { if (!isNull(ArgList) && !isUndefined(ArgList)) { Object scmO = actualObject.get("_step_"); Script scm = (Script) scmO; String strType = ((String) ArgList[0]).toLowerCase(); String strMessage = (String) ArgList[1]; if (strType.equals("b")) { scm.logBasic(strMessage); } else if (strType.equals("d")) { scm.logDebug(strMessage); } else if (strType.equals("l")) { scm.logDetailed(strMessage); } else if (strType.equals("e")) { scm.logError(strMessage); } else if (strType.equals("m")) { scm.logMinimal(strMessage); } else if (strType.equals("r")) { scm.logRowlevel(strMessage); } } } catch (Exception e) { // Ignore errors } break; default: throw new RuntimeException("The function call writeToLog requires 1 or 2 arguments."); } }
From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java
public static void LoadScriptFromTab(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { try {// w w w . j av a 2 s . c om for (int i = 0; i < ArgList.length; i++) { // don't worry about "undefined" arguments String strToLoad = (String) ArgList[i]; String strScript = actualObject.get(strToLoad).toString(); actualContext.eval(strScript, actualObject); } } catch (Exception e) { // System.out.println(e.toString()); } }
From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java
public static void setVariable(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { String sArg1 = ""; String sArg2 = ""; String sArg3 = ""; if (ArgList.length == 3) { try {//from ww w .j a va 2s.co m Object scmo = actualObject.get("_step_"); Object scmO = scmo; if (scmO instanceof ScriptInterface) { ScriptInterface scm = (ScriptInterface) scmO; sArg1 = (String) ArgList[0]; sArg2 = (String) ArgList[1]; sArg3 = (String) ArgList[2]; if ("s".equals(sArg3)) { // System wide properties System.setProperty(sArg1, sArg2); // Set also all the way to the root as else we will take // stale values scm.setVariable(sArg1, sArg2); VariableSpace parentSpace = scm.getParentVariableSpace(); while (parentSpace != null) { parentSpace.setVariable(sArg1, sArg2); parentSpace = parentSpace.getParentVariableSpace(); } } else if ("r".equals(sArg3)) { // Upto the root... this should be the default. scm.setVariable(sArg1, sArg2); VariableSpace parentSpace = scm.getParentVariableSpace(); while (parentSpace != null) { parentSpace.setVariable(sArg1, sArg2); parentSpace = parentSpace.getParentVariableSpace(); } } else if ("p".equals(sArg3)) { // Upto the parent scm.setVariable(sArg1, sArg2); VariableSpace parentSpace = scm.getParentVariableSpace(); if (parentSpace != null) { parentSpace.setVariable(sArg1, sArg2); } } else if ("g".equals(sArg3)) { // Upto the grand parent scm.setVariable(sArg1, sArg2); VariableSpace parentSpace = scm.getParentVariableSpace(); if (parentSpace != null) { parentSpace.setVariable(sArg1, sArg2); VariableSpace grandParentSpace = parentSpace.getParentVariableSpace(); if (grandParentSpace != null) { grandParentSpace.setVariable(sArg1, sArg2); } } } else { throw new RuntimeException( "The argument type of function call setVariable should either be \"s\", \"r\", \"p\", or \"g\"."); } } // Else: Ignore for now... if we're executing via the Test Button } catch (Exception e) { throw new RuntimeException(e.toString()); } } else { throw new RuntimeException("The function call setVariable requires 3 arguments."); } }
From source file:org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.java
@Override public Traversal.Admin eval(final Bytecode bytecode, final Bindings bindings, final String traversalSource) throws ScriptException { // these validations occur before merging in bytecode bindings which will override existing ones. need to // extract the named traversalsource prior to that happening so that bytecode bindings can share the same // namespace as global bindings (e.g. traversalsources and graphs). if (traversalSource.equals(HIDDEN_G)) throw new IllegalArgumentException( "The traversalSource cannot have the name " + HIDDEN_G + " - it is reserved"); if (bindings.containsKey(HIDDEN_G)) throw new IllegalArgumentException("Bindings cannot include " + HIDDEN_G + " - it is reserved"); if (!bindings.containsKey(traversalSource)) throw new IllegalArgumentException( "The bindings available to the ScriptEngine do not contain a traversalSource named: " + traversalSource); final Object b = bindings.get(traversalSource); if (!(b instanceof TraversalSource)) throw new IllegalArgumentException(traversalSource + " is of type " + b.getClass().getSimpleName() + " and is not an instance of TraversalSource"); final Bindings inner = new SimpleBindings(); inner.putAll(bindings);/*from www . jav a2 s . co m*/ inner.putAll(bytecode.getBindings()); inner.put(HIDDEN_G, b); return (Traversal.Admin) this.eval(GroovyTranslator.of(HIDDEN_G).translate(bytecode), inner); }