<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-26314880</id><updated>2012-01-29T09:28:59.954-08:00</updated><category term='Scala XSD XML'/><category term='Clojure'/><category term='Java'/><category term='remote REPL'/><category term='REPL'/><title type='text'>Sean Wellington's Blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://sean8223.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26314880/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://sean8223.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Sean Wellington</name><uri>http://www.blogger.com/profile/02056362838637172606</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-26314880.post-2146261110071149815</id><published>2009-09-10T11:36:00.000-07:00</published><updated>2009-10-02T09:15:14.803-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Scala XSD XML'/><title type='text'>XSD Validation in Scala</title><content type='html'>One of the coolest features of Scala is that it treats XML as a first-class data type. It can be embedded directly into program code (much like E4X in Actionscript) and processed very succinctly with the language's pattern matching constructs.&lt;br /&gt;&lt;br /&gt;But one key feature that the Scala libraries are lacking is support for validation of XML documents against an XSD schema. Fortunately, the JDK has a standard mechanism for this in the &lt;code&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/package-summary.html"&gt;javax.xml.validation&lt;/a&gt;&lt;/code&gt; package. And with a little bit of work we can combine these capabilities to get the best of both platforms.&lt;br /&gt;&lt;br /&gt;The &lt;code&gt;&lt;a href="http://www.scala-lang.org/docu/files/api/scala/xml/XML$object.html"&gt;scala.xml.XML&lt;/a&gt;&lt;/code&gt; object provides a set of simple functions for reading XML from various sources (files, streams, etc.). A quick inspection of the code in this class shows all the &lt;code&gt;load*&lt;/code&gt; methods to be wrappers around the &lt;code&gt;loadXML&lt;/code&gt; method of the &lt;code&gt;&lt;a href="http://www.scala-lang.org/docu/files/api/scala/xml/parsing/NoBindingFactoryAdapter.html"&gt;scala.xml.parsing.NoBindingFactoryAdapter&lt;/a&gt;&lt;/code&gt; class.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;NoBindingFactoryAdapter&lt;/code&gt; (via its parent class &lt;code&gt;&lt;a href="http://www.scala-lang.org/docu/files/api/scala/xml/parsing/FactoryAdapter.html"&gt;scala.xml.parsing.FactoryAdapter&lt;/a&gt;&lt;/code&gt;) implements the standard &lt;code&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/org/xml/sax/helpers/DefaultHandler.html"&gt;org.xml.sax.helpers.DefaultHandler&lt;/a&gt;&lt;/code&gt; interface and serves as a bridge between the Java and Scala XML worlds. The relevant &lt;code&gt;loadXML&lt;/code&gt; method is defined on &lt;code&gt;FactoryAdapter&lt;/code&gt; and looks like this:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;def loadXML(source: InputSource): Node = {&lt;br /&gt;   // create parser&lt;br /&gt;   val parser: SAXParser = try {&lt;br /&gt;     val f = SAXParserFactory.newInstance()&lt;br /&gt;     f.setNamespaceAware(false)&lt;br /&gt;     f.newSAXParser()&lt;br /&gt;   } catch {&lt;br /&gt;     case e: Exception =&amp;gt;&lt;br /&gt;     Console.err.println(&amp;quot;error: Unable to instantiate parser&amp;quot;)&lt;br /&gt;     throw e&lt;br /&gt;   }&lt;br /&gt;         &lt;br /&gt;   // parse file&lt;br /&gt;   scopeStack.push(TopScope)&lt;br /&gt;   parser.parse(source, this)&lt;br /&gt;   scopeStack.pop&lt;br /&gt;   return rootElem&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Essentially, this method creates a standard &lt;code&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/SAXParser.html"&gt;javax.xml.parsers.SAXParser&lt;/a&gt;&lt;/code&gt; object and passes itself in as the &lt;code&gt;ContentHandler&lt;/code&gt;. The callback methods in &lt;code&gt;FactoryAdapter&lt;/code&gt; convert the standard SAX parsing events into instances of the core Scala XML types. This is our hook for introducing XSD validation.&lt;br /&gt;&lt;br /&gt;The &lt;code&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/ValidatorHandler.html"&gt;javax.xml.validation.ValidatorHandler&lt;/a&gt;&lt;/code&gt; is a class that acts as a filter of sorts (although not a true &lt;code&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/org/xml/sax/XMLFilter.html"&gt;org.xml.sax.XMLFilter&lt;/a&gt;&lt;/code&gt;), intercepting callbacks sent from an SAX parser to a &lt;code&gt;ContentHandler&lt;/code&gt;, and generating errors if the data doesn't conform to its schema definition. We will create a subclass of &lt;code&gt;NoBindingFactoryAdapter&lt;/code&gt; that interposes a &lt;code&gt;ValidatorHandler&lt;/code&gt; between the parser and functions defined in the &lt;code&gt;FactoryAdapter&lt;/code&gt; to more-or-less transparently implement validation.&lt;br /&gt;&lt;br /&gt;We will use the &lt;code&gt;loadXML&lt;/code&gt; method of &lt;code&gt;FactoryAdapter&lt;/code&gt; as a starting point for implementation:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;import javax.xml.parsers.SAXParser&lt;br /&gt;import javax.xml.parsers.SAXParserFactory&lt;br /&gt;import javax.xml.validation.Schema&lt;br /&gt;import javax.xml.validation.ValidatorHandler&lt;br /&gt;import org.xml.sax.XMLReader&lt;br /&gt;&lt;br /&gt;class SchemaAwareFactoryAdapter(schema:Schema) extends NoBindingFactoryAdapter {&lt;br /&gt;&lt;br /&gt;  override def loadXML(source: InputSource): Elem = {&lt;br /&gt;    // create parser&lt;br /&gt;    val parser: SAXParser = try {&lt;br /&gt;      val f = SAXParserFactory.newInstance()&lt;br /&gt;      f.setNamespaceAware(true)&lt;br /&gt;      f.setFeature(&amp;quot;http://xml.org/sax/features/namespace-prefixes&amp;quot;, true)&lt;br /&gt;      f.newSAXParser()&lt;br /&gt;    } catch {&lt;br /&gt;      case e: Exception =&amp;gt;&lt;br /&gt;        Console.err.println(&amp;quot;error: Unable to instantiate parser&amp;quot;)&lt;br /&gt;        throw e&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    val xr = parser.getXMLReader()&lt;br /&gt;    val vh = schema.newValidatorHandler()&lt;br /&gt;    vh.setContentHandler(this)&lt;br /&gt;    xr.setContentHandler(vh)&lt;br /&gt;    &lt;br /&gt;    // parse file&lt;br /&gt;    scopeStack.push(TopScope)&lt;br /&gt;    xr.parse(source)&lt;br /&gt;    scopeStack.pop&lt;br /&gt;    return rootElem.asInstanceOf[Elem]&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The key differences here are that we have enabled namespace awareness on the parser (which is a requirement for schema validation!) and stuck our &lt;code&gt;ValidatorHandler&lt;/code&gt; in between the parser and the &lt;code&gt;FactoryAdapter&lt;/code&gt; instance. Because we are actually overriding &lt;code&gt;NoBindingFactoryAdapter#loadXML&lt;/code&gt; (which has a return type of &lt;code&gt;&lt;a href="http://www.scala-lang.org/docu/files/api/scala/xml/Elem$object.html"&gt;Elem&lt;/a&gt;&lt;/code&gt;) we need the cast in the final line.&lt;br /&gt;&lt;br /&gt;So now we can validate XML as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;// A schema can be loaded in like ...&lt;br /&gt;&lt;br /&gt;val sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)&lt;br /&gt;val s = sf.newSchema(new StreamSource(new File(&amp;quot;foo.xsd&amp;quot;)))&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// and whenever we would want to do something like:&lt;br /&gt;&lt;br /&gt;val is = new InputSource(new File(&amp;quot;foo.xml&amp;quot;))&lt;br /&gt;val xml = XML.load(is)&lt;br /&gt;&lt;br /&gt;// instead we'll use our class:&lt;br /&gt; &lt;br /&gt;val is = new InputSource(new File(&amp;quot;foo.xml&amp;quot;))&lt;br /&gt;val xml = new SchemaAwareFactoryAdapter(s).loadXML(is)&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26314880-2146261110071149815?l=sean8223.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sean8223.blogspot.com/feeds/2146261110071149815/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26314880&amp;postID=2146261110071149815' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26314880/posts/default/2146261110071149815'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26314880/posts/default/2146261110071149815'/><link rel='alternate' type='text/html' href='http://sean8223.blogspot.com/2009/09/xsd-validation-in-scala.html' title='XSD Validation in Scala'/><author><name>Sean Wellington</name><uri>http://www.blogger.com/profile/02056362838637172606</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26314880.post-8564904403734389774</id><published>2009-06-05T05:35:00.001-07:00</published><updated>2009-06-10T04:55:40.941-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='REPL'/><category scheme='http://www.blogger.com/atom/ns#' term='remote REPL'/><category scheme='http://www.blogger.com/atom/ns#' term='Clojure'/><title type='text'>Adding a Remotely Accessible REPL to a Clojure Application</title><content type='html'>One of the distinguishing features of Lisp systems, including Clojure, is that the components that parse and compile code are accessible to user programs, even at runtime (i.e. "&lt;a href="http://www.paulgraham.com/diff.html"&gt;the whole language always available&lt;/a&gt;"). This enables sophisticated language features like &lt;a href="http://clojure.org/macros"&gt;macros&lt;/a&gt;, and is very different than the traditional Java worldview of distinct compile- and run-times.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The most familiar application of this concept is the interactive REPL that comes with Clojure.&lt;/div&gt;&lt;div&gt;&lt;div&gt;Because the REPL is written in Clojure itself, it is available to any user program as the function &lt;code&gt;clojure.main/repl. &lt;/code&gt;We can use this in conjunction with the networking libraries from the JDK to add a REPL interface to our application that is accessible over a socket connection. This gives us the ability to interact with and even modify a running application (e.g. by adding new code or changing data values) simply by connecting to the REPL and evaluating Clojure expressions. Moreover, since none of these activities requires an application restart or a lengthy rebuild process, it can be a &lt;span class="Apple-style-span" style="font-style: italic;"&gt;huge&lt;/span&gt; time saver during the development process. &lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;There are only a few modifications to our skeletal web app to make this happen. First, I'll make a slight modification to the &lt;code&gt;ClojureContextListener&lt;/code&gt; so that it accepts a whitespace-separated list of files to evaluate on startup, rather than just a single file:&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;public void contextInitialized(ServletContextEvent sce) {&lt;br /&gt;   try {&lt;br /&gt;      ServletContext sc = sce.getServletContext();&lt;br /&gt;      String evalOnContextInitialized = sc.getInitParameter("evalOnContextInitialized");&lt;br /&gt;      String[] scripts = evalOnContextInitialized.split("\\s+");&lt;br /&gt;      for (String scripts : scripts) {&lt;br /&gt;         RT.loadResourceScript(script);&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;   catch (Exception e) {&lt;br /&gt;      // log an error message here ...&lt;br /&gt;   }&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;Then we'll create the file &lt;code&gt;myapp/repl.clj&lt;/code&gt;, in the &lt;code&gt;WEB-INF/classes&lt;/code&gt; directory of our application that provides the functions we need to start and run the REPL. Our REPL will run on a separate thread and serve a single client at a time. Because the built-in &lt;code&gt;repl&lt;/code&gt; function expects to send and receive data from &lt;code&gt;*in*&lt;/code&gt;, &lt;code&gt;*out*&lt;/code&gt; and &lt;code&gt;*err*&lt;/code&gt;, we just need to rebind them to the input and output streams associated with our socket connection:&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;(ns myapp.repl&lt;br /&gt;(:require clojure.main)&lt;br /&gt;   (:import (java.io InputStreamReader PrintWriter)&lt;br /&gt;                  (java.net ServerSocket Socket)&lt;br /&gt;                  (clojure.lang LineNumberingPushbackReader)))&lt;br /&gt;&lt;br /&gt;(defn do-on-thread&lt;br /&gt;   "Create a new thread and run function f on it. Returns the thread object that&lt;br /&gt;    was created."&lt;br /&gt;   [f]&lt;br /&gt;   (let [thread (new Thread f)]&lt;br /&gt;      (.start thread)&lt;br /&gt;      thread))&lt;br /&gt;&lt;br /&gt;(defn socket-repl&lt;br /&gt;   "Start a new REPL that is connected to the input/output streams of&lt;br /&gt;    socket."&lt;br /&gt;   [socket]&lt;br /&gt;   (let [socket-in (new LineNumberingPushbackReader&lt;br /&gt;                                 (new InputStreamReader&lt;br /&gt;                                    (.getInputStream socket)))&lt;br /&gt;           socket-out (new PrintWriter&lt;br /&gt;                                   (.getOutputStream socket) true)]&lt;br /&gt;      (binding [*in* socket-in&lt;br /&gt;                       *out* socket-out&lt;br /&gt;                       *err* socket-out]&lt;br /&gt;         (clojure.main/repl))))&lt;br /&gt;&lt;br /&gt;(defn start-repl-server&lt;br /&gt;   "Creates a new thread and starts a REPL server on listening on port. Returns&lt;br /&gt;    the server socket that was just created."&lt;br /&gt;   [port]&lt;br /&gt;   (let [server-socket (new ServerSocket port 0)]&lt;br /&gt;      (do-on-thread #(while true (socket-repl (.accept server-socket))))&lt;br /&gt;      server-socket))&lt;/code&gt;&lt;/pre&gt;And we'll add at the end of the file the following snippet that starts the REPL server, if the system property &lt;code&gt;replPort&lt;/code&gt; has been defined:&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;(let [repl-port (System/getProperty replPort)]&lt;br /&gt;(if (not (null? repl-port))&lt;br /&gt;   (def *repl-server* (start-repl-server (Integer/parseInt repl-port)))))&lt;/code&gt;&lt;/pre&gt;This will allow us to disable the remote REPL (for example, in production) by simply omitting the replPort property. Lastly we'll add this to web.xml&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;   &amp;lt;context-param&amp;gt;&lt;br /&gt;      &amp;lt;param-name&amp;gt;evalOnContextInitialized&amp;lt;/param-name&amp;gt;&lt;br /&gt;      &amp;lt;param-value&amp;gt;myapp/repl.clj myapp/web.clj&amp;lt;/param-value&amp;gt;&lt;br /&gt;      &amp;lt;/context-param&amp;gt;&lt;/code&gt;&lt;/pre&gt;Assuming that we have started our application with &lt;code&gt;-DreplPort=12345&lt;/code&gt; option, at this point we can any generic client (such as netcat) to connect to our application:&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;tinman:~ sean$ nc localhost 12345&lt;br /&gt;clojure.core=&amp;gt; &lt;/code&gt;&lt;/pre&gt; &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;UPDATE:&lt;/b&gt;It turns out that a remote REPL capability is available in the &lt;code&gt;clojure.contrib.server-socket&lt;/code&gt; package. I found it accidentally when my Emacs tags file led me to its &lt;code&gt;socket-repl&lt;/code&gt; function instead of my own. Interestingly, the two implementations are very similar.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26314880-8564904403734389774?l=sean8223.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sean8223.blogspot.com/feeds/8564904403734389774/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26314880&amp;postID=8564904403734389774' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26314880/posts/default/8564904403734389774'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26314880/posts/default/8564904403734389774'/><link rel='alternate' type='text/html' href='http://sean8223.blogspot.com/2009/06/adding-remotely-accessible-repl-to.html' title='Adding a Remotely Accessible REPL to a Clojure Application'/><author><name>Sean Wellington</name><uri>http://www.blogger.com/profile/02056362838637172606</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26314880.post-7088030214346088905</id><published>2009-06-03T07:41:00.001-07:00</published><updated>2009-06-03T11:58:42.783-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Clojure'/><title type='text'></title><content type='html'>Setting up a Clojure development environment is is fairly straightforward, although the tool support is fairly limited at present. The choices are:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;An Emacs &lt;a href="http://clojure.codestuffs.com/"&gt;clojure-mode&lt;/a&gt;, and that provides some support for the SLIME Lisp development environment;&lt;/li&gt;&lt;li&gt;An Eclipse plugin, &lt;a href="http://code.google.com/p/clojure-dev/"&gt;Clojure-dev;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;A NetBeans plugin, &lt;a href="http://www.enclojure.org/"&gt;Enclojure&lt;/a&gt;.&lt;/li&gt;&lt;/ol&gt;All of these are fairly rudimentary. They each support syntax highlighting and formatting and some degree of REPL integration. The Eclipse and NetBeans plugins provide the project management capabilities of their environments, but the more advanced features (refactoring, automatic symbol cross-referencing, debugging) aren't available. Hopefully they will be added as the tools mature.&lt;br /&gt;&lt;br /&gt;At this point, the most advanced version is Enclojure. Having been an Eclipse user for the past few years, learning a new tool has been somewhat annoying, but most of the features are present in NetBeans. The key advantage of Enclojure (over Clojure-dev) at this point is the ability to connect to a remote REPL, which is a very cool feature that I'll talk more about in a future post.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26314880-7088030214346088905?l=sean8223.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sean8223.blogspot.com/feeds/7088030214346088905/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26314880&amp;postID=7088030214346088905' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26314880/posts/default/7088030214346088905'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26314880/posts/default/7088030214346088905'/><link rel='alternate' type='text/html' href='http://sean8223.blogspot.com/2009/06/setting-up-clojure-development.html' title=''/><author><name>Sean Wellington</name><uri>http://www.blogger.com/profile/02056362838637172606</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26314880.post-1909842676891676700</id><published>2009-06-01T07:51:00.000-07:00</published><updated>2009-06-01T14:07:50.445-07:00</updated><title type='text'></title><content type='html'>Because Clojure has such tight integration with the JVM, embedding it into a Java application is a snap. In particular, writing Clojure code that runs inside of a J2EE web container and handles web requests is trivial.&lt;br /&gt;&lt;br /&gt;We just need to create two simple pieces of scaffolding code in Java to help the container find and dispatch web requests to our Clojure functions.&lt;br /&gt;&lt;br /&gt;The first item is a &lt;code&gt;ServletContextListener&lt;/code&gt; that initializes the Clojure runtime and evaluates our code when the web application is first loaded by the container:&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;package myapp.servlet;&lt;br /&gt;&lt;br /&gt;import javax.servlet.ServletContextEvent;&lt;br /&gt;import javax.servlet.ServletContextListener;&lt;br /&gt;&lt;br /&gt;import clojure.lang.RT;&lt;br /&gt;&lt;br /&gt;public class ClojureContextListener implements ServletContextListener {&lt;br /&gt;&lt;br /&gt;   public void contextInitialized(ServletContextEvent sce) {&lt;br /&gt;      try {&lt;br /&gt;         ServletContext sc = sce.getServletContext();&lt;br /&gt;         String script = sc.getInitParameter("evalOnContextInitialized");&lt;br /&gt;         RT.loadResourceScript(script);&lt;br /&gt;      }&lt;br /&gt;      catch (Exception e) {&lt;br /&gt;         // log an error message ...&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   public void contextDestroyed(ServletContextEvent sce) {&lt;br /&gt;      // nothing to do here ...&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;We'll expect a&lt;code&gt;&amp;lt;context-param&amp;gt;&lt;/code&gt; "evalOnContextInitialized" from the application's deployment descriptor to contain the name of a file containing our functions.&lt;br /&gt;&lt;br /&gt;The second bit of helper code is a simple &lt;code&gt;HttpServlet&lt;/code&gt; that delegates calls to its &lt;code&gt;doService&lt;/code&gt; method to a Clojure function of our choice:&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;package myapp.servlet;&lt;br /&gt;&lt;br /&gt;import javax.servlet.ServletConfig;&lt;br /&gt;import javax.servlet.ServletException;&lt;br /&gt;import javax.servlet.http.HttpServlet;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import javax.servlet.http.HttpServletResponse;&lt;br /&gt;&lt;br /&gt;import clojure.lang.RT;&lt;br /&gt;import clojure.lang.Var;&lt;br /&gt;&lt;br /&gt;public class ClojureServlet extends HttpServlet {&lt;br /&gt;&lt;br /&gt;   private Var entryPoint;&lt;br /&gt;&lt;br /&gt;   @Override&lt;br /&gt;   public void init(ServletConfig sc) throws ServletException {&lt;br /&gt;      String entryPointString = sc.getInitParameter("entryPoint");&lt;br /&gt;      if (entryPointString != null) {&lt;br /&gt;         String[] nsAndName = entryPointString.split("/", 2);&lt;br /&gt;         entryPoint = RT.var(nsAndName[0], nsAndName[1]);&lt;br /&gt;      }&lt;br /&gt;      else {&lt;br /&gt;         throw new ServletException("Servlet " + sc.getServletName() +&lt;br /&gt;                                    " missing 'entryPoint' parameter!");&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   @Override&lt;br /&gt;   protected void service(HttpServletRequest req, HttpServletResponse resp)&lt;br /&gt;   throws ServletException {&lt;br /&gt;      try {&lt;br /&gt;         entryPoint.invoke(req, resp);&lt;br /&gt;      }&lt;br /&gt;      catch (Exception e) {&lt;br /&gt;         throw new ServletException(e);&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;The name of the Clojure function will be specified as an &lt;code&gt;&amp;lt;init-param&amp;gt;&lt;/code&gt; in the servlet's configuration. We will follow the Clojure convention of &lt;span style="font-style: italic;"&gt;namespace/name&lt;/span&gt; convention for identifying our function.&lt;br /&gt;&lt;br /&gt;The final (and somewhat anti-climactic) piece of the puzzle is to create the Clojure code that actually handles the web request. This will be be a function of two arguments that accepts the &lt;code&gt;javax.servlet.http.HttpServletRequest&lt;/code&gt; and &lt;code&gt;javax.servlet.http.HttpServletResponse&lt;/code&gt; objects provided by the web container. We'll put in a simple "Hello world" handler to demonstrate.&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;(ns myapp.web)&lt;br /&gt;&lt;br /&gt;(defn hello&lt;br /&gt;   "Hello world"&lt;br /&gt;   [req resp]&lt;br /&gt;   (.. resp (getWriter) (println) (str "&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;" "Hello world" "&amp;lt;/html&amp;gt;&amp;lt;body&amp;gt;"))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;Since our &lt;code&gt;hello&lt;/code&gt; function is in the the "myapp.web" namespace, it should be contained in a file named myapp/web.clj, that is on the application's CLASSPATH. In our case, this means in a directory beneath the WEB-INF/classes of the WAR file.&lt;br /&gt;&lt;br /&gt;Accordingly, our &lt;code&gt;web.xml&lt;/code&gt; file should look like:&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;&amp;lt;web-app version="2.4"&lt;br /&gt;      xmlns="http://java.sun.com/xml/ns/j2ee"&lt;br /&gt;      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&lt;br /&gt;      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&amp;gt;&lt;br /&gt;&lt;br /&gt;   &amp;lt;context-param&amp;gt;&lt;br /&gt;      &amp;lt;param-name&amp;gt;evalOnContextInitialized&amp;lt;/param-name&amp;gt;&lt;br /&gt;      &amp;lt;param-value&amp;gt;myapp/web.clj&amp;lt;/param-value&amp;gt;&lt;br /&gt;   &amp;lt;/context-param&amp;gt;&lt;br /&gt;&lt;br /&gt;   &amp;lt;listener&amp;gt;&lt;br /&gt;      &amp;lt;listener-class&amp;gt;myapp.servlet.ClojureContextListener&amp;lt;/listener-class&amp;gt;&lt;br /&gt;   &amp;lt;/listener&amp;gt;&lt;br /&gt;&lt;br /&gt;   &amp;lt;servlet&amp;gt;&lt;br /&gt;      &amp;lt;servlet-name&amp;gt;ClojureServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;      &amp;lt;servlet-class&amp;gt;myapp.servlet.ClojureServlet&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;      &amp;lt;init-param&amp;gt;&lt;br /&gt;         &amp;lt;param-name&amp;gt;entryPoint&amp;lt;/param-name&amp;gt;&lt;br /&gt;         &amp;lt;param-value&amp;gt;myapp.web/hello&amp;lt;/param-value&amp;gt;&lt;br /&gt;      &amp;lt;/init-param&amp;gt;&lt;br /&gt;   &amp;lt;/servlet&amp;gt;&lt;br /&gt;&lt;br /&gt;   &amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;      &amp;lt;servlet-name&amp;gt;ClojureServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;      &amp;lt;url-pattern&amp;gt;/hello&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;   &amp;lt;/servlet-mapping&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/web-app&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;And the resulting web application will be laid out as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px dashed rgb(153, 153, 153); padding: 5px; overflow: auto; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; line-height: 14px; width: 100%;"&gt;&lt;code&gt;myapp.war/&lt;br /&gt;  WEB-INF/&lt;br /&gt;     web.xml&lt;br /&gt;     classes/&lt;br /&gt;        myapp/&lt;br /&gt;           web.clj&lt;br /&gt;           servlet/&lt;br /&gt;              ClojureServlet.class&lt;br /&gt;              ClojureContextListener.class&lt;br /&gt;     lib/&lt;br /&gt;        clojure-1.0.0.jar&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;Note that the single clojure-1.0.0.jar file is the only required dependency. That's all there is to it!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26314880-1909842676891676700?l=sean8223.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sean8223.blogspot.com/feeds/1909842676891676700/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26314880&amp;postID=1909842676891676700' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26314880/posts/default/1909842676891676700'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26314880/posts/default/1909842676891676700'/><link rel='alternate' type='text/html' href='http://sean8223.blogspot.com/2009/06/because-clojure-has-such-tight.html' title=''/><author><name>Sean Wellington</name><uri>http://www.blogger.com/profile/02056362838637172606</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-26314880.post-9104550936598147680</id><published>2009-05-29T15:12:00.000-07:00</published><updated>2009-06-01T09:08:39.819-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='Clojure'/><title type='text'></title><content type='html'>Clojure is a new dynamic programming language for the JVM. It is a dialect of Lisp that provides:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Well defined concurrency semantics for all language features. Clojure requires a functional programming style, and as such variables are single assignment and most data is immutable. For mutable data, Clojure supplies a software transaction memory system.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Total integration with the JVM. Clojure data elements are instances of &lt;code&gt;java.lang.Number&lt;/code&gt;, &lt;code&gt;java.lang.String&lt;/code&gt;, etc. There is no bridging of parallel type systems (for example, from a canonical C language implementation). Clojure code compiles directly to Java bytecode and can be profiled and debugged using standard tools. Access to Java code/libraries from Clojure is totally seamless.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Collection types as first-class language features. There is syntax for dealing with sets, maps and vectors, and their implementations are have the same immutablility and concurrency semantics as the other data types.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Macros. Coming from a Java background this is perhaps &lt;span style="font-style: italic;"&gt;the &lt;/span&gt;coolest feature of Clojure.&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;There are several excellent introductions to the language. The "Clojure for Java Programmers" (&lt;a href="http://blip.tv/file/982823"&gt;first part&lt;/a&gt;, &lt;a href="http://blip.tv/file/982957"&gt;second part&lt;/a&gt;), assumes no familiarity with Lisp and is a decent introduction to the language. The version of the lecture for experienced Lisp programmers (&lt;a href="http://blip.tv/file/1313398"&gt;first part&lt;/a&gt;, &lt;a href="http://blip.tv/file/1313503"&gt;second part&lt;/a&gt;) goes into the language in much greater detail and is highly recommended. Once you have sat through the "non-Lisp" version you will have enough background to comprend the "Lisp" version.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/26314880-9104550936598147680?l=sean8223.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sean8223.blogspot.com/feeds/9104550936598147680/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=26314880&amp;postID=9104550936598147680' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/26314880/posts/default/9104550936598147680'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/26314880/posts/default/9104550936598147680'/><link rel='alternate' type='text/html' href='http://sean8223.blogspot.com/2009/05/clojure-is-new-dynamic-programming.html' title=''/><author><name>Sean Wellington</name><uri>http://www.blogger.com/profile/02056362838637172606</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
