Ignore:
Timestamp:
07/01/10 20:57:11 (12 years ago)
Author:
astalla
Message:

Added classpath manipulation primitives: java:add-to-classpath and java:dump-classpath

Location:
trunk/abcl/src/org/armedbear/lisp
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/src/org/armedbear/lisp/Autoload.java

    r12749 r12773  
    515515        autoload(PACKAGE_JAVA, "%load-java-class-from-byte-array", "RuntimeClass");
    516516        autoload(PACKAGE_JAVA, "get-default-classloader", "JavaClassLoader");
     517        autoload(PACKAGE_JAVA, "make-classloader", "JavaClassLoader");
     518        autoload(PACKAGE_JAVA, "add-to-classpath", "JavaClassLoader");
     519        autoload(PACKAGE_JAVA, "dump-classpath", "JavaClassLoader");
    517520        autoload(PACKAGE_MOP, "funcallable-instance-function", "StandardGenericFunction", false);
    518521        autoload(PACKAGE_MOP, "generic-function-name", "StandardGenericFunction", true);
  • trunk/abcl/src/org/armedbear/lisp/JavaClassLoader.java

    r12715 r12773  
    4040import java.util.Set;
    4141import java.net.URL;
    42 
    43 public class JavaClassLoader extends java.net.URLClassLoader {
     42import java.net.URLClassLoader;
     43
     44public class JavaClassLoader extends URLClassLoader {
    4445
    4546    private static JavaClassLoader persistentInstance;
     
    169170    };
    170171
     172    private static final Primitive DUMP_CLASSPATH = new pf_dump_classpath();
     173    private static final class pf_dump_classpath extends Primitive
     174    {
     175        pf_dump_classpath()
     176        {
     177            super("dump-classpath", PACKAGE_JAVA, true, "&optional classloader");
     178        }
     179
     180        @Override
     181        public LispObject execute() {
     182      return execute(new JavaObject(getCurrentClassLoader()));
     183        }
     184
     185        @Override
     186        public LispObject execute(LispObject classloader) {
     187      LispObject list = NIL;
     188      Object o = classloader.javaInstance();
     189      while(o instanceof ClassLoader) {
     190    ClassLoader cl = (ClassLoader) o;
     191    list = list.push(dumpClassPath(cl));
     192    o = cl.getParent();
     193      }
     194      return list.nreverse();
     195        }
     196    };
     197
     198    private static final Primitive ADD_TO_CLASSPATH = new pf_add_to_classpath();
     199    private static final class pf_add_to_classpath extends Primitive
     200    {
     201        pf_add_to_classpath()
     202        {
     203            super("add-to-classpath", PACKAGE_JAVA, true, "jar-or-jars &optional (classloader (get-current-classloader))");
     204        }
     205
     206        @Override
     207        public LispObject execute(LispObject jarOrJars) {
     208      return execute(jarOrJars, new JavaObject(getCurrentClassLoader()));
     209        }
     210
     211        @Override
     212        public LispObject execute(LispObject jarOrJars, LispObject classloader) {
     213      Object o = classloader.javaInstance();
     214      if(o instanceof JavaClassLoader) {
     215    JavaClassLoader jcl = (JavaClassLoader) o;
     216    if(jarOrJars instanceof Cons) {
     217        while(jarOrJars != NIL) {
     218      addURL(jcl, jarOrJars.car());
     219      jarOrJars = jarOrJars.cdr();
     220        }
     221    } else {
     222        addURL(jcl, jarOrJars);
     223    }
     224    return T;
     225      } else {
     226    return error(new TypeError(o + " must be an instance of " + JavaClassLoader.class.getName()));
     227      }
     228        }
     229    };
     230
     231    protected static void addURL(JavaClassLoader jcl, LispObject jar) {
     232  try {
     233      if(jar instanceof Pathname) {
     234    jcl.addURL(((Pathname) jar).toURL());
     235      } else if(jar instanceof AbstractString) {
     236    jcl.addURL(new Pathname(jar.toString()).toURL());
     237      } else {
     238    error(new TypeError(jar + " must be a pathname designator"));
     239      }
     240  } catch(java.net.MalformedURLException e) {
     241      error(new LispError(jar + " is not a valid URL"));
     242  }
     243    }
     244
     245
     246    public static LispObject dumpClassPath(ClassLoader o) {
     247  if(o instanceof URLClassLoader) {
     248      LispObject list = NIL;
     249      for(URL u : ((URLClassLoader) o).getURLs()) {
     250    list = list.push(new Pathname(u));
     251      }
     252      return new Cons(new JavaObject(o), list.nreverse());
     253  } else {
     254      return new JavaObject(o);
     255  }
     256    }
     257
    171258    public static ClassLoader getCurrentClassLoader() {
    172259  LispObject classLoader = CLASSLOADER.symbolValueNoThrow();
  • trunk/abcl/src/org/armedbear/lisp/Pathname.java

    r12696 r12773  
    23432343    }
    23442344
     2345    public URL toURL() throws MalformedURLException {
     2346  if(isURL()) {
     2347      return new URL(getNamestring());
     2348  } else {
     2349      return toFile().toURL();
     2350  }
     2351    }
     2352
     2353    public File toFile() {
     2354  if(!isURL()) {
     2355      return new File(getNamestring());
     2356  } else {
     2357      throw new RuntimeException(this + " does not represent a file");
     2358  }
     2359    }
     2360
    23452361    static {
    23462362        LispObject obj = Symbol.DEFAULT_PATHNAME_DEFAULTS.getSymbolValue();
Note: See TracChangeset for help on using the changeset viewer.