posts - 97,  comments - 93,  trackbacks - 0

          Most developers have heard of, and possibly used, scripting languages such as Ruby, JavaScript, and Python. These dynamic languages are enjoying a resurgence in popularity, largely because of their flexibility and simplicity, and the productivity gains they promise.

          Java 6 comes with built-in support for scripting languages. You can embed scripts in various scripting languages into your Java applications, passing parameters, evaluating expressions, and retrieving results. And you can do it all pretty seamlessly.

          First of all, you obtain a new ScriptEngine object from a ScriptEngineManager, as shown here:

          ScriptEngineManager manager = new ScriptEngineManager();

          ScriptEngine engine = manager.getEngineByName("js");

          Each scripting language has its own unique identifier. The "js" here means you're dealing with JavaScript.

          Now you can start having some fun. Interacting with a script is easy and intuitive. You can assign scripting variables using the put() method and evaluate the script using the eval() method,. which returns the most recently evaluated expression processed by the script. And that pretty much covers the essentials. Here's an example that puts it all together:

          engine.put("cost", 1000);
          String decision = (String) engine.eval(
          "if ( cost >= 100){ " +
          "decision = 'Ask the boss'; " +
          "} else {" +
          "decision = 'Buy it'; " +
          "}");
          assert ("Ask the boss".equals(decision));

          You can do more than just pass variables to your scripts— you can also invoke Java classes from within your scripts. Using the importPackage() function enables you to import Java packages, as shown here:

          				
          
          engine.eval("importPackage(java.util); " +
          "today = new Date(); " +
          "print('Today is ' + today);");

          Another cool feature is the Invocable interface, which lets you invoke a function by name within a script. This lets you write libraries in scripting languages, which you can use by calling key functions from your Java application. You just pass the name of the function you want to call, an array of Objects for the parameters, and you're done! Here's an example:

          				
          
          engine.eval("function calculateInsurancePremium(age) {...}");
          Invocable invocable = (Invocable) engine;
          Object result = invocable.invokeFunction("calculateInsurancePremium",
          new Object[] {37});

          You actually can do a fair bit more than what I've shown here. For example, you can pass a Reader object to the eval() method, which makes it easy to store scripts in external files, or bind several Java objects to JavaScript variables using a Map-like Binding object. You can also compile some scripting languages to speed up processing. But you probably get the idea that the integration with Java is smooth and well thought-out.

          posted on 2007-03-07 21:49 wqwqwqwqwq 閱讀(475) 評(píng)論(0)  編輯  收藏 所屬分類: SUN Tech
          <2007年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567




          常用鏈接

          留言簿(10)

          隨筆分類(95)

          隨筆檔案(97)

          文章檔案(10)

          相冊(cè)

          J2ME技術(shù)網(wǎng)站

          java技術(shù)相關(guān)

          mess

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          校園夢(mèng)網(wǎng)網(wǎng)絡(luò)電話,中國(guó)最優(yōu)秀的網(wǎng)絡(luò)電話
          主站蜘蛛池模板: 德保县| 革吉县| 木兰县| 安顺市| 龙泉市| 基隆市| 定襄县| 五指山市| 焉耆| 九龙城区| 庐江县| 浦北县| 宜章县| 赤城县| 剑川县| 丰原市| 天峻县| 潼南县| 江西省| 金堂县| 梁平县| 鹤壁市| 铅山县| 化德县| 咸阳市| 清远市| 滁州市| 邢台市| 康定县| 仙游县| 贵阳市| 西宁市| 饶平县| 独山县| 郧西县| 宁明县| 巴马| 深州市| 应城市| 九龙县| 江北区|