/** * Returns JAR archive structure. * * @param jarClass any class within the JAR * @param allowedExtensions list of extension filters * @param allowedPackgages list of allowed packages * @param listener jar download listener * @return JAR archive structure */ public static JarStructure getJarStructure( final Class jarClass, final List<String> allowedExtensions, final List<String> allowedPackgages, final FileDownloadListener listener) { try { final CodeSource src = jarClass.getProtectionDomain().getCodeSource(); if (src != null) { // Creating structure // Source url final URL jarUrl = src.getLocation(); final URI uri = jarUrl.toURI(); // Source file final File jarFile; final String scheme = uri.getScheme(); if (scheme != null && scheme.equalsIgnoreCase("file")) { // Local jar-file jarFile = new File(uri); } else { // Remote jar-file jarFile = FileUtils.downloadFile( jarUrl.toString(), File.createTempFile("jar_file", ".tmp"), listener); } // Creating final JarEntry jarEntry = new JarEntry(JarEntryType.jarEntry, jarFile.getName()); final JarStructure jarStructure = new JarStructure(jarEntry); jarStructure.setJarLocation(jarFile.getAbsolutePath()); // Reading all entries and parsing them into structure final ZipInputStream zip = new ZipInputStream(jarUrl.openStream()); ZipEntry zipEntry; while ((zipEntry = zip.getNextEntry()) != null) { final String entryName = zipEntry.getName(); if (isAllowedPackage(entryName, allowedPackgages) && (zipEntry.isDirectory() || isAllowedExtension(entryName, allowedExtensions))) { parseElement(jarEntry, entryName, zipEntry); } } zip.close(); return jarStructure; } } catch (final IOException e) { FlatLafLogger.error(ReflectUtils.class, e); } catch (final URISyntaxException e) { FlatLafLogger.error(ReflectUtils.class, e); } return null; }
/** * Returns JAR location File for the specified class. * * @param jarClass any class from that JAR * @return JAR location File */ public static File getJarLocationFile(final Class jarClass) { try { final CodeSource src = jarClass.getProtectionDomain().getCodeSource(); if (src != null) { final URL jarUrl = src.getLocation(); final URI uri = jarUrl.toURI(); final String scheme = uri.getScheme(); if (scheme != null && scheme.equalsIgnoreCase("file")) { return new File(uri); } } } catch (final URISyntaxException e) { FlatLafLogger.error(ReflectUtils.class, e); } return null; }