Java – the starting point for converting regular servlet encoding to my DSL

Clojure provides a good java interoperability But I really want to have this:

(servlet IndexServlet
  (service[parmas] ....)
  (do-post[params] ....)
  (do-get [params] ....))

(servlet-filter SecurityFilter
  (do-filter [params] ....))

I guess this is called DSL. In the LISP world, it is done through macros

I'm not sure how / where to start The refiy and extends forms certainly play an important role here, but I don't know how they fit into macros

How to start this DSL? I really appreciate a clip, tips and techniques

Solution

You may need to look at ring's jetty adapter for an example of a servlet implementation in clojure The source can be used here (link to the source of version 1.1) In particular, the first function defined in this namespace, proxy handler, returns a handler according to the abstract class provided by jetty

If you choose to implement a similar method (provide some off the shelf methods impls based on your servlet Java classes), you will need to use a proxy; If you only need to implement interfaces (no subclasses), you may need to improve The usefulness of macros depends on which parts of the implementation will be repaired; Ring's jetty adapter will not benefit from the use of macros, but you can (for example, if you want to implement class extensions / interfaces as parameters, as the problem seems to indicate)

In any case, any function you choose to implement needs to be part of the interface or protocol Therefore, implement javax servlet. Servlets and other operations foo may be as follows:

(import (javax.servlet Servlet ServletRequest ServletResponse))

(defprotocol PFoo
  (foo [this x y z]))

(reify
  Servlet
  (service [this ^ServletRequest req ^ServletResponse res]
    ...)
  ;; other Servlet methods here...
  PFoo
  (foo [this x y z]
    ...))

You can then wrap it in macros to provide any required syntax sugar Note that reify doesn't really care how interface / protocol names and method definitions are exchanged in its body, so you can let macros emit

(reify
  Servlet PFoo ... ; other interfaces & protocols
  (service [...] ...)
  (foo [...] ...)
  ;; other methods
  )

If that's more convenient

A sketch of a macro, implemented with the name of the servlet interface (possibly extending javax. Servlet. Servlet), and using some additional methods to inject the protocol:

(defprotocol PFancyServlet
  (do-get [this ...])
  (do-post [this ...]))

(defmacro servlet [servlet-iface & meths]
   `(reify ~servlet-iface PFancyServlet
      ~@meths))

Methods need to include do get, do post and servlet Iface methods; You can add some parameter validation to ensure this An example phone:

(servlet SomeServletInterface
  (service [this ...] ...)
  ;; ...
  (do-get [this ...] ...)
  (do-post [this ...] ...))
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>