forked from clojure/java.classpath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasspath.clj
More file actions
76 lines (65 loc) · 2.48 KB
/
Copy pathclasspath.clj
File metadata and controls
76 lines (65 loc) · 2.48 KB
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
;;; classpath.clj: utilities for working with the Java class path
;; by Stuart Sierra, http://stuartsierra.com/
;; April 19, 2009
;; Copyright (c) Rich Hickey, Stuart Sierra, and contributors.
;; All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.
(ns
^{:author "Stuart Sierra"
:doc "Utilities for dealing with the JVM's classpath"}
clojure.java.classpath
(:require [clojure.java.io :as io])
(:import (java.io File)
(java.util.jar JarFile JarEntry)
(java.net URL URLClassLoader)))
(defn jar-file?
"Returns true if file is a normal file with a .jar or .JAR extension."
[f]
(let [file (io/file f)]
(and (.isFile file)
(or (.endsWith (.getName file) ".jar")
(.endsWith (.getName file) ".JAR")))))
(defn filenames-in-jar
"Returns a sequence of Strings naming the non-directory entries in
the JAR file."
[^JarFile jar-file]
(map #(.getName ^JarEntry %)
(filter #(not (.isDirectory ^JarEntry %))
(enumeration-seq (.entries jar-file)))))
(defn system-classpath
"Returns a sequence of File paths from the 'java.class.path' system
property."
[]
(map #(File. ^String %)
(.split (System/getProperty "java.class.path")
(System/getProperty "path.separator"))))
(defn loader-classpath
"Returns a sequence of File paths from a classloader."
[loader]
(when (instance? java.net.URLClassLoader loader)
(map
#(java.io.File. (.getPath ^java.net.URL %))
(.getURLs ^java.net.URLClassLoader loader))))
(defn classpath
"Returns a sequence of File objects of the elements on the classpath."
([classloader]
(distinct
(mapcat
loader-classpath
(take-while
identity
(iterate #(.getParent ^ClassLoader %) classloader)))))
([] (classpath (clojure.lang.RT/baseLoader))))
(defn classpath-directories
"Returns a sequence of File objects for the directories on classpath."
[]
(filter #(.isDirectory ^File %) (classpath)))
(defn classpath-jarfiles
"Returns a sequence of JarFile objects for the JAR files on classpath."
[]
(map #(JarFile. ^File %) (filter jar-file? (classpath))))