8199912: jshell tool: /open from URI
authorrfield
Fri, 04 May 2018 23:19:10 -0700
changeset 50017 88cc95780b6e
parent 50016 947f79c91b35
child 50018 3ba0d8631f24
8199912: jshell tool: /open from URI Reviewed-by: rfield Contributed-by: mvala@redhat.com
src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
test/langtools/jdk/jshell/ToolBasicTest.java
--- a/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Fri May 04 16:49:22 2018 -0700
+++ b/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java	Fri May 04 23:19:10 2018 -0700
@@ -40,6 +40,9 @@
 import java.lang.module.ModuleDescriptor;
 import java.lang.module.ModuleFinder;
 import java.lang.module.ModuleReference;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
 import java.nio.charset.Charset;
 import java.nio.file.FileSystems;
 import java.nio.file.Files;
@@ -2999,13 +3002,22 @@
                 } else {
                     Path path = toPathResolvingUserHome(filename);
                     String resource;
-                    scanner = new Scanner(
-                            (!Files.exists(path) && (resource = getResource(filename)) != null)
-                            ? new StringReader(resource) // Not found as file, but found as resource
-                            : new FileReader(path.toString())
-                    );
+                    if (Files.exists(path)) {
+                        scanner = new Scanner(new FileReader(path.toString()));
+                    } else if ((resource = getResource(filename)) != null) {
+                        scanner = new Scanner(new StringReader(resource));
+                    } else {
+                        try {
+                            var url = new URL(filename);
+                            scanner = new Scanner(url.openStream());
+                        } catch (MalformedURLException mue) {
+                            throw new FileNotFoundException(filename);
+                        }
+                    }
                 }
-                run(new ScannerIOContext(scanner));
+                try (var scannerIOContext = new ScannerIOContext(scanner)) {
+                    run(scannerIOContext);
+                }
                 return true;
             } catch (FileNotFoundException e) {
                 errormsg("jshell.err.file.not.found", context, filename, e.getMessage());
--- a/test/langtools/jdk/jshell/ToolBasicTest.java	Fri May 04 16:49:22 2018 -0700
+++ b/test/langtools/jdk/jshell/ToolBasicTest.java	Fri May 04 23:19:10 2018 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797 8175304 8167554 8180508 8166232 8196133
+ * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797 8175304 8167554 8180508 8166232 8196133 8199912
  * @summary Tests for Basic tests for REPL tool
  * @modules jdk.compiler/com.sun.tools.javac.api
  *          jdk.compiler/com.sun.tools.javac.main
@@ -39,6 +39,8 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -52,6 +54,7 @@
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import com.sun.net.httpserver.HttpServer;
 import org.testng.annotations.Test;
 
 import static org.testng.Assert.assertEquals;
@@ -491,6 +494,49 @@
         }
     }
 
+    public void testOpenLocalFileUrl() {
+        Compiler compiler = new Compiler();
+        Path path = compiler.getPath("testOpen.repl");
+        compiler.writeToFile(path, "int a = 10;int b = 20;int c = a + b;\n");
+        for (String s : new String[]{"/o", "/open"}) {
+            test(
+                    (a) -> assertCommand(a, s + " file://" + path.toString(), ""),
+                    (a) -> assertCommand(a, "a", "a ==> 10"),
+                    (a) -> assertCommand(a, "b", "b ==> 20"),
+                    (a) -> assertCommand(a, "c", "c ==> 30")
+            );
+        }
+    }
+
+    public void testOpenFileOverHttp() throws IOException {
+        var script = "int a = 10;int b = 20;int c = a + b;";
+
+        var localhostAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), 0);
+        var httpServer = HttpServer.create(localhostAddress, 0);
+        try {
+            httpServer.createContext("/script", exchange -> {
+                exchange.sendResponseHeaders(200, script.length());
+                try (var output = exchange.getResponseBody()) {
+                    output.write(script.getBytes());
+                }
+            });
+            httpServer.setExecutor(null);
+            httpServer.start();
+
+            var urlAddress = "http:/" + httpServer.getAddress().toString() + "/script";
+            for (String s : new String[]{"/o", "/open"}) {
+                test(
+                        (a) -> assertCommand(a, s + " " + urlAddress, ""),
+                        (a) -> assertCommand(a, "a", "a ==> 10"),
+                        (a) -> assertCommand(a, "b", "b ==> 20"),
+                        (a) -> assertCommand(a, "c", "c ==> 30")
+                );
+            }
+        } finally {
+            httpServer.stop(0);
+        }
+    }
+
     public void testOpenResource() {
         test(
                 (a) -> assertCommand(a, "/open PRINTING", ""),