package geniusweb.partiesserver; import java.io.File; import java.io.IOException; import java.net.JarURLConnection; import java.net.MalformedURLException; import java.net.URISyntaxException; 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; /** * @param url The URL is like * jar:file:/Web%20Servers/../partiesserver-1.4.1/partiesrepo/agentgg-1.5.5.jar!/ * * @param parent the parent classloader. */ public JarClassLoader1(URL url, ClassLoader parent) { super(new URL[] { url }, parent); this.u = url; // we need to get to the actual file. This is very tricky, // most solutions on the web don't work, throw, don't remove %20,etc // url.toURI throws for multi-protocol URLs like "jar:file://...". // https://docs.oracle.com/javase/7/docs/api/java/net/JarURLConnection.html // the syntax of a JAR URL is: jar:!/{entry} String filename = url.getPath(); // file:/bla%20bla/file.jar!/ filename = filename.substring(0, filename.indexOf("!/")); File jarfile; try { jarfile = new File(new URL(filename).toURI()); } catch (MalformedURLException | URISyntaxException e) { throw new IllegalArgumentException( "file " + filename + " seems malformed", e); } 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; } }