Author: keith Date: Sat Jul 12 03:36:14 2008 New Revision: 19164 URL: http://wso2.org/svn/browse/wso2?view=rev&revision=19164
Log: Adding some documentation and also Throwing Mashup faults when faults occur Modified: trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java Modified: trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java URL: http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java?rev=19164&r1=19163&r2=19164&view=diff ============================================================================== --- trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java (original) +++ trunk/mashup/java/modules/hostobjects/src/org/wso2/mashup/hostobjects/system/SystemHostObject.java Sat Jul 12 03:36:14 2008 @@ -18,7 +18,6 @@ import org.apache.axiom.om.OMElement; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axiom.om.util.UUIDGenerator; -import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.MessageContext; @@ -53,11 +52,11 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.net.MalformedURLException; import java.net.SocketException; import java.net.URI; @@ -104,14 +103,14 @@ * system.wait(1000); * </pre> * - * @throws AxisFault + * @throws MashupFault */ public static void jsFunction_wait(Context cx, Scriptable thisObj, Object[] arguments, - Function funObj) throws AxisFault { + Function funObj) throws MashupFault { try { SystemHostObject system = checkInstance(thisObj); if (arguments.length > 1) { - throw new AxisFault("Invalid number of arguments."); + throw new MashupFault("Invalid number of arguments."); } if (arguments.length == 0) { Thread.sleep(10); @@ -122,10 +121,10 @@ Integer timePeriod = (Integer) arguments[0]; Thread.sleep(timePeriod.longValue()); } else { - throw new AxisFault("Unsupported parameter."); + throw new MashupFault("Unsupported parameter."); } } catch (Throwable e) { - throw AxisFault.makeFault(e); + throw new MashupFault(e); } } @@ -188,7 +187,7 @@ * </pre> */ public static void jsFunction_include(Context cx, Scriptable thisObj, Object[] arguments, - Function funObj) throws IOException { + Function funObj) throws MashupFault { // sanity check SystemHostObject system = checkInstance(thisObj); JavaScriptEngine engine = (JavaScriptEngine) getTopLevelScope(thisObj); @@ -246,31 +245,30 @@ Reader reader; String path = arguments[i].toString(); File f = new File(resourceFolder, path); - - // Check whether this is a file in the service.resources directory - if (f.exists() && !f.isDirectory()) { - reader = new FileReader(f); - } else { - // This is not a file.. So we check whether this is a URL - reader = readFromURI(baseURI, path); - } - // We change the scriptName in the engine so that when warnings are displayed they use the name of the // included scripr rather than the calling script name. String parentScriptName = engine.getScriptName(); + try { + // Check whether this is a file in the service.resources directory + if (f.exists() && !f.isDirectory()) { + reader = new FileReader(f); + } else { + // This is not a file.. So we check whether this is a URL + reader = readFromURI(baseURI, path); + } + engine.setScriptName(path); engine.evaluate(reader); } catch (IOException e) { - throw e; + throw new MashupFault(e); } finally { engine.setScriptName(parentScriptName); } } } - private static Reader readFromURI(URI baseURI, String path) throws MashupFault, IOException, - MalformedURLException { + private static Reader readFromURI(URI baseURI, String path) throws MashupFault { Reader reader; // Not a file in the service.resources Dir.. Then check whether this is // a URI. @@ -362,7 +360,7 @@ * </pre> */ public static String jsFunction_setInterval(Context cx, Scriptable thisObj, Object[] arguments, - Function funObj) throws IOException { + Function funObj) throws MashupFault { SystemHostObject systemHostObject = checkInstance(thisObj); @@ -408,7 +406,7 @@ if ((arguments[0] instanceof Function) || ((arguments[0] instanceof String))) { jsFunction = arguments[0]; } else { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The first parameter must be a JavaScript function."); } @@ -416,7 +414,7 @@ try { frequency = ((Number) arguments[1]).longValue(); } catch (NumberFormatException e) { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The second parameter must be the execution frequency in milliseconds."); } @@ -435,7 +433,7 @@ try { scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) { - throw new AxisFault("Failed to schedule javascript execution.", e); + throw new MashupFault("Failed to schedule javascript execution.", e); } break; @@ -445,7 +443,7 @@ if ((arguments[0] instanceof Function) || ((arguments[0] instanceof String))) { jsFunction = arguments[0]; } else { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The first parameter must be a JavaScript function."); } @@ -453,7 +451,7 @@ try { frequency = ((Number) arguments[1]).longValue(); } catch (NumberFormatException e) { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The second parameter must be the execution frequency in milliseconds."); } @@ -481,7 +479,7 @@ functionParams = new Object[tempParamHolder.size()]; tempParamHolder.toArray(functionParams); } else { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The third parameter must be an Array of parameters to the argument or null."); } } @@ -501,7 +499,7 @@ try { scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) { - throw new AxisFault("Failed to schedule javascript execution.", e); + throw new MashupFault("Failed to schedule javascript execution.", e); } break; @@ -511,7 +509,7 @@ if ((arguments[0] instanceof Function) || ((arguments[0] instanceof String))) { jsFunction = arguments[0]; } else { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The first parameter must be a JavaScript function."); } @@ -519,7 +517,7 @@ try { frequency = ((Number) arguments[1]).longValue(); } catch (NumberFormatException e) { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The second parameter must be the execution frequency in milliseconds."); } @@ -547,7 +545,7 @@ functionParams = new Object[tempParamHolder.size()]; tempParamHolder.toArray(functionParams); } else { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The third parameter must be an Array of parameters to the argument or null."); } } @@ -556,7 +554,7 @@ try { startTime = (Date) Context.jsToJava(arguments[3], Date.class); } catch (EvaluatorException e) { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The third parameter must be the start time in date format."); } @@ -575,7 +573,7 @@ try { scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) { - throw new AxisFault("Failed to schedule javascript execution.", e); + throw new MashupFault("Failed to schedule javascript execution.", e); } break; @@ -585,7 +583,7 @@ if ((arguments[0] instanceof Function) || ((arguments[0] instanceof String))) { jsFunction = arguments[0]; } else { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The first parameter must be a JavaScript function."); } @@ -593,7 +591,7 @@ try { frequency = ((Number) arguments[1]).longValue(); } catch (NumberFormatException e) { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The second parameter must be the execution frequency in milliseconds."); } @@ -621,7 +619,7 @@ functionParams = new Object[tempParamHolder.size()]; tempParamHolder.toArray(functionParams); } else { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The third parameter must be an Array of parameters to the argument or null."); } } @@ -630,7 +628,7 @@ try { startTime = (Date) Context.jsToJava(arguments[3], Date.class); } catch (EvaluatorException e) { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The third parameter must be the start time in date format."); } @@ -638,7 +636,7 @@ try { endTime = (Date) Context.jsToJava(arguments[4], Date.class); } catch (EvaluatorException e) { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The fourth parameter must be the end time in date format."); } @@ -657,12 +655,12 @@ try { scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) { - throw new AxisFault("Failed to schedule javascript execution.", e); + throw new MashupFault("Failed to schedule javascript execution.", e); } break; default: - throw new AxisFault("Invalid number of parameters."); + throw new MashupFault("Invalid number of parameters."); } //Storing the Job ID in the Axis Service to be used during undeployment to unschedule it @@ -683,14 +681,14 @@ * </pre> */ public static void jsFunction_clearInterval(Context cx, Scriptable thisObj, Object[] arguments, - Function funObj) throws IOException { + Function funObj) throws MashupFault { SystemHostObject systemHostObject = checkInstance(thisObj); if (arguments[0] instanceof String) { deleteJob(arguments); } else { - throw new AxisFault("Invalid parameter"); + throw new MashupFault("Invalid parameter"); } } @@ -708,14 +706,14 @@ * </pre> */ public static void jsFunction_notifyMonitor(Context cx, Scriptable thisObj, Object[] arguments, - Function funObj) throws AxisFault { + Function funObj) throws MashupFault { String message; String title; int severity; try { SystemHostObject system = checkInstance(thisObj); if (arguments.length > 3 || arguments.length < 1) { - throw new AxisFault("Invalid number of arguments."); + throw new MashupFault("Invalid number of arguments."); } if (arguments.length == 1 && arguments[0] instanceof String) { message = (String) arguments[0]; @@ -732,13 +730,21 @@ severity = ((Integer) arguments[2]).intValue(); MashupUtils.notifyMonitor(title, message, severity); } else { - throw new AxisFault("Unsupported parameter."); + throw new MashupFault("Unsupported parameter."); } } catch (Throwable e) { - throw AxisFault.makeFault(e); + throw new MashupFault(e); } } + /** + * <p> + * Utility function to get an XML file over the network + * </p> + * <pre> + * var history = system.getXML('http://wso2.org/repos/wso2/trunk/mashup/java/modules/samples/upgradeChecker/upgradeChecker.resources/history.xml'); + * </pre> + */ public static Object jsFunction_getXML(Context context, Scriptable thisObj, Object[] arguments, Function funObj) throws MashupFault { if (arguments.length != 1 || arguments[0] == null || !(arguments[0] instanceof String)) { @@ -788,7 +794,7 @@ * setTimeout() also returns a numeric timeout ID that can be used to track the timeout. This is most commonly used with the clearTimeout() method */ public static String jsFunction_setTimeout(Context cx, Scriptable thisObj, Object[] arguments, - Function funObj) throws IOException { + Function funObj) throws MashupFault { SystemHostObject systemHostObject = checkInstance(thisObj); @@ -826,7 +832,7 @@ if ((arguments[0] instanceof Function) || ((arguments[0] instanceof String))) { jsFunction = arguments[0]; } else { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The first parameter must be a JavaScript function."); } @@ -834,7 +840,7 @@ try { startInMilliSeconds = ((Number) arguments[1]).longValue(); } catch (NumberFormatException e) { - throw new AxisFault( + throw new MashupFault( "Invalid parameter. The second parameter must be the start time from now in milliseconds."); } @@ -851,7 +857,7 @@ try { scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) { - throw new AxisFault("Failed to schedule javascript execution.", e); + throw new MashupFault("Failed to schedule javascript execution.", e); } return jobId; @@ -870,14 +876,14 @@ * where timeoutId is the ID of the timeout as returned from the setTimeout() method call. */ public static void jsFunction_clearTimeout(Context cx, Scriptable thisObj, Object[] arguments, - Function funObj) throws IOException { + Function funObj) throws MashupFault { SystemHostObject systemHostObject = checkInstance(thisObj); if (arguments[0] instanceof String) { deleteJob(arguments); } else { - throw new AxisFault("Invalid parameter"); + throw new MashupFault("Invalid parameter"); } } @@ -915,7 +921,7 @@ * 'info', 'warn', 'debug', 'error' or 'fatal'. The logging level defaults to 'info' when one is not provided. */ public static void jsFunction_log(Context cx, Scriptable thisObj, Object[] arguments, - Function funObj) throws IOException { + Function funObj) throws MashupFault { String logMessage; String logLevel; _______________________________________________ Mashup-dev mailing list [email protected] http://wso2.org/cgi-bin/mailman/listinfo/mashup-dev
