forked from laurentpetit/ring-java-servlet
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating a ServletContextListener that will run the functions specifi…
…ed in the context parameters context-init and context-destroy when the application is initialized or stopped. Both parameters are optional. Also included a small utility function to convert context params to a map. The project now needs lein 2.0.0-preview10 to make use of the "provided" dependencies.
- Loading branch information
Showing
5 changed files
with
80 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
(defproject org.lpetit.ring/ring-java-servlet "0.1.0" | ||
(defproject org.lpetit.ring/ring-java-servlet "0.2.0" | ||
:description "Ring generic servlet for using ring without AOT." | ||
:url "http://github.com/laurentpetit/ring-java-servlet" | ||
:dependencies [[ring/ring-servlet "0.3.5" :exclusions [javax.servlet/servlet-api]]] | ||
:dev-dependencies [[javax.servlet/servlet-api "2.5"]] | ||
:aot [org.lpetit.ring.servlet.RingHttpServlet]) | ||
:dependencies [[ring/ring-servlet "1.1.0" :exclusions [javax.servlet/servlet-api]]] | ||
:profiles {:provided | ||
{:dependencies | ||
[[javax.servlet/servlet-api "2.5"]]}} | ||
:aot [org.lpetit.ring.servlet.RingHttpServlet org.lpetit.ring.servlet.RingServletContextListener]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/org/lpetit/ring/servlet/RingServletContextListener.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
(ns org.lpetit.ring.servlet.RingServletContextListener | ||
(:use org.lpetit.ring.servlet.util) | ||
(:gen-class :implements [javax.servlet.ServletContextListener]) | ||
(:import [javax.servlet ServletContextEvent])) | ||
|
||
(defn run [contextEvent symbol-str] | ||
(let [ctx (.getServletContext contextEvent)] | ||
(if-let [the-fn (.getInitParameter ctx symbol-str)] | ||
(let [resolved-fn (require-and-resolve the-fn)] | ||
(resolved-fn ctx))))) | ||
|
||
(defn -contextInitialized [this ^ServletContextEvent contextEvent] | ||
(run contextEvent "context-init")) | ||
|
||
(defn -contextDestroyed [this ^ServletContextEvent contextEvent] | ||
(run contextEvent "context-destroy")) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
(ns org.lpetit.ring.servlet.util | ||
(:import [javax.servlet ServletContext])) | ||
|
||
(defn require-and-resolve [symbol-str] | ||
(let [[n h] (map symbol ((juxt namespace name) (symbol symbol-str)))] | ||
(require n) | ||
(ns-resolve (the-ns n) h))) | ||
|
||
(defn context-params [^ServletContext ctx] | ||
"Given a servlet context, it returns a map with all the context parameters. Keys are keywords." | ||
(into {} (for [param (enumeration-seq (.getInitParameterNames ctx)) | ||
:let [value (.getInitParameter ctx param)]] | ||
[(keyword param) value]))) |