You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Launcher.java 4.0KB

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