1
1
package scalive ;
2
2
3
3
import java .io .File ;
4
+ import java .lang .reflect .InvocationTargetException ;
4
5
import java .lang .reflect .Method ;
6
+ import java .net .MalformedURLException ;
5
7
import java .net .URL ;
6
8
import java .net .URLClassLoader ;
7
9
import java .util .Arrays ;
8
10
9
11
public class Classpath {
10
- private static Method addURL = getAddURL ();
12
+ private static final Method addURL = getAddURL ();
11
13
12
14
// http://stackoverflow.com/questions/8222976/why-urlclassloader-addurl-protected-in-java
13
15
private static Method getAddURL () {
14
16
try {
15
17
Method method = URLClassLoader .class .getDeclaredMethod ("addURL" , URL .class );
16
18
method .setAccessible (true );
17
19
return method ;
18
- } catch (Exception e ) {
19
- e .printStackTrace ();
20
- return null ;
20
+ } catch (NoSuchMethodException e ) {
21
+ throw new RuntimeException (e );
21
22
}
22
23
}
23
24
@@ -30,7 +31,7 @@ private static Method getAddURL() {
30
31
*
31
32
* @param jarPrefix JAR file name prefix to search; example: "scalive-agent" will match "scalive-agent-xxx.jar"
32
33
*/
33
- public static String findJar (String [] jarSearchDirs , String jarPrefix ) throws Exception {
34
+ public static String findJar (String [] jarSearchDirs , String jarPrefix ) throws IllegalStateException {
34
35
String maxBaseName = null ;
35
36
File maxFile = null ;
36
37
for (String jarSearchDir : jarSearchDirs ) {
@@ -50,22 +51,26 @@ public static String findJar(String[] jarSearchDirs, String jarPrefix) throws Ex
50
51
}
51
52
52
53
if (maxFile == null )
53
- throw new Exception ("Could not find " + jarPrefix + " in " + join (jarSearchDirs , File .pathSeparator ));
54
+ throw new IllegalStateException ("Could not find " + jarPrefix + " in " + join (jarSearchDirs , File .pathSeparator ));
54
55
else
55
56
return maxFile .getPath ();
56
57
}
57
58
58
- public static void addPath (URLClassLoader cl , String path ) throws Exception {
59
+ public static void addPath (
60
+ URLClassLoader cl , String path
61
+ ) throws MalformedURLException , InvocationTargetException , IllegalAccessException {
59
62
URL url = new File (path ).toURI ().toURL ();
60
63
URL [] urls = cl .getURLs ();
61
64
if (!Arrays .asList (urls ).contains (url )) addURL .invoke (cl , url );
62
65
}
63
66
64
67
/** Combination of {@link #findJar(String[], String)} and {@link #addPath(URLClassLoader, String)}. */
65
- public static void findAndAddJar (URLClassLoader cl , String [] jarSearchDirs , String jarPrefix ) throws Exception {
68
+ public static void findAndAddJar (
69
+ URLClassLoader cl , String [] jarSearchDirs , String jarPrefix
70
+ ) throws IllegalAccessException , InvocationTargetException , MalformedURLException {
66
71
String jar = findJar (jarSearchDirs , jarPrefix );
67
72
addPath (cl , jar );
68
- System . out . println ( "[Scalive] Load " + jar );
73
+ Log . log ( " Load " + jar );
69
74
}
70
75
71
76
/**
@@ -74,7 +79,7 @@ public static void findAndAddJar(URLClassLoader cl, String[] jarSearchDirs, Stri
74
79
*/
75
80
public static void findAndAddJar (
76
81
URLClassLoader cl , String representativeClass , String [] jarSearchDirs , String jarPrefix
77
- ) throws Exception {
82
+ ) throws IllegalAccessException , MalformedURLException , InvocationTargetException {
78
83
try {
79
84
Class .forName (representativeClass , true , cl );
80
85
} catch (ClassNotFoundException e ) {
@@ -88,7 +93,9 @@ public static String getClasspath(URLClassLoader cl) {
88
93
return join (urls , File .pathSeparator );
89
94
}
90
95
91
- public static String getScalaVersion (ClassLoader cl ) throws Exception {
96
+ public static String getScalaVersion (
97
+ ClassLoader cl
98
+ ) throws ClassNotFoundException , NoSuchMethodException , InvocationTargetException , IllegalAccessException {
92
99
Class <?> k = Class .forName ("scala.util.Properties" , true , cl );
93
100
Method m = k .getDeclaredMethod ("versionNumberString" );
94
101
return (String ) m .invoke (k );
0 commit comments