List of usage examples for javax.script Bindings get
public Object get(Object key);
From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java
public static Object fireToDB(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { Object oRC = new Object(); if (ArgList.length == 2) { try {//from www . j av a 2 s . c o m Object scmO = actualObject.get("_step_"); ScriptValuesMod scm = (ScriptValuesMod) 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().getThreadName(), 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 Object getTransformationName(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { try {/*from ww w . ja v a 2 s .co m*/ Object objTranName = actualObject.get("_TransformationName_"); return objTranName; } catch (Exception e) { throw new RuntimeException(e.toString()); } }
From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.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 w w .j a v a 2 s.co m Object scmo = actualObject.get("_step_"); Object scmO = scmo; if (scmO instanceof ScriptValuesMod) { ScriptValuesMod scm = (ScriptValuesMod) 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:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java
public static void writeToLog(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { switch (ArgList.length) { case 1:/*w w w.j a v a 2 s .com*/ try { if (!isNull(ArgList) && !isUndefined(ArgList)) { Object scmO = actualObject.get("_step_"); ScriptValuesMod scm = (ScriptValuesMod) scmO; String strMessage = (String) ArgList[0]; scm.logDebug(strMessage); } } catch (Exception e) { } break; case 2: try { if (!isNull(ArgList) && !isUndefined(ArgList)) { Object scmO = actualObject.get("_step_"); ScriptValuesMod scm = (ScriptValuesMod) scmO; String strType = (String) ArgList[0]; String strMessage = (String) ArgList[1]; if (strType.toLowerCase().equals("b")) scm.logBasic(strMessage); else if (strType.toLowerCase().equals("d")) scm.logDebug(strMessage); else if (strType.toLowerCase().equals("l")) scm.logDetailed(strMessage); else if (strType.toLowerCase().equals("e")) scm.logError(strMessage); else if (strType.toLowerCase().equals("m")) scm.logMinimal(strMessage); else if (strType.toLowerCase().equals("r")) scm.logRowlevel(strMessage); } } catch (Exception e) { } break; default: throw new RuntimeException("The function call writeToLog requires 1 or 2 arguments."); } }
From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java
public static void LoadScriptFromTab(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { try {//from w ww.j a v a 2s. c o m 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 RowMetaInterface getOutputRowMeta(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { if (ArgList.length == 0) { try {/*from w w w. j a va2 s . c o m*/ Object scmO = actualObject.get("_step_"); try { Script scm = (Script) scmO; return scm.getOutputRowMeta(); } catch (Exception e) { ScriptDummy scm = (ScriptDummy) scmO; return scm.getOutputRowMeta(); } } catch (Exception e) { throw new RuntimeException( "Unable to get the output row metadata because of an error: " + Const.CR + e.toString()); } } else { throw new RuntimeException("The function call getOutputRowMeta doesn't require arguments."); } }
From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java
public static RowMetaInterface getInputRowMeta(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { if (ArgList.length == 0) { try {/* w ww . j av a 2s .c o m*/ Object scmO = actualObject.get("_step_"); try { Script scm = (Script) scmO; return scm.getInputRowMeta(); } catch (Exception e) { ScriptDummy scm = (ScriptDummy) scmO; return scm.getInputRowMeta(); } } catch (Exception e) { throw new RuntimeException( "Unable to get the input row metadata because of an error: " + Const.CR + e.toString()); } } else { throw new RuntimeException("The function call getInputRowMeta doesn't require arguments."); } }
From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java
public static double getProcessCount(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { if (ArgList.length == 1) { try {/*from w w w . ja va 2 s.c o m*/ Object scmO = actualObject.get("_step_"); ScriptInterface scm = (ScriptInterface) scmO; String strType = ((String) ArgList[0]).toLowerCase(); if (strType.equals("i")) { return scm.getLinesInput(); } else if (strType.equals("o")) { return scm.getLinesOutput(); } else if (strType.equals("r")) { return scm.getLinesRead(); } else if (strType.equals("u")) { return scm.getLinesUpdated(); } else if (strType.equals("w")) { return scm.getLinesWritten(); } else if (strType.equals("e")) { return scm.getLinesRejected(); } else { return 0; } } catch (Exception e) { // throw new RuntimeException(e.toString()); return 0; } } else { throw new RuntimeException("The function call getProcessCount requires 1 argument."); } }
From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.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 w w w .j av a 2s .c o m*/ Object scmo = actualObject.get("_step_"); Object scmO = scmo; if (scmO instanceof StepInterface) { StepInterface scm = (StepInterface) 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.pentaho.di.trans.steps.script.ScriptAddedFunctions.java
public static void putRow(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) { if (ArgList.length == 1) { try {/*from w w w . j a va2 s . co m*/ Object[] newRow = (Object[]) ArgList[0]; Object scmO = actualObject.get("_step_"); try { Script step = (Script) scmO; step.putRow(step.getOutputRowMeta(), newRow); } catch (Exception e) { ScriptDummy step = (ScriptDummy) scmO; step.putRow(step.getOutputRowMeta(), newRow); } } catch (Exception e) { throw new RuntimeException("Unable to pass the new row to the next step(s) because of an error: " + Const.CR + e.toString()); } } else { throw new RuntimeException( "The function call putRow requires 1 argument : the output row data (Object[])"); } }