/** * Construct a new PropertyValuePresenterRegistry * @param loadDefaults <code>true</code> to load default {@link PropertyValuePresenter}s from * <code>META-INF/services</code> using <code>com.holonplatform.core.property.PropertyValuePresenter</code> * files using default ClassLoader. Every default presenter will be registered using an always * <code>true</code> condition. */ public DefaultPropertyValuePresenterRegistry(boolean loadDefaults) { this(loadDefaults, ClassUtils.getDefaultClassLoader()); }
/** * Detect the bean validation API version for given ClassLoader. * @param classLoader The ClassLoader to use * @return Bean validation API version, or {@link BeanValidationAPIVersion#UNKNOWN} if not detected */ public static BeanValidationAPIVersion getVersion(ClassLoader classLoader) { if (classLoader != null) { if (ClassUtils.isPresent("javax.validation.Validation", classLoader)) { if (ClassUtils.isPresent("javax.validation.ClockProvider", classLoader)) { return BeanValidationAPIVersion.V_20x; } return BeanValidationAPIVersion.V_1x; } } return BeanValidationAPIVersion.UNKNOWN; }
/** * 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()); }
/** * Load a {@link Properties} instance reading properties key/values from given file name. The file reference is * obtained using default (current thread or system) {@link ClassLoader}. * * <p> * See {@link ClassLoader#getResource(String)} for informations about file name resolution strategy. * </p> * * @param filename File name to read * @param lenient if <code>true</code>, when file is not found returns <code>null</code> without throwing any * exception * @return {@link Properties} instance containing all the properties read from file, if any * @throws IOException Error reading properties file */ public static Properties loadProperties(String filename, boolean lenient) throws IOException { return loadProperties(filename, ClassUtils.getDefaultClassLoader(), lenient); }
/** * 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; } }
/** * Build a {@link ConfigPropertyProvider} using given <code>sourceFileName</code> as property source. * <p> * This method uses {@link ClassLoader#getResourceAsStream(String)} method to load the property stream, using the * same conventions to locate the resource to load. * </p> * @param sourceFileName Property file name * @param classLoader ClassLoader to use. If <code>null</code>, the default current classloader is used * @return ConfigPropertyProvider with given <code>sourceFileName</code> as property source. * @throws IOException File not found or read error */ static ConfigPropertyProvider using(String sourceFileName, ClassLoader classLoader) throws IOException { return using(ClassUtils.loadProperties(sourceFileName, classLoader, false)); }
/** * 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); }); }
/** * Build a {@link ConfigPropertyProvider} using given <code>sourceFileName</code> as property source. * <p> * This method uses {@link ClassLoader#getResourceAsStream(String)} method to load the property stream, using the * same conventions to locate the resource to load. * </p> * @param sourceFileName Property file name * @param classLoader ClassLoader to use. If <code>null</code>, the default current classloader is used * @param lenient <code>true</code> to ignore source loading errors and return a provider with an empty set of * properties * @return ConfigPropertyProvider with given <code>sourceFileName</code> as property source. * @throws IOException File not found or read error */ static ConfigPropertyProvider using(String sourceFileName, ClassLoader classLoader, boolean lenient) throws IOException { return using(ClassUtils.loadProperties(sourceFileName, classLoader, lenient)); }
/** * Construct a new PropertyRendererRegistry * @param loadDefaults <code>true</code> to load default {@link PropertyRenderer}s from * <code>META-INF/services</code> using <code>com.holonplatform.core.property.PropertyRenderer</code> files * using default ClassLoader. Every default renderer will be registered using an always <code>true</code> * condition. */ public DefaultPropertyRendererRegistry(boolean loadDefaults) { this(loadDefaults, ClassUtils.getDefaultClassLoader()); }
/** * Checks whether the <code>Secured</code> annotation is available from classpath. * @param classLoader ClassLoader to use * @return <code>true</code> if present */ private static boolean isSecuredAnnotationsPresent(ClassLoader classLoader) { if (SECURED_ANNOTATION_PRESENT.containsKey(classLoader)) { Boolean present = SECURED_ANNOTATION_PRESENT.get(classLoader); return (present != null && present.booleanValue()); } boolean present = ClassUtils.isPresent("org.springframework.security.access.annotation.Secured", classLoader); SECURED_ANNOTATION_PRESENT.put(classLoader, present); return present; }
/** * Obtains the default {@link ClassLoader} to use. * <p> * By default {@link ClassUtils#getDefaultClassLoader()} is used. * </p> * @return the default {@link ClassLoader} */ public ClassLoader getDefaultClassLoader() { return classLoader == null ? ClassUtils.getDefaultClassLoader() : classLoader; }
/** * Checks whether the JPA API is available from classpath. * @param classLoader ClassLoader to use * @return <code>true</code> if present */ public static boolean isJpaApiPresent(ClassLoader classLoader) { if (JPA_API_PRESENT.containsKey(classLoader)) { Boolean present = JPA_API_PRESENT.get(classLoader); return (present != null && present.booleanValue()); } boolean present = ClassUtils.isPresent("javax.persistence.Persistence", classLoader); JPA_API_PRESENT.put(classLoader, present); return present; }
/** * Create class instance using given class name and classloader * @param name Class name * @param classLoader Class loader to use, or <code>null</code> to use default * @return Class instance * @throws ClassNotFoundException Class definition with given name was not found */ public static Class<?> forName(String name, ClassLoader classLoader) throws ClassNotFoundException { assert name != null : "Class name must be not null"; ClassLoader cl = classLoader; if (cl == null) { cl = getDefaultClassLoader(); } return (cl != null ? cl.loadClass(name) : Class.forName(name)); }
/** * Checks whether bean validation API is available from classpath. * @param classLoader ClassLoader to use * @return <code>true</code> if present */ private static boolean isBeanValidationApiPresent(ClassLoader classLoader) { if (BEAN_VALIDATION_API_PRESENT.containsKey(classLoader)) { Boolean present = BEAN_VALIDATION_API_PRESENT.get(classLoader); return (present != null && present.booleanValue()); } boolean present = ClassUtils.isPresent("javax.validation.Validation", classLoader); BEAN_VALIDATION_API_PRESENT.put(classLoader, present); // check version BEAN_VALIDATION_API_VERSION.put(classLoader, BeanValidationAPIVersion.getVersion(classLoader)); return present; }
/** * Get default Holon configuration properties provider using default {@link ClassUtils#getDefaultClassLoader()} * ClassLoader. * @return Holon configuration properties providers, or an empty list if none available */ public static List<ConfigPropertyProvider> defaultConfigPropertyProviders() { return defaultConfigPropertyProviders(ClassUtils.getDefaultClassLoader()); }
/** * Create a new {@link RestClient} instance using default {@link ClassLoader} and the implementation whith given * fully qualified class name. * @param fullyQualifiedClassName The {@link RestClient} implementation fully qualified class name to obtain * @return A new {@link RestClient} instance * @throws RestClientCreationException If the implementation which corresponds to given fully qualified class name * is not available or a instance creation error occurred */ static RestClient create(String fullyQualifiedClassName) { return create(fullyQualifiedClassName, ClassUtils.getDefaultClassLoader()); }
/** * Return the default {@link PropertyRenderer}s using given <code>classLoader</code>. * <p> * The default {@link PropertyRenderer}s are loaded using fully qualified name of its implementation class name to a * <code>com.holonplatform.core.property.PropertyRenderer</code> file in the <code>META-INF/services</code> * directory. * </p> * @param classLoader ClassLoader to use, or <code>null</code> for the default ClassLoader * @return Default PropertyRenderers, or an empty List if none */ public static List<PropertyRenderer> getDefaultRenderers(ClassLoader classLoader) { return ensureInited((classLoader != null) ? classLoader : ClassUtils.getDefaultClassLoader()); }
/** * Return the default {@link PropertyValuePresenter}s using given <code>classLoader</code>. * <p> * The default {@link PropertyValuePresenter}s are loaded using fully qualified name of its implementation class * name to a <code>com.holonplatform.core.property.PropertyValuePresenter</code> file in the * <code>META-INF/services</code> directory. * </p> * @param classLoader ClassLoader to use, or <code>null</code> for the default ClassLoader * @return Default PropertyValuePresenters, or an empty List if none */ public static List<PropertyValuePresenter> getDefaultPresenters(ClassLoader classLoader) { return ensureInited((classLoader != null) ? classLoader : ClassUtils.getDefaultClassLoader()); }
/** * Return the default {@link PropertyRendererRegistry} using given <code>classLoader</code>. * <p> * The default registry is inited loading {@link PropertyRenderer}s using fully qualified name of its implementation * class name to a <code>com.holonplatform.core.property.PropertyRenderer</code> file in the * <code>META-INF/services</code> directory. * </p> * @param classLoader ClassLoader to use, or <code>null</code> for the default ClassLoader * @return Default PropertyRendererRegistry */ public static PropertyRendererRegistry getDefault(ClassLoader classLoader) { return ensureInited((classLoader != null) ? classLoader : ClassUtils.getDefaultClassLoader()); }
/** * Return the default {@link PropertyValuePresenterRegistry} using given <code>classLoader</code>. * <p> * The default registry is inited loading {@link PropertyValuePresenter}s using fully qualified name of its * implementation class name to a <code>com.holonplatform.core.property.PropertyValuePresenter</code> file in the * <code>META-INF/services</code> directory. * </p> * @param classLoader ClassLoader to use, or <code>null</code> for the default ClassLoader * @return Default PropertyValuePresenterRegistry */ public static PropertyValuePresenterRegistry getDefault(ClassLoader classLoader) { return ensureInited((classLoader != null) ? classLoader : ClassUtils.getDefaultClassLoader()); }