The first thing or the most important thing I wanted to learn in Clojure is the Java-Interop stuff. Since this would give me instant productive feeling aligning with my current line of work. Hippo Repository is built on top of Apache Jackrabbit and java clients can connect to the repository over RMI. I wanted to create a simple clojure program to connect to the running Hippo Repository instance and bask in the glow of REPL. So without much further ado here are the steps.
Step 0.
Install Emacs (this should be a no-brainer)
Step 1.
Install Leiningen.
Step 2. Create a new lein project
1 |
lein new hippo-clojure |
Step 3. Open project.clj and edit it to look like following:
1 2 3 4 5 6 7 8 9 10 11 12 |
(defproject hippo-clojure "1.0.0-SNAPSHOT" :description "clojure - with - hippocms" :dependencies [[org.clojure/clojure "1.2.1"] [org.clojure/clojure-contrib "1.2.0"] [javax.jcr/jcr "2.0"] [org.slf4j/slf4j-api "1.6.1"] [org.slf4j/slf4j-log4j12 "1.6.1"] [org.apache.jackrabbit/jackrabbit-jcr-rmi "2.1.2"] [org.onehippo.cms7/hippo-repository-api "2.20.00"] [org.onehippo.cms7/hippo-repository-builtin "2.20.00"]] :repositories {"hippo-maven2" "http://maven.onehippo.com/maven2/"} :jvm-opts ["-Djava.security.manager"]) |
We are using java.security.manager to enable the RMI Class loading security stuff.
Step 4. Hack the .java.policy to grant all the permissions. This file should be kept in your home directory.
WARNING!! Don’t do this on production system.
1 2 3 |
grant { permission java.security.AllPermission "", ""; }; |
Step 5. Open a terminal and change to the hippo-clojuredirectory and download the dependencies using the following command:
1 |
lein deps |
Step 6. Open a new terminal and switch to a directory where you have your Hippo CMS project. Take a look at Hippo Trail on how to create new project. In the Hippo CMS project directory run the following command. You may wan to do it in a new terminal process.
1 |
mvn -P cargo.run |
Step 7. Go back to the terminal where you have opened the hippo-clojure directory, and run the following command:
1 |
lein repl |
Step 8. Finally, connect to Hippo Repository in REPL using Clojure/Java interop
1 2 3 4 5 6 |
(def repository (org.apache.jackrabbit.rmi.repository.RMIRemoteRepository. "//localhost:1099/hipporepository")) (def credentials (javax.jcr.SimpleCredentials. "admin" (.toCharArray "admin"))) (def session (.login repository credentials)) (def some-node (.getNode session "/content/documents/somenode")) (.getString (.getProperty some-node "hippotranslation:locale")) |
Make sure you change the node name and property name to something that reflects your repository structure.
BTW, the methods which return an iterator (e.g. Item#getProperties) can be converted to a seq using iterator-seq function and treated like a regular seq in clojure. So start up and hack away!
Leave a Reply