2 package net.sourceforge.guacamole.net.basic;
5 import java.io.FilenameFilter;
6 import java.net.MalformedURLException;
8 import java.net.URLClassLoader;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import net.sourceforge.guacamole.GuacamoleException;
12 import net.sourceforge.guacamole.net.basic.properties.BasicGuacamoleProperties;
13 import net.sourceforge.guacamole.properties.GuacamoleProperties;
16 * Guacamole - Clientless Remote Desktop
17 * Copyright (C) 2010 Michael Jumper
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU Affero General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU Affero General Public License for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 public class GuacamoleClassLoader extends ClassLoader {
35 private URLClassLoader classLoader = null;
37 private static GuacamoleException exception = null;
38 private static GuacamoleClassLoader instance = null;
43 // Attempt to create singleton classloader which loads classes from
44 // all .jar's in the lib directory defined in guacamole.properties
45 instance = new GuacamoleClassLoader(
46 GuacamoleProperties.getProperty(BasicGuacamoleProperties.LIB_DIRECTORY)
50 catch (GuacamoleException e) {
51 // On error, record exception
57 private GuacamoleClassLoader(File libDirectory) throws GuacamoleException {
59 // If no directory provided, just direct requests to parent classloader
60 if (libDirectory == null)
63 // Validate directory is indeed a directory
64 if (!libDirectory.isDirectory())
65 throw new GuacamoleException(libDirectory + " is not a directory.");
67 // Get list of URLs for all .jar's in the lib directory
68 Collection<URL> jarURLs = new ArrayList<URL>();
69 for (File file : libDirectory.listFiles(new FilenameFilter() {
72 public boolean accept(File dir, String name) {
74 // If it ends with .jar, accept the file
75 return name.endsWith(".jar");
83 // Add URL for the .jar to the jar URL list
84 jarURLs.add(file.toURI().toURL());
87 catch (MalformedURLException e) {
88 throw new GuacamoleException(e);
93 // Set delegate classloader to new URLClassLoader which loads from the
96 URL[] urls = new URL[jarURLs.size()];
97 classLoader = new URLClassLoader(
98 jarURLs.toArray(urls),
99 getClass().getClassLoader()
104 public static GuacamoleClassLoader getInstance() throws GuacamoleException {
106 // If instance could not be created, rethrow original exception
107 if (exception != null) throw exception;
114 protected Class<?> findClass(String name) throws ClassNotFoundException {
116 // If no classloader, use super
117 if (classLoader == null)
118 return super.findClass(name);
120 // Otherwise, delegate
121 return classLoader.loadClass(name);