Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #43 from tobias/dynapath

Use dynapath for manipulating the effective classpath
  • Loading branch information...
commit 84561ad8daa38db94c74a66f274a370623a8eecd 2 parents b52c22e + b3b301e
@cemerick authored
Showing with 16 additions and 37 deletions.
  1. +6 −0 pom.xml
  2. +10 −37 src/main/clojure/cemerick/pomegranate.clj
View
6 pom.xml
@@ -34,6 +34,7 @@
<aetherVersion>1.13.1</aetherVersion>
<mavenVersion>3.0.4</mavenVersion>
<wagonVersion>2.2</wagonVersion>
+ <dynapathVersion>0.2.1</dynapathVersion>
</properties>
<dependencies>
@@ -67,6 +68,11 @@
<artifactId>maven-aether-provider</artifactId>
<version>${mavenVersion}</version>
</dependency>
+ <dependency>
+ <groupId>org.tcrawley</groupId>
+ <artifactId>dynapath</artifactId>
+ <version>${dynapathVersion}</version>
+ </dependency>
<!-- wagons for dependency resolution -->
<dependency>
View
47 src/main/clojure/cemerick/pomegranate.clj
@@ -2,7 +2,8 @@
(:import (clojure.lang DynamicClassLoader)
(java.net URL URLClassLoader))
(:require [clojure.java.io :as io]
- [cemerick.pomegranate.aether :as aether])
+ [cemerick.pomegranate.aether :as aether]
+ [dynapath.util :as dp])
(:refer-clojure :exclude (add-classpath)))
;; call-method pulled from clojure.contrib.reflect, (c) 2010 Stuart Halloway & Contributors
@@ -21,36 +22,6 @@
(doto (.setAccessible true))
(.invoke obj (into-array Object args))))
-(defprotocol URLClasspath
- "Ability to dynamically add urls to classloaders.
-
-This protocol is an implementation detail. Use
-`modifiable-classloader?` and `add-classpath` or `add-dependencies`
-unless you are extending a type to this protocol."
- (^{:private true} can-modify? [this] "Returns true if the given classloader can be modified.")
- (^{:private true} add-url [this url] "add the url to the classpath"))
-
-(extend-type DynamicClassLoader
- URLClasspath
- (can-modify? [this] true)
- (add-url [this url] (.addURL this url)))
-
-(def ^:private url-classloader-base
- {:can-modify? (constantly true)
- :add-url (fn [this url]
- (call-method URLClassLoader 'addURL [URL] this url))})
-
-(extend URLClassLoader URLClasspath url-classloader-base)
-
-(defmacro when-resolves
- [sym & body]
- (when (resolve sym) `(do ~@body)))
-
-(when-resolves sun.misc.Launcher
- (extend sun.misc.Launcher$ExtClassLoader URLClasspath
- (assoc url-classloader-base
- :can-modify? (constantly false))))
-
(defn classloader-hierarchy
"Returns a seq of classloaders, with the tip of the hierarchy first.
Uses the current thread context ClassLoader as the tip ClassLoader
@@ -63,10 +34,10 @@ unless you are extending a type to this protocol."
(defn modifiable-classloader?
"Returns true iff the given ClassLoader is of a type that satisfies
- the URLClasspath protocol, and it can be modified."
+ the dynapath.dynamic-classpath/DynamicClasspath protocol, and it can
+ be modified."
[cl]
- (and (satisfies? URLClasspath cl)
- (can-modify? cl)))
+ (dp/addable-classpath? cl))
(defn add-classpath
"A corollary to the (deprecated) `add-classpath` in clojure.core. This implementation
@@ -74,12 +45,14 @@ unless you are extending a type to this protocol."
to add that path to the right classloader (with the search rooted at the current
thread's context classloader)."
([jar-or-dir classloader]
- (add-url classloader (.toURL (io/file jar-or-dir))))
+ (if-not (dp/add-classpath-url classloader (.toURL (io/file jar-or-dir)))
+ (throw (IllegalStateException. (str classloader " is not a modifiable classloader")))))
([jar-or-dir]
(let [classloaders (classloader-hierarchy)]
(if-let [cl (last (filter modifiable-classloader? classloaders))]
(add-classpath jar-or-dir cl)
- (throw (IllegalStateException. "Could not find a suitable classloader to modify from " classloaders))))))
+ (throw (IllegalStateException. (str "Could not find a suitable classloader to modify from "
+ classloaders)))))))
(defn add-dependencies
"Resolves a set of dependencies, optionally against a set of additional Maven repositories,
@@ -115,7 +88,7 @@ unless you are extending a type to this protocol."
([classloaders]
[]
(->> (reverse classloaders)
- (mapcat #(when (instance? URLClassLoader %) (.getURLs %)))
+ (mapcat #(dp/classpath-urls %))
(map str)))
([] (get-classpath (classloader-hierarchy))))
Please sign in to comment.
Something went wrong with that request. Please try again.