package geniusweb.partiesserver; import java.io.File; import java.io.IOException; import java.net.JarURLConnection; import java.net.URL; import java.net.URLClassLoader; import java.util.jar.Attributes; /** * Classloader that ensures loaded code is kept separate from other loaded code. * This is to prevent dependency conflicts between parties that we load. * */ class JarClassLoader1 extends URLClassLoader { private URL u; public JarClassLoader1(URL url, ClassLoader parent) { super(new URL[] { url }, parent); this.u = url; String filename = url.getPath(); if (filename.startsWith("file:")) { filename = filename.substring(5); } if (filename.endsWith("/")) { filename = filename.substring(0, filename.length() - 1); } if (filename.endsWith("!")) { filename = filename.substring(0, filename.length() - 1); } File jarfile = new File(filename); if (jarfile == null || !jarfile.exists()) { throw new IllegalArgumentException( "file does not exist:" + jarfile); } if (!jarfile.canRead()) { throw new IllegalArgumentException( "File " + jarfile + " has no read permissions"); } if (!jarfile.isFile() || !jarfile.getName().endsWith(".jar")) { throw new IllegalArgumentException("File " + jarfile + " must be a file and have extension .jar"); } } public String getMainClassName() throws IOException { JarURLConnection uc = (JarURLConnection) u.openConnection(); Attributes attr = uc.getMainAttributes(); return attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null; } }