<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Joey Gibson&#039;s Blog &#187; jython</title>
	<atom:link href="http://joeygibson.com/category/tech/jython/feed/" rel="self" type="application/rss+xml" />
	<link>http://joeygibson.com</link>
	<description>Java, Clojure, Scala, Groovy, Ruby, Python, Lisp, Objective-C, OSX, politics, religion, Koine Greek, Tae Kwon Do, Spanish and much more!</description>
	<lastBuildDate>Wed, 08 Feb 2012 03:50:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Jython Is Just Too Useful</title>
		<link>http://joeygibson.com/2003/05/22/jython-is-just-too-useful/</link>
		<comments>http://joeygibson.com/2003/05/22/jython-is-just-too-useful/#comments</comments>
		<pubDate>Thu, 22 May 2003 13:20:00 +0000</pubDate>
		<dc:creator>Joey Gibson</dc:creator>
				<category><![CDATA[jython]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://JythonIsUseful</guid>
		<description><![CDATA[A colleague just came to me asking about Java serialization and output options. We&#8217;re going to store some partially filled Serializable DTOs in a BLOB in our database so he needed some info. Our talk then turned to options for &#8230; <a href="http://joeygibson.com/2003/05/22/jython-is-just-too-useful/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A colleague just came to me asking about Java serialization and output options. We&#8217;re going to store some partially filled <a href="http://java.sun.com/j2se/1.3/docs/api/java/io/Serializable.html">Serializable</a> DTOs in a BLOB in our database so he needed some info. Our talk then turned to options for storage and such. He already knew about using <a href="http://java.sun.com/j2se/1.3/docs/api/java/io/ObjectOutputStream.html">ObjectOutputStream</a> on top of a <a href="http://java.sun.com/j2se/1.3/docs/api/java/io/FilterOutputStream.html">FileOutputStream</a>, but I also told him how to get a byte array from the object using a <a href="http://java.sun.com/j2se/1.3/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream</a>. To illustrate, I fired up <a href="http://www.jython.org">Jython</a> in interactive mode and typed the following:</p>
<div class="boxed">
<pre> <span style="color: blue;">jython</span>
*sys-package-mgr*: processing modified jar, 'C:\AspectJ1.1\lib\aspectjrt.jar'
Jython 2.1 on java1.3.1_02 (JIT: null)
Type "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; from java.io import *
&gt;&gt;&gt; from java.util import*
&gt;&gt;&gt; m = ArrayList()
&gt;&gt;&gt; m.add("Foo")
1
&gt;&gt;&gt; m.add("Bar")
1
&gt;&gt;&gt; m
[Foo, Bar]
&gt;&gt;&gt; baos = ByteArrayOutputStream()
&gt;&gt;&gt; oos = ObjectOutputStream(baos)
&gt;&gt;&gt; oos.writeObject(m)
&gt;&gt;&gt; oos.close()
&gt;&gt;&gt; <span style="color: red;">dir(baos.class)
['__init__', 'reset', 'size', 'toByteArray', 'toString', 'write', 'writeTo']</span>
&gt;&gt;&gt; baos.toByteArray()
array([-84, -19, 0, 5, 115, 114, 0, 19, 106, 97,
  118, 97, 46, 117, 116, 105, 108, 46, 65, 114, 114,
  97, 121, 76, 105, 115, 116, 120, -127, -46, 29,
  -103, -57, 97, -99, 3, 0, 1, 73, 0, 4, 115, 105,
  122, 101, 120, 112, 0, 0, 0, 2, 119, 4, 0, 0, 0,
  10, 116, 0, 3, 70, 111, 111, 116, 0, 3, 66, 97,
  114, 120], byte)
&gt;&gt;&gt; bais = ByteArrayInputStream(baos.toByteArray())
&gt;&gt;&gt; ois = ObjectInputStream(bais)
&gt;&gt;&gt; x = ois.readObject()
&gt;&gt;&gt; x
[Foo, Bar]
&gt;&gt;&gt; m
[Foo, Bar]</pre>
</div>
<p>There are lots of cool things there, but specifically, notice the bits in red. I couldn&#8217;t remember the method to call to get the byte array, so by using the Python <code>dir()</code> method on the class of the object, I got a list of available methods. <code>toByteArray()</code> was the ticket and you can see both the array itself and then that I went further and deserialized the byte array using a <a href="http://java.sun.com/j2se/1.3/docs/api/java/io/ByteArrayInputStream.html">ByteArrayInputStream</a>. Think about how many lines of Java code I would have had to write to show him the same thing. But even if the syntax were just as verbose as Java, not having an edit-compile-run cycle made the demo far faster and productive than it would have otherwise been.</p>
]]></content:encoded>
			<wfw:commentRss>http://joeygibson.com/2003/05/22/jython-is-just-too-useful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cool Use of Jython</title>
		<link>http://joeygibson.com/2002/12/31/cool-use-of-jython/</link>
		<comments>http://joeygibson.com/2002/12/31/cool-use-of-jython/#comments</comments>
		<pubDate>Tue, 31 Dec 2002 14:03:00 +0000</pubDate>
		<dc:creator>Joey Gibson</dc:creator>
				<category><![CDATA[jython]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://JythonWebService</guid>
		<description><![CDATA[I just had to share this. I am working on a very large project using BEA&#8216;s WebLogic 7. This project takes a good 10 minutes to go through an entire compile/deploy cycle. This is a real hassle when I need &#8230; <a href="http://joeygibson.com/2002/12/31/cool-use-of-jython/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><!-- _tech_jython_JythonWebService --> I just had to share this. I am working on a very large project using  <a href="http://bea.com">BEA</a>&#8216;s <a href="http://bea.com/products/weblogic/server/index.shtml">WebLogic 7</a>. This project takes a good 10 minutes to go through an entire compile/deploy cycle. This is a real hassle when I need to noodle something out, test some idea, etc. I use <a href="http://jython.org">Jython</a> for all manner of nifty things including testing things from the client side of my app, but what if I wanted to test something from within the container? Sure I could write a  <a href="http://jakarta.apache.org/cactus/">Cactus</a> test (and I have written many), but  adding/changing a Cactus test results in a compile/deploy cycle. What I needed was something better.</p>
<p>What I came up with is a truly (dirty) <a href="http://www.tuxedo.org/~esr/jargon/html/entry/hack.html">hack</a> that is totally insecure yet really useful. <span class="smallType">(IOW, don&#8217;t put it on a production server.)</span></p>
<p>I embeded a Jython interpreter inside an Apache <a href="http://xml.apache.org/axis/">Axis</a> web service. I then have a short Jython script on the client side that sends an arbitraty  script to the server, which is executed and then the stdout/stderr are bundled up and sent back in the  <a href="http://www.w3.org/TR/SOAP/">SOAP</a> response. I think this is really cool.</p>
<p>The files involved are</p>
<ul>
<li><a href="#JythonWebService">JythonWebService.java</a> &#8211; the SOAP service</li>
<li><a href="#remotepy">remote.py</a> &#8211; the client side &#8220;main&#8221; program</li>
<li><a href="#remotebat">remote.bat</a> &#8211; batch file to run remote.py</li>
<li><a href="#test">test.py</a> &#8211; example script</li>
</ul>
<p>The web service is where the real work occurs, but it&#8217;s brain-dead simple. Here&#8217;s  <a name="JythonWebService">JythonWebService </a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.joeygibson.soap</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.StringWriter</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.python.util.PythonInterpreter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.python.core.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JythonWebService
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> PythonInterpreter interp <span style="color: #339933;">=</span>
        <span style="color: #000000; font-weight: bold;">new</span> PythonInterpreter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Writer</span> out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Writer</span> err <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> JythonWebService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        interp.<span style="color: #006633;">setOut</span><span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        interp.<span style="color: #006633;">setErr</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> exec<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> script<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        interp.<span style="color: #006633;">exec</span><span style="color: #009900;">&#40;</span>script<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">StringBuffer</span> results <span style="color: #339933;">=</span>
            <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">&quot;----- StdOut -----<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        results.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>out.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        results.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>----- StdErr -----<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        results.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>err.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> results.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Simple, eh? We create the Jython interpreter and then give it two  <a href="http://java.sun.com/j2se/1.3/docs/api/java/io/StringWriter.html">StringWriter</a>s that will capture stdout and stderr, respectively. Then in the <code>exec</code> method we accept the script from the SOAP envelope, let the interpreter execute it, and then bundle up the stdout and stderr with nice little markers to differentiate them. That then goes back over the wire to the caller.</p>
<p>Next is <a name="remotepy">remote.py</a> that executes the service from the client side.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">string</span>, <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">from</span> java.<span style="color: black;">io</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> org.<span style="color: black;">apache</span>.<span style="color: black;">axis</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> org.<span style="color: black;">apache</span>.<span style="color: black;">axis</span>.<span style="color: black;">client</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> javax.<span style="color: #dc143c;">xml</span>.<span style="color: black;">namespace</span> <span style="color: #ff7700;font-weight:bold;">import</span> QName
&nbsp;
<span style="color: #ff7700;font-weight:bold;">global</span> service, call
&nbsp;
service = Service<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
call = service.<span style="color: black;">createCall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
call.<span style="color: black;">setTargetEndpointAddress</span><span style="color: black;">&#40;</span>
    <span style="color: #483d8b;">&quot;http://localhost:7001/ivr/services/AxisServlet&quot;</span><span style="color: black;">&#41;</span>
call.<span style="color: black;">setOperationName</span><span style="color: black;">&#40;</span>
    QName<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;JythonWebService&quot;</span>, <span style="color: #483d8b;">&quot;exec&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
scriptLines = f.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
script = <span style="color: #dc143c;">string</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>scriptLines, <span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
ret = call.<span style="color: black;">invoke</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>script<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> ret</pre></td></tr></table></div>

<p>Here we first import the necessary packages/classes from both Java and Jython. We then create the Axis Service and Call objects, setting the endpoint and operation name on the call. Next we load up  the specifed file (this could easily be changed to support multiple scripts from the command line), concatenate each line into one big string, and then execute the  web service. The script is then passed as the sole argument (wrapped in an array) to  the exec method of Call, which executes the remote call. Finally the result is printed.</p>
<p>Next in line is <a name="remotebat">remote.bat</a> the batch file I used to execute the client. It just sets up a reasonable classpath and then invokes Jython.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="dos" style="font-family:monospace;"><span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">ECHO</span> OFF
<span style="color: #b1b100; font-weight: bold;">setlocal</span>
&nbsp;
<span style="color: #b1b100; font-weight: bold;">set</span> CLASSPATH=<span style="color: #33cc33;">&amp;</span>lt;jars from the axis/lib directory<span style="color: #33cc33;">&amp;</span>gt;
<span style="color: #00b100; font-weight: bold;">call</span> jython remote.py <span style="color: #33cc33;">%</span><span style="color: #448888;">*</span></pre></td></tr></table></div>

<p>This adds all the jar files in the axis/lib directory to the classpath and then runs our remote.py script through Jython. You may have to get creative with how you set your classpath. I use <a href="http://www.jpsoft.com/4ntdes.htm">4NT</a> as my command prompt, which has much longer command line allowances. The setting of the classpath can be a pretty long string and CMD.exe may barf on it. YMMV.</p>
<p>Finally we need to test something. Here&#8217;s <a name="test">test.py</a> that will run on the server via our  web service</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> javax.<span style="color: black;">naming</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
ic = InitialContext<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
vv = ic.<span style="color: black;">lookup</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ejb/VVLookup&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">create</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
l = vv.<span style="color: black;">getValidValuesForFieldNumber</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1558</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> l: <span style="color: #ff7700;font-weight:bold;">print</span> i</pre></td></tr></table></div>

<p>Obviosly you would have to tailor this for your setup since it&#8217;s doubtful that you have a stateless session bean deployed at ejb/VVLookup&#8230; Anyway, you can see that I create an InitialContext (with no parameters since we&#8217;re inside the container) do a lookup, execute a method that returns a collection and then iterate over it, printing them out. Since  print goes to stdout, the output will be captured and then returned to the client.</p>
<p>That&#8217;s it! If anyone want&#8217;s a copy, let me know. It&#8217;s so simple, though, that you could just do it yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://joeygibson.com/2002/12/31/cool-use-of-jython/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

