-
Notifications
You must be signed in to change notification settings - Fork 100
/
war.clj
216 lines (187 loc) · 7.59 KB
/
war.clj
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(ns leiningen.ring.war
(:require [leiningen.compile :as compile]
[clojure.java.io :as io]
[clojure.string :as string]
[leinjacker.deps :as deps])
(:use [clojure.data.xml :only [sexp-as-element indent-str]]
leiningen.ring.util)
(:import [java.util.jar Manifest
JarEntry
JarOutputStream]
[java.io BufferedOutputStream
FileOutputStream
ByteArrayInputStream]))
(defn default-war-name [project]
(or (get-in project [:ring :war-name])
(str (:name project) "-" (:version project) ".war")))
(defn war-file-path [project war-name]
(let [target-dir (or (:target-dir project) (:target-path project))]
(.mkdirs (io/file target-dir))
(str target-dir "/" war-name)))
(defn skip-file? [project war-path file]
(or (re-find #"^\.?#" (.getName file))
(re-find #"~$" (.getName file))
(some #(re-find % war-path)
(get-in project [:ring :war-exclusions] [#"(^|/)\."]))))
(defn- to-byte-stream [^String s]
(ByteArrayInputStream. (.getBytes s)))
(def default-ring-manifest
{"Created-By" "Leiningen Ring Plugin"
"Built-By" (System/getProperty "user.name")
"Build-Jdk" (System/getProperty "java.version")})
(defn make-manifest [user-manifest]
(Manifest.
(to-byte-stream
(reduce
(fn [accumulated-manifest [k v]]
(str accumulated-manifest "\n" k ": " v))
"Manifest-Version: 1.0"
(merge default-ring-manifest user-manifest)))))
(defn default-servlet-class [project]
(let [handler-sym (get-in project [:ring :handler])
ns-parts (-> (namespace handler-sym)
(string/replace "-" "_")
(string/split #"\.")
(butlast)
(vec)
(conj "servlet"))]
(string/join "." ns-parts)))
(defn servlet-class [project]
(or (get-in project [:ring :servlet-class])
(default-servlet-class project)))
(defn servlet-ns [project]
(-> (servlet-class project)
(string/replace "_" "-")))
(defn servlet-name [project]
(or (get-in project [:ring :servlet-name])
(str (get-in project [:ring :handler])
" servlet")))
(defn has-listener? [project]
(let [ring-options (:ring project)]
(or (contains? ring-options :init)
(contains? ring-options :destroy))))
(defn default-listener-class [project]
(let [listener-sym (or (get-in project [:ring :init])
(get-in project [:ring :destroy]))
ns-parts (-> (namespace listener-sym)
(string/replace "-" "_")
(string/split #"\.")
(butlast)
(vec)
(conj "listener"))]
(string/join "." ns-parts)))
(defn listener-class [project]
(or (get-in project [:ring :listener-class])
(default-listener-class project)))
(defn listener-ns [project]
(-> (listener-class project)
(string/replace "_" "-")))
(defn url-pattern [project]
(or (get-in project [:ring :url-pattern])
"/*"))
(defn make-web-xml [project]
(let [ring-options (:ring project)]
(if (contains? ring-options :web-xml)
(slurp (:web-xml ring-options))
(indent-str
(sexp-as-element
[:web-app
(if (has-listener? project)
[:listener
[:listener-class (listener-class project)]])
[:servlet
[:servlet-name (servlet-name project)]
[:servlet-class (servlet-class project)]]
[:servlet-mapping
[:servlet-name (servlet-name project)]
[:url-pattern (url-pattern project)]]])))))
(defn generate-handler [project handler-sym]
(if (get-in project [:ring :servlet-path-info?] true)
`(fn [request#]
(~handler-sym
(assoc request#
:path-info (.getPathInfo (:servlet-request request#))
:context (.getContextPath (:servlet-request request#)))))
handler-sym))
(defn compile-servlet [project]
(let [handler-sym (get-in project [:ring :handler])
handler-ns (symbol (namespace handler-sym))
servlet-ns (symbol (servlet-ns project))]
(compile-form project servlet-ns
`(do (ns ~servlet-ns
(:require ring.util.servlet ~handler-ns)
(:gen-class :extends javax.servlet.http.HttpServlet))
(ring.util.servlet/defservice
~(generate-handler project handler-sym))))))
(defn compile-listener [project]
(let [init-sym (get-in project [:ring :init])
destroy-sym (get-in project [:ring :destroy])
init-ns (and init-sym (symbol (namespace init-sym)))
destroy-ns (and destroy-sym (symbol (namespace destroy-sym)))
project-ns (symbol (listener-ns project))]
(compile-form project project-ns
`(do (ns ~project-ns
(:require ~@(set (remove nil? [init-ns destroy-ns])))
(:gen-class :implements [javax.servlet.ServletContextListener]))
~(let [servlet-context-event (gensym)]
`(do
(defn ~'-contextInitialized [this# ~servlet-context-event]
~(if init-sym
`(~init-sym)))
(defn ~'-contextDestroyed [this# ~servlet-context-event]
~(if destroy-sym
`(~destroy-sym)))))))))
(defn create-war [project file-path]
(-> (FileOutputStream. file-path)
(BufferedOutputStream.)
(JarOutputStream. (make-manifest (:manifest project)))))
(defn write-entry [war war-path entry]
(.putNextEntry war (JarEntry. war-path))
(io/copy entry war))
(defn str-entry [war war-path content]
(write-entry war war-path (to-byte-stream content)))
(defn in-war-path [war-path root file]
(str war-path
(-> (.toURI (io/file root))
(.relativize (.toURI file))
(.getPath))))
(defn file-entry [war project war-path file]
(when (and (.exists file)
(.isFile file)
(not (skip-file? project war-path file)))
(write-entry war war-path file)))
(defn dir-entry [war project war-root dir-path]
(doseq [file (file-seq (io/file dir-path))]
(let [war-path (in-war-path war-root dir-path file)]
(file-entry war project war-path file))))
(defn war-resources-path [project]
(:war-resources-path project "war-resources"))
(defn write-war [project war-path]
(with-open [war-stream (create-war project war-path)]
(doto war-stream
(str-entry "WEB-INF/web.xml" (make-web-xml project))
(dir-entry project "WEB-INF/classes/" (:compile-path project)))
(doseq [path (distinct (concat [(:source-path project)] (:source-paths project)
[(:resources-path project)] (:resource-paths project)))
:when path]
(dir-entry war-stream project "WEB-INF/classes/" path))
(dir-entry war-stream project "" (war-resources-path project))
war-stream))
(defn add-servlet-dep [project]
(deps/add-if-missing project '[ring/ring-servlet "1.1.6"]))
(defn war
"Create a $PROJECT-$VERSION.war file."
([project]
(war project (default-war-name project)))
([project war-name]
(ensure-handler-set! project)
(let [project (add-servlet-dep project)
result (compile/compile project)]
(when-not (and (number? result) (pos? result))
(let [war-path (war-file-path project war-name)]
(compile-servlet project)
(if (has-listener? project)
(compile-listener project))
(write-war project war-path)
(println "Created" war-path)
war-path)))))