* make cmdline scripting more flexible (allow system://in and script://<expression>)

This commit is contained in:
Reinhard Pointner 2012-04-06 04:50:46 +00:00
parent 4d037086a9
commit 596471c885
3 changed files with 32 additions and 16 deletions

View File

@ -7,8 +7,8 @@ import static net.sourceforge.tuned.FileUtilities.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@ -143,16 +143,23 @@ public class ArgumentBean {
}
public URL getScriptLocation() {
public URI getScriptLocation() {
try {
return new URL(script);
} catch (MalformedURLException eu) {
return new URI(script);
} catch (URISyntaxException eu) {
if (script.startsWith("script://")) {
try {
return new URI("script", script.substring(9), null, null, null);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}
try {
File file = new File(script);
if (!file.exists())
if (!file.exists()) {
throw new FileNotFoundException(file.getPath());
return file.toURI().toURL();
}
return file.toURI();
} catch (Exception e) {
throw new IllegalArgumentException(e);
}

View File

@ -91,7 +91,7 @@ public class ArgumentProcessor {
Bindings bindings = new SimpleBindings();
bindings.put("args", args.getFiles(false));
Analytics.trackEvent("CLI", "ExecuteScript", args.getScriptLocation().getProtocol());
Analytics.trackEvent("CLI", "ExecuteScript", args.getScriptLocation().getScheme());
ScriptShell shell = new ScriptShell(cli, args, args.trustScript, AccessController.getContext());
shell.run(args.getScriptLocation(), bindings);
}

View File

@ -10,9 +10,11 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FilePermission;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.ReflectPermission;
import java.net.SocketPermission;
import java.net.URL;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.security.AccessControlContext;
@ -95,9 +97,17 @@ class ScriptShell {
}
public Object run(URL scriptLocation, Bindings bindings) throws Throwable {
if (scriptLocation.getProtocol().equals("file")) {
return run(new File(scriptLocation.toURI()), bindings);
public Object run(URI scriptLocation, Bindings bindings) throws Throwable {
if (scriptLocation.getScheme().equals("file")) {
return run(new InputStreamReader(new FileInputStream(new File(scriptLocation)), "UTF-8"), bindings);
}
if (scriptLocation.getScheme().equals("system")) {
return run(new InputStreamReader(System.in), bindings);
}
if (scriptLocation.getScheme().equals("script")) {
return run(new StringReader(scriptLocation.getAuthority()), bindings);
}
// fetch remote script only if modified
@ -112,9 +122,8 @@ class ScriptShell {
}
public Object run(File scriptFile, Bindings bindings) throws Throwable {
String script = readAll(new InputStreamReader(new FileInputStream(scriptFile), "UTF-8"));
return evaluate(script, bindings);
public Object run(Reader script, Bindings bindings) throws Throwable {
return evaluate(readAll(script), bindings);
}