01: /*
02: * @(#)addClassPath.java 1.1 05/05/19
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import java.util.List;
12: import java.util.ArrayList;
13: import java.net.URL;
14: import java.net.URLClassLoader;
15: import java.net.MalformedURLException;
16: import java.io.File;
17: import pnuts.lang.Context;
18: import pnuts.lang.PnutsFunction;
19: import pnuts.lang.PnutsException;
20:
21: public class addClassPath extends PnutsFunction {
22: public addClassPath() {
23: super ("addClassPath");
24: }
25:
26: public boolean defined(int nargs) {
27: return nargs == 1;
28: }
29:
30: protected Object exec(Object[] args, Context context) {
31: if (args.length != 1) {
32: undefined(args, context);
33: return null;
34: }
35: Object path = args[0];
36: URL url;
37: try {
38: if (path instanceof String) {
39: File file = PathHelper.getFile((String) path, context);
40: url = file.getAbsoluteFile().toURL();
41: } else if (path instanceof File) {
42: url = ((File) path).getAbsoluteFile().toURL();
43: } else if (path instanceof URL) {
44: url = (URL) path;
45: } else {
46: throw new IllegalArgumentException(String.valueOf(path));
47: }
48: } catch (MalformedURLException mue) {
49: throw new PnutsException(mue, context);
50: }
51: URL[] urls = getClassPath.getURLs(context);
52: ArrayList newList = new ArrayList();
53: newList.add(url);
54: if (urls != null) {
55: for (int i = 0; i < urls.length; i++) {
56: if (!urls[i].equals(url)) {
57: newList.add(urls[i]);
58: }
59: }
60: }
61: URL[] newURLs = (URL[]) newList
62: .toArray(new URL[newList.size()]);
63: setClassPath.setURLs(newURLs, context);
64: return null;
65: }
66:
67: public String toString() {
68: return "function addClassPath(pathOrURL)";
69: }
70: }
|