//Variables to be declared to the BSFManager (are available in scripts under aliases)
Writer out = null;
Reader in = null;
BufferInputStream inStream = null;
BufferOutputStream outStream = null;
Context context = null;
Properties props = null;
boolean isFile = false;
if (helpRequested()) {
printHelp();
return;
}
PrintWriter err = new PrintWriter(new BufferWriter(getStdOut()), true);
// now lookup ScriptingServices
ScriptingCentral sc = (ScriptingCentral) getShell().getContext().get("ScriptingServices");
if (sc == null) {
if (canThrowEx()) {
throw new RuntimeException("ScriptingServices not bound in context.");
} else {
err.println("ScriptingServices not bound in context.");
return;
}
}
String file = null;
String vfile = null;
Reader reader = null;
String enc = shell.getEnvProperty("ENCODING");
String [] args = null;
if(params.length >0) {
int i = 0;
String temp = params[i];
if("-f".equals(temp) || "--file".equals(temp)) {
if(i<params.length-1) {
file = params[++i];
} else {
error("File name missing after parameter: " + temp);
return;
}
} else if("-v".equals(params[0]) || "--vfile".equals(params[0])) {
if(i<params.length-1) {
vfile = params[++i];
} else {
error("File name missing after parameter: " + temp);
return;
}
}
}
if(file != null || vfile != null) {
args = new String[params.length-2];
for(int i=2; i<params.length; i++) {
args[i-2] = params[i];
}
fname = file != null ? file : vfile;
String ext = IOUtils.getExt(fname);
if (ext == null) {
if (canThrowEx()) {
throw new RuntimeException("Can't determine scripting language - file has no extension.");
} else {
err.println("Can't determine scripting language - file has no extension.");
return;
}
}
langName = sc.getLanguageForExtension(ext);
if (langName == null) {
if (canThrowEx()) {
throw new RuntimeException("No scripting language registered for the specified extensions: " + ext);
} else {
err.println("No scripting language registered for the specified extensions: " + ext);
return;
}
}
InputStream ins = null;
if(file != null) {
ins = new FileInputStream(file);
} else {
VFS vfs = shell.getVFS();
FileName pwd = new FileName(shell.getEnvProperty("PWD"));
FileName path = new FileName(vfile);
if (path.isRelative())
path = pwd.absolutize(path);
path = vfs.resolve(shell.getUserCtx(), path, false);
ins = new VFSInputStream(new SecureVFS(vfs, shell.getUserCtx()), path.toString());
}
if (enc == null || enc.trim().length() == 0) {
reader = new BufferedReader(new InputStreamReader(ins));
} else {
reader = new BufferedReader(new InputStreamReader(ins, enc));
}
isFile = true;
} else {
args = params;
InputStream ins = new BufferInputStream(getStdIn());
if (enc == null || enc.trim().length() == 0) {
reader = new BufferedReader(new InputStreamReader(ins));
} else {
reader = new BufferedReader(new InputStreamReader(ins, enc));
}
langName = ((BufferedReader) reader).readLine();
if (langName.length() > 2)
langName = langName.substring(2).trim();
String lngName = sc.getLanguageForName(langName);
if (lngName == null) {
if (canThrowEx()) {
throw new RuntimeException("Scripting language is not supported: " + langName);
} else {
err.println("Scripting language is not supported: " + langName);
return;
}
}
langName = lngName;
}
BSFManager manager = sc.getManager(langName);
inStream = getStdInStream();
outStream = getStdOutStream();
context = shell.getContext();
props = shell.getEnvProperties();
out = new PrintWriter(new BufferedWriter(new BufferWriter(getStdOut())), true);
in = new BufferedReader(new BufferReader(getStdIn()));
InBuffer stdin = getStdIn();
OutBuffer stdout = getStdOut();
manager.declareBean("args", args, args.getClass());
manager.declareBean("shell", shell, shell.getClass());
manager.declareBean("instream", inStream, inStream.getClass());
manager.declareBean("outstream", outStream, outStream.getClass());
manager.declareBean("context", context, context.getClass());
manager.declareBean("props", props, props.getClass());
manager.declareBean("out", out, out.getClass());
manager.declareBean("cin", in, in.getClass());
manager.declareBean("stdin", stdin, stdin.getClass());