Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Clojure REPL from java, providing custom context

Tags:

clojure

How can I start a Clojure REPL from my java application and provide some "predefined variables" (I´m a Clojure newbie, I guess there´s a better term for that)? In fact I already tried to make it work by coincidence... I started with clojure.main and added an additional call to RT.var(). Is this the correct way to do it?

import clojure.lang.Symbol;
import clojure.lang.Var;
import clojure.lang.RT;

public class MyClojure {

    final static private Symbol CLOJURE_MAIN = Symbol.intern("clojure.main");
    final static private Var REQUIRE = RT.var("clojure.core", "require");
    final static private Var MAIN = RT.var("clojure.main", "main");

    // Provide some context, this is my custimisation...
    @SuppressWarnings("unused")
    final static private Var Test = RT.var("testme", "myvar", "This is the initial value");

    public static void main(String[] args) throws Exception {
         REQUIRE.invoke(CLOJURE_MAIN);
         MAIN.applyTo(RT.seq(args));
    }
}

EDIT: My application is not a web application. It´s a standalone desktop application that basically displays/edits a pojo data model. I now want to add support to expose the data model from the running application to a clojure repl. I would then be able to modify/inspect the data model thrugh both, the original application and the repl.

like image 859
zedoo Avatar asked Feb 04 '12 17:02

zedoo


1 Answers

you're looking for the clojure.contrib/server-socket's create-repl-server function:

here is an excerpt from the examples:

(use '[clojure.contrib.server-socket :only [create-repl-server]])
(gen-class
    :name "org.project.ReplServer"
    :implements [javax.servlet.ServletContextListener])
(defn -contextInitialized [this e]
    (.start (Thread. (partial create-repl-server 11111))))

there are also a lot of ideas on this SO question

like image 91
Arthur Ulfeldt Avatar answered Oct 01 '22 23:10

Arthur Ulfeldt