static { Attributes m = null; final URL loc = Prop.LOCATION; if (loc != null) { final String jar = loc.getFile(); try { final ClassLoader cl = JarManifest.class.getClassLoader(); final Enumeration<URL> list = cl.getResources("META-INF/MANIFEST.MF"); while (list.hasMoreElements()) { final URL url = list.nextElement(); if (!url.getFile().contains(jar)) continue; final InputStream in = url.openStream(); try { m = new Manifest(in).getMainAttributes(); break; } finally { in.close(); } } } catch (final IOException ex) { Util.errln(ex); } } MAP = m; }
private ClassLoader getRootLoader() { ClassLoader ret; for (ret = this.getClass().getClassLoader(); ret != null && ret.getParent() != null; ret = ret.getParent()) {} return ret; }
private static void setupJurisdictionPolicies() throws Exception { String javaHomeDir = System.getProperty("java.home"); String sep = File.separator; String pathToPolicyJar = javaHomeDir + sep + "lib" + sep + "security" + sep; File exportJar = new File(pathToPolicyJar, "US_export_policy.jar"); File importJar = new File(pathToPolicyJar, "local_policy.jar"); URL jceCipherURL = ClassLoader.getSystemResource("javax/crypto/Cipher.class"); if ((jceCipherURL == null) || !exportJar.exists() || !importJar.exists()) { throw new SecurityException("Cannot locate policy or framework files!"); } // Read jurisdiction policies. CryptoPermissions defaultExport = new CryptoPermissions(); CryptoPermissions exemptExport = new CryptoPermissions(); loadPolicies(exportJar, defaultExport, exemptExport); CryptoPermissions defaultImport = new CryptoPermissions(); CryptoPermissions exemptImport = new CryptoPermissions(); loadPolicies(importJar, defaultImport, exemptImport); // Merge the export and import policies for default applications. if (defaultExport.isEmpty() || defaultImport.isEmpty()) { throw new SecurityException("Missing mandatory jurisdiction " + "policy files"); } defaultPolicy = defaultExport.getMinimum(defaultImport); // Merge the export and import policies for exempt applications. if (exemptExport.isEmpty()) { exemptPolicy = exemptImport.isEmpty() ? null : exemptImport; } else { exemptPolicy = exemptExport.getMinimum(exemptImport); } }
protected void addRuntimeClasses(JarOutputStream jar) throws IOException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (cl == null) { cl = getClass().getClassLoader(); } ArrayList<String> runtimeClasses = getRuntimeClasses(cl); for (String c : runtimeClasses) { jar.putNextEntry(new JarEntry(c)); InputStream in = cl.getResourceAsStream(c); int ch; while ((ch = in.read()) != -1) { jar.write((char) ch); } jar.closeEntry(); in.close(); } }
protected ArrayList<String> getRuntimeClasses(ClassLoader cl) throws IOException { ArrayList<String> classes = new ArrayList<String>(); InputStream in = cl.getResourceAsStream("com/sforce/ws/runtime-classes.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { classes.add(line); } reader.close(); return classes; }
public static void unzipInteralZip( ClassLoader classLoader, String resourcePath, File libDir, boolean debug) { if (debug) System.out.println("Extracting " + resourcePath); libDir.mkdir(); URL resource = classLoader.getResource(resourcePath); if (resource == null) { System.err.println("Could not find the " + resourcePath + " on classpath!"); System.exit(1); } class PrintDot extends TimerTask { public void run() { System.out.print("."); } } Timer timer = new Timer(); PrintDot task = new PrintDot(); timer.schedule(task, 0, 2000); try { BufferedInputStream bis = new BufferedInputStream(resource.openStream()); JarInputStream jis = new JarInputStream(bis); JarEntry je = null; while ((je = jis.getNextJarEntry()) != null) { java.io.File f = new java.io.File(libDir.toString() + java.io.File.separator + je.getName()); if (je.isDirectory()) { f.mkdir(); continue; } File parentDir = new File(f.getParent()); if (!parentDir.exists()) { parentDir.mkdir(); } FileOutputStream fileOutStream = new FileOutputStream(f); writeStreamTo(jis, fileOutStream, 8 * KB); if (f.getPath().endsWith("pack.gz")) { unpack(f); fileOutStream.close(); f.delete(); } } } catch (Exception exc) { task.cancel(); exc.printStackTrace(); } task.cancel(); }
/** * Enumerates the resouces in a give package name. This works even if the resources are loaded * from a jar file! * * <p>Adapted from code by mikewse on the java.sun.com message boards. * http://forum.java.sun.com/thread.jsp?forum=22&thread=30984 * * @param packageName The package to enumerate * @return A Set of Strings for each resouce in the package. */ public static Set getResoucesInPackage(String packageName) throws IOException { String localPackageName; if (packageName.endsWith("/")) { localPackageName = packageName; } else { localPackageName = packageName + '/'; } Enumeration dirEnum = ClassLoader.getSystemResources(localPackageName); Set names = new HashSet(); // Loop CLASSPATH directories while (dirEnum.hasMoreElements()) { URL resUrl = (URL) dirEnum.nextElement(); // Pointing to filesystem directory if (resUrl.getProtocol().equals("file")) { File dir = new File(resUrl.getFile()); File[] files = dir.listFiles(); if (files != null) { for (int i = 0; i < files.length; i++) { File file = files[i]; if (file.isDirectory()) continue; names.add(localPackageName + file.getName()); } } // Pointing to Jar file } else if (resUrl.getProtocol().equals("jar")) { JarURLConnection jconn = (JarURLConnection) resUrl.openConnection(); JarFile jfile = jconn.getJarFile(); Enumeration entryEnum = jfile.entries(); while (entryEnum.hasMoreElements()) { JarEntry entry = (JarEntry) entryEnum.nextElement(); String entryName = entry.getName(); // Exclude our own directory if (entryName.equals(localPackageName)) continue; String parentDirName = entryName.substring(0, entryName.lastIndexOf('/') + 1); if (!parentDirName.equals(localPackageName)) continue; names.add(entryName); } } else { // Invalid classpath entry } } return names; }
public static void copyInternalFile(ClassLoader classLoader, String resourcePath, File dest) { URL resource = classLoader.getResource(resourcePath); try { BufferedInputStream bis = new BufferedInputStream(resource.openStream()); FileOutputStream output = new FileOutputStream(dest); writeStreamTo(bis, output, 8 * KB); output.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static void main(String[] args) throws Exception { System.out.println( "Checking that all known MBeans that are " + "NotificationBroadcasters have sane " + "MBeanInfo.getNotifications()"); System.out.println("Checking platform MBeans..."); checkPlatformMBeans(); URL codeBase = ClassLoader.getSystemResource("javax/management/MBeanServer.class"); if (codeBase == null) { throw new Exception("Could not determine codeBase for " + MBeanServer.class); } System.out.println(); System.out.println("Looking for standard MBeans..."); String[] classes = findStandardMBeans(codeBase); System.out.println("Testing standard MBeans..."); for (int i = 0; i < classes.length; i++) { String name = classes[i]; Class<?> c; try { c = Class.forName(name); } catch (Throwable e) { System.out.println(name + ": cannot load (not public?): " + e); continue; } if (!NotificationBroadcaster.class.isAssignableFrom(c)) { System.out.println(name + ": not a NotificationBroadcaster"); continue; } if (Modifier.isAbstract(c.getModifiers())) { System.out.println(name + ": abstract class"); continue; } NotificationBroadcaster mbean; Constructor<?> constr; try { constr = c.getConstructor(); } catch (Exception e) { System.out.println(name + ": no public no-arg constructor: " + e); continue; } try { mbean = (NotificationBroadcaster) constr.newInstance(); } catch (Exception e) { System.out.println(name + ": no-arg constructor failed: " + e); continue; } check(mbean); } System.out.println(); System.out.println("Testing some explicit cases..."); check(new RelationService(false)); /* We can't do this: check(new RequiredModelMBean()); because the Model MBean spec more or less forces us to use the names GENERIC and ATTRIBUTE_CHANGE for its standard notifs. */ checkRMIConnectorServer(); System.out.println(); if (!suspicious.isEmpty()) System.out.println("SUSPICIOUS CLASSES: " + suspicious); if (failed.isEmpty()) System.out.println("TEST PASSED"); else { System.out.println("TEST FAILED: " + failed); System.exit(1); } }