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
73 lines (60 loc) · 2.37 KB
/
Copy pathclasspath.clj
File metadata and controls
73 lines (60 loc) · 2.37 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
;;; 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 classloader-paths
"Returns a sequence of File paths for a URLClassLoader."
[loader]
(when (instance? URLClassLoader loader)
(map #(File. (.getPath ^URL %)) (.getURLs ^URLClassLoader loader))))
(defn system-classpath
"Returns a sequence of File paths from the 'java.class.path' system
property."
[]
(classloader-paths (.getClassLoader clojure.lang.RT)))
(defn clojure-classpath
"Returns a sequence of File paths from Clojure's base classloader."
[]
(classloader-paths (clojure.lang.RT/baseLoader)))
(defn classpath
"Returns a sequence of File objects of the elements on the
classpath."
[]
(distinct (concat (clojure-classpath) (system-classpath))))
(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))))