Skip to content

Commit cbea02e

Browse files
committed
Support multiple jarpaths
1 parent c2b2751 commit cbea02e

File tree

6 files changed

+52
-43
lines changed

6 files changed

+52
-43
lines changed

dev/zip/scalive

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ else
1111
fi
1212
cd "$ROOT_DIR"
1313

14-
CLASS_PATH="$ROOT_DIR/2.10.3/*:$ROOT_DIR/*"
14+
CLASS_PATH="$ROOT_DIR/*:."
1515

1616
# Use exec to be compatible with daemontools:
1717
# http://cr.yp.to/daemontools.html
18-
exec java $JAVA_OPTS -cp $CLASS_PATH scalive.AgentLoader $ROOT_DIR "$@"
18+
exec java $JAVA_OPTS -cp $CLASS_PATH scalive.AgentLoader $ROOT_DIR $@

dev/zip/scalive.bat

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
# You may need to customize memory config below to optimize for your environment
21
set JAVA_OPTS=-Djava.awt.headless=true
32

43
set ROOT_DIR=%~dp0
54
cd "%$ROOT_DIR%"
65

7-
set CLASS_PATH="%ROOT_DIR%\2.10.3\*;%ROOT_DIR%\*"
6+
set CLASS_PATH="%ROOT_DIR%\*;."
87

98
java %JAVA_OPTS% -cp %CLASS_PATH% scalive.AgentLoader %$ROOT_DIR% %*

src/main/java/scalive/Agent.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package scalive;
22

3+
import java.io.File;
34
import java.lang.instrument.Instrumentation;
45
import java.net.ServerSocket;
56
import java.net.Socket;
67

78
public class Agent {
89
/**
9-
* @param agentArgs <jarpath> <server port>
10+
* @param agentArgs <jarpath1>[<File.pathSeparator><jarpath2>...] <server port>
1011
*
1112
* jarpath is the absolute path this directory:
1213
*
@@ -22,9 +23,9 @@ public class Agent {
2223
* }}}
2324
*/
2425
public static void agentmain(String agentArgs, Instrumentation inst) throws Exception {
25-
final String[] args = agentArgs.split(" ");
26-
final String jarpath = args[0];
27-
final int port = Integer.parseInt(args[1]);
26+
final String[] args = agentArgs.split(" ");
27+
final String[] jarpaths = args[0].split(File.pathSeparator);
28+
final int port = Integer.parseInt(args[1]);
2829

2930
System.out.println("[Scalive] REPL server starts at port " + port);
3031
final ServerSocket server = new ServerSocket(port);
@@ -41,7 +42,7 @@ public void run() {
4142
try {
4243
Socket client = server.accept(); // Block until a connection comes in
4344
server.close(); // Accept no other clients
44-
Server.serve(client, jarpath);
45+
Server.serve(client, jarpaths);
4546
} catch (Exception e) {
4647
e.printStackTrace();
4748
}

src/main/java/scalive/AgentLoader.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import com.sun.tools.attach.VirtualMachine;
55
import com.sun.tools.attach.VirtualMachineDescriptor;
66

7+
import java.io.File;
78
import java.net.URLClassLoader;
89
import java.util.Iterator;
910

1011
public class AgentLoader {
1112
/**
12-
* @param args <jarpath> [pid]
13+
* @param args <jarpath1>[<File.pathSeparator><jarpath2>...] [pid]
1314
*
1415
* jarpath is the absolute path this directory:
1516
*
@@ -28,7 +29,7 @@ public class AgentLoader {
2829
*/
2930
public static void main(String[] args) throws Exception {
3031
if (args.length != 1 && args.length != 2) {
31-
System.out.println("Arguments: <jarpath> [pid]");
32+
System.out.println("Arguments: <jarpath1>[<File.pathSeparator><jarpath2>...] [pid]");
3233
return;
3334
}
3435

@@ -39,9 +40,9 @@ public static void main(String[] args) throws Exception {
3940
return;
4041
}
4142

42-
String jarpath = args[0];
43-
String pid = args[1];
44-
loadAgent(jarpath, pid);
43+
String jarpaths = args[0];
44+
String pid = args[1];
45+
loadAgent(jarpaths, pid);
4546
}
4647

4748
/**
@@ -75,12 +76,13 @@ private static void listJvmProcesses() {
7576
}
7677
}
7778

78-
private static void loadAgent(String jarpath, String pid) throws Exception {
79-
final String agentJar = Classpath.findJar(jarpath, "scalive");
79+
private static void loadAgent(String jarpaths, String pid) throws Exception {
80+
final String[] ary = jarpaths.split(File.pathSeparator);
81+
final String agentJar = Classpath.findJar(ary, "scalive");
8082
final VirtualMachine vm = VirtualMachine.attach(pid);
8183
final int port = Client.getFreePort();
8284

83-
vm.loadAgent(agentJar, jarpath + " " + port);
85+
vm.loadAgent(agentJar, jarpaths + " " + port);
8486
Runtime.getRuntime().addShutdownHook(new Thread() {
8587
@Override public void run() {
8688
try {

src/main/java/scalive/Classpath.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ private static Method getAddURL() {
2727
*
2828
* @param jarbase Ex: "scalive-agent", not "scalive-agent-1.0.jar"
2929
*/
30-
public static String findJar(String jarpath, String jarbase) throws Exception {
31-
File dir = new File(jarpath);
32-
File[] files = dir.listFiles();
30+
public static String findJar(String[] jarpaths, String jarbase) throws Exception {
31+
for (String jarpath: jarpaths) {
32+
File dir = new File(jarpath);
33+
File[] files = dir.listFiles();
3334

34-
for (int i = 0; i < files.length; i++) {
35-
File file = files[i];
36-
String name = file.getName();
37-
if (file.isFile() && name.endsWith(".jar") && name.startsWith(jarbase))
38-
return file.getPath();
35+
for (int i = 0; i < files.length; i++) {
36+
File file = files[i];
37+
String name = file.getName();
38+
if (file.isFile() && name.endsWith(".jar") && name.startsWith(jarbase))
39+
return file.getPath();
40+
}
3941
}
40-
throw new Exception("Could not find " + jarbase + " in " + jarpath);
42+
43+
throw new Exception("Could not find " + jarbase + " in " + join(jarpaths, File.pathSeparator));
4144
}
4245

4346
public static void addPath(URLClassLoader cl, String path) throws Exception {
@@ -46,36 +49,40 @@ public static void addPath(URLClassLoader cl, String path) throws Exception {
4649
}
4750

4851
/** Combination of findJar and addPath. */
49-
public static void findAndAddJar(URLClassLoader cl, String jarpath, String jarbase) throws Exception {
50-
String jar = findJar(jarpath, jarbase);
52+
public static void findAndAddJar(URLClassLoader cl, String[] jarpaths, String jarbase) throws Exception {
53+
String jar = findJar(jarpaths, jarbase);
5154
addPath(cl, jar);
5255
}
5356

5457
public static void addJarToURLClassLoader(
55-
URLClassLoader cl, String jarpath, String jarbase, String representativeClass
58+
URLClassLoader cl, String[] jarpaths, String jarbase, String representativeClass
5659
) throws Exception {
5760
try {
5861
Class.forName(representativeClass, true, cl);
5962
} catch (ClassNotFoundException e) {
6063
System.out.println("[Scalive] Load " + jarbase);
61-
Classpath.findAndAddJar(cl, jarpath, jarbase);
64+
Classpath.findAndAddJar(cl, jarpaths, jarbase);
6265
}
6366
}
6467

6568
// http://stackoverflow.com/questions/4121567/embedded-scala-repl-inherits-parent-classpath
6669
public static String getURLClasspath(URLClassLoader cl) {
6770
URL[] urls = cl.getURLs();
68-
StringBuffer buf = new StringBuffer();
69-
for (URL url: urls) {
70-
if (buf.length() > 0) buf.append(File.pathSeparator);
71-
buf.append(url);
72-
}
73-
return buf.toString();
71+
return join(urls, File.pathSeparator);
7472
}
7573

7674
public static String getScalaVersion(ClassLoader cl) throws Exception {
7775
Class<?> k = Class.forName("scala.util.Properties", true, cl);
7876
Method m = k.getDeclaredMethod("versionNumberString");
7977
return (String) m.invoke(k);
8078
}
79+
80+
private static String join(Object[] xs, String separator) {
81+
StringBuffer buf = new StringBuffer();
82+
for (Object x: xs) {
83+
if (buf.length() > 0) buf.append(separator);
84+
buf.append(x);
85+
}
86+
return buf.toString();
87+
}
8188
}

src/main/java/scalive/Server.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Server {
1111
// Load this Scala version if Scala has not been loaded in the target process
1212
private static final String DEFAULT_SCALA_VERSION = "2.10.3";
1313

14-
public static void serve(Socket client, String jarpath) throws Exception {
14+
public static void serve(Socket client, String[] jarpaths) throws Exception {
1515
InputStream in = client.getInputStream();
1616
OutputStream out = client.getOutputStream();
1717

@@ -25,7 +25,7 @@ public static void serve(Socket client, String jarpath) throws Exception {
2525

2626
try {
2727
URLClassLoader cl = (URLClassLoader) ClassLoader.getSystemClassLoader();
28-
addJarsToURLClassLoader(cl, jarpath);
28+
addJarsToURLClassLoader(cl, jarpaths);
2929

3030
String classpath = Classpath.getURLClasspath(cl);
3131
Class<?> repl = Class.forName("scalive.Repl");
@@ -40,16 +40,16 @@ public static void serve(Socket client, String jarpath) throws Exception {
4040
}
4141
}
4242

43-
private static void addJarsToURLClassLoader(URLClassLoader cl, String jarpath) throws Exception {
43+
private static void addJarsToURLClassLoader(URLClassLoader cl, String[] jarpaths) throws Exception {
4444
// Try scala-library first
45-
Classpath.addJarToURLClassLoader(cl, jarpath, "scala-library-" + DEFAULT_SCALA_VERSION, "scala.AnyVal");
45+
Classpath.addJarToURLClassLoader(cl, jarpaths, "scala-library-" + DEFAULT_SCALA_VERSION, "scala.AnyVal");
4646

4747
// So that we can get the actual Scala version being used
4848
String version = Classpath.getScalaVersion(cl);
4949

50-
Classpath.addJarToURLClassLoader(cl, jarpath, "scala-reflect-" + version, "scala.reflect.runtime.JavaUniverse");
51-
Classpath.addJarToURLClassLoader(cl, jarpath, "scala-compiler-" + version, "scala.tools.nsc.interpreter.ILoop");
50+
Classpath.addJarToURLClassLoader(cl, jarpaths, "scala-reflect-" + version, "scala.reflect.runtime.JavaUniverse");
51+
Classpath.addJarToURLClassLoader(cl, jarpaths, "scala-compiler-" + version, "scala.tools.nsc.interpreter.ILoop");
5252

53-
Classpath.addJarToURLClassLoader(cl, jarpath, "scalive", "scalive.Repl");
53+
Classpath.addJarToURLClassLoader(cl, jarpaths, "scalive", "scalive.Repl");
5454
}
5555
}

0 commit comments

Comments
 (0)