Author: mreutegg
Date: Wed May 21 14:39:29 2014
New Revision: 1596585

URL: http://svn.apache.org/r1596585
Log:
OAK-1845: Add command to execute script

Initial support for JavaScript file

Modified:
    
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/console/Command.java

Modified: 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/console/Command.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/console/Command.java?rev=1596585&r1=1596584&r2=1596585&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/console/Command.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/console/Command.java
 Wed May 21 14:39:29 2014
@@ -16,10 +16,13 @@
  */
 package org.apache.jackrabbit.oak.console;
 
+import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
 import java.io.Writer;
 import java.lang.reflect.Modifier;
 import java.util.Arrays;
@@ -32,6 +35,8 @@ import java.util.SortedMap;
 import java.util.concurrent.TimeUnit;
 
 import javax.annotation.Nonnull;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
 
 import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore;
@@ -380,6 +385,31 @@ public abstract class Command {
         }
     }
 
+    public static final class Eval extends Command {
+
+        public Eval() {
+            this.description = "Evaluate a script.";
+        }
+
+        @Override
+        public void execute(@Nonnull ConsoleSession session,
+                            @Nonnull InputStream in,
+                            @Nonnull OutputStream out) throws Exception {
+            File scriptFile = new File(args);
+            if (!scriptFile.exists()) {
+                println("Script file not found: " + args, out);
+                return;
+            }
+            PrintWriter writer = new PrintWriter(out);
+            ScriptEngineManager factory = new ScriptEngineManager();
+            ScriptEngine engine = factory.getEngineByName("JavaScript");
+            engine.put("session", session);
+            engine.put("out", writer);
+            engine.eval(new FileReader(scriptFile));
+            writer.flush();
+        }
+    }
+
     //-----------------------< DocumentNodeStore specific 
>---------------------
 
     abstract static class DocumentNodeStoreCommand extends Command {


Reply via email to