[1] | 1 | package geniusweb.partiesserver;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.io.IOException;
|
---|
| 5 | import java.net.JarURLConnection;
|
---|
| 6 | import java.net.URL;
|
---|
| 7 | import java.net.URLClassLoader;
|
---|
| 8 | import java.util.jar.Attributes;
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | * Classloader that ensures loaded code is kept separate from other loaded code.
|
---|
| 12 | * This is to prevent dependency conflicts between parties that we load.
|
---|
| 13 | *
|
---|
| 14 | */
|
---|
| 15 | class JarClassLoader1 extends URLClassLoader {
|
---|
| 16 |
|
---|
| 17 | private URL u;
|
---|
| 18 |
|
---|
| 19 | public JarClassLoader1(URL url, ClassLoader parent) {
|
---|
| 20 | super(new URL[] { url }, parent);
|
---|
| 21 | this.u = url;
|
---|
| 22 |
|
---|
| 23 | String filename = url.getPath();
|
---|
| 24 | if (filename.startsWith("file:")) {
|
---|
| 25 | filename = filename.substring(5);
|
---|
| 26 | }
|
---|
| 27 | if (filename.endsWith("/")) {
|
---|
| 28 | filename = filename.substring(0, filename.length() - 1);
|
---|
| 29 | }
|
---|
| 30 | if (filename.endsWith("!")) {
|
---|
| 31 | filename = filename.substring(0, filename.length() - 1);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | File jarfile = new File(filename);
|
---|
| 35 |
|
---|
| 36 | if (jarfile == null || !jarfile.exists()) {
|
---|
| 37 | throw new IllegalArgumentException(
|
---|
| 38 | "file does not exist:" + jarfile);
|
---|
| 39 | }
|
---|
| 40 | if (!jarfile.canRead()) {
|
---|
| 41 | throw new IllegalArgumentException(
|
---|
| 42 | "File " + jarfile + " has no read permissions");
|
---|
| 43 | }
|
---|
| 44 | if (!jarfile.isFile() || !jarfile.getName().endsWith(".jar")) {
|
---|
| 45 | throw new IllegalArgumentException("File " + jarfile
|
---|
| 46 | + " must be a file and have extension .jar");
|
---|
| 47 |
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public String getMainClassName() throws IOException {
|
---|
| 53 | JarURLConnection uc = (JarURLConnection) u.openConnection();
|
---|
| 54 | Attributes attr = uc.getMainAttributes();
|
---|
| 55 | return attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;
|
---|
| 56 | }
|
---|
| 57 | } |
---|