<?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>&#964;&#949;&#967;&#957;&#959;&#963;&#959;&#966;&#953;&#945; &#187; Java</title>
	<atom:link href="http://lackoftalent.org/michael/blog/category/development/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://lackoftalent.org/michael/blog</link>
	<description>The occasional rambling of a digital library artisan</description>
	<lastBuildDate>Mon, 10 Oct 2011 12:33:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Jython scp</title>
		<link>http://lackoftalent.org/michael/blog/2008/04/17/jython-scp/</link>
		<comments>http://lackoftalent.org/michael/blog/2008/04/17/jython-scp/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 20:08:42 +0000</pubDate>
		<dc:creator>Michael Giarlo</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Transfer]]></category>

		<guid isPermaLink="false">http://lackoftalent.org/michael/blog/?p=124</guid>
		<description><![CDATA[In spite of some open questions, I&#039;ve been making some progress on my Jython-based transport tool. Right now it&#039;s pretty dumb and simple: it copies files to and fro via scp. Being a newb at both Java and Jython made finding the right libraries a bit of a challenge, and so I&#039;m posting some code [...]]]></description>
			<content:encoded><![CDATA[<abbr class="unapi-id" title="oai:lackoftalent.org:technosophia:124"><!-- &nbsp; --></abbr>
<p>In spite of some <a href="http://lackoftalent.org/michael/blog/2008/04/11/jythons-and-javas-and-bears-oh-my/" target="_blank">open questions</a>, I&#039;ve been making some progress on my Jython-based transport tool.  Right now it&#039;s pretty dumb and simple: it copies files to and fro via scp.</p>
<p>Being a newb at both Java and Jython made finding the right libraries a bit of a challenge, and so I&#039;m posting some code here for folks in the same boat.  It&#039;s not particularly pretty due to 1) wanting to get something working very quickly, and 2) weird errors when I try to make things prettier (such as getting rid of the hard-coded bits), but I&#039;ll resolve these soon.<br />
<span id="more-124"></span><br />
The jython code:</p>
<pre name="code" class="python">
# Biter.py
import java
from org.apache.tools.ant import Project
from org.apache.tools.ant.taskdefs.optional import ssh

class Biter(java.lang.Object):
    def __init__(self):
        self.keyfile = '/home/user/.ssh/id_rsa'

    def transport(self, from_uri, to_uri):
        "@sig public void transport(String from_uri, String to_uri)"
        scp = ssh.Scp()
        scp.setKeyfile(self.keyfile)
        scp.setPassphrase('')
        scp.setTrust(True)
        scp.setProject(Project())
        scp.setFile(from_uri)
        scp.setTodir(to_uri)
        scp.execute()
        print "%s -&gt; %s" % (from_uri, to_uri)
</pre>
<p>I set my CLASSPATH to include /usr/share/jython2.2.1/jython.jar, /usr/share/java/jsch-0.1.28.jar, /usr/share/ant/lib/ant-jsch.jar, /usr/share/ant/lib/ant.jar, and &#039;.&#039;, and then compile to bytecode via <code>jythonc -idp gov.loc.repository.transport Biter.py</code>.  Once it&#039;s compiled, I can use the Biter class in Java code thusly:</p>
<pre name="code" class="java">
# BiterClient.java
import gov.loc.repository.transport.Biter;

public class BiterClient {
    public static void main(String[] args) {
        Biter biter = new Biter();
        biter.transport("/home/user/tmp/test1", "user@example.org:tmp/test2");
        biter.transport("user@example.org:tmp/test2", "/home/user/tmp/test3");
    }
}
</pre>
<p>I should note that the passphrase and keyfile are not necessary if you&#039;re passing valid username and password credentials in the remote to_ or from_uri (and the server in question supports password auth).  But pubkey auth is useful for our purposes and that&#039;s why it&#039;s in there.</p>
<p>Hopefully someone somewhere finds this helpful. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://lackoftalent.org/michael/blog/2008/04/17/jython-scp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jythons and Javas and bears, oh my!</title>
		<link>http://lackoftalent.org/michael/blog/2008/04/11/jythons-and-javas-and-bears-oh-my/</link>
		<comments>http://lackoftalent.org/michael/blog/2008/04/11/jythons-and-javas-and-bears-oh-my/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 01:41:52 +0000</pubDate>
		<dc:creator>Michael Giarlo</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Digital Libraries and Archives]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Preservation]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Repositories]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Transfer]]></category>

		<guid isPermaLink="false">http://lackoftalent.org/michael/blog/?p=122</guid>
		<description><![CDATA[It&#039;s hard to believe but I&#039;ve been at the new job for six months already, a full half-year come the 29th. Some days it seems like I&#039;ve been here forever; others like I&#039;m still a rank newb. I haven&#039;t written terribly much about what I&#039;ve been up to (but I assure you I&#039;ve been busy). [...]]]></description>
			<content:encoded><![CDATA[<abbr class="unapi-id" title="oai:lackoftalent.org:technosophia:122"><!-- &nbsp; --></abbr>
<p>It&#039;s hard to believe but I&#039;ve been at the <a href="http://lackoftalent.org/michael/blog/2007/09/27/october/" target="_blank">new job</a> for six months already, a full half-year come the 29th.  Some days it seems like I&#039;ve been here forever; others like I&#039;m still a rank newb.   I haven&#039;t written terribly much about what I&#039;ve been up to (but I assure you I&#039;ve been busy).  Let me rectify that.</p>
<h2><strong>The Transfer Problem</strong></h2>
<p>Two of the projects I&#039;ve been working on relate to a fairly general problem that we like to call &#034;transfer,&#034; which revolves around, well, transferring files to and fro.  Sounds simple.  <em>Is</em> simple.  That is, until you start thinking about preservation and accounting for a highly heterogeneous network with idiosyncratic nodes, esoteric storage software, and differential firewall rules.  And that&#039;s where it gets interesting (and problematic). <span id="more-122"></span>The transferring itself, or copying of files from one location to another which we call &#034;transport,&#034; is the easiest part.  We like to use common tools in our environment.  It makes life easy.  And so good ol&#039; <a href="http://en.wikipedia.org/wiki/Secure_copy" target="_blank">scp</a> seems like an obvious choice to handle the job.</p>
<p>Since preservation is a core aspect of our &#034;repository,&#034; a term which I use loosely, we must build certain other functionalities into the transfer process: validation, verification, inventory, backup, ingest, and so forth.  Every time a file is copied to a non-transient location, we verify the file against a (<a href="http://www.faqs.org/rfcs/rfc3174.html" target="_blank">SHA1</a>) checksum and record an event for auditing purposes.</p>
<h2><strong>Repository Workflows</strong></h2>
<p>Some steps in the transfer process are routine and best handled by machines. Thus we automate them with scripts and code.  Others require human intervention.  This introduces another key aspect of our repository needs: workflows.  No two projects will have the same workflow and yet all will have some steps in common.  We&#039;re using JBoss&#039;s <a href="http://www.jboss.org/jbossjbpm/" target="_blank">jBPM</a> library for workflow management and it is more than capable of handling our workflow needs.  It allows us to model complex and varied flows in a robust and not <em>ad hoc</em> way; it does seem preferable to me to model our workflows via jBPM&#039;s graphical editor (serialized to <a href="http://docs.jboss.com/jbpm/v3/userguide/jpdl.html" target="_blank">JPDL</a> XML) rather than copying around blocks of code and otherwise modeling the workflow procedurally in business logic.</p>
<p>One of my coworkers (<a href="http://www.dlib.org/dlib/july07/littman/07littman.html" target="_blank">the author of this</a>) designed a complete workflow system in jBPM last summer and I&#039;ve taken on implementing, tweaking, and testing this system, which has required bootstrapping my sorry-arse Java skills and learning jBPM.  Though I find it difficult to think in Java patterns and generally find it a burdensome environment, I&#039;m quite impressed by jBPM.  I&#039;ve been working on various updates of and unit test coverage for the workflow system, which has been a crash course in a number of Java technologies and a perfect first task at LC, as it gets me into the guts of things.  The Java stack we use for our workflow is highly abstracted and componentized, which is conveniently modular&#8230; but it&#039;s also Java: fairly heavy and arguably not as agile as dynamic languages such as Python.</p>
<h2><strong>Two Great Tastes?</strong></h2>
<p>So recently we&#039;ve begun to think about implementing some transfer and workflow components in <a href="http://www.jython.org/" target="_blank">Jython</a> (Python written in Java).  Why?  The value of Jython is as follows:</p>
<ol>
<li><strong>Ease of deployment</strong> &#8211; Deploying jar files to existing JVMs in production environments (which we do not control) is a simple task, or at least simpler than some other options.</li>
<li><strong>Interoperability</strong> &#8211; Our stack is primarily Java-based and so interoperating with existing Java components means not having to rewrite functionality in other languages.  Jython allows Python to talk to Java and vice versa.</li>
<li><strong>Familiarity</strong> &#8211; It&#039;s Python, and we like Python.  It&#039;s the closest my team has to a <em>lingua franca</em> and so it increases the chances of sharing code, maintaining code, and so forth.</li>
</ol>
<p>It does not come without its drawbacks:</p>
<ol>
<li><strong>Currency</strong> &#8211; The Jython project went moribund for a few years or so and the latest stable version is now 2.2.1.  Compare that to the latest version of Python: <a href="http://www.python.org/download/releases/2.5.2/" target="_blank">2.5.2</a>.  I don&#039;t begrudge the Jython developers, though.  I&#039;m glad some folks picked the project up, dusted it off, and breathed new life into it.   I am also glad that they&#039;re skipping 2.3 and 2.4 releases and plowing right into a 2.5 release.  Because of this currency issue, some Python libraries won&#039;t work with the latest Jython and that means you&#039;re stuck looking for outdated, potentially vulnerable Python libraries, or hooking into Java libraries for the same functionality (which inevitably means more lines of code).  I&#039;m no lines of code fetishist but it does militate against the goal of agility somewhat.One does have the option of living on the edge and trying out the 2.5 branch, but that seems out of step with an infrastructure that is supposed to preserve terabytes upon terabytes of our nation&#039;s, and the world&#039;s, intellectual property.  It&#039;s a responsibility I do not take lightly, as much as I&#039;d like to be on the bleeding edge.</li>
<li><strong>Interoperability difficulties &#8211; </strong>Talking to Java from Jython is a snap: just <code>import java</code> at the top of your script, and, assuming your classpath is copacetic, <em>voila:</em> you have access to Java libraries in your Python code!  Talking to your Jython modules from Java code is, well, a little more complicated. Read on.</li>
</ol>
<p>Despite the caveats it does seem a sane, reasonable, and potentially productive path to go down. Right?  I am specifically looking to implement two workflow components in Jython: one for transport (wrapping Ant&#039;s<a href="http://www.jcraft.com/jsch/" target="_blank"> JSch</a> library, which provides a slick scp API) and the other for automation of <a href="http://www.opensolaris.org/os/community/zfs/" target="_blank">ZFS</a> filesystem/volume creation on the staging server.  Nothing arcane, nothing tricky, nothing fancy.  So it must be easy!  Right? &#8230;</p>
<h2><strong>Lessons Learned?</strong></h2>
<p>I&#039;m beginning to wonder about the feasibility of using Jython to make bits of our Java stack more agile. Specifically, there are three ways to get at Jython code from Java:</p>
<ol>
<li>Compile to bytecode/jar via the Jython compiler (jythonc) and reference your Jython objects and methods as though they were <a href="http://en.wikipedia.org/wiki/POJO" target="_blank">POJOs</a></li>
<li>Embed a Jython interpreter</li>
<li>Instantiate a (<a href="http://jcp.org/en/jsr/detail?id=223" target="_blank">JSR-223</a>) script engine</li>
</ol>
<p>Option 1 is nice because you get object- and method-level interop.  However, <a href="http://www.jython.org/Project/jythonc.html" target="_blank">jythonc</a> is unsupported and will disappear.  This does not seem sustainable though I might be able to limp along a while.  And there are <a href="http://sourceforge.net/mailarchive/message.php?msg_name=96c4692d0802280853r70e6b74fg71aed3cdd7701cbf%40mail.gmail.com" target="_blank">signs of hope</a>:</p>
<blockquote><p>Though jythonc is going away, all of the capabilities it provides will be present in 2.5 in other forms.  We&#039;re adding functionality to expose Python classes as Java classes using decorators to replace the docstring class creation that jythonc provided, and we&#039;re adding static compilation of proxy classes so regular jython can run in applets and other environments with restrictive classloaders.  We&#039;re definitely doing something about jythonc.</p></blockquote>
<p>That doesn&#039;t help much <em>now</em>, of course, but just because jythonc goes away does not mean my jars will stop working.</p>
<p>The suggested methods for option 2 [<a href="http://wiki.python.org/jython/JythonMonthly/Articles/October2006/3" target="_blank">1</a>, <a href="http://wiki.python.org/jython/JythonMonthly/Articles/September2006/1" target="_blank">2</a>] seem to be more trouble than they&#039;re worth.  If the goal is more agile development for certain components, the reliance upon multiple, separate Java classes &#8212; an interface class and an object factory, in the examples listed &#8212; to get a ten-line Jython script working, this seems suboptimal, both inefficient and not straightforwardly maintainable; it seems, to me and my Java-dumb ways, rather baroque.</p>
<p>Option 3 [<a href="http://wiki.python.org/jython/JythonMonthly/Articles/October2006/1" target="_blank">1</a>, <a href="http://www.jython.org/Project/userguide.html#using-jsr-223" target="_blank">2</a>] is more appealing than option 2 as it does not rely upon these other classes specifically for Jython code.  But the number of lines of Java code that must be wrapped around the Jython to get it working looks like overkill for the drop-dead simple scripts I&#039;m writing &#8212; it might be easier, for instance, to just write the darn things in Java and be done with it.  (Did I just say that?)</p>
<h2><strong>Conclusions</strong></h2>
<p>Options 2 and 3 are similar as both involve embedding Jython code, or referencing files with Jython code, and interpreting the code within Java.  Generally, I worry that either option would obviate the benefit of agile Jython scripting because you wind up wrapping the code in so much Java.  I offer two disclaimers to counter my objections:</p>
<ol>
<li>Cleverer Java coders than myself could, I am almost certain, find ways to build abstractions (or abstractions of abstractions) to eliminate the &#034;lines of code&#034; and &#034;many separate classes per Jython script&#034; issues</li>
<li>The value of Jython in our Java environment increases proportionately with the complexity of the component &#8212; given the overhead, a short Java class seems easier and more straightforward to implement than a short embedded Jython script.  On the other hand, there&#039;s value in embedding a Jython script that&#039;d be an order of magnitude simpler than its Java analog.</li>
</ol>
<p>At least, that&#039;s the state of my head right now re: getting Jython and Java to play nice.  If I make any breakthroughs or give up entirely, I&#039;ll post follow-ups.</p>
<p>I am but a Java philistine and a Jython neophyte, so I remain humbly open-minded.  I would greatly appreciate comments, questions, corrections, smackdowns, sagacious advice, and so on.</p>
]]></content:encoded>
			<wfw:commentRss>http://lackoftalent.org/michael/blog/2008/04/11/jythons-and-javas-and-bears-oh-my/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

