1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
package mon.test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ResourceBundle;
public class ClassPathBuilder {
/**
* Les librairies à ajouter au Classpath sous tous les systèmes
*/
private static String[] requiredLibs;
/**
* Les librairies à rajouter au classpath quand on est sous Windows
*/
private static String[] win32Libs;
/**
* Les librairies à rajouter au classpath quand on est sous Mac
*/
private static String[] macLibs;
static
{
// Initialisation des librairies
ResourceBundle rb = ResourceBundle.getBundle("classpath");
int reqLibCount = Integer.parseInt(rb.getString("requiredLibs.count"));
requiredLibs = new String[reqLibCount];
for (int i = 0; i< reqLibCount; i++)
{
requiredLibs[i] = rb.getString("requiredLib." + i);
}
int win32LibCount = Integer.parseInt(rb.getString("win32Libs.count"));
win32Libs = new String[win32LibCount];
for (int i = 0; i< win32LibCount; i++)
{
win32Libs[i] = rb.getString("win32Lib." + i);
}
int macLibCount = Integer.parseInt(rb.getString("macLibs.count"));
macLibs = new String[macLibCount];
for (int i = 0; i< macLibCount; i++)
{
macLibs[i] = rb.getString("macLib." + i);
}
}
private static final boolean isOnWindows = (System.getProperty("os.name").indexOf("Win") != -1);
private static final boolean isOnMac = (System.getProperty("os.name").indexOf("Mac") != -1);
private static HashMap<String,File> foundLibs = null;
private static File trouverLibrairie(String nomLib)
{
File fichierLib = new File(nomLib);
if (fichierLib.exists())
{
return fichierLib;
}
else
{
fichierLib = new File("./lib/" + nomLib);
if (fichierLib.exists())
{
return fichierLib;
}
}
System.err.println(nomLib + " requise mais non trouvee");
return null;
}
private static HashMap<String,File> getFoundLibs()
{
if (foundLibs != null) return foundLibs;
foundLibs = new HashMap<String,File>();
int numLibs = requiredLibs.length;
if (isOnWindows) numLibs += win32Libs.length;
if (isOnMac) numLibs += macLibs.length;
// Chargement des librairies communes
for (int i=0; i < requiredLibs.length; i++)
{
File f = trouverLibrairie(requiredLibs[i]);
if (f != null) foundLibs.put(requiredLibs[i], f);
}
// Et des librairies Windows s'il y a lieu
if (isOnWindows)
{
for (int i=0 ; i < win32Libs.length ; i++)
{
File f = trouverLibrairie(win32Libs[i]);
if (f != null) foundLibs.put(win32Libs[i], f);
}
}
// Et des librairies Mac s'il y a lieu
if (isOnMac)
{
for (int i=0 ; i < macLibs.length ; i++)
{
File f = trouverLibrairie(macLibs[i]);
if (f != null) foundLibs.put(macLibs[i], f);
}
}
return foundLibs;
}
/**
* @return Le ClassLoader initialisé avec le classpath convenable pour l'application
*
*/
public static ClassLoader getClassLoader()
{
URL[] urls = new URL[getFoundLibs().size()];
Iterator<File> fichiers = getFoundLibs().values().iterator();
for (int i=0; i < urls.length; i++)
{
try
{
urls[i] = fichiers.next().toURL();
} catch (MalformedURLException e) {}
}
// Et on crée le ClassLoader avec les jars en paramètres
ClassLoader parent = Thread.currentThread().getContextClassLoader();
if (parent == null) {
parent = ClassPathBuilder.class.getClassLoader();
}
if (parent == null) {
parent = ClassLoader.getSystemClassLoader();
}
return new URLClassLoader(urls, parent);
}
public static String getStringValue()
{
StringBuilder sb = new StringBuilder();
Iterator<File> fichiers = getFoundLibs().values().iterator();
if (fichiers.hasNext()) {
sb.append( fichiers.next().getPath() );
}
while (fichiers.hasNext()) {
sb.append(File.pathSeparatorChar).append( fichiers.next().getPath() );
}
return sb.toString();
}
} |
Partager