@Test(expected = ClassCastException.class) public void testFindClassesFromJarClassCastException() throws Exception { URL url = Paths.get("target" + File.separator + "test-dependencies" + File.separator + "atlas-model.jar").toUri().toURL(); AtlasUtil.findClassesFromJar(url); }
protected static List<Class<?>> findClassesFromJar(URL jarFileUrl) { List<Class<?>> classNames = new ArrayList<Class<?>>(); JarURLConnection connection = null; try { connection = (JarURLConnection) jarFileUrl.openConnection(); } catch (IOException e) { LOG.warn(String.format("Unable to load classes from jar file=%s msg=%s", jarFileUrl, e.getMessage()), e); return classNames; } try (ZipInputStream zip = new ZipInputStream( new FileInputStream(new File(connection.getJarFileURL().toURI())))) { for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { if (!entry.isDirectory() && entry.getName().endsWith(".class")) { String className = entry.getName().replace('/', '.'); className = className.substring(0, className.length() - ".class".length()); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = AtlasUtil.class.getClassLoader(); } try { Class<?> clazz = Class.forName(className, false, classLoader); classNames.add(clazz); } catch (ClassNotFoundException e) { LOG.warn(String.format("Unable to load class=%s from jar file=%s msg=%s", className, jarFileUrl, e.getMessage()), e); } } } } catch (URISyntaxException | IOException e) { LOG.warn(String.format("Unable to load classes from jar file=%s msg=%s", jarFileUrl, e.getMessage()), e); } return classNames; }
AtlasUtil { protected static List<Class<?>> findClassesFromJar(URL jarFileUrl) { List<Class<?>> classNames = new ArrayList<Class<?>>(); JarURLConnection connection = null; try { connection = (JarURLConnection) jarFileUrl.openConnection(); } catch (IOException e) { LOG.warn(String.format("Unable to load classes from jar file=%s msg=%s", jarFileUrl, e.getMessage()), e); return classNames; } try (ZipInputStream zip = new ZipInputStream( new FileInputStream(new File(connection.getJarFileURL().toURI())))) { for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { if (!entry.isDirectory() && entry.getName().endsWith(".class")) { String className = entry.getName().replace('/', '.'); className = className.substring(0, className.length() - ".class".length()); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = AtlasUtil.class.getClassLoader(); } try { Class<?> clazz = Class.forName(className, false, classLoader); classNames.add(clazz); } catch (ClassNotFoundException e) { LOG.warn(String.format("Unable to load class=%s from jar file=%s msg=%s", className, jarFileUrl, e.getMessage()), e); } } } } catch (URISyntaxException | IOException e) { LOG.warn(String.format("Unable to load classes from jar file=%s msg=%s", jarFileUrl, e.getMessage()), e); } return classNames; } }
AtlasUtil { protected static List<Class<?>> findClassesFromJar(URL jarFileUrl) { List<Class<?>> classNames = new ArrayList<Class<?>>(); JarURLConnection connection = null; try { connection = (JarURLConnection) jarFileUrl.openConnection(); } catch (IOException e) { LOG.warn(String.format("Unable to load classes from jar file=%s msg=%s", jarFileUrl, e.getMessage()), e); return classNames; } try (ZipInputStream zip = new ZipInputStream( new FileInputStream(new File(connection.getJarFileURL().toURI())))) { for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { if (!entry.isDirectory() && entry.getName().endsWith(".class")) { String className = entry.getName().replace('/', '.'); className = className.substring(0, className.length() - ".class".length()); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = AtlasUtil.class.getClassLoader(); } try { Class<?> clazz = Class.forName(className, false, classLoader); classNames.add(clazz); } catch (ClassNotFoundException e) { LOG.warn(String.format("Unable to load class=%s from jar file=%s msg=%s", className, jarFileUrl, e.getMessage()), e); } } } } catch (URISyntaxException | IOException e) { LOG.warn(String.format("Unable to load classes from jar file=%s msg=%s", jarFileUrl, e.getMessage()), e); } return classNames; }  }
AtlasUtil { protected static List<Class<?>> findClassesFromJar(URL jarFileUrl) { List<Class<?>> classNames = new ArrayList<Class<?>>(); JarURLConnection connection = null; try { connection = (JarURLConnection) jarFileUrl.openConnection(); } catch (IOException e) { LOG.warn(String.format("Unable to load classes from jar file=%s msg=%s", jarFileUrl, e.getMessage()), e); return classNames; } try (ZipInputStream zip = new ZipInputStream( new FileInputStream(new File(connection.getJarFileURL().toURI())))) { for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { if (!entry.isDirectory() && entry.getName().endsWith(".class")) { String className = entry.getName().replace('/', '.'); className = className.substring(0, className.length() - ".class".length()); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = AtlasUtil.class.getClassLoader(); } try { Class<?> clazz = Class.forName(className, false, classLoader); classNames.add(clazz); } catch (ClassNotFoundException e) { LOG.warn(String.format("Unable to load class=%s from jar file=%s msg=%s", className, jarFileUrl, e.getMessage()), e); } } } } catch (URISyntaxException | IOException e) { LOG.warn(String.format("Unable to load classes from jar file=%s msg=%s", jarFileUrl, e.getMessage()), e); } return classNames; }  static Properties loadPropertiesFromURL(URL url); static boolean isEmpty(String string); static boolean matchUriModule(String uriA, String uriB); static String getUriScheme(String atlasUri); static String getUriModule(String atlasUri); static String getUriDataType(String atlasUri); static String getUriModuleVersion(String atlasUri); static String getUriParameterValue(String atlasUri, String key); static Map<String, String> getUriParameters(String atlasUri); static int countCharacters(String text, char match); static List<Class<?>> findClassesForPackage(String scannedPackage); static void addAudit(AtlasSession session, String docId, String message, String path, AuditStatus status,
            String value); static Audit createAudit(AuditStatus status, String docId, String docName, String path, String value, String message); static void addAudit(AtlasSession session, Validation validation); static void addAudits(AtlasInternalSession session, String docId, List<Audit> audits); static AuditStatus toAuditStatus(ValidationStatus vstatus); static String getDocumentNameById(AtlasMapping mapping, String docId); static String getChainedMessage(Throwable t); static String escapeForUri(String source); static String unescapeFromUri(String uri); static void deleteDirectory(File targetDir); static void deleteDirectoryContents(File targetDir); static void copyFile(Path sourcePath, Path destPath); }
AtlasUtil { protected static List<Class<?>> findClassesFromJar(URL jarFileUrl) { List<Class<?>> classNames = new ArrayList<Class<?>>(); JarURLConnection connection = null; try { connection = (JarURLConnection) jarFileUrl.openConnection(); } catch (IOException e) { LOG.warn(String.format("Unable to load classes from jar file=%s msg=%s", jarFileUrl, e.getMessage()), e); return classNames; } try (ZipInputStream zip = new ZipInputStream( new FileInputStream(new File(connection.getJarFileURL().toURI())))) { for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) { if (!entry.isDirectory() && entry.getName().endsWith(".class")) { String className = entry.getName().replace('/', '.'); className = className.substring(0, className.length() - ".class".length()); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = AtlasUtil.class.getClassLoader(); } try { Class<?> clazz = Class.forName(className, false, classLoader); classNames.add(clazz); } catch (ClassNotFoundException e) { LOG.warn(String.format("Unable to load class=%s from jar file=%s msg=%s", className, jarFileUrl, e.getMessage()), e); } } } } catch (URISyntaxException | IOException e) { LOG.warn(String.format("Unable to load classes from jar file=%s msg=%s", jarFileUrl, e.getMessage()), e); } return classNames; }  static Properties loadPropertiesFromURL(URL url); static boolean isEmpty(String string); static boolean matchUriModule(String uriA, String uriB); static String getUriScheme(String atlasUri); static String getUriModule(String atlasUri); static String getUriDataType(String atlasUri); static String getUriModuleVersion(String atlasUri); static String getUriParameterValue(String atlasUri, String key); static Map<String, String> getUriParameters(String atlasUri); static int countCharacters(String text, char match); static List<Class<?>> findClassesForPackage(String scannedPackage); static void addAudit(AtlasSession session, String docId, String message, String path, AuditStatus status,
            String value); static Audit createAudit(AuditStatus status, String docId, String docName, String path, String value, String message); static void addAudit(AtlasSession session, Validation validation); static void addAudits(AtlasInternalSession session, String docId, List<Audit> audits); static AuditStatus toAuditStatus(ValidationStatus vstatus); static String getDocumentNameById(AtlasMapping mapping, String docId); static String getChainedMessage(Throwable t); static String escapeForUri(String source); static String unescapeFromUri(String uri); static void deleteDirectory(File targetDir); static void deleteDirectoryContents(File targetDir); static void copyFile(Path sourcePath, Path destPath); static final int SPLIT_LIMIT; static final String NEW_LINE_CHARS; }