BeanShell (Object scripting language)

Source: Internet
Author: User
Tags closure

BeanShell Use background: The company's capture management platform, the company's product personnel every day to see some of the local operators site violations, complaints, announcements and other information to monitor, in order to ensure that the product personnel are not necessary every day on each site to see information, so for internal demand, we adopt a crawl strategy, Use the program to simulate login every website, and then to the information we want, such as the announcement information to crawl, of course, after crawling to the Web page with Htmlparse technology to deal with, and then we extracted the information displayed to our platform, so that product personnel do not need to log on to each site to see information, Just log on to our crawl platform to see the latest announcements and other information on each website. Because each operator's site is different, that is to crawl the various business is not the same , in this case, we use BeanShell to deal with various business. For different sites using different BSH files to deal with, so that each site's crawl code is placed in a different BSH file, do not have to crawl a Web site to modify the code again, republish. This can reduce a lot of work. Here is a brief introduction to BeanShell.

First: BeanShell: is an object scripting language (http://www.beanshell.org). BeanShell's interpreter is written in Java, open source and free, and it works "It takes a scripted object as a simple closure (simply method closure) to support it, as in JavaScript. It has some of the following features: Use the Java Reflection API to provide real-time interpretation of Java statements and expressions, to transparently access any Java objects and APIs, and to run under four modes, such as command-line mode, console mode, applet mode, and remote threading server mode; In a program, you can work in small programs (applets) normally (without compilers or class loaders), and a very compact interpreter jar file size of 175k.

The use of BeanShell can deal with the actual irregular business , for example, we know that an insurance company has a lot of risks, each risk of investment and revenue algorithm is very complex, can not use the rules of relational database model to describe, So many of the insurance systems in the processing of the risk of the algorithm is hard code, if you want to add a new type of insurance in the system, it is very troublesome to rewrite the code and redistribute it, which is very painful for the development/maintenance staff and customers, and with BeanShell, we can free ourselves from this pain, Use different scripts for each type of insurance, the new type of insurance we have a new script, which can meet the requirements of business change, but also reduce the workload, save costs.

BeanShell A feature of the dynamic execution of Java code, a script file change does not affect the current script's invocation, and the new script will take effect on the next call to the script, unlike the configuration file, which typically requires a reboot.

A simple example of a BeanShell:

By creating a BeanShell interpreter, using the eval () or source () command, you can find the value of the text expression and run the script in your application. If you want to use an object within your script, you can use the set () method to pass the object's variable reference to the BeanShell and obtain the result by using the Get () method.

In the Java file

package com.an;

import java.io.FileNotFoundException;

import java.io.IOException;

Import BSH. Evalerror;

Import BSH. interpreter;

Public class Testbeanshell {

/**

* A simple to BeanShell file incoming parameters, and then. java File receive parameters for processing, and then return the result

* Print the results in the. java file

*/

Public Static void Main (string[] args) {

Interpreter inter = new interpreter ();

Try {

Pass in Parameters to BSH file (BeanShell file can be used directly, no longer defined)

Inter.set ("Invalue",new Integer (1));

Calling and executing the BSH file

Inter.source ("Src/com/an/testbeanshell.bsh");

Get the results in the BSH file

String Resultvalue = Inter.get ("Outvalue"). ToString ();

System.out.println ("===== output =========" +resultvalue);

Catch (Evalerror e) {

E.printstacktrace ();

Catch (FileNotFoundException e) {

E.printstacktrace ();

Catch (IOException e) {

E.printstacktrace ();

}

}

}

BSH file

Print () function in BeanShell

Print ("======= incoming parameter =======" +invalue);

Outvalue = invalue+1;

Some of the most commonly used commands ( Green is what I use in the project ) :

Print (): Very similar to Ava's System.out.println (), unless it guarantees that the output is always the command line. Print () can also display the types of objects (such as arrays), but more detailed than Java.

Source (), run (): reads a BSH script to the interpreter or runs in another interpreter.

FRAME ()-Displays a frame or JFrame GUI component.
Load (), save ()-loads and saves a serialized object to a file.
CD (), Cat (), dir (), pwd (), etc.-Unix-like shell commands.
EXEC ()-run a local program.
JAVAP ()-Prints an object's methods and fields, similar to the Java JAVAP command.
Setaccessibility ()-opens unrestricted access to private and protected components.

Scripting Methods

You can declare and use methods in BSH, as in Java classes. Only BSH methods can have dynamic (loose) parameters and return types.

Definition method in BSH file (two numbers added)

public int Add (int num1, int num2) {

return num1+num2;

}

int result = Add (1,2);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.