source: src/main/java/geniusweb/partiesserver/jarClassLoader1.java@ 2

Last change on this file since 2 was 1, checked in by bart, 5 years ago

Initial Release

File size: 1.5 KB
Line 
1package geniusweb.partiesserver;
2
3import java.io.File;
4import java.io.IOException;
5import java.net.JarURLConnection;
6import java.net.URL;
7import java.net.URLClassLoader;
8import 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 */
15class 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}
Note: See TracBrowser for help on using the repository browser.