/** * Determine whether the Class identified by the supplied name is present and can be loaded. * @param className The name of the class to check * @param classLoader Class loader to use, <code>null</code> to use default * @return <code>false</code> if either the class or one of its dependencies is not present or cannot be loaded, * <code>true</code> otherwise */ public static boolean isPresent(String className, ClassLoader classLoader) { try { forName(className, classLoader); return true; } catch (@SuppressWarnings("unused") Throwable ex) { // Class or one of its dependencies is not present... return false; } }
/** * Create class instance using given class name and {@link #getDefaultClassLoader()} ClassLoader * @param name Class name * @return Class instance * @throws ClassNotFoundException Class definition with given name was not found */ public static Class<?> forName(String name) throws ClassNotFoundException { return forName(name, getDefaultClassLoader()); }
/** * Get the Mongo driver version informations. * @param classLoader ClassLoader to use (not null) * @return Mongo driver version informations */ public static MongoVersion getMongoVersion(ClassLoader classLoader) { ObjectUtils.argumentNotNull(classLoader, "ClassLoader must be not null"); return MONGO_VERSIONS.computeIfAbsent(classLoader, cl -> { try { final Class<?> cls = ClassUtils.forName("com.mongodb.internal.build.MongoDriverVersion", cl); final Field fld = cls.getDeclaredField("VERSION"); Object value = fld.get(null); if (value != null && value instanceof String) { return new DefaultMongoVersion(true, (String) value); } } catch (Exception e) { LOGGER.warn("Failed to detect Mongo driver version"); LOGGER.debug(() -> "Failed to detect Mongo driver version using MongoDriverVersion class", e); } return new DefaultMongoVersion(false, null); }); }