Commits

timemachine committed 54a52c8

Added scriptFile to ScriptingDateListProvider.

Comments (0)

Files changed (1)

timemachine-scheduler/src/main/java/timemachine/scheduler/schedule/ScriptingDateListProvider.java

  */
 package timemachine.scheduler.schedule;
 
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.URL;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 import timemachine.scheduler.SchedulerException;
 import timemachine.scheduler.schedule.DateListSchedule.DateListProvider;
+import timemachine.scheduler.support.ClasspathURLStreamHandler;
 import timemachine.scheduler.support.Utils;
 
 /**
  * This provider will take data map with the following keys:
  * <code>scriptEngineName</code> A java scripting engine name. Default to JavaScript.
  * <code>scriptText</code> Script text that evaluate into java.util.List<Date> as result.
+ * <code>scriptFile</code> Script file that evaluate into java.util.List<Date> as result.
  * 
  * @author Zemian Deng
  */
 		ScriptEngineManager factory = new ScriptEngineManager();
 		scriptEngine = factory.getEngineByName(scriptEngineName);
 		if (scriptEngine == null) {
-			throw new SchedulerException("Failed to find ScriptEngine " + scriptEngineName);
+			throw new SchedulerException("Failed to generate date list: not found ScriptEngine " + scriptEngineName);
 		}
 	}
 	
 			init(schedule);
 		
 		List<Date> result = null;
-		try {
-			Bindings bindings = scriptEngine.createBindings();
-			bindings.put("dateListSchedule", schedule);
-			bindings.put("dateListProvider", this);
-			bindings.put("dateListProviderDataMap", providerDataMap);
-			bindings.put("logger", logger);
-			
-			String scriptText = providerDataMap.get("scriptText");
-			result = (List<Date>)scriptEngine.eval(scriptText, bindings);
-		} catch (ScriptException e) {
-			throw new SchedulerException("Failed to evaluate script to date list.", e);
+		Bindings bindings = scriptEngine.createBindings();
+		bindings.put("dateListSchedule", schedule);
+		bindings.put("dateListProvider", this);
+		bindings.put("dateListProviderDataMap", providerDataMap);
+		bindings.put("logger", logger);
+
+		String scriptText = providerDataMap.get("scriptText");
+		String scriptFile = providerDataMap.get("scriptFile");
+		if (scriptText != null) {
+			try {
+				result = (List<Date>)scriptEngine.eval(scriptText, bindings);
+			} catch (ScriptException e) {
+				throw new SchedulerException("Failed to generate date list: failed to evaluate script to date list.", e);
+			}
+		} else if (scriptFile != null){
+			Reader reader = null;
+			try {
+				URL url = ClasspathURLStreamHandler.createURL(scriptFile);
+				logger.debug("Reading scriptFile {}", url);
+				InputStream inStream = url.openStream();
+				reader = new InputStreamReader(inStream);
+				scriptEngine.eval(reader, bindings);
+			} catch (FileNotFoundException e) {
+				throw new SchedulerException("Failed to generate date list: failed to find script " + scriptFile, e);
+			} catch (ScriptException e) {
+				throw new SchedulerException("Failed to generate date list: failed to run script " + scriptFile, e);
+			} catch (IOException e) {
+				throw new SchedulerException("Failed to generate date list: failed to read script " + scriptFile, e);
+			} finally {
+				try {
+					if (reader != null) {
+						reader.close();
+					}
+				} catch (IOException e) {
+					throw new SchedulerException("Failed to generate date list: failed to close reader for script " + scriptFile, e);
+				}
+			}
+		} else {
+			throw new SchedulerException("Failed to generate date list: missing scriptText or scriptFile" +
+					" in provider data map.");
 		}
 		return result;
 	}