Commits

taka2ru committed 348a506

changed interface for #2 ,
but we cannot use method overriding as Axis2 does not support that feature.

Comments (0)

Files changed (2)

src/clj_soap/core.clj

 
 ;;; Defining SOAP Server
 
+(defn flatten1 [coll] (mapcat identity coll))
+
+(defn gen-class-method-decls [method-defs]
+  (flatten1
+    (letfn [(gen-decl [method-name arglist body]
+              [method-name
+               (vec (for [arg arglist] (or (:tag (meta arg)) String)))
+               (or (:tag (meta arglist)) 'void)])]
+      (for [method-def method-defs]
+        (cond
+          (vector? (second method-def))
+          (list (let [[method-name arglist & body] method-def]
+                  (gen-decl method-name arglist body)))
+          (seq? (second method-def))
+          (let [[method-name & deflist] method-def]
+            (for [[arglist & body] deflist]
+              (gen-decl method-name arglist body))))))))
+
+(defn gen-method-defs [prefix method-defs]
+  (flatten1
+    (for [method-def method-defs]
+      (cond
+        (vector? (second method-def))
+        (list (let [[method-name arglist & body] method-def]
+                `(defn ~(symbol (str prefix method-name))
+                   ~(vec (cons 'this arglist)) ~@body)))
+        (seq? (second method-def))
+        (let [[method-name & deflist] method-def]
+          (cons
+            `(defmulti ~(symbol (str prefix method-name))
+               (fn [~'this & args#] (vec (map class args#))))
+            (for [[arglist & body] deflist]
+              `(defmethod ~(symbol (str prefix method-name))
+                 ~(vec (map #(:tag (meta %)) arglist))
+                 ~(vec (cons 'this arglist)) ~@body))))))))
+
+
 (defmacro defservice
   "Define SOAP class.
   i.e. (defsoap some.package.KlassName (myfunc [String a int b] String (str a (* b b))))"
        (gen-class
          :name ~class-name
          :prefix ~prefix
-         :methods ~(vec (for [method-def method-defs]
-                          (let [[method-name arglist & _] method-def]
-                            [method-name
-                             (vec (for [arg arglist]
-                                    (or (:tag (meta arg)) String)))
-                             (or (:tag (meta method-def)) 'void)]))))
-       ~@(for [[method-name arglist & body] method-defs]
-           `(defn ~(symbol (str prefix method-name))
-              ~(vec (cons 'this arglist))
-              ~@body)))))
+         :methods ~(vec (gen-class-method-decls method-defs)))
+       ~@(gen-method-defs prefix method-defs))))
 
 (defn serve
   "Start SOAP server.

test/clj_soap/test/core.clj

 
 (defservice jp.myclass.MyApp
   (changeval [^String string] (dosync (ref-set test-value string)))
-  ^Double (hypotenuse [^Double x ^Double y] (Math/sqrt (+ (* x x) (* y y)))))
+  (hypotenuse ^Double [^Double x ^Double y] (Math/sqrt (+ (* x x) (* y y))))
+  (doubl1 (^String [^String x] (str x x))
+          (^Double [^Double x] (+ x x)))
+  (doubl2 (^Double [^Double x] (+ x x))
+          (^String [^String x] (str x x))))
 
 (deftest test-my-app
   (serve "jp.myclass.MyApp")
   (let [cl (client-fn "http://localhost:6060/axis2/services/MyApp?wsdl")]
     (is (= 5.0 (cl :hypotenuse 3 4)) "SOAP call with return value")
     (cl :changeval "piyopiyo")
-    (is (= "piyopiyo" @test-value) "SOAP call without return value")))
+    (is (= "piyopiyo" @test-value) "SOAP call without return value")
+    ; Axis2 does not support method overloading.
+    ;(is (= 10.0 (cl :doubl1 5.0)))
+    ;(is (= "abcabc" (cl :doubl1 "abc")))
+    ;(is (= 10.0 (cl :doubl2 5.0)))
+    ;(is (= "abcabc" (cl :doubl2 "abc")))
+    ))