public Reader openFile(String s) { try { // first try to read file from local file system File file = new File(resourceFolder + "/" + s); if (file.exists()) { return new FileReader(file); } // next try for files included in jar try { Reader r = new InputStreamReader(this.getClass().getResourceAsStream("/" + s)); if (r != null) return r; } catch (Exception e) { e.printStackTrace(); } ClassLoader loader = getClass().getClassLoader(); // Enumeration<java.net.URL> urls = loader.getResources(arg0); java.net.URL url = getClass().getClassLoader().getResource(resourceFolder + "/" + s); System.err.println("jar url " + url); // or URL from web if (url == null) url = new java.net.URL(s); java.net.URLConnection site = url.openConnection(); InputStream is = site.getInputStream(); return new InputStreamReader(is); } catch (IOException ioe) { System.err.println("Could not open " + s); return null; } }
/** * get the context for the AppletClassLoader we are creating. the context is granted permission to * create the class loader, connnect to the codebase, and whatever else the policy grants to all * codebases. */ private AccessControlContext getAccessControlContext(final URL codebase) { PermissionCollection perms = AccessController.doPrivileged( new PrivilegedAction<PermissionCollection>() { @Override public PermissionCollection run() { Policy p = java.security.Policy.getPolicy(); if (p != null) { return p.getPermissions( new CodeSource(null, (java.security.cert.Certificate[]) null)); } else { return null; } } }); if (perms == null) perms = new Permissions(); // XXX: this is needed to be able to create the classloader itself! perms.add(SecurityConstants.CREATE_CLASSLOADER_PERMISSION); Permission p; java.net.URLConnection urlConnection = null; try { urlConnection = codebase.openConnection(); p = urlConnection.getPermission(); } catch (java.io.IOException ioe) { p = null; } if (p != null) perms.add(p); if (p instanceof FilePermission) { String path = p.getName(); int endIndex = path.lastIndexOf(File.separatorChar); if (endIndex != -1) { path = path.substring(0, endIndex + 1); if (path.endsWith(File.separator)) { path += "-"; } perms.add(new FilePermission(path, SecurityConstants.FILE_READ_ACTION)); } } else { URL locUrl = codebase; if (urlConnection instanceof JarURLConnection) { locUrl = ((JarURLConnection) urlConnection).getJarFileURL(); } String host = locUrl.getHost(); if (host != null && (host.length() > 0)) perms.add(new SocketPermission(host, SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION)); } ProtectionDomain domain = new ProtectionDomain( new CodeSource(codebase, (java.security.cert.Certificate[]) null), perms); AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] {domain}); return acc; }