Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(maint) Fix classpath stuff for Java 9 #99

Merged
merged 3 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: clojure
jdk:
- oraclejdk9
- oraclejdk8
- oraclejdk7
- openjdk7
- openjdk6
notifications:
email: false
hipchat:
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
[cheshire]

[org.ini4j/ini4j "0.5.2"]
[org.tcrawley/dynapath "0.2.5"]
[org.tcrawley/dynapath "1.0.0"]
[digest "1.4.3"]

]
Expand Down
10 changes: 10 additions & 0 deletions src/puppetlabs/kitchensink/classpath.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
[cl]
(dp/addable-classpath? cl))

(defn- ensure-modifiable-classloader
"Check if there is a modifiable classloader in the current hierarchy, and add
one if not."
[]
(let [classloader (.. Thread currentThread getContextClassLoader)]
(when (not-any? modifiable-classloader? (classloader-hierarchy classloader))
(let [new-cl (clojure.lang.DynamicClassLoader. classloader)]
(.. Thread currentThread (setContextClassLoader new-cl))))))

(defn add-classpath
"A corollary to the (deprecated) `add-classpath` in clojure.core. This implementation
requires a java.io.File or String path to a jar file or directory, and will attempt
Expand All @@ -57,6 +66,7 @@
(if-not (dp/add-classpath-url classloader (.toURL (file jar-or-dir)))
(throw (IllegalStateException. (str classloader " is not a modifiable classloader")))))
([jar-or-dir]
(ensure-modifiable-classloader)
(let [classloaders (classloader-hierarchy)]
(if-let [cl (last (filter modifiable-classloader? classloaders))]
(add-classpath jar-or-dir cl)
Expand Down
20 changes: 9 additions & 11 deletions test/puppetlabs/kitchensink/classpath_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
(:import (java.net URL)))

(deftest with-additional-classpath-entries-test
(let [paths ["/foo" "/bar"]
get-urls #(into #{}
(.getURLs (.getContextClassLoader (Thread/currentThread))))]
(let [paths ["classpath-test"]
get-resource #(-> (Thread/currentThread)
.getContextClassLoader
(.getResource "does-not-exist-anywhere-else"))]
(with-additional-classpath-entries
paths
(testing "classloader now includes the new paths"
(let [urls (get-urls)]
(is (contains? urls (URL. "file:/foo")))
(is (contains? urls (URL. "file:/bar"))))))
(testing "classloader has been restored to its previous state"
(let [urls (get-urls)]
(is (not (contains? urls (URL. "file:/foo"))))
(is (not (contains? urls (URL. "file:/bar"))))))))
(testing "classloader now includes the new path"
(is (get-resource))))

(testing "classloader no longer includes the new path"
(is (not (get-resource))))))