This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 206
/
operation.cljc
47 lines (41 loc) · 1.88 KB
/
operation.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(ns onyx.peer.operation
(:require [onyx.types :refer [->Link]]
[onyx.static.util :refer [kw->fn]]))
#?(:clj
(defn get-method-java [class-name method-name]
(let [ms (filter #(= (.getName ^Class %) method-name)
(.getMethods (Class/forName class-name)))]
(if (= 1 (count ms))
(first ms)
(throw (Exception. (format "Multiple methods found for %s/%s. Only one method may be defined." class-name method-name)))))))
#?(:clj
(defn build-fn-java
"Builds a clojure fn from a static java method.
Note, may be slower than it should be because of varargs, and the many
(unnecessary) calls to partial in apply-function above."
[kw]
(when (namespace kw)
(throw (ex-info "Namespaced keywords cannot be used for java static method fns. Use in form :java.lang.Math.sqrt" {:kw kw})))
(let [path (clojure.string/split (name kw) #"[.]")
class-name (clojure.string/join "." (butlast path))
method-name (last path)
method (get-method-java class-name method-name)]
(fn [& args]
(.invoke ^java.lang.reflect.Method method nil #^"[Ljava.lang.Object;" (into-array Object args))))))
(defn resolve-fn [task-map]
(kw->fn (:onyx/fn task-map)))
(defn start-lifecycle? [event]
true)
#?(:clj
(defn resolve-task-fn [entry]
(let [f (if (or (:onyx/fn entry)
(= (:onyx/type entry) :function))
(case (:onyx/language entry)
:java (build-fn-java (:onyx/fn entry))
(kw->fn (:onyx/fn entry))))]
(or f identity))))
#?(:clj
(defn instantiate-plugin-instance [class-name pipeline-data]
(.newInstance (.getDeclaredConstructor ^Class (Class/forName class-name)
(into-array Class [clojure.lang.IPersistentMap]))
(into-array [pipeline-data]))))