tags:

views:

395

answers:

4

Could someone of you show me how to effectively use "Scripting Engine" inside Java?

What are all the right use-case to use scripting engine?

Is there any open source project using "Scripting Engine"?

One thing comes to mind is "Closure, Functional programming" support is possible, but it is more of technical use than "Application Requirement"

Edit Added: Configurable, Plugins are ok. But still so many patterns (visitor, decorator) on high level can do the same.

I don't know the requirement well... how effectively it could be used in J2EE patterns... where it could complement with the existing patterns.

Moreover I would like to see more answers with some business usecases may be like finding complex discount for a product during sale based on membership or location. finding ranking for a complex algorithm. Especially why not java in some scenario? (or C# in .Net world)

+1  A: 

"What are all the right use-case to use scripting engine?" This is a pretty vague question. There are many use cases. Here are just a few I can think of right away:

  1. Plugin/extension system
  2. IDE
  3. Programming tutorial with live demos

I am assuming you are referring to JSR 223 in particular. If so, you should look at scripting.dev.java.net

Matthew Flaschen
+1  A: 

Here is what Sun have to say: Who is the Java Scripting API For? and Reasons to Use a Scripting Language.

Sinan Ünür
+2  A: 

In Java 6, scripting engine support is built in. e.g.

    // create a script engine manager
    ScriptEngineManager factory = new ScriptEngineManager();

    // create a JavaScript engine
    ScriptEngine engine = factory.getEngineByName("JavaScript");

    // evaluate JavaScript code from String
    engine.eval("print('Hello, World')");

Why would you use one ? Some reasons:

  1. you have a library in a scripting language that you want to use in Java (e.g. a Python library that you could run via Jython)
  2. You want to provide a configurable programming mechanism for customers, such that they can provide short code snippets. e.g. I've done this in the past allowing customers to write filters using Javascript (e.g. is x < 2 and y > 5 and z > 10 ?).
  3. You can implement more complex logic in tools like Ant by scripting directly in the config file
  4. You can implement solutions in a language more suited to that domain (e.g. using lambdas via Clojure) but maintain your reliance on the JVM

Implementations include Rhino (a Java implementation of Javascript), Jython (a Java Python) and many more.

Brian Agnew
A: 

I haven't used Javascript specifically, but I've integrated Groovy into my application framework to provide a domain specific language (DSL). I've created functions and classes that hook into my application.

The user is allowed to script common operations within the application (macros) as well as implement lightweight processing to avoid the much heavier code-compiler-jar-deploy solution. If the user has an idea for a plugin to my processing framework they can prototype via Groovy in realtime and move back to java (maybe even native) when there is time (or when speed is needed). Keep in mind that scripting is typically orders of magniture slower than java/c#/c/c++

basszero