forked from mcfunley/juds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJUDS.java
83 lines (67 loc) · 2.64 KB
/
JUDS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.etsy.net;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URLClassLoader;
import java.net.URL;
import java.io.File;
import java.net.MalformedURLException;
public class JUDS {
/**
* A constant for the datagram socket type (connectionless).
*/
public static final int SOCK_DGRAM = 0;
/**
* A constant for the stream oriented stream socket type (connection-based)
*/
public static final int SOCK_STREAM = 1;
public static final int SERVER = 0;
public static final int CLIENT = 1;
static URL jarURL;
static {
if(System.getenv("JUDSDIR") != null) {
try {
jarURL = new File(System.getenv("JUDSDIR")).toURI().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException("Unable to create URL from path " + System.getenv("JUDSDIR"));
}
} else {
URL url = JUDS.class
.getProtectionDomain()
.getCodeSource()
.getLocation();
URL prepared = null;
if ("jar".equals(url.getProtocol()) || "wsjar".equals(url.getProtocol())) {
String filePath = url.getPath();
int end = filePath.indexOf("!/");
if (end >= 0) {
filePath = filePath.substring(0, end);
if (filePath.contains("file://") && !filePath.contains("file:////")) {
filePath = filePath.replaceFirst("file://", "file:////");
}
try {
prepared = new URL(filePath);
} catch (MalformedURLException ex) {
}
}
}
if (prepared == null) {
prepared = url;
}
jarURL = prepared;
}
}
private static ClassLoader judsCl = new URLClassLoader(
new URL[] { jarURL },
ClassLoader.getSystemClassLoader());
public static Object safeSocket(int type, String socketFile, int socketType)
throws ClassNotFoundException, NoSuchMethodException,
InstantiationException, IllegalAccessException,
InvocationTargetException {
String name = (type == SERVER) ? "Server" : "Client";
Class c = Class.forName("com.etsy.net.UnixDomainSocket"+name,
true, judsCl);
Constructor ctor = c.getConstructor(new Class[] {
String.class, int.class });
return ctor.newInstance(new Object[] { socketFile, socketType });
}
}