Author: tyrell
Date: Tue Feb 26 09:08:57 2008
New Revision: 14236
Log:
Fixing MASHUP-623
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
==============================================================================
---
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
Tue Feb 26 09:08:57 2008
@@ -24,6 +24,8 @@
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Function;
@@ -64,6 +66,8 @@
private static final long serialVersionUID = 5003413793187124449L;
+ private static final Log log = LogFactory.getLog(SystemHostObject.class);
+
public void jsConstructor() {
}
@@ -315,14 +319,6 @@
* var id = system.setInterval('myJavaScriptFunction("I am a parameter
value");', 2000, null, startTime, endtime);
* </pre>
*
- * @param cx - The Rhino context which is created for each
invocation of a function
- * @param thisObj
- * @param arguments The expected arguments are; the JavaScript function to
execute and
- * the frequency of execution. Optionally you can provide
a start and end time.
- * The default start time is 'after given number of
milliseconds in the frequency parameter' and end time is 'indefinitely'.
- * @param funObj
- * @return String - An id representing a handle to the timer
- * @throws IOException
*/
public static String jsFunction_setInterval(Context cx, Scriptable
thisObj, Object[] arguments,
Function funObj) throws
IOException {
@@ -330,7 +326,8 @@
SystemHostObject systemHostObject = checkInstance(thisObj);
Object object =
cx.getThreadLocal(JavaScriptEngineConstants.AXIS2_SERVICE);
- Object currentMessageContext =
cx.getThreadLocal(JavaScriptEngineConstants.AXIS2_MESSAGECONTEXT);
+ Object currentMessageContext =
+
cx.getThreadLocal(JavaScriptEngineConstants.AXIS2_MESSAGECONTEXT);
AxisService axisService;
if (object instanceof AxisService) {
@@ -646,11 +643,6 @@
* system.clearInterval(id);
* </pre>
*
- * @param cx
- * @param thisObj
- * @param arguments The expected argument is a string representing the ID
of the scheduled function
- * @param funObj
- * @throws IOException
*/
public static void jsFunction_clearInterval(Context cx, Scriptable
thisObj, Object[] arguments,
Function funObj) throws
IOException {
@@ -658,22 +650,7 @@
SystemHostObject systemHostObject = checkInstance(thisObj);
if (arguments[0] instanceof String) {
- String jobId = (String) arguments[0];
-
- //Get the scheduler instance stored in the Axis configuration
context
- ServerManager serverManager = ServerManager.getInstance();
- ConfigurationContext configContext = serverManager.configContext;
- AxisConfiguration configuration =
configContext.getAxisConfiguration();
-
- Parameter parameter = configuration
- .getParameter(MashupConstants.QUARTZ_FUNCTION_SCHEDULER);
- Scheduler scheduler = (Scheduler) parameter.getValue();
-
- try {
- scheduler.deleteJob(jobId, null);
- } catch (SchedulerException e) {
- throw new MashupFault("Failed to remove job id from the
scheduler", e);
- }
+ deleteJob(arguments);
} else {
throw new AxisFault("Invalid parameter");
}
@@ -725,9 +702,10 @@
}
public static Object jsFunction_getXML(Context context, Scriptable
thisObj, Object[] arguments,
- Function funObj) throws
MashupFault {
+ Function funObj) throws MashupFault
{
if (arguments.length != 1 || arguments[0] == null || !(arguments[0]
instanceof String)) {
- throw new MashupFault("The getXML function should be called with a
single parameter which is the url to fetch XML from");
+ throw new MashupFault(
+ "The getXML function should be called with a single
parameter which is the url to fetch XML from");
}
String urlString = (String) arguments[0];
URL url = null;
@@ -752,11 +730,12 @@
if (code == HttpURLConnection.HTTP_OK) {
StAXOMBuilder staxOMBuilder = new StAXOMBuilder(in);
OMElement omElement = staxOMBuilder.getDocumentElement();
- Object[] objects = {omElement};
+ Object[] objects = { omElement };
return context.newObject(thisObj, "XML", objects);
} else {
httpCon.disconnect();
- throw new MashupFault("Could not get the content of " +
urlString + " . The HTTP Code returned was " +
+ throw new MashupFault("Could not get the content of " +
urlString +
+ " . The HTTP Code returned was " +
code);
}
} catch (MalformedURLException e) {
@@ -764,8 +743,140 @@
} catch (IOException e) {
throw new MashupFault(e);
} catch (XMLStreamException e) {
- throw new MashupFault("Could not get the convert the content of "
+ urlString + " to XML. You may have " +
+ throw new MashupFault("Could not get the convert the content of "
+ urlString +
+ " to XML. You may have " +
"to use the scraper object to get this url and tidy it");
}
}
+
+ /**
+ * setTimeout() allows you to specify that a piece of JavaScript code
(called an expression) will be run a specified number
+ * of milliseconds from when the setTimeout() method was called.
+ * <p/>
+ * <p/>
+ * <pre>
+ * ex: setTimeout (expression, timeout);
+ * </pre>
+ * <p/>
+ * <p/>
+ * where expression is the JavaScript code to run after timeout
milliseconds have elapsed.
+ * <p/>
+ * 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 {
+
+ SystemHostObject systemHostObject = checkInstance(thisObj);
+
+ Object object =
cx.getThreadLocal(JavaScriptEngineConstants.AXIS2_SERVICE);
+ Object currentMessageContext =
+
cx.getThreadLocal(JavaScriptEngineConstants.AXIS2_MESSAGECONTEXT);
+
+ AxisService axisService;
+ if (object instanceof AxisService) {
+ axisService = (AxisService) object;
+ } else {
+ throw new MashupFault("Error obtaining the MessageContext.");
+ }
+
+ //Get the scheduler instance stored in the Axis configuration context
+ ServerManager serverManager = ServerManager.getInstance();
+ ConfigurationContext configContext = serverManager.configContext;
+ AxisConfiguration configuration = configContext.getAxisConfiguration();
+
+ Parameter parameter = configuration
+ .getParameter(MashupConstants.QUARTZ_FUNCTION_SCHEDULER);
+ Scheduler scheduler = (Scheduler) parameter.getValue();
+
+ //Generating UUIDs for job and trigger
+ String jobId = UUIDGenerator.getUUID().substring(9);
+
+ //Creating the quartz job
+ JobDetail jobDetail = new JobDetail(jobId, null,
FunctionSchedulingJob.class);
+
+ //Creating the trigger. Instanciation of this will depend on the
parameters passed
+ SimpleTrigger trigger = null;
+
+ Object jsFunction = null;
+ long startInMilliSeconds = 0;
+
+ //Extracting the scheduled javascript code from the arguments
+ if ((arguments[0] instanceof Function) || ((arguments[0] instanceof
String))) {
+ jsFunction = arguments[0];
+ } else {
+ throw new AxisFault(
+ "Invalid parameter. The first parameter must be a
JavaScript function.");
+ }
+
+ //Extracting the frequency from the arguments
+ try {
+ startInMilliSeconds = ((Number) arguments[1]).longValue();
+ } catch (NumberFormatException e) {
+ startInMilliSeconds = 0;
+ }
+
+ //Storing the function meta-data to be used by the job at execution
time
+ jobDetail.getJobDataMap()
+ .put(FunctionSchedulingJob.JAVASCRIPT_FUNCTION, jsFunction);
+ jobDetail.getJobDataMap()
+ .put(FunctionSchedulingJob.PARENT_MESSAGE_CONTEXT,
currentMessageContext);
+
+ //Creating the trigger. There will be a one-to-one mapping between
jobs and triggers in this implementation
+ trigger = new SimpleTrigger(jobId + "-trigger", null,
+ new Date(System.currentTimeMillis() +
startInMilliSeconds));
+
+ try {
+ scheduler.scheduleJob(jobDetail, trigger);
+ } catch (SchedulerException e) {
+ throw new AxisFault("Failed to schedule javascript execution.", e);
+ }
+
+ return jobId;
+ }
+
+
+ /**
+ * Sometimes it's useful to be able to cancel a timer before it goes off.
The clearTimeout() method lets us do exactly that.
+ * <p/>
+ *
+ * <pre>
+ * ex: clearTimeout ( timeoutId );
+ * </pre>
+ *
+ * <p/>
+ * 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 {
+
+ SystemHostObject systemHostObject = checkInstance(thisObj);
+
+ if (arguments[0] instanceof String) {
+ deleteJob(arguments);
+ } else {
+ throw new AxisFault("Invalid parameter");
+ }
+
+ }
+
+ private static void deleteJob(Object[] arguments) throws MashupFault {
+ String jobId = (String) arguments[0];
+
+ //Get the scheduler instance stored in the Axis configuration context
+ ServerManager serverManager = ServerManager.getInstance();
+ ConfigurationContext configContext = serverManager.configContext;
+ AxisConfiguration configuration = configContext.getAxisConfiguration();
+
+ Parameter parameter = configuration
+ .getParameter(MashupConstants.QUARTZ_FUNCTION_SCHEDULER);
+ Scheduler scheduler = (Scheduler) parameter.getValue();
+
+ try {
+ scheduler.deleteJob(jobId, null);
+ log.info("Deleted the scheduled function execution with id " +
jobId);
+ } catch (SchedulerException e) {
+ throw new MashupFault("Failed to remove job id from the
scheduler", e);
+ }
+ }
}
_______________________________________________
Mashup-dev mailing list
[email protected]
http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev