diff options
Diffstat (limited to 'src/com/gitblit/Launcher.java')
-rw-r--r-- | src/com/gitblit/Launcher.java | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/com/gitblit/Launcher.java b/src/com/gitblit/Launcher.java new file mode 100644 index 00000000..a55056d5 --- /dev/null +++ b/src/com/gitblit/Launcher.java @@ -0,0 +1,117 @@ +package com.gitblit;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.security.ProtectionDomain;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Launch helper class that adds all jars found in the local "lib" folder and
+ * then calls the application main. Using this technique we do not have to
+ * specify a classpath and we can dynamically add jars to the distribution.
+ *
+ */
+public class Launcher {
+
+ public final static boolean debug = false;
+
+ public static void main(String[] args) {
+ if (debug)
+ System.out.println("jcp=" + System.getProperty("java.class.path"));
+
+ ProtectionDomain protectionDomain = Launcher.class.getProtectionDomain();
+ final String launchJar = protectionDomain.getCodeSource().getLocation().toExternalForm();
+ if (debug)
+ System.out.println("launcher=" + launchJar);
+
+ Build.runtime();
+
+ // Load the JARs in the lib and ext folder
+ String[] folders = new String[] { "lib", "ext" };
+ List<File> jars = new ArrayList<File>();
+ for (String folder : folders) {
+ if (folder == null)
+ continue;
+ File libFolder = new File(folder);
+ if (!libFolder.exists())
+ continue;
+ try {
+ libFolder = libFolder.getCanonicalFile();
+ } catch (IOException iox) {
+ }
+ jars.addAll(findJars(libFolder));
+ }
+
+ if (jars.size() == 0) {
+ for (String folder : folders) {
+ File libFolder = new File(folder);
+ System.err.println("Failed to find any JARs in " + libFolder.getPath());
+ }
+ System.exit(-1);
+ } else {
+ for (File jar : jars) {
+ try {
+ addJarFile(jar);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ }
+ }
+
+ // Start Server
+ GitBlitServer.main(args);
+ }
+
+ public static List<File> findJars(File folder) {
+ List<File> jars = new ArrayList<File>();
+ if (folder.exists()) {
+ File[] libs = folder.listFiles(new FileFilter() {
+ @Override
+ public boolean accept(File file) {
+ return !file.isDirectory() && file.getName().toLowerCase().endsWith(".jar");
+ }
+ });
+ if (libs != null && libs.length > 0) {
+ jars.addAll(Arrays.asList(libs));
+ if (debug) {
+ for (File jar : jars)
+ System.out.println("found " + jar);
+ }
+ }
+ }
+ return jars;
+ }
+
+ /**
+ * Parameters of the method to add an URL to the System classes.
+ */
+ private static final Class<?>[] parameters = new Class[] { URL.class };
+
+ /**
+ * Adds a file to the classpath
+ *
+ * @param f
+ * the file to be added
+ * @throws IOException
+ */
+ public static void addJarFile(File f) throws IOException {
+ URL u = f.toURI().toURL();
+ if (debug)
+ System.out.println("load=" + u.toExternalForm());
+ URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
+ Class<?> sysclass = URLClassLoader.class;
+ try {
+ Method method = sysclass.getDeclaredMethod("addURL", parameters);
+ method.setAccessible(true);
+ method.invoke(sysloader, new Object[] { u });
+ } catch (Throwable t) {
+ throw new IOException("Error, could not add " + f.getPath() + " to system classloader", t);
+ }
+ }
+}
|