private static List<Class<?>> getClassesInDirectory( File directory, String packageName, ClassLoader classLoader) throws UnsupportedEncodingException { List<Class<?>> classes = new ArrayList<Class<?>>(); // Capture all the .class files in this directory // Get the list of the files contained in the package String[] files = directory.list(); for (String currentFile : files) { // we are only interested in .class files if (currentFile.endsWith(".class")) { // removes the .class extension // CHECKSTYLE:OFF try { classes.add( Class.forName( packageName + '.' + currentFile.substring(0, currentFile.length() - 6))); } catch (Throwable e) { // do nothing. this class hasn't been found by the loader, and we don't care. } // CHECKSTYLE:ON } else { // It's another package String subPackageName = packageName + '.' + currentFile; // Ask for all resources for the path URL resource = classLoader.getResource(subPackageName.replace('.', '/')); File subDirectory = new File(URLDecoder.decode(resource.getPath(), "UTF-8")); List<Class<?>> classesForSubPackage = getClassesInDirectory(subDirectory, subPackageName, classLoader); classes.addAll(classesForSubPackage); } } return classes; }
private static List<Class<?>> getClassesInJarFile(String jar, String packageName) throws IOException { List<Class<?>> classes = new ArrayList<Class<?>>(); JarInputStream jarFile = null; jarFile = new JarInputStream(new FileInputStream(jar)); JarEntry jarEntry; while ((jarEntry = jarFile.getNextJarEntry()) != null) { if (jarEntry != null) { String className = jarEntry.getName(); if (className.endsWith(".class")) { className = className.substring(0, className.length() - ".class".length()).replace('/', '.'); if (className.startsWith(packageName)) { // CHECKSTYLE:OFF try { classes.add(Class.forName(className.replace('/', '.'))); } catch (Throwable e) { // do nothing. this class hasn't been found by the loader, and we don't care. } // CHECKSTYLE:ON } } } } closeJarFile(jarFile); return classes; }
public static Class<?> tryToLoadClass(String className) { try { return Class.forName(className); } catch (ClassNotFoundException e) { return null; } }
public DLNAMediaDatabase(String name) { String dir = "database"; dbName = name; File fileDir = new File(dir); if (Platform.isWindows()) { String profileDir = configuration.getProfileDirectory(); url = String.format("jdbc:h2:%s\\%s/%s", profileDir, dir, dbName); fileDir = new File(profileDir, dir); } else { url = Constants.START_URL + dir + "/" + dbName; } dbDir = fileDir.getAbsolutePath(); LOGGER.debug("Using database URL: " + url); LOGGER.info("Using database located at: " + dbDir); try { Class.forName("org.h2.Driver"); } catch (ClassNotFoundException e) { LOGGER.error(null, e); } JdbcDataSource ds = new JdbcDataSource(); ds.setURL(url); ds.setUser("sa"); ds.setPassword(""); cp = JdbcConnectionPool.create(ds); }
public DLNAMediaDatabase(String name) { dbName = name; File profileDirectory = new File(configuration.getProfileDirectory()); dbDir = new File( profileDirectory.isDirectory() ? configuration.getProfileDirectory() : null, "database") .getAbsolutePath(); url = Constants.START_URL + dbDir + File.separator + dbName; LOGGER.debug("Using database URL: " + url); LOGGER.info("Using database located at: " + dbDir); try { Class.forName("org.h2.Driver"); } catch (ClassNotFoundException e) { LOGGER.error(null, e); } JdbcDataSource ds = new JdbcDataSource(); ds.setURL(url); ds.setUser("sa"); ds.setPassword(""); cp = JdbcConnectionPool.create(ds); }