01: /*
02: * @(#)setThreadClassPath.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution of
07: * this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.tools;
10:
11: import java.io.File;
12: import java.io.IOException;
13: import java.net.URL;
14: import java.net.URLClassLoader;
15:
16: import pnuts.lang.Context;
17: import pnuts.lang.PnutsFunction;
18:
19: /*
20: * setThreadClassPath(["path1", "path2", ...])
21: */
22: public class setThreadClassPath extends PnutsFunction {
23: private final static String CWD = "cwd".intern();
24:
25: public setThreadClassPath() {
26: super ("setThreadClassPath");
27: }
28:
29: public boolean defined(int nargs) {
30: return nargs == 1;
31: }
32:
33: protected Object exec(Object[] args, Context context) {
34: if (args.length != 1) {
35: undefined(args, context);
36: return null;
37: }
38: String cwd = (String) context.get(CWD);
39: try {
40: Object[] arg = (Object[]) args[0];
41: int len = arg.length;
42: URL[] urls = new URL[len];
43: for (int i = 0; i < len; i++) {
44: File f = new File(cwd, (String) arg[i]);
45: String path;
46: if (f.isDirectory()) {
47: path = f.getCanonicalPath() + "/";
48: } else {
49: path = f.getCanonicalPath();
50: }
51: urls[i] = new URL("file", null, 0, path);
52: }
53: Thread.currentThread().setContextClassLoader(
54: new URLClassLoader(urls));
55: } catch (IOException e) {
56: // ignore
57: }
58: return null;
59: }
60: }
|