Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 77 lines (65 sloc) 2.539 kb
739a74a @abedra Initial conversion. Combines c.c.classpath and c.c.jar
abedra authored
1 ;;; classpath.clj: utilities for working with the Java class path
2
3 ;; by Stuart Sierra, http://stuartsierra.com/
4 ;; April 19, 2009
5
8b02684 @stuarthalloway eliminate reflection, update copyright statement
stuarthalloway authored
6 ;; Copyright (c) Rich Hickey, Stuart Sierra, and contributors.
7 ;; All rights reserved. The use
739a74a @abedra Initial conversion. Combines c.c.classpath and c.c.jar
abedra authored
8 ;; and distribution terms for this software are covered by the Eclipse
9 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
10 ;; which can be found in the file epl-v10.html at the root of this
11 ;; distribution. By using this software in any fashion, you are
12 ;; agreeing to be bound by the terms of this license. You must not
13 ;; remove this notice, or any other, from this software.
14
15
f1b9bfc @hugoduncan Make classpath return classpath elements from all parent classloaders
hugoduncan authored
16 (ns
17 ^{:author "Stuart Sierra"
739a74a @abedra Initial conversion. Combines c.c.classpath and c.c.jar
abedra authored
18 :doc "Utilities for dealing with the JVM's classpath"}
19 clojure.java.classpath
8b02684 @stuarthalloway eliminate reflection, update copyright statement
stuarthalloway authored
20 (:require [clojure.java.io :as io])
739a74a @abedra Initial conversion. Combines c.c.classpath and c.c.jar
abedra authored
21 (:import (java.io File)
9e70cab @stuartsierra Use both java.class.path and Clojure's URLClassLoader
stuartsierra authored
22 (java.util.jar JarFile JarEntry)
23 (java.net URL URLClassLoader)))
8b02684 @stuarthalloway eliminate reflection, update copyright statement
stuarthalloway authored
24
739a74a @abedra Initial conversion. Combines c.c.classpath and c.c.jar
abedra authored
25 (defn jar-file?
26 "Returns true if file is a normal file with a .jar or .JAR extension."
8b02684 @stuarthalloway eliminate reflection, update copyright statement
stuarthalloway authored
27 [f]
28 (let [file (io/file f)]
29 (and (.isFile file)
30 (or (.endsWith (.getName file) ".jar")
31 (.endsWith (.getName file) ".JAR")))))
739a74a @abedra Initial conversion. Combines c.c.classpath and c.c.jar
abedra authored
32
33 (defn filenames-in-jar
34 "Returns a sequence of Strings naming the non-directory entries in
35 the JAR file."
36 [^JarFile jar-file]
8b02684 @stuarthalloway eliminate reflection, update copyright statement
stuarthalloway authored
37 (map #(.getName ^JarEntry %)
38 (filter #(not (.isDirectory ^JarEntry %))
739a74a @abedra Initial conversion. Combines c.c.classpath and c.c.jar
abedra authored
39 (enumeration-seq (.entries jar-file)))))
40
9e70cab @stuartsierra Use both java.class.path and Clojure's URLClassLoader
stuartsierra authored
41 (defn system-classpath
42 "Returns a sequence of File paths from the 'java.class.path' system
43 property."
739a74a @abedra Initial conversion. Combines c.c.classpath and c.c.jar
abedra authored
44 []
8b02684 @stuarthalloway eliminate reflection, update copyright statement
stuarthalloway authored
45 (map #(File. ^String %)
739a74a @abedra Initial conversion. Combines c.c.classpath and c.c.jar
abedra authored
46 (.split (System/getProperty "java.class.path")
47 (System/getProperty "path.separator"))))
48
f1b9bfc @hugoduncan Make classpath return classpath elements from all parent classloaders
hugoduncan authored
49 (defn loader-classpath
50 "Returns a sequence of File paths from a classloader."
51 [loader]
52 (when (instance? java.net.URLClassLoader loader)
53 (map
54 #(java.io.File. (.getPath ^java.net.URL %))
55 (.getURLs ^java.net.URLClassLoader loader))))
9e70cab @stuartsierra Use both java.class.path and Clojure's URLClassLoader
stuartsierra authored
56
57 (defn classpath
f1b9bfc @hugoduncan Make classpath return classpath elements from all parent classloaders
hugoduncan authored
58 "Returns a sequence of File objects of the elements on the classpath."
59 ([classloader]
60 (distinct
61 (mapcat
62 loader-classpath
63 (take-while
64 identity
c561c48 @jafingerhut CLASSPATH-3: Eliminate one use of reflection in java.classpath
jafingerhut authored
65 (iterate #(.getParent ^ClassLoader %) classloader)))))
f1b9bfc @hugoduncan Make classpath return classpath elements from all parent classloaders
hugoduncan authored
66 ([] (classpath (clojure.lang.RT/baseLoader))))
9e70cab @stuartsierra Use both java.class.path and Clojure's URLClassLoader
stuartsierra authored
67
739a74a @abedra Initial conversion. Combines c.c.classpath and c.c.jar
abedra authored
68 (defn classpath-directories
69 "Returns a sequence of File objects for the directories on classpath."
70 []
8b02684 @stuarthalloway eliminate reflection, update copyright statement
stuarthalloway authored
71 (filter #(.isDirectory ^File %) (classpath)))
739a74a @abedra Initial conversion. Combines c.c.classpath and c.c.jar
abedra authored
72
73 (defn classpath-jarfiles
74 "Returns a sequence of JarFile objects for the JAR files on classpath."
75 []
8b02684 @stuarthalloway eliminate reflection, update copyright statement
stuarthalloway authored
76 (map #(JarFile. ^File %) (filter jar-file? (classpath))))
Something went wrong with that request. Please try again.