Kensuke Matsuzaki
2013-04-15 ed566a162c780faa0d6c84fac8fbbd3738586445
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  */
f6b200 16 package com.gitblit.client;
5fe7df 17
f6b200 18 import java.awt.Color;
JM 19 import java.awt.EventQueue;
20 import java.awt.FontMetrics;
21 import java.awt.Graphics2D;
22 import java.awt.SplashScreen;
5fe7df 23 import java.io.File;
JM 24 import java.io.FileFilter;
25 import java.io.IOException;
26 import java.lang.reflect.Method;
27 import java.net.URL;
28 import java.net.URLClassLoader;
2a7306 29 import java.text.MessageFormat;
5fe7df 30 import java.util.ArrayList;
JM 31 import java.util.Arrays;
9b72a2 32 import java.util.Collections;
5fe7df 33 import java.util.List;
JM 34
f6b200 35 import com.gitblit.Constants;
22fc5e 36
5fe7df 37 /**
f6b200 38  * Downloads dependencies and launches Gitblit Manager.
892570 39  * 
JM 40  * @author James Moger
5fe7df 41  * 
JM 42  */
f6b200 43 public class GitblitManagerLauncher {
5fe7df 44
2a7306 45     public static final boolean DEBUG = false;
JM 46
47     /**
48      * Parameters of the method to add an URL to the System classes.
49      */
50     private static final Class<?>[] PARAMETERS = new Class[] { URL.class };
5fe7df 51
JM 52     public static void main(String[] args) {
f6b200 53         final SplashScreen splash = SplashScreen.getSplashScreen();
JM 54         
55         File libFolder = new File("ext");
56         List<File> jars = findJars(libFolder.getAbsoluteFile());
57         
9b72a2 58         // sort the jars by name and then reverse the order so the newer version
JM 59         // of the library gets loaded in the event that this is an upgrade
60         Collections.sort(jars);
61         Collections.reverse(jars);
f6b200 62         for (File jar : jars) {
JM 63             try {
64                 updateSplash(splash, Translation.get("gb.loading") + " " + jar.getName() + "...");
65                 addJarFile(jar);
66             } catch (IOException e) {
5fe7df 67
JM 68             }
69         }
f6b200 70         
JM 71         updateSplash(splash, Translation.get("gb.starting") + " Gitblit Manager...");
72         GitblitManager.main(args);
5fe7df 73     }
JM 74
f6b200 75     private static void updateSplash(final SplashScreen splash, final String string) {
JM 76         if (splash == null) {
77             return;
78         }
79         try {
80             EventQueue.invokeAndWait(new Runnable() {
81                 public void run() {
82                     Graphics2D g = splash.createGraphics();
83                     if (g != null) {
84                         // Splash is 320x120
85                         FontMetrics fm = g.getFontMetrics();
86                         
87                         // paint startup status
88                         g.setColor(Color.darkGray);
89                         int h = fm.getHeight() + fm.getMaxDescent();
90                         int x = 5;
91                         int y = 115;
92                         int w = 320 - 2 * x;
93                         g.fillRect(x, y - h, w, h);
94                         g.setColor(Color.lightGray);
95                         g.drawRect(x, y - h, w, h);
96                         g.setColor(Color.WHITE);
97                         int xw = fm.stringWidth(string);
98                         g.drawString(string, x + ((w - xw) / 2), y - 5);
99                         
100                         // paint version
101                         String ver = "v" + Constants.getVersion();
102                         int vw = g.getFontMetrics().stringWidth(ver);
103                         g.drawString(ver, 320 - vw - 5, 34);
104                         g.dispose();
105                         splash.update();
106                     }
107                 }
108             });
109         } catch (Throwable t) {
110             t.printStackTrace();
111         }
112     }
113     
5fe7df 114     public static List<File> findJars(File folder) {
JM 115         List<File> jars = new ArrayList<File>();
116         if (folder.exists()) {
117             File[] libs = folder.listFiles(new FileFilter() {
118                 @Override
119                 public boolean accept(File file) {
120                     return !file.isDirectory() && file.getName().toLowerCase().endsWith(".jar");
121                 }
122             });
123             if (libs != null && libs.length > 0) {
124                 jars.addAll(Arrays.asList(libs));
2a7306 125                 if (DEBUG) {
JM 126                     for (File jar : jars) {
5fe7df 127                         System.out.println("found " + jar);
2a7306 128                     }
5fe7df 129                 }
JM 130             }
131         }
831469 132
5fe7df 133         return jars;
JM 134     }
135
136     /**
137      * Adds a file to the classpath
138      * 
139      * @param f
140      *            the file to be added
141      * @throws IOException
142      */
143     public static void addJarFile(File f) throws IOException {
cf9550 144         if (f.getName().indexOf("-sources") > -1 || f.getName().indexOf("-javadoc") > -1) {
JM 145             // don't add source or javadoc jars to runtime classpath
146             return;
147         }
5fe7df 148         URL u = f.toURI().toURL();
2a7306 149         if (DEBUG) {
5fe7df 150             System.out.println("load=" + u.toExternalForm());
2a7306 151         }
5fe7df 152         URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
JM 153         Class<?> sysclass = URLClassLoader.class;
154         try {
2a7306 155             Method method = sysclass.getDeclaredMethod("addURL", PARAMETERS);
5fe7df 156             method.setAccessible(true);
JM 157             method.invoke(sysloader, new Object[] { u });
158         } catch (Throwable t) {
2a7306 159             throw new IOException(MessageFormat.format(
JM 160                     "Error, could not add {0} to system classloader", f.getPath()), t);
5fe7df 161         }
JM 162     }
f6b200 163
5fe7df 164 }