FreeWRL/FreeX3D  3.0.0
FWJavaScriptClassLoader.java
1 package vrml;
2 import java.io.*;
3 import java.net.URL;
4 import java.net.SocketPermission;
5 import java.net.MalformedURLException;
6 import java.util.Enumeration;
7 import java.util.NoSuchElementException;
8 import java.util.PropertyPermission;
9 import java.security.*;
10 import java.security.cert.Certificate;
11 
12 
13 public final class FWJavaScriptClassLoader extends SecureClassLoader {
14  URL baseURL;
15  CodeSource myCodeSource;
16 
17 
21  public FWJavaScriptClassLoader(String url) {
22  System.out.println ("new FWJavaScriptClassLoader - url is " + url);
23  try {
24  baseURL = new java.net.URL(url);
25 
26  myCodeSource = new CodeSource(baseURL, (Certificate[]) null);
27  } catch (MalformedURLException ex) {
28  throw new InternalError("Script URL malformed: "+url);
29  }
30  }
31 
32  protected Class findClass(String name)
33  throws ClassNotFoundException
34  {
35  System.err.println("LOADING CLASS '"+name+"'");
36  try {
37  byte[] b = readFile(name.replace('.', '/') + ".class");
38  return defineClass(name, b, 0, b.length, myCodeSource);
39  } catch (IOException e) {
40  throw new ClassNotFoundException(name);
41  }
42  }
43 
44  private static final String props[] = {
45  "file.separator",
46  "path.separator",
47  "java.class.version",
48  "java.vendor",
49  "java.version",
50  "java.specification.name",
51  "java.specification.vendor",
52  "java.specification.version",
53  "java.vendor.url",
54  "java.vm.name",
55  "java.vm.vendor",
56  "java.vm.version",
57  "java.vm.specification.name",
58  "java.vm.specification.vendor",
59  "java.vm.specification.version",
60  "line.separator",
61  "os.name",
62  "os.arch",
63  "os.version"
64  };
65 
66  protected PermissionCollection getPermissions(CodeSource codesource) {
67  Permissions perms = new Permissions();
68  URL url = codesource.getLocation();
69  perms.add(new SocketPermission(url.getHost(), "connect,accept"));
70  if (url.getProtocol().equals("file")) {
71  /* local script */
72  String path = url.getFile().replace('/', File.separatorChar);
73  path = path.substring(0, path.lastIndexOf(File.separatorChar)+1);
74  perms.add(new FilePermission(path+"*", "read,write,delete"));
75  }
76  for (int i = 0; i < props.length; i++)
77  perms.add(new PropertyPermission(props[i], "read"));
78  //System.err.println("Script permission are "+perms);
79  return perms;
80  }
81 
82  private byte[] readFile(String name) throws IOException
83  {
84  InputStream is = getResourceAsStream(name);
85  ByteArrayOutputStream bao = new ByteArrayOutputStream();
86  byte[] buff = new byte[4096];
87  int len;
88  while ((len = is.read(buff)) > 0)
89  bao.write(buff, 0, len);
90  return bao.toByteArray();
91  }
92 
93  protected URL findResource(String name)
94  {
95  try {
96  System.err.println("LOADING RESOURCE '"+name+"'");
97  return new URL(baseURL, name);
98  } catch (MalformedURLException ex) {
99  return null;
100  }
101  }
102 
103  protected Enumeration findResources(String name) throws IOException
104  {
105  final URL url = new URL(baseURL, name);
106  return new Enumeration() {
107  boolean hasMore = true;
108  public boolean hasMoreElements() {
109  return hasMore;
110  }
111  public Object nextElement() {
112  if (!hasMore)
113  throw new NoSuchElementException();
114  hasMore = false;
115  return url;
116  }
117  };
118  }
119 }