Commits

Zemian Deng committed 8878cb8

Unify web's scripting console servlet to use ScriptingUtils.

  • Participants
  • Parent commits 6dc29b2

Comments (0)

Files changed (1)

timemachine-web/src/main/java/timemachine/schedulerweb/ScriptingShellServlet.java

  */
 package timemachine.schedulerweb;
 
-import groovy.lang.Binding;
-import groovy.lang.GroovyShell;
-
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.Map;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import timemachine.scheduler.support.ScriptingUtils;
+
 /**
  * Serve the script editing form pages.
  * @author Zemian Deng
 		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
 		PrintWriter webWriter = new PrintWriter(outStream);
 		
-		Binding binding = new Binding();
-		binding.setVariable("webout", webWriter);
-		binding.setVariable("logger", logger);
-		binding.setVariable("servletContext", getServletContext());
-		binding.setVariable("scheduler", getServletContext().getAttribute("scheduler"));
-		binding.setVariable("request", req);
-		GroovyShell shell = new GroovyShell(binding);
-		String code = req.getParameter("code");
-		shell.evaluate(code);
-
+		Map<String, Object> bindings = new HashMap<String, Object>();
+		bindings.put("webout", webWriter);
+		bindings.put("logger", logger);
+		bindings.put("servletContext", getServletContext());
+		bindings.put("scheduler", getServletContext().getAttribute("scheduler"));
+		bindings.put("request", req);
+		String scriptText = req.getParameter("code");
+		String scriptEngineName = "Groovy";
+		ScriptingUtils.runScriptText(scriptText, scriptEngineName, bindings);
+		
+		// cleanup
 		webWriter.close();
-		String webWriterResult = outStream.toString();
 		
-		req.setAttribute("code", code);
-		req.setAttribute("webWriterResult", webWriterResult);
-		req.setAttribute("postSuccessful", true);		
+		// Forward to view if script didn't write their own output
 		if(!resp.isCommitted()) {
+			String webWriterResult = outStream.toString();
+			req.setAttribute("code", scriptText);
+			req.setAttribute("webWriterResult", webWriterResult);
+			req.setAttribute("postSuccessful", true);
 			req.getRequestDispatcher(VIEW).forward(req, resp);
 		}
 	}