commit | author | age
|
5fe7df
|
1 |
package com.gitblit;
|
JM |
2 |
|
|
3 |
import java.io.File;
|
|
4 |
import java.io.FileFilter;
|
|
5 |
import java.io.IOException;
|
|
6 |
import java.lang.reflect.Method;
|
|
7 |
import java.net.URL;
|
|
8 |
import java.net.URLClassLoader;
|
|
9 |
import java.security.ProtectionDomain;
|
|
10 |
import java.util.ArrayList;
|
|
11 |
import java.util.Arrays;
|
|
12 |
import java.util.List;
|
|
13 |
|
|
14 |
/**
|
|
15 |
* Launch helper class that adds all jars found in the local "lib" folder and
|
|
16 |
* then calls the application main. Using this technique we do not have to
|
|
17 |
* specify a classpath and we can dynamically add jars to the distribution.
|
|
18 |
*
|
|
19 |
*/
|
|
20 |
public class Launcher {
|
|
21 |
|
|
22 |
public final static boolean debug = false;
|
|
23 |
|
|
24 |
public static void main(String[] args) {
|
|
25 |
if (debug)
|
|
26 |
System.out.println("jcp=" + System.getProperty("java.class.path"));
|
|
27 |
|
|
28 |
ProtectionDomain protectionDomain = Launcher.class.getProtectionDomain();
|
|
29 |
final String launchJar = protectionDomain.getCodeSource().getLocation().toExternalForm();
|
|
30 |
if (debug)
|
|
31 |
System.out.println("launcher=" + launchJar);
|
|
32 |
|
|
33 |
Build.runtime();
|
|
34 |
|
|
35 |
// Load the JARs in the lib and ext folder
|
|
36 |
String[] folders = new String[] { "lib", "ext" };
|
|
37 |
List<File> jars = new ArrayList<File>();
|
|
38 |
for (String folder : folders) {
|
|
39 |
if (folder == null)
|
|
40 |
continue;
|
|
41 |
File libFolder = new File(folder);
|
|
42 |
if (!libFolder.exists())
|
|
43 |
continue;
|
|
44 |
try {
|
|
45 |
libFolder = libFolder.getCanonicalFile();
|
|
46 |
} catch (IOException iox) {
|
|
47 |
}
|
|
48 |
jars.addAll(findJars(libFolder));
|
|
49 |
}
|
|
50 |
|
|
51 |
if (jars.size() == 0) {
|
|
52 |
for (String folder : folders) {
|
|
53 |
File libFolder = new File(folder);
|
|
54 |
System.err.println("Failed to find any JARs in " + libFolder.getPath());
|
|
55 |
}
|
|
56 |
System.exit(-1);
|
|
57 |
} else {
|
|
58 |
for (File jar : jars) {
|
|
59 |
try {
|
|
60 |
addJarFile(jar);
|
|
61 |
} catch (Throwable t) {
|
|
62 |
t.printStackTrace();
|
|
63 |
}
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
// Start Server
|
|
68 |
GitBlitServer.main(args);
|
|
69 |
}
|
|
70 |
|
|
71 |
public static List<File> findJars(File folder) {
|
|
72 |
List<File> jars = new ArrayList<File>();
|
|
73 |
if (folder.exists()) {
|
|
74 |
File[] libs = folder.listFiles(new FileFilter() {
|
|
75 |
@Override
|
|
76 |
public boolean accept(File file) {
|
|
77 |
return !file.isDirectory() && file.getName().toLowerCase().endsWith(".jar");
|
|
78 |
}
|
|
79 |
});
|
|
80 |
if (libs != null && libs.length > 0) {
|
|
81 |
jars.addAll(Arrays.asList(libs));
|
|
82 |
if (debug) {
|
|
83 |
for (File jar : jars)
|
|
84 |
System.out.println("found " + jar);
|
|
85 |
}
|
|
86 |
}
|
|
87 |
}
|
|
88 |
return jars;
|
|
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Parameters of the method to add an URL to the System classes.
|
|
93 |
*/
|
|
94 |
private static final Class<?>[] parameters = new Class[] { URL.class };
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Adds a file to the classpath
|
|
98 |
*
|
|
99 |
* @param f
|
|
100 |
* the file to be added
|
|
101 |
* @throws IOException
|
|
102 |
*/
|
|
103 |
public static void addJarFile(File f) throws IOException {
|
cf9550
|
104 |
if (f.getName().indexOf("-sources") > -1 || f.getName().indexOf("-javadoc") > -1) {
|
JM |
105 |
// don't add source or javadoc jars to runtime classpath
|
|
106 |
return;
|
|
107 |
}
|
5fe7df
|
108 |
URL u = f.toURI().toURL();
|
JM |
109 |
if (debug)
|
|
110 |
System.out.println("load=" + u.toExternalForm());
|
|
111 |
URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
|
|
112 |
Class<?> sysclass = URLClassLoader.class;
|
|
113 |
try {
|
|
114 |
Method method = sysclass.getDeclaredMethod("addURL", parameters);
|
|
115 |
method.setAccessible(true);
|
|
116 |
method.invoke(sysloader, new Object[] { u });
|
|
117 |
} catch (Throwable t) {
|
|
118 |
throw new IOException("Error, could not add " + f.getPath() + " to system classloader", t);
|
|
119 |
}
|
|
120 |
}
|
|
121 |
}
|