|
31 | 31 |
|
32 | 32 | (defn url-protocol
|
33 | 33 | "Get the URL protocol as a string, e.g. http, file, jar."
|
| 34 | + ^String |
34 | 35 | [^java.net.URL url]
|
35 | 36 | (.getProtocol url))
|
36 | 37 |
|
| 38 | +(def jar? #{"jar"}) |
| 39 | + |
| 40 | +(def file? #{"file"}) |
| 41 | + |
37 | 42 | (defn url-to-file-within-archive?
|
38 |
| - "Does this URL point to a file inside a jar (or zip) archive. |
39 |
| - i.e. does it use the jar: protocol." |
| 43 | + "Does this URL point to a file inside a jar (or zip) archive? |
| 44 | + i.e., does it use the jar: protocol." |
| 45 | + [^java.net.URL url] |
| 46 | + (jar? (url-protocol url))) |
| 47 | + |
| 48 | +(defn direct-url-to-file? |
| 49 | + "Does this URL point to a file directly? |
| 50 | + i.e., does it use the file: protocol." |
40 | 51 | [^java.net.URL url]
|
41 |
| - (= "jar" (url-protocol url))) |
| 52 | + (file? (url-protocol url))) |
42 | 53 |
|
43 | 54 | (defn resource-jarfile
|
44 | 55 | "Given a jar:file:...!/... URL, return the location of the jar file on the
|
45 | 56 | filesystem. Returns nil on any other URL."
|
46 | 57 | ^File [^URL jar-resource]
|
47 |
| - (assert (= "jar" (url-protocol jar-resource))) |
| 58 | + (assert (jar? (url-protocol jar-resource))) |
48 | 59 | (let [^JarURLConnection conn (.openConnection jar-resource)
|
49 | 60 | inner-url (.getJarFileURL conn)]
|
50 |
| - (when (= "file" (url-protocol inner-url)) |
| 61 | + (when (file? (url-protocol inner-url)) |
51 | 62 | (io/as-file inner-url))))
|
52 | 63 |
|
53 | 64 | (defn resource-artifact
|
|
59 | 70 | Throws when the URL is not a `file:` or `jar:` URL."
|
60 | 71 | ^File [^java.net.URL resource]
|
61 | 72 | (let [protocol (url-protocol resource)]
|
62 |
| - (case protocol |
63 |
| - "file" |
| 73 | + (cond |
| 74 | + (file? protocol) |
64 | 75 | (io/as-file resource)
|
65 |
| - "jar" |
| 76 | + |
| 77 | + (jar? protocol) |
66 | 78 | (resource-jarfile resource)
|
67 |
| - #_else |
| 79 | + |
| 80 | + :else |
68 | 81 | (throw (ex-info (str "URLs with a " protocol
|
69 | 82 | " protocol can't be situated on the filesystem.")
|
70 | 83 | {:resource resource})))))
|
|
0 commit comments