/** * An example of a variable-arguments method. * * All variable arguments methods must have the same number and * types of parameters, and must be static. <p> * @param cx the Context of the current thread * @param thisObj the JavaScript 'this' value. * @param args the array of arguments for this call * @param funObj the function object of the invoked JavaScript function * This value is useful to compute a scope using * Context.getTopLevelScope(). * @return computes the string values and types of 'this' and * of each of the supplied arguments and returns them in a string. * * @see org.mozilla.javascript.ScriptableObject#getTopLevelScope */ @JSFunction public static Object varargs(Context cx, Scriptable thisObj, Object[] args, Function funObj) { StringBuffer buf = new StringBuffer(); buf.append("this = "); buf.append(Context.toString(thisObj)); buf.append("; args = ["); for (int i=0; i < args.length; i++) { buf.append(Context.toString(args[i])); if (i+1 != args.length) buf.append(", "); } buf.append("]"); return buf.toString(); }
/** * Wraps {@link Job#setSpeculativeExecution(boolean)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setSpeculativeExecution(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { if (args.length == 1) { if (JavaScriptUtils.isDefined(args[0])) { if (!(args[0] instanceof Boolean)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_MUST_BE_BOOL); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } ((JobWrap)thisObj).job.setSpeculativeExecution(Boolean.valueOf(args[0].toString())); return thisObj; }
/** * Wraps {@link CounterGroup#findCounter(String)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return the counter */ @JSFunction public static Object findCounter(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; if (args.length < 1) { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } final CounterGroupWrap self = (CounterGroupWrap)thisObj; final Counter counter = self.counterGroup.findCounter(arg0.toString()); CounterWrap counterWrap = null; if (counter != null) { counterWrap = CounterWrap.getInstance(self.runtime, counter); } return counterWrap == null ? Undefined.instance : counterWrap; }
/** * Wraps {@link Job#setMapSpeculativeExecution(boolean)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setMapSpeculativeExecution(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { if (args.length == 1) { if (JavaScriptUtils.isDefined(args[0])) { if (!(args[0] instanceof Boolean)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_MUST_BE_BOOL); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } ((JobWrap)thisObj).job.setMapSpeculativeExecution(Boolean.valueOf(args[0].toString())); return thisObj; }
/** * Wraps {@link Configuration#get(String)} and {@link Configuration#get(String, String)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return the string configuration value */ @JSFunction public static Object get(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; if (args.length < 1) { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_OR_TWO_ARGS_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } final ConfigurationWrap self = (ConfigurationWrap)thisObj; final Object arg1 = args.length >= 2 ? args[1] : Undefined.instance; String confStr; if (JavaScriptUtils.isDefined(arg0) && JavaScriptUtils.isDefined(arg1)) { confStr = self.conf.get(arg0.toString(), arg1.toString()); } else { confStr = self.conf.get(arg0.toString()); } return confStr == null ? Context.getUndefinedValue() : confStr; }
/** * Wraps {@link Configuration#getFloat(String, float)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return the float configuration value */ @JSFunction public static Object getFloat(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; final Object arg1 = args.length >= 2 ? args[1] : Undefined.instance; if (args.length < 2) { throw Utils.makeError(ctx, thisObj, LembosMessages.TWO_ARGS_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } else if (!JavaScriptUtils.isDefined(arg1)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_REQUIRED); } else if (!(arg1 instanceof Number)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_ARG_MUST_BE_NUM); } // Converted to double because Float is not something supported by JavaScript return Double.valueOf(Float.toString(((ConfigurationWrap)thisObj).conf .getFloat(arg0.toString(), JavaScriptUtils.fromNumber(arg1).floatValue()))); }
/** * Wraps {@link Configuration#getInt(String, int)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return the int configuration value */ @JSFunction public static Object getInt(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; final Object arg1 = args.length >= 2 ? args[1] : Undefined.instance; if (args.length < 2) { throw Utils.makeError(ctx, thisObj, LembosMessages.TWO_ARGS_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } else if (!JavaScriptUtils.isDefined(arg1)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_REQUIRED); } else if (!(arg1 instanceof Number)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_ARG_MUST_BE_NUM); } return ((ConfigurationWrap)thisObj).conf.getInt(arg0.toString(), JavaScriptUtils.fromNumber(arg1).intValue()); }
/** * Wraps {@link Configuration#getLong(String, long)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return the int configuration value */ @JSFunction public static Object getLong(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; final Object arg1 = args.length >= 2 ? args[1] : Undefined.instance; if (args.length < 2) { throw Utils.makeError(ctx, thisObj, LembosMessages.TWO_ARGS_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } else if (!JavaScriptUtils.isDefined(arg1)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_REQUIRED); } else if (!(arg1 instanceof Number)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_ARG_MUST_BE_NUM); } return ((ConfigurationWrap)thisObj).conf.getLong(arg0.toString(), JavaScriptUtils.fromNumber(arg1).longValue()); }
/** * Wraps {@link Configuration#set(String, String)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object set(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; final Object arg1 = args.length >= 2 ? args[1] : Undefined.instance; if (args.length < 2) { throw Utils.makeError(ctx, thisObj, LembosMessages.TWO_ARGS_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } else if (!JavaScriptUtils.isDefined(arg1)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_REQUIRED); } ((ConfigurationWrap)thisObj).conf.set(arg0.toString(), arg1.toString()); return thisObj; }
/** * Wraps {@link Configuration#setBoolean(String, boolean)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setBoolean(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; final Object arg1 = args.length >= 2 ? args[1] : Undefined.instance; if (args.length < 2) { throw Utils.makeError(ctx, thisObj, LembosMessages.TWO_ARGS_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } else if (!JavaScriptUtils.isDefined(arg1)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_REQUIRED); } else if (!(arg1 instanceof Boolean)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_MUST_BE_BOOL); } ((ConfigurationWrap)thisObj).conf.setBoolean(arg0.toString(), (Boolean)arg1); return thisObj; }
/** * Wraps {@link Configuration#setBooleanIfUnset(String, boolean)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setBooleanIfUnset(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; final Object arg1 = args.length >= 2 ? args[1] : Undefined.instance; if (args.length < 2) { throw Utils.makeError(ctx, thisObj, LembosMessages.TWO_ARGS_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } else if (!JavaScriptUtils.isDefined(arg1)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_REQUIRED); } else if (!(arg1 instanceof Boolean)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_MUST_BE_BOOL); } ((ConfigurationWrap)thisObj).conf.setBooleanIfUnset(arg0.toString(), (Boolean)arg1); return thisObj; }
/** * Wraps {@link Configuration#setFloat(String, float)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setFloat(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; final Object arg1 = args.length >= 2 ? args[1] : Undefined.instance; if (args.length < 2) { throw Utils.makeError(ctx, thisObj, LembosMessages.TWO_ARGS_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } else if (!JavaScriptUtils.isDefined(arg1)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_REQUIRED); } else if (!(arg1 instanceof Number)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_ARG_MUST_BE_NUM); } ((ConfigurationWrap)thisObj).conf.setFloat(arg0.toString(), JavaScriptUtils.fromNumber(arg1).floatValue()); return thisObj; }
/** * Wraps {@link Configuration#setIfUnset(String, String)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setIfUnset(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; final Object arg1 = args.length >= 2 ? args[1] : Undefined.instance; if (args.length < 2) { throw Utils.makeError(ctx, thisObj, LembosMessages.TWO_ARGS_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } else if (!JavaScriptUtils.isDefined(arg1)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_REQUIRED); } ((ConfigurationWrap)thisObj).conf.setIfUnset(arg0.toString(), arg1.toString()); return thisObj; }
/** * Wraps {@link Configuration#setInt(String, int)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setInt(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; final Object arg1 = args.length >= 2 ? args[1] : Undefined.instance; if (args.length < 2) { throw Utils.makeError(ctx, thisObj, LembosMessages.TWO_ARGS_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } else if (!JavaScriptUtils.isDefined(arg1)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_REQUIRED); } else if (!(arg1 instanceof Number)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_ARG_MUST_BE_NUM); } ((ConfigurationWrap)thisObj).conf.setInt(arg0.toString(), JavaScriptUtils.fromNumber(arg1).intValue()); return thisObj; }
/** * Wraps {@link Configuration#setLong(String, long)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setLong(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; final Object arg1 = args.length >= 2 ? args[1] : Undefined.instance; if (args.length < 2) { throw Utils.makeError(ctx, thisObj, LembosMessages.TWO_ARGS_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } else if (!JavaScriptUtils.isDefined(arg1)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_REQUIRED); } else if (!(arg1 instanceof Number)) { throw Utils.makeError(ctx, thisObj, LembosMessages.SECOND_ARG_ARG_MUST_BE_NUM); } ((ConfigurationWrap)thisObj).conf.setLong(arg0.toString(), JavaScriptUtils.fromNumber(arg1).longValue()); return thisObj; }
/** * Wraps {@link TaskInputOutputContext#write(Object, Object)} to conditionally serialize the key and value to * the appropriate {@link org.apache.hadoop.io.WritableComparable} and {@link org.apache.hadoop.io.Writable}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call (unused) * @param func the function called (unused) */ @JSFunction @SuppressWarnings({ "unchecked" // Unavoidable }) public static void write(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { if (args.length != 2) { throw Utils.makeError(ctx, thisObj, "Two arguments expected"); } try { final TaskInputOutputContextWrap self = (TaskInputOutputContextWrap)thisObj; final Object jsKey = args[0]; final Object jsVal = args[1]; self.context.write(ConversionUtils.jsToWritableComparable(jsKey, self.scope), ConversionUtils.jsToWritable(jsVal, self.scope)); } catch (InterruptedException | IOException e) { throw Utils.makeError(ctx, thisObj, "Unexpected exception: " + e.getMessage()); } }
/** * Wraps {@link Job#setReduceSpeculativeExecution(boolean)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setReduceSpeculativeExecution(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { if (args.length == 1) { if (JavaScriptUtils.isDefined(args[0])) { if (!(args[0] instanceof Boolean)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_MUST_BE_BOOL); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } ((JobWrap)thisObj).job.setReduceSpeculativeExecution(Boolean.valueOf(args[0].toString())); return thisObj; }
/** * Wraps {@link Counters#getGroup(String)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return the counter group */ @JSFunction public static Object getGroup(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; if (args.length < 1) { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } else if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } final CountersWrap self = (CountersWrap)thisObj; final CounterGroup counterGroup = self.counters.getGroup(arg0.toString()); CounterGroupWrap counterGroupWrap = null; if (counterGroup != null) { counterGroupWrap = CounterGroupWrap.getInstance(self.runtime, counterGroup); } return counterGroupWrap == null ? Undefined.instance : counterGroupWrap; }
/** * Wraps {@link Counters#incrAllCounters(Counters)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object incrAllCounters(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } else if (!(arg0 instanceof CountersWrap)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_MUST_BE_COUNTERS); } ((CountersWrap)thisObj).counters.incrAllCounters(((CountersWrap)arg0).counters); return thisObj; }
/** * Wraps {@link Job#getCounters()}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return the counters wrapper */ @JSFunction public static Object getCounters(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final JobWrap self = (JobWrap)thisObj; Counters counters; try { counters = self.job.getCounters(); } catch (IOException e) { throw Utils.makeError(ctx, thisObj, e.getMessage()); } CountersWrap countersWrap = null; if (counters != null) { countersWrap = CountersWrap.getInstance(self.runtime, counters); } return countersWrap == null ? Undefined.instance : countersWrap; }
/** * Wraps {@link Job#setJobName(String)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setJobName(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { if (args.length == 1) { if (!JavaScriptUtils.isDefined(args[0])) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } ((JobWrap)thisObj).job.setJobName(args[0].toString()); return thisObj; }
/** * Wraps {@link Job#waitForCompletion(boolean)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return the result of the job */ @JSFunction public static Object waitForCompletion(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { if (args.length == 1) { if (JavaScriptUtils.isDefined(args[0])) { if (!(args[0] instanceof Boolean)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_MUST_BE_BOOL); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } try { return ((JobWrap)thisObj).job.waitForCompletion(Boolean.valueOf(args[0].toString())); } catch (ClassNotFoundException | InterruptedException | IOException e) { throw Utils.makeError(ctx, thisObj, e.getMessage()); } }
/** * Wraps {@link Job#setWorkingDirectory(Path)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setWorkingDirectory(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { if (args.length == 1) { if (!JavaScriptUtils.isDefined(args[0])) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } try { ((JobWrap)thisObj).job.setWorkingDirectory(new Path(URI.create(args[0].toString()))); } catch (IOException e) { throw Utils.makeError(ctx, thisObj, e.getMessage()); } return thisObj; }
/** * Wraps {@link Counter#setValue(long)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setValue(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { final Object arg0 = args.length >= 1 ? args[0] : Undefined.instance; if (!JavaScriptUtils.isDefined(arg0)) { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } else if (!(arg0 instanceof Number)) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_MUST_BE_NUM); } ((CounterWrap)thisObj).counter.setValue(JavaScriptUtils.fromNumber(arg0).longValue()); return thisObj; }
/** * Wraps {@link Job#setJarByClass(Class)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setJarByClass(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { if (args.length == 1) { if (!JavaScriptUtils.isDefined(args[0])) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } final String className = args[0].toString(); try { ((JobWrap)thisObj).job.setJarByClass(Class.forName(className)); } catch (ClassNotFoundException e) { throw Utils.makeError(ctx, thisObj, LembosMessages.CLASS_NOT_FOUND); } return thisObj; }
@JSFunction public void write(Object message) { if (message instanceof String) { message = ((String) message).replaceAll("\n$", ""); } LOGGER.info(message); }
private static String getPropertyName(String methodName, String prefix, Annotation annotation) { if (prefix != null) { return methodName.substring(prefix.length()); } String propName = null; if (annotation instanceof JSGetter) { propName = ((JSGetter) annotation).value(); if (propName == null || propName.length() == 0) { if (methodName.length() > 1 && (methodName.startsWith("取") || methodName.startsWith("get"))) { propName = methodName.substring(3); if (Character.isUpperCase(propName.charAt(0))) { if (propName.length() == 1) { propName = propName.toLowerCase(); } else if (!Character.isUpperCase(propName.charAt(1))){ propName = Character.toLowerCase(propName.charAt(0)) + propName.substring(1); } } } } } else if (annotation instanceof JSFunction) { propName = ((JSFunction) annotation).value(); } else if (annotation instanceof JSStaticFunction) { propName = ((JSStaticFunction) annotation).value(); } if (propName == null || propName.length() == 0) { propName = methodName; } return propName; }
private static String getPropertyName(String methodName, String prefix, Annotation annotation) { if (prefix != null) { return methodName.substring(prefix.length()); } String propName = null; if (annotation instanceof JSGetter) { propName = ((JSGetter) annotation).value(); if (propName == null || propName.length() == 0) { if (methodName.length() > 3 && methodName.startsWith("get")) { propName = methodName.substring(3); if (Character.isUpperCase(propName.charAt(0))) { if (propName.length() == 1) { propName = propName.toLowerCase(); } else if (!Character.isUpperCase(propName.charAt(1))){ propName = Character.toLowerCase(propName.charAt(0)) + propName.substring(1); } } } } } else if (annotation instanceof JSFunction) { propName = ((JSFunction) annotation).value(); } else if (annotation instanceof JSStaticFunction) { propName = ((JSStaticFunction) annotation).value(); } if (propName == null || propName.length() == 0) { propName = methodName; } return propName; }
/** * Read the remaining lines in the file and return them in an array. * * Implements a JavaScript function.<p> * * This is a good example of creating a new array and setting * elements in that array. * * @exception IOException if an error occurred while accessing the file * associated with this object */ @JSFunction public Object readLines() throws IOException { List<String> list = new ArrayList<String>(); String s; while ((s = readLine()) != null) { list.add(s); } String[] lines = list.toArray(new String[list.size()]); Scriptable scope = ScriptableObject.getTopLevelScope(this); Context cx = Context.getCurrentContext(); return cx.newObject(scope, "Array", lines); }
/** * Read a character. * * @exception IOException if an error occurred while accessing the file * associated with this object, or EOFException if the object * reached the end of the file */ @JSFunction public String readChar() throws IOException { int i = getReader().read(); if (i == -1) return null; char[] charArray = { (char) i }; return new String(charArray); }
/** * Close the file. It may be reopened. * * Implements a JavaScript function. * @exception IOException if an error occurred while accessing the file * associated with this object */ @JSFunction public void close() throws IOException { if (reader != null) { reader.close(); reader = null; } else if (writer != null) { writer.close(); writer = null; } }
/** * Get the Java reader. */ @JSFunction("getReader") public Object getJSReader() { if (reader == null) return null; // Here we use toObject() to "wrap" the BufferedReader object // in a Scriptable object so that it can be manipulated by // JavaScript. Scriptable parent = ScriptableObject.getTopLevelScope(this); return Context.javaToJS(reader, parent); }
/** * Get the Java writer. * * @see File#getReader * */ @JSFunction public Object getWriter() { if (writer == null) return null; Scriptable parent = ScriptableObject.getTopLevelScope(this); return Context.javaToJS(writer, parent); }
/** * Wraps {@link Job#setOutputValueClass(Class)}. * * @param ctx the JavaScript context (unused) * @param thisObj the 'this' object of the caller * @param args the arguments for the call * @param func the function called (unused) * * @return this */ @JSFunction public static Object setOutputValueClass(final Context ctx, final Scriptable thisObj, final Object[] args, final Function func) { if (args.length == 1) { if (!JavaScriptUtils.isDefined(args[0])) { throw Utils.makeError(ctx, thisObj, LembosMessages.FIRST_ARG_REQUIRED); } } else { throw Utils.makeError(ctx, thisObj, LembosMessages.ONE_ARG_EXPECTED); } final String className = args[0].toString(); try { final Class outputValueClass = Class.forName(className); if (!Writable.class.isAssignableFrom(outputValueClass)) { throw Utils.makeError(ctx, thisObj, LembosMessages.makeInvalidClassErrorMessage(Writable.class, outputValueClass)); } ((JobWrap)thisObj).job.setOutputValueClass((Class<? extends Writable>)outputValueClass); } catch (ClassNotFoundException e) { throw Utils.makeError(ctx, thisObj, LembosMessages.CLASS_NOT_FOUND); } return thisObj; }