1 18 package org.apache.batik.script.rhino; 19 20 import java.io.File ; 21 import java.io.FilePermission ; 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.net.URLClassLoader ; 25 import java.security.AccessController ; 26 import java.security.AccessControlContext ; 27 import java.security.CodeSource ; 28 import java.security.Permission ; 29 import java.security.PermissionCollection ; 30 import java.security.ProtectionDomain ; 31 import java.security.PrivilegedAction ; 32 33 import org.mozilla.javascript.GeneratedClassLoader; 34 35 42 public class RhinoClassLoader extends URLClassLoader implements GeneratedClassLoader { 43 46 protected URL documentURL; 47 48 51 protected CodeSource codeSource; 52 53 59 protected AccessControlContext rhinoAccessControlContext; 60 61 66 public RhinoClassLoader(URL documentURL, ClassLoader parent){ 67 super(documentURL != null ? new URL []{documentURL} : new URL []{}, 68 parent); 69 this.documentURL = documentURL; 70 if (documentURL != null){ 71 codeSource = new CodeSource (documentURL, null); 72 } 73 74 ProtectionDomain rhinoProtectionDomain 79 = new ProtectionDomain (codeSource, 80 getPermissions(codeSource)); 81 82 rhinoAccessControlContext 83 = new AccessControlContext (new ProtectionDomain []{ 84 rhinoProtectionDomain}); 85 } 86 87 90 static URL [] getURL(ClassLoader parent) { 91 if (parent instanceof RhinoClassLoader) { 92 URL documentURL = ((RhinoClassLoader)parent).documentURL; 93 if (documentURL != null) { 94 return new URL [] {documentURL}; 95 } else { 96 return new URL [] {}; 97 } 98 } else { 99 return new URL [] {}; 100 } 101 } 102 103 106 public Class defineClass(String name, 107 byte[] data) { 108 return super.defineClass(name, data, 0, data.length, codeSource); 109 } 110 111 114 public void linkClass(Class clazz) { 115 super.resolveClass(clazz); 116 } 117 118 122 public AccessControlContext getAccessControlContext() { 123 return rhinoAccessControlContext; 124 } 125 126 132 protected PermissionCollection getPermissions(CodeSource codesource) { 133 PermissionCollection perms = null; 134 135 if (codesource != null) { 136 perms = super.getPermissions(codesource); 137 } 138 139 if (documentURL != null && perms != null) { 140 Permission p = null; 141 Permission dirPerm = null; 142 try { 143 p = documentURL.openConnection().getPermission(); 144 } catch (IOException e){ 145 p = null; 146 } 147 148 if (p instanceof FilePermission ){ 149 String path = p.getName(); 150 if (!path.endsWith(File.separator)) { 151 int dirEnd = path.lastIndexOf(File.separator); 154 if (dirEnd != -1){ 155 path = path.substring(0, dirEnd + 1); 157 path += "-"; 158 dirPerm = new FilePermission (path, "read"); 159 perms.add(dirPerm); 160 } 161 } 162 } 163 } 164 165 return perms; 166 } 167 } 168 169 | Popular Tags |