Skip to content

Commit 365956f

Browse files
committed
More repl cleanup + ofbiz run serivce + bb tasks
1 parent 2de8bad commit 365956f

File tree

6 files changed

+271
-152
lines changed

6 files changed

+271
-152
lines changed

README.adoc

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
= Clojure repl component for OFBiz
2+
3+
Uses [nrepl](https://nrepl.org/nrepl/index.html) library to start an instance of nRepl inside Apache OFBiz.
4+
Once we have a Repl inside OFBiz we can interact with OFBiz remotely.
5+
It's basically a RPC (Remote Procedure Call) system with a programming language.
6+
7+
Things you can do once connected to OFBiz repl:
8+
9+
* More or less anything you could do via Java/Groovy code loaded in OFBiz
10+
** Interact with a running OFBiz instance from a shell like environment (Repl is more powerfull then the shell)
11+
* List services
12+
* Call services
13+
* Build an arsenal of admin scripts and manage OFBiz instance via repl like a boss via babashka
14+
15+
16+
More advanced things:
17+
* Prototype functionality and experiment with instant feedback - search for Repl driven development videos
18+
* Load new servicess dinamically ?!
19+
* Develop services for OFBiz in clojure and leverage all it's wonderfull properties
20+
* Use data visualization tools like [Proto-repl](https://github.com/jasongilman/proto-repl) to explore and do interactive Business Inteligence.
21+
* Design reports using real data using something like [Gorilla-repl](http://gorilla-repl.org/)
22+
23+
Watch it on youtube https://youtu.be/XXxJDZRzu_E["Video of OFBiz Clojure repl"]
24+
25+
I believe the OFBiz development experience can look like this:
26+
27+
* https://www.youtube.com/watch?v=ZkJX9ciI5aM&t=1824s
28+
* https://www.youtube.com/watch?v=Bs44qdAX5yo
29+
30+
== We can help you
31+
32+
I am availble for contracting work via my company.
33+
I would love to work more with OFBiz in general and with Clojure on OFBiz in particular.
34+
If you do like this work plese let me know [[email protected]](mailto:[email protected]) .
35+
36+
== How does it work?
37+
38+
The plugin exposes services that allow a user to start/stop an instance of nRepl inside OFBiz application.
39+
40+
You can find the plugin sources at [netdava/ofbiz-clojure-repl](https://github.com/netdava/ofbiz-clojure-repl) .
41+
42+
== Using this plugin
43+
44+
=== Usage from Web UI
45+
46+
Clone Apache OFBiz trunk project as described in the online (https://OFBiz.apache.org/developers.html)[OFBiz Getting started] page.
47+
48+
After that you need to clone this project inside `plugins` directory of OFBiz.
49+
50+
Once that is done you can start the project.
51+
52+
IMPORTANT: You will need to add clojars to OFBiz build.gradle. See https://issues.apache.org/jira/browse/OFBIZ-12263 and https://github.com/apache/ofbiz-framework/pull/304/files .
53+
54+
[source,bash]
55+
----
56+
# Build the project - download all dependencies
57+
./gradlew build
58+
# Prepare OFBiz for start - load data
59+
./gradlew cleanAll loadAll
60+
# Run OFBiz. You can look for the plugin in the logs.
61+
./gradlew ofbiz
62+
# Open the link in the browser and start clojure repl manually.
63+
xdg-open https://localhost:8443/accounting
64+
----
65+
66+
The plugin comes with a container implementation that will start the repl out of the box.
67+
68+
WARNING: Do not expose the repl port outside of localhost since it has no security.
69+
Anyone with repl access will be able to do *ANYTHING* to your OFBiz instance, including removing all your data.
70+
71+
72+
.OFBiz-clojure-repl container auto-starting logs
73+
[source,log]
74+
----
75+
2021-06-27 13:27:31,101 |OFBiz-JobQueue-0 |ClojureREPLService |I| nRepl is not running
76+
2021-06-27 13:27:31,101 |OFBiz-JobQueue-1 |ClojureREPLService |I| Starting nRrepl on port 7888
77+
2021-06-27 13:27:31,103 |OFBiz-JobQueue-0 |ServiceDispatcher |T| Sync service [default/stopRepl] finished in [3] milliseconds
78+
2021-06-27 13:27:34,230 |OFBiz-JobQueue-1 |ClojureREPLService |I| nREPL server started on port 7888 on host 127.0.0.1 - nrepl://127.0.0.1:7888
79+
----
80+
81+
== Connecting to the repl and using it
82+
83+
Once you have a running repl you can use it.
84+
85+
There are two main ways:
86+
* Connect to repl from a babashka script - this is very good for DevOps management
87+
* Connect to repl from IDE (Calva, Spacemacs,etc) - good for developing, prototyping.
88+
89+
=== Usage from babashka
90+
91+
Babashka is a "Fast native Clojure scripting runtime" (bash gone clojure).
92+
Call clojure scripts with miliseconds startup time.
93+
94+
There is support for link:https://babashka.org/[babashka] scripts - see `bb.edn` file and call `bb tasks` to see a list of tasks.
95+
96+
.Calling `bb tasks` gets
97+
[source,bash]
98+
----
99+
The following tasks are available:
100+
101+
list-services List ofbiz services
102+
get-model-service Get the ModelService of a service
103+
run-service Run OFBiz service
104+
----
105+
106+
From the example scripts you can expand and build your own.
107+
108+
=== Usage from Calva / Clojure IDE
109+
110+
Just open your favorite Clojure IDE and connect to the repl, then explore OFBiz.
111+
You can use IntelliJ + Cursive or Visual Studio Code with Calva or whatever you wish.
112+
113+
Load the code bellow and call some functions.
114+
115+
.How to use plugin from Clojure
116+
[source,clojure]
117+
----
118+
(ns ofbiz-ops
119+
(:require [clojure.string :as str])
120+
(:import [com.netdava.ofbiz.clojure ClojureREPLService]
121+
[org.apache.ofbiz.service DispatchContext]))
122+
123+
(def ^DispatchContext dispatch-ctx (.get ClojureREPLService/CONTEXT "ofbiz-dispatch-context"))
124+
125+
(defn list-services
126+
"List ofbiz services.
127+
Can be supplied with a filter function to filter services.
128+
129+
Example:
130+
;; filter services that contain 'repl' in their name
131+
(ofbiz-ops/list-services (fn [s] (clojure.string/includes? (.toLowerCase s) \"repl\")))"
132+
([]
133+
(.getAllServiceNames dispatch-ctx))
134+
([filter-fn]
135+
(filter filter-fn (list-services))))
136+
137+
(defn get-model-service
138+
"Gets the ModelService instance that corresponds to given the name.
139+
140+
Example:
141+
(ofbiz-ops/get-model-service \"startRepl\" )"
142+
143+
([service]
144+
(-> dispatch-ctx
145+
(.getModelService service))))
146+
147+
(defn run-sync
148+
"Calls OFBiz service in sync mode."
149+
[service ctx]
150+
(-> dispatch-ctx
151+
(.getDispatcher)
152+
(.runSync service ctx)))
153+
----

README.md

Lines changed: 0 additions & 110 deletions
This file was deleted.

bb.edn

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@
1919
(binding [*out* *err*]
2020
(println args))
2121
(apply shell args))
22-
(def ofbiz-ops-opts [["-p" "--port PORT" "App repl port."
23-
:default 43851
24-
:parse-fn (fn [p] (Integer/parseInt p))
25-
:validate [(fn [p] (< 0 p 0x10000)) "Must be a number between 0 and 65536"]]
26-
["-H" "--host HOST" "App host."
27-
:default "dev8.int.drevidence.com"]
28-
["-h" "--help" "Print help information."]])
2922
(defn custom-parse-opts
3023
[cli-args cli-opts]
3124
(let [parsed-opts (parse-opts cli-args cli-opts)
@@ -41,14 +34,17 @@
4134
(println "Allowed options are:\n" summary "\n")
4235
(System/exit 0))
4336
parsed-opts)))
44-
ofbiz {:doc "Print hello ofbiz"
45-
:task (let [parsed-opts (custom-parse-opts *command-line-args* ofbiz-ops-opts)
46-
{{:keys [env]} :options} parsed-opts
47-
opts {:out (str env "/app-secrets.json")
48-
:extra-env {"VAULT_ADDR" vault-addr}}]
49-
(println "salut"))}
50-
test {:doc "Test"
51-
:task (let [services-str (repl/ofbiz-run "localhost" 7888 "(list-services)")
52-
q (deref repl/queue)
53-
res (repl/filter-values q)]
54-
(println (:value res)))}}}
37+
list-services {:doc "List ofbiz services"
38+
:task (let [q (repl/ofbiz
39+
"localhost" 7888
40+
(repl/string-it (ofbiz-ops/list-services (fn [s] (clojure.string/includes? (.toLowerCase s) "test"))))
41+
999)]
42+
(println (repl/first-response-value q 999)))}
43+
get-model-service {:doc "Get the ModelService of a service"
44+
:task (let [expr-str (repl/string-it (ofbiz-ops/get-model-service "simpleMapListTest"))
45+
q (repl/ofbiz "localhost" 7888 expr-str 999)]
46+
(println (repl/first-response-value q 999)))}
47+
run-service {:doc "Run OFBiz service"
48+
:task (let [expr-str (repl/string-it (ofbiz-ops/run-sync "testScv" {"defaultValue" 123.45 "message" "Hello OFBiz from Clojure REPL"}))
49+
q (repl/ofbiz "localhost" 7888 expr-str 999)]
50+
(println (repl/first-response-value q 999)))}}}

0 commit comments

Comments
 (0)