<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>Weiqi Gao&#039;s Observations - beanshell tag</title>
  <link>http://www.weiqigao.com/blog/tags/beanshell/</link>
  <description>Sharing My Experience...</description>
  <language>en</language>
  <copyright>Weiqi Gao</copyright>
  <lastBuildDate>Fri, 11 May 2012 12:48:36 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  <image>
    <url>http://pebble.sourceforge.net/common/images/powered-by-pebble.gif</url>
    <title>Weiqi Gao&#039;s Observations</title>
    <link>http://www.weiqigao.com/blog/</link>
  </image>
  
  
  <item>
    <title>Kyle Cordes: Scripting Your Java App</title>
    <link>http://www.weiqigao.com/blog/2006/11/10/kyle_cordes_scripting_your_java_app.html</link>
    
      
        <description>
          &lt;p&gt;This month&#039;s &lt;a href= &#034;http://www.ociweb.com/javasig/&#034; &gt;St. Louis JUG&lt;/a&gt; meeting featured &lt;a href= &#034;http://kylecordes.com/&#034; &gt;Kyle Cordes&lt;/a&gt; talking about scripting Java applications&lt;/p&gt;

&lt;p&gt;&#034;I attended one of &lt;a href= &#034;http://www.edwardtufte.com/tufte/&#034; &gt;Edward Tufte&lt;/a&gt;&#039;s classes,&#034; started Kyle.  I know right away that I won&#039;t be able to get the PowerPoint for the talk at the end.  Kyle did the presentation in Eclipse and showed code.&lt;/p&gt;

&lt;p&gt;Kyle handed out a one page summary that contained all the main points of the talk in complete sentences and paragraphs.  Since it&#039;s not very creative to simply copy that sheet of paper into this blog, I&#039;ll summarize the talk into bullet points.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;History of scripting&lt;/li&gt;
&lt;li&gt;Bean Scripting Framework (BSF)&lt;/li&gt;
&lt;li&gt;BeanShell (JSR 274)&lt;/li&gt;
&lt;li&gt;Groovy (JSR 241)&lt;/li&gt;
&lt;li&gt;Java 6 scripting (JSR 223)&lt;/li&gt;
&lt;li&gt;Demos&lt;/li&gt;
&lt;li&gt;Rhino (Mozilla&#039;s Java based JavaScript engine)&lt;/li&gt;
&lt;li&gt;&#034;You won&#039;t get into trouble by using the scripting language in the JDK&#034;&lt;/li&gt;
&lt;li&gt;We have been very happy using Rhino to scripting an enterprisey application&lt;/li&gt;
&lt;li&gt;It&#039;s surprisingly easy to add scripting support&lt;/li&gt;
&lt;li&gt;What&#039;s a scripting language
&lt;ul&gt;
&lt;li&gt;Dynamic typing&lt;/li&gt;
&lt;li&gt;Interpreted execution&lt;/li&gt;
&lt;li&gt;eval&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Why script
&lt;ul&gt;
&lt;li&gt;Configuration&lt;/li&gt;
&lt;li&gt;More important in commercial systems&lt;/li&gt;
&lt;li&gt;&#034;Alternate hard and soft layers&#034;&lt;/li&gt;
&lt;li&gt;modular &amp;amp; testable&lt;/li&gt;
&lt;li&gt;Users will use scripting to create incredible values&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Kinds of scripting APIs
&lt;ul&gt;
&lt;li&gt;Rules scripting&lt;/li&gt;
&lt;li&gt;Plugin scripting&lt;/li&gt;
&lt;li&gt;External scripting&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&#034;color:red&#034;&gt;Sandbox&lt;/span&gt; is very important (scripts shouldn&#039;t be able to see your app&#039;s internals)&lt;/li&gt;
&lt;/li&gt;
&lt;/ul&gt;
        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2006/11/10/kyle_cordes_scripting_your_java_app.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2006/11/10/kyle_cordes_scripting_your_java_app.html</guid>
    <pubDate>Fri, 10 Nov 2006 06:02:41 GMT</pubDate>
  </item>
  
  <item>
    <title>Where Is My java.home? </title>
    <link>http://www.weiqigao.com/blog/2006/06/14/where_is_my_java_home.html</link>
    
      
        <description>
          &lt;p&gt;From time to time, I need to find out what my JVM thinks a certain system property is set.  For example, the installation instructions of some software packages would say something like &#034;put this file in the lib subdirectory of what your JVM thinks is java.home.&#034;&lt;/p&gt;

&lt;p&gt;What I would usually do is to write a throw away Java program that prints out the said property.  And of course you get tired of doing this tedious chore after a while.  That&#039;s when you write a little script to do the work.&lt;/p&gt;

&lt;p&gt;Here&#039;s my little script.  It&#039;s call &lt;tt&gt;jenv&lt;/tt&gt;, and it is written in the &lt;a href= &#034;http://beanshell.org/&#034; &gt;BeanShell&lt;/a&gt; scripting language:&lt;/p&gt;

&lt;pre style=&#034;margin-left:3em&#034;&gt;[weiqi@gao]$ &lt;b&gt;cat jenv&lt;/b&gt;
#!/usr/bin/env bsh

// Get all the system properties
props = System.getProperties();

if (bsh.args.length == 0) {
  // print them all
  for (name : props.propertyNames()) {
    print(name + &#034;=&#034; + props.getProperty(name));
  }
} else {
  outer_for:
  for (name : props.propertyNames()) {
    for (regex : bsh.args) {
      if (name.matches(&#034;.*&#034; + regex + &#034;.*&#034;)) {
        print(name + &#034;=&#034; + props.getProperty(name));
        break outer_for;
      }
    }
  }
}
&lt;/pre&gt;

&lt;p&gt;It can be run anywhere a bash shell is available (Cygwin on Windows or Linux.)  When this script is run bash delegates to another shell script called &lt;tt&gt;bsh&lt;/tt&gt; passing the path name of the script file and any arguments.  On Linux, my &lt;tt&gt;bsh&lt;/tt&gt; script contains only the line:&lt;/p&gt;

&lt;pre style=&#034;margin-left:3em&#034;&gt;java bsh.Interpreter &#034;$@&#034;&lt;/pre&gt;

&lt;p&gt;In Cygwin, since the file name passed to &lt;tt&gt;bsh&lt;/tt&gt; is in Cygwin format, which Java doesn&#039;t understand, my &lt;tt&gt;bsh&lt;/tt&gt; script has to do a little bit more:&lt;/p&gt;

&lt;pre style=&#034;margin-left:3em&#034;&gt;#!/bin/bash
#
# Run the BeanShell command line interpreter
#
declare -a args
declare -i index

until [ -z &#034;$1&#034; ]
do
  if echo $1 | egrep -q &#039;^/|^\.&#039;
  then
    args[index]=`cygpath -a -w $1`
  else
    args[index]=$1
  fi
  ((index++))
  shift
done

java bsh.Interpreter ${args[@]}&lt;/pre&gt;

&lt;p&gt;The weird looking loop is there to straighten out any Cygwin path names to the Windows format so that Java can understand them.  It&#039;s stolen from &lt;a href= &#034;http://www.weiqigao.com/blog/2005/03/23/activestate_perl_under_cygwin_bash.html&#034; &gt;another script I&#039;ve written&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now that I have the scripts, finding out the values of system properties is as easy as finding out the values of environment variables.  The command &lt;tt&gt;jenv&lt;/tt&gt; by itself print out all system properties.  If any command line arguments are supplied, they are used as regular expressions to filter the system properties.  Only system properties whose name contains a substring that matches one of the regular expressions are printed.&lt;/p&gt;

&lt;pre style=&#034;margin-left:3em&#034;&gt;[weiqi@gao]$ &lt;b&gt;jenv user.home&lt;/b&gt;
user.home=C:\Documents and Settings\gao_w
[weiqi@gao]$ &lt;b&gt;jenv user home&lt;/b&gt;
user.country=US
user.dir=u:\bin
user.variant=
user.home=C:\Documents and Settings\gao_w
user.timezone=
user.name=gaow
java.home=C:\Program Files\Java\jre1.5.0_07
user.language=en&lt;/pre&gt;
        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2006/06/14/where_is_my_java_home.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2006/06/14/where_is_my_java_home.html</guid>
    <pubDate>Thu, 15 Jun 2006 01:06:07 GMT</pubDate>
  </item>
  
  </channel>
</rss>

