1 | package geniusweb.partiesserver;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.net.JarURLConnection;
|
---|
6 | import java.net.MalformedURLException;
|
---|
7 | import java.net.URISyntaxException;
|
---|
8 | import java.net.URL;
|
---|
9 | import java.net.URLClassLoader;
|
---|
10 | import java.util.jar.Attributes;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Classloader that ensures loaded code is kept separate from other loaded code.
|
---|
14 | * This is to prevent dependency conflicts between parties that we load.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | class JarClassLoader1 extends URLClassLoader {
|
---|
18 |
|
---|
19 | private URL u;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * @param url The URL is like
|
---|
23 | * jar:file:/Web%20Servers/../partiesserver-1.4.1/partiesrepo/agentgg-1.5.5.jar!/
|
---|
24 | *
|
---|
25 | * @param parent the parent classloader.
|
---|
26 | */
|
---|
27 | public JarClassLoader1(URL url, ClassLoader parent) {
|
---|
28 | super(new URL[] { url }, parent);
|
---|
29 | this.u = url;
|
---|
30 |
|
---|
31 | // we need to get to the actual file. This is very tricky,
|
---|
32 | // most solutions on the web don't work, throw, don't remove %20,etc
|
---|
33 | // url.toURI throws for multi-protocol URLs like "jar:file://...".
|
---|
34 |
|
---|
35 | // https://docs.oracle.com/javase/7/docs/api/java/net/JarURLConnection.html
|
---|
36 | // the syntax of a JAR URL is: jar:<url>!/{entry}
|
---|
37 |
|
---|
38 | String filename = url.getPath(); // file:/bla%20bla/file.jar!/
|
---|
39 | filename = filename.substring(0, filename.indexOf("!/"));
|
---|
40 | File jarfile;
|
---|
41 | try {
|
---|
42 | jarfile = new File(new URL(filename).toURI());
|
---|
43 | } catch (MalformedURLException | URISyntaxException e) {
|
---|
44 | throw new IllegalArgumentException(
|
---|
45 | "file " + filename + " seems malformed", e);
|
---|
46 | }
|
---|
47 |
|
---|
48 | if (jarfile == null || !jarfile.exists()) {
|
---|
49 | throw new IllegalArgumentException(
|
---|
50 | "file does not exist:" + jarfile);
|
---|
51 | }
|
---|
52 | if (!jarfile.canRead()) {
|
---|
53 | throw new IllegalArgumentException(
|
---|
54 | "File " + jarfile + " has no read permissions");
|
---|
55 | }
|
---|
56 | if (!jarfile.isFile() || !jarfile.getName().endsWith(".jar")) {
|
---|
57 | throw new IllegalArgumentException("File " + jarfile
|
---|
58 | + " must be a file and have extension .jar");
|
---|
59 |
|
---|
60 | }
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | public String getMainClassName() throws IOException {
|
---|
65 | JarURLConnection uc = (JarURLConnection) u.openConnection();
|
---|
66 | Attributes attr = uc.getMainAttributes();
|
---|
67 | return attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;
|
---|
68 | }
|
---|
69 | } |
---|