Java SE 6 scripting engine for the use in procedures [job is reposted]
Abstract: Java SE 6 scripting engine for the use in procedures [job is reposted]
Java SE 6 of the procedures for the use in scripting engine
Source Development天æž
————————————————– ——————————
Java SE 5 now has been issued in the next Java SE 6 will also be released. Java SE 6 more Java SE5 a big improvement, and its more powerful, and is designed specifically for Vista, which means that Java SE 6 Vista is the best choice. The Java SE 6 provides the most numerous new features will become its biggest selling point.
Java SE 6 has attracted the most attention One of the new features is support for embedded script. By default, Java SE 6 support JavaScript, but that does not mean that the Java SE 6 can only support JavaScript. Java SE 6 in a number of interfaces to define a standard script, which is JSR223. Through the realization of these interfaces can support Java SE 6 arbitrary scripting language (such as PHP or Ruby).
Running a script
In the use of Java SE 6 run scripts before, we must know that you support the Java SE 6 what scripting language. Javax.script package in a lot of categories, but these categories is the main ScriptEngineManager. Through this type can be present to support Java SE 6 all scripts. As in the following example will list all can use the script engine factory.
Import javax.script .*;
Import java.io. *;
Import java.util .*;
Import static java.lang.System .*;
Public class ListScriptEngines
(
Public static void main (String args [])
(
ScriptEngineManager manager = new ScriptEngineManager ();
/ / Get all the scripts engine plant List factories = manager.getEngineFactories ();
/ / This is the Java SE 5 and the new Java SE 6 For statements for grammar (ScriptEngineFactory factory: factories)
(
/ / Print information out.printf script ( "Name:% s% n" +
"Version:% s% n" +
"Language name:% s% n" +
"Language version:% s% n" +
"Extensions:% s% n" +
"Mime types:% s% n" +
"Names:% n% s",
Factory.getEngineName (),
Factory.getEngineVersion (),
Factory.getLanguageName (),
Factory.getLanguageVersion (),
Factory.getExtensions (),
Factory.getMimeTypes (),
Factory.getNames ());
/ / Get the current script engine ScriptEngine engine = factory.getScriptEngine ();
)
)
)
The above examples must be in the Java SE 6 compiler. Import static java.lang.System .* which is a new grammar, the System will be all static members cited the future can be used directly out, or err in the.
By running java ListScriptEngines, will show the following information
Name: Mozilla Rhino
Version: 1.6 release 2
Language name: ECMAScript
Language version: 1.6
Extensions: [js]
Mime types: [application / javascript, application / ecmascript, text / javascript, text / ecmascript]
Names: [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]
Is the bottom line in the script aliases, is the arbitrary use them in a can. Have a specific script engine are three kinds of methods.
According to extension by the script engine
ScriptEngine engine = manager.getEngineByExtension ( "js");
GetEngineByExtension parameters is Extensions: [js] […] in part.
According to the script by Mime type engine
ScriptEngine engine = manager.getEngineByMimeType ( "text / javascript");
GetEngineByMimeType parameters can be Mime types: [application / javascript, application / ecmascript, text / javascript, text / ecmascript] any one, can be text / javascript into text / ecmascript.
According to the script by name engine
ScriptEngine engine = manager.getEngineByName ( "javascript");
GetEngineByName the parameters can be Names: [js, rhino, JavaScript, javascript, ECMAScript, ecmascript] any one, as can be changed ecmascript javascript.
Above have been discussed for the first step in the implementation of the script is available with a script engine. After the completion of the work on this script can be used to implement the corresponding engine of the script. We can use the eval ScriptEngine approach to the implementation of the script. Eval method is overloaded several times, but the most common is public Object eval (String script).
The following example demonstrates how to use eval javascript approach to the implementation of the script.
Import javax.script .*;
Import java.io. *;
Import static java.lang.System .*;
Public class FirstJavaScript
(
Public static void main (String args [])
(
ScriptEngineManager manager = new ScriptEngineManager ();
/ / Get javascript scripting engine ScriptEngine engine = manager.getEngineByName ( "javascript");
Try
(
/ / Running scripts, and return to the current hours Double hour = (Double) engine.eval ( "var date = new Date ();" + "date.getHours ();");
String msg;
/ / Hours will be converted to information if greetings (hour <10)
(
Msg = "good morning";
)
Else if (hour <16)
(
Msg = "good afternoon";
)
Else if (hour <20)
(
Msg = "good evening";
)
Else
(
Msg = "Good Night";
)
Out.printf ( "hour% s:% n% s", hour, msg);
)
Catch (ScriptException e)
(
Err.println (e);
)
)
)
The above examples are by the current hours, and translate them into greeting. The output of the above information:
9.0 hours: Good morning
The most noteworthy example is the implementation of the two scripts, the last sentence is date.getHours (). Not assign a value to this variable javascript. At this time, eval method will return this value. This is somewhat similar to the C language (…) operators. If (c = a + b, c + d), this expression is the return value of a + b + d.
And an interactive scripting language
Operation of the above example is a very simple script. The script is isolated, not the Java script to the transfer of any value. Although this script back to a value, but this is the way to return to the implicit.
In addition to these simple scripting engine features, but also provides us with a more powerful functions. Even the Java scripting language in the transmission parameters, the script language can also be variable in the value of removed. These functions depend on the two methods ScriptEngine put and get.
Put two parameters, the script is a variable name, and the other is variable, the value of Object Types, therefore, can deliver any value.
Get a parameter is the name of the script variables.
The following code will be adopted by a string javascript script flip (the string is passed through java javascript), and then through the java this was overturned by the characters, and then output.
Import javax.script .*;
Import java.io. *;
Import static java.lang.System .*;
Public class ReverseString
(
Public static void main (String args [])
(
ScriptEngineManager manager = new ScriptEngineManager ();
/ / Create javascript scripting engine ScriptEngine engine = manager.getEngineByName ( "javascript");
Try
(
/ / Variable name and value abcdefg variable transmission javascript scripting engine.put ( "name", "abcdefg");
/ / Beginning of the implementation of the script engine.eval ( "var output =" +
"For (i = 0; i <= name.length; i + +) (" +
"Name.charAt output = (i) + output" +
"}");
/ / Output variables have the value of String name = (String) engine.get ( "output");
Out.printf (the "flip after the string:% s", name);
)
Catch (ScriptException e)
(
Err.println (e);
)
)
)
The output of the code above is: was overturned after the string: gfedcba
Let scripts run faster
As we all know, explain operation mode is the slowest operation mode. A few examples of these are, without exception, to explain the running. Because Java EE 6 engine can support script achieve any script engine interface language. There are many such language compiler provides a function, that is, in the first run scripts before these scripts will be compiled (compiler general here will not generate executable files, but only in memory compilers more easily run into the way) , and then implementation. If certain scripts to run several times at the turn of the case, the use of this method is very fast. We can use the compile method ScriptEngine compiler. Not all support script compiler engine, and only a scripting engine Compilable interface can be used only to compile compiler, or will throw a mistake. The following examples will demonstrate how to use the method of compiling compile and run javascript script.
Import javax.script .*;
Import java.io. *;
Import static java.lang.System .*;
Public class CompileScript
(
Public static void main (String args [])
(
ScriptEngineManager manager = new ScriptEngineManager ();
ScriptEngine engine = manager.getEngineByName ( "javascript");
Engine.put (the "counter", 0) / / javascript to send a parameter / / determine whether to support this script compiler engine function if (engine instanceof Compilable)
(
Compilable compEngine = (Compilable) engine;
Try
(
/ / Compiler CompiledScript script = compEngine.compile ( "function count () (" +
"Counter counter +1 =" +
"Return counter;" +
"); Count ();");
Out.printf ( "Counter:% n% s", script.eval ());
Out.printf ( "Counter:% n% s", script.eval ());
Out.printf ( "Counter:% n% s", script.eval ());
)
Catch (ScriptException e)
(
Err.println (e);
)
)
Else
(
Err.println ( "The engine does not support script compiler!");
)
)
)
The above code running after the display information is as follows:
Counter: 1.0
Counter: 2.0
Counter: 3.0
In this example, the first method will be adopted compile script compiler, and then through the eval method of repeated calls. In this code there is only one function, and hence, the return to the eval function values.
Scripting language called dynamic method
There is only one example of the above functions can be carried out through the eval call and the value of its return. But if the script to a number of function or through user input to determine which function calls, which require the use of dynamically invoke method calls. And compiler, scripting engine Invocable interface must be achieved before we can call dynamic scripting language in the way. The following examples will demonstrate how dynamic invocation way to run above the string javascript flip the script.
Import javax.script .*;
Import java.io. *;
Import static java.lang.System .*;
Public class InvocableTest
(
Public static void main (String args [])
(
ScriptEngineManager manager = new ScriptEngineManager ();
ScriptEngine engine = manager.getEngineByName ( "javascript");
If (engine instanceof Invocable)
(
Try
(
Engine.eval ( "function reverse (name) (" +
"Var output =" +
"For (i = 0; i <= name.length; i + +) (" +
"Name.charAt output = (i) + output" +
") Return output ;}");
Invocable invokeEngine = (Invocable) engine;
Object o = invokeEngine.invoke ( "reverse", name);
Out.printf ( "flip after the string:% s", name);
)
Catch (NoSuchMethodException e)
(
Err.println (e);
)
Catch (ScriptException e)
(
Err.println (e);
)
)
Else
(
Err.println ( "The script does not support dynamic invocation engine");
)
)
Dynamic Interface Implementation
There is a scripting engine more attractive features, and that is to achieve dynamic interface. If we want to implement asynchronous script, through to the implementation of multi-threaded, it InvokeEngine must implement the Runnable interface which can pass Thread launched multi-threading. Therefore, it can be passed getInterface InvokeEngine dynamic approach to the realization of the Runnable interface. This step can be divided into three general conduct.
1. Use javascript function of the preparation of a run
Engine.eval ( "function run () (print (asynchronous implementation );}");
2. Through getInterface method Runnable interface
Runnable runner = invokeEngine.getInterface (Runnable.class);
3. Start of the use of multithreading Thread
Thread t = new Thread (runner);
T.start ();
Below is a detailed realization of this function code.
Import javax.script .*;
Import static java.lang.System .*;
Public class InterfaceTest
(
Public static void main (String args [])
(
ScriptEngineManager manager = new ScriptEngineManager ();
ScriptEngine engine = manager.getEngineByName ( "javascript");
Try
(
Engine.eval ( "function run () (print (asynchronous transfer );}");
Invocable invokeEngine = (Invocable) engine;
Runnable runner = invokeEngine.getInterface (Runnable.class);
Thread t = new Thread (runner);
T.start ();
T.join ();
)
Catch (InterruptedException e)
(
Err.println (e);
)
Catch (ScriptException e)
(
System.err.println (e);
)
)
)
In fact, the above code is achieved through javascript run Runnable interface methods.
↑ Back
Tags: Java procedures






