commit | author | age
|
f13c4c
|
1 |
/*
|
JM |
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 |
*/
|
5fe7df
|
16 |
package com.gitblit;
|
JM |
17 |
|
|
18 |
import java.io.File;
|
|
19 |
import java.io.FileFilter;
|
|
20 |
import java.io.IOException;
|
|
21 |
import java.lang.reflect.Method;
|
|
22 |
import java.net.URL;
|
|
23 |
import java.net.URLClassLoader;
|
|
24 |
import java.security.ProtectionDomain;
|
2a7306
|
25 |
import java.text.MessageFormat;
|
5fe7df
|
26 |
import java.util.ArrayList;
|
JM |
27 |
import java.util.Arrays;
|
9b72a2
|
28 |
import java.util.Collections;
|
5fe7df
|
29 |
import java.util.List;
|
JM |
30 |
|
22fc5e
|
31 |
import com.gitblit.build.Build;
|
JM |
32 |
|
5fe7df
|
33 |
/**
|
892570
|
34 |
* Launch helper class that adds all jars found in the local "lib" & "ext"
|
JM |
35 |
* folders and then calls the application main. Using this technique we do not
|
|
36 |
* have to specify a classpath and we can dynamically add jars to the
|
|
37 |
* distribution.
|
|
38 |
*
|
|
39 |
* This class also downloads all runtime dependencies, if they are not found.
|
|
40 |
*
|
|
41 |
* @author James Moger
|
5fe7df
|
42 |
*
|
JM |
43 |
*/
|
|
44 |
public class Launcher {
|
|
45 |
|
2a7306
|
46 |
public static final boolean DEBUG = false;
|
JM |
47 |
|
|
48 |
/**
|
|
49 |
* Parameters of the method to add an URL to the System classes.
|
|
50 |
*/
|
|
51 |
private static final Class<?>[] PARAMETERS = new Class[] { URL.class };
|
5fe7df
|
52 |
|
JM |
53 |
public static void main(String[] args) {
|
2a7306
|
54 |
if (DEBUG) {
|
5fe7df
|
55 |
System.out.println("jcp=" + System.getProperty("java.class.path"));
|
2a7306
|
56 |
ProtectionDomain protectionDomain = Launcher.class.getProtectionDomain();
|
JM |
57 |
System.out.println("launcher="
|
|
58 |
+ protectionDomain.getCodeSource().getLocation().toExternalForm());
|
|
59 |
}
|
5fe7df
|
60 |
|
892570
|
61 |
// download all runtime dependencies
|
5fe7df
|
62 |
Build.runtime();
|
JM |
63 |
|
|
64 |
// Load the JARs in the lib and ext folder
|
|
65 |
String[] folders = new String[] { "lib", "ext" };
|
|
66 |
List<File> jars = new ArrayList<File>();
|
|
67 |
for (String folder : folders) {
|
2a7306
|
68 |
if (folder == null) {
|
5fe7df
|
69 |
continue;
|
JM |
70 |
}
|
2a7306
|
71 |
File libFolder = new File(folder);
|
JM |
72 |
if (!libFolder.exists()) {
|
|
73 |
continue;
|
|
74 |
}
|
|
75 |
List<File> found = findJars(libFolder.getAbsoluteFile());
|
|
76 |
jars.addAll(found);
|
5fe7df
|
77 |
}
|
9b72a2
|
78 |
// sort the jars by name and then reverse the order so the newer version
|
JM |
79 |
// of the library gets loaded in the event that this is an upgrade
|
|
80 |
Collections.sort(jars);
|
|
81 |
Collections.reverse(jars);
|
5fe7df
|
82 |
|
JM |
83 |
if (jars.size() == 0) {
|
|
84 |
for (String folder : folders) {
|
|
85 |
File libFolder = new File(folder);
|
831469
|
86 |
// this is a test of adding a comment
|
JM |
87 |
// more really interesting things
|
|
88 |
System.err.println("Failed to find any really cool JARs in " + libFolder.getPath());
|
5fe7df
|
89 |
}
|
JM |
90 |
System.exit(-1);
|
|
91 |
} else {
|
|
92 |
for (File jar : jars) {
|
|
93 |
try {
|
831469
|
94 |
jar.canRead();
|
5fe7df
|
95 |
addJarFile(jar);
|
JM |
96 |
} catch (Throwable t) {
|
|
97 |
t.printStackTrace();
|
|
98 |
}
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
|
102 |
// Start Server
|
|
103 |
GitBlitServer.main(args);
|
|
104 |
}
|
|
105 |
|
|
106 |
public static List<File> findJars(File folder) {
|
|
107 |
List<File> jars = new ArrayList<File>();
|
|
108 |
if (folder.exists()) {
|
|
109 |
File[] libs = folder.listFiles(new FileFilter() {
|
|
110 |
@Override
|
|
111 |
public boolean accept(File file) {
|
|
112 |
return !file.isDirectory() && file.getName().toLowerCase().endsWith(".jar");
|
|
113 |
}
|
|
114 |
});
|
|
115 |
if (libs != null && libs.length > 0) {
|
|
116 |
jars.addAll(Arrays.asList(libs));
|
2a7306
|
117 |
if (DEBUG) {
|
JM |
118 |
for (File jar : jars) {
|
5fe7df
|
119 |
System.out.println("found " + jar);
|
2a7306
|
120 |
}
|
5fe7df
|
121 |
}
|
JM |
122 |
}
|
|
123 |
}
|
831469
|
124 |
|
5fe7df
|
125 |
return jars;
|
JM |
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Adds a file to the classpath
|
|
130 |
*
|
|
131 |
* @param f
|
|
132 |
* the file to be added
|
|
133 |
* @throws IOException
|
|
134 |
*/
|
|
135 |
public static void addJarFile(File f) throws IOException {
|
cf9550
|
136 |
if (f.getName().indexOf("-sources") > -1 || f.getName().indexOf("-javadoc") > -1) {
|
JM |
137 |
// don't add source or javadoc jars to runtime classpath
|
|
138 |
return;
|
|
139 |
}
|
5fe7df
|
140 |
URL u = f.toURI().toURL();
|
2a7306
|
141 |
if (DEBUG) {
|
5fe7df
|
142 |
System.out.println("load=" + u.toExternalForm());
|
2a7306
|
143 |
}
|
5fe7df
|
144 |
URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
|
JM |
145 |
Class<?> sysclass = URLClassLoader.class;
|
|
146 |
try {
|
2a7306
|
147 |
Method method = sysclass.getDeclaredMethod("addURL", PARAMETERS);
|
5fe7df
|
148 |
method.setAccessible(true);
|
JM |
149 |
method.invoke(sysloader, new Object[] { u });
|
|
150 |
} catch (Throwable t) {
|
2a7306
|
151 |
throw new IOException(MessageFormat.format(
|
JM |
152 |
"Error, could not add {0} to system classloader", f.getPath()), t);
|
5fe7df
|
153 |
}
|
JM |
154 |
}
|
|
155 |
}
|