GithubHelp home page GithubHelp logo

dynapath's Introduction

dynapath

dynapath provides a protocol and util functions for class loaders that make their effective classpaths readable and/or modifiable.

Rationale

Clojure uses a clojure.lang.DynamicClassLoader by default (an extension of java.net.URLClassLoader), which provides .getURLs for reading the effective classpath and .addURL for modifying it. It's common for projects that need to read or modify the effective classpath to assume that a URLClassLoader is always available. But in some environments, the available class loader may not be a URLClassLoader, and may not be readable or modifiable.

Some projects (notably pomegranate) handle this by providing a protocol that can be implemented for other class loaders that may provide similar functionality.

dynapath provides a protocol that is based on an extraction of pomegranate's protocol, and is intended to be a standard way for accessing or modifying the effective classpath. Using dynapath in your library instead of assuming a class loader or implementing your own protocol provides the following benefits:

  • Your library can work with any modifiable/readable class loader without any changes
  • Any project that has already implemented DynamicClasspath for whatever esoteric class loader they are using will not need any other changes to use your library as well

Usage

Add it as a dependency:

For a Leiningen project:

[dynapath "0.2.0"]

For a maven project:

<dependency>
  <groupId>dynapath</groupId>
  <artifactId>dynapath</artifactId>
  <version>0.2.0</version>
</dependency>

If you need to access or modify the effective classpath:

(require '[dynapath.util :as dp])

;; returns a seq of the urls for the classloader. Takes any classloader
;; (whether it implements DynamicClasspath or not) and does the right thing
(dp/classpath-urls a-classloader)
  
;; returns a seq of all the urls available from the classloader and its 
;; parentage chain
(dp/all-classpath-urls a-classloader)

;; adds a url to the given classloader if it is addable
(dp/add-classpath-url a-classloader a-url)

Loading the dynapath.defaults namespace will automatically implement classpath-urls and add-classpath-url for URLClassLoader and DynamicClassLoader.

If you need to implement DynamicClasspath:

(require '[dynapath.dynamic-classpath :as dc])

(extend-type AReadableButNotModfiableClassLoader
  dc/DynamicClasspath
  (can-read? [_] true)
  (can-add? [_] false)
  (classpath-urls [cl] (seq ...)))

(extend AReadableAndModifiableClassLoader
  dc/DynamicClasspath
  (assoc dc/base-readable-addable-classpath ;; implements can-read? and can-add?
         :classpath-urls (fn [cl] ...)
         :add-classpath-url (fn [cl url] ...)))

Who's using it?

There are currently pending pull requests for:

Are you using it? If so, add yourself to this list and send me a PR.

License

Copyright © 2012 Tobias Crawley

Distributed under the Eclipse Public License.

dynapath's People

Contributors

tobias avatar xumingming avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.