/** * Determines if a named Class can be loaded or not. * * @param className The class name. * @return {@code true} if the class could be found or {@code false} otherwise. */ private static boolean isClassAvailable(final String className) { try { return LoaderUtil.loadClass(className) != null; } catch (final Throwable e) { return false; } }
private static EventDataConverter createConverter() { try { LoaderUtil.loadClass("org.slf4j.ext.EventData"); return new EventDataConverter(); } catch (final ClassNotFoundException cnfe) { return null; } }
/** * Determines if a named Class can be loaded or not. * * @param className The class name. * @return {@code true} if the class could be found or {@code false} otherwise. * @since 2.7 */ public static boolean isClassAvailable(final String className) { try { final Class<?> clazz = loadClass(className); return clazz != null; } catch (final ClassNotFoundException | LinkageError e) { return false; } catch (final Throwable e) { LowLevelLogUtil.logException("Unknown error checking for existence of class: " + className, e); return false; } }
/** * Loads and instantiates a Class using the default constructor. * * @param className The class name. * @return new instance of the class. * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @since 2.1 */ @SuppressWarnings("unchecked") public static <T> T newInstanceOf(final String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { return newInstanceOf((Class<T>) loadClass(className)); }
private static Class<? extends MessageFactory> createClassForProperty(final String property, final Class<ReusableMessageFactory> reusableParameterizedMessageFactoryClass, final Class<ParameterizedMessageFactory> parameterizedMessageFactoryClass) { try { final String fallback = Constants.ENABLE_THREADLOCALS ? reusableParameterizedMessageFactoryClass.getName() : parameterizedMessageFactoryClass.getName(); final String clsName = PropertiesUtil.getProperties().getStringProperty(property, fallback); return LoaderUtil.loadClass(clsName).asSubclass(MessageFactory.class); } catch (final Throwable t) { return parameterizedMessageFactoryClass; } }
private static Class<? extends FlowMessageFactory> createFlowClassForProperty(final String property, final Class<DefaultFlowMessageFactory> defaultFlowMessageFactoryClass) { try { final String clsName = PropertiesUtil.getProperties().getStringProperty(property, defaultFlowMessageFactoryClass.getName()); return LoaderUtil.loadClass(clsName).asSubclass(FlowMessageFactory.class); } catch (final Throwable t) { return defaultFlowMessageFactoryClass; } }
/** * Determines if a named Class can be loaded or not. * * @param className The class name. * @return {@code true} if the class could be found or {@code false} otherwise. */ private static boolean isClassAvailable(final String className) { try { return LoaderUtil.loadClass(className) != null; } catch (final Throwable e) { return false; } }
/** * Load a Class by name. Note that unlike {@link ClassLoader#loadClass(String) ClassLoader.loadClass}, this method * will initialize the class as well if it hasn't been already. This is equivalent to the calling the {@link * ClassLoader#loadClass(String, boolean) protected version} with the second parameter equal to {@code true}. * * @param className The class name. * @return The Class. * @throws ClassNotFoundException if the Class could not be found. */ public static Class<?> loadClass(final String className) throws ClassNotFoundException { return LoaderUtil.loadClass(className); }
private static Class<? extends StringMap> createCachedClass(final String className) { if (className == null) { return null; } try { return LoaderUtil.loadClass(className).asSubclass(StringMap.class); } catch (final Exception any) { return null; } }
private static EventDataConverter createConverter() { try { LoaderUtil.loadClass("org.slf4j.ext.EventData"); return new EventDataConverter(); } catch (final ClassNotFoundException cnfe) { return null; } }
private static void addFactory(final Collection<ConfigurationFactory> list, final String factoryClass) { try { addFactory(list, LoaderUtil.loadClass(factoryClass).asSubclass(ConfigurationFactory.class)); } catch (final Exception ex) { LOGGER.error("Unable to load class {}", factoryClass, ex); } }
private static AsyncQueueFullPolicy createCustomRouter(final String router) { try { final Class<? extends AsyncQueueFullPolicy> cls = LoaderUtil.loadClass(router).asSubclass(AsyncQueueFullPolicy.class); LOGGER.debug("Creating custom AsyncQueueFullPolicy '{}'", router); return cls.newInstance(); } catch (final Exception ex) { LOGGER.debug("Using DefaultAsyncQueueFullPolicy. Could not create custom AsyncQueueFullPolicy '{}': {}", router, ex.toString()); return new DefaultAsyncQueueFullPolicy(); } }
/** * Loads and instantiates a Class using the default constructor. * * @param className The class name. * @return new instance of the class. * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @since 2.1 */ @SuppressWarnings("unchecked") public static <T> T newInstanceOf(final String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { return newInstanceOf((Class<T>) loadClass(className)); }
private static Class<? extends MessageFactory> createClassForProperty(final String property, final Class<ReusableMessageFactory> reusableParameterizedMessageFactoryClass, final Class<ParameterizedMessageFactory> parameterizedMessageFactoryClass) { try { final String fallback = Constants.ENABLE_THREADLOCALS ? reusableParameterizedMessageFactoryClass.getName() : parameterizedMessageFactoryClass.getName(); final String clsName = PropertiesUtil.getProperties().getStringProperty(property, fallback); return LoaderUtil.loadClass(clsName).asSubclass(MessageFactory.class); } catch (final Throwable t) { return parameterizedMessageFactoryClass; } }
/** * Determines if a named Class can be loaded or not. * * @param className The class name. * @return {@code true} if the class could be found or {@code false} otherwise. * @since 2.7 */ public static boolean isClassAvailable(final String className) { try { final Class<?> clazz = loadClass(className); return clazz != null; } catch (final ClassNotFoundException | LinkageError e) { return false; } catch (final Throwable e) { LowLevelLogUtil.logException("Unknown error checking for existence of class: " + className, e); return false; } }
private Class loadClass(String className, String argName) { if (className == null) { throw new ConfigurationException(String.format("No %s provided for %s", argName, JacksonMixIn.PLUGIN_NAME)); } try { return LoaderUtil.loadClass(className); } catch (ClassNotFoundException e) { throw new ConfigurationException(String.format("Cannot load %s: %s for %s", argName, className, JacksonMixIn.PLUGIN_NAME)); } }
private static Class<? extends FlowMessageFactory> createFlowClassForProperty(final String property, final Class<DefaultFlowMessageFactory> defaultFlowMessageFactoryClass) { try { final String clsName = PropertiesUtil.getProperties().getStringProperty(property, defaultFlowMessageFactoryClass.getName()); return LoaderUtil.loadClass(clsName).asSubclass(FlowMessageFactory.class); } catch (final Throwable t) { return defaultFlowMessageFactoryClass; } }
private Class loadClass(String className, String argName) { if (className == null) { throw new ConfigurationException(String.format("No %s provided for %s", argName, JacksonMixIn.PLUGIN_NAME)); } try { return LoaderUtil.loadClass(className); } catch (ClassNotFoundException e) { throw new ConfigurationException(String.format("Cannot load %s: %s for %s", argName, className, JacksonMixIn.PLUGIN_NAME)); } }
static ExceptionHandler<RingBufferLogEvent> getAsyncLoggerExceptionHandler() { final String cls = PropertiesUtil.getProperties().getStringProperty("AsyncLogger.ExceptionHandler"); if (cls == null) { return new AsyncLoggerDefaultExceptionHandler(); } try { @SuppressWarnings("unchecked") final Class<? extends ExceptionHandler<RingBufferLogEvent>> klass = (Class<? extends ExceptionHandler<RingBufferLogEvent>>) LoaderUtil.loadClass(cls); return klass.newInstance(); } catch (final Exception ignored) { LOGGER.debug("Invalid AsyncLogger.ExceptionHandler value: error creating {}: ", cls, ignored); return new AsyncLoggerDefaultExceptionHandler(); } }
static ExceptionHandler<AsyncLoggerConfigDisruptor.Log4jEventWrapper> getAsyncLoggerConfigExceptionHandler() { final String cls = PropertiesUtil.getProperties().getStringProperty("AsyncLoggerConfig.ExceptionHandler"); if (cls == null) { return new AsyncLoggerConfigDefaultExceptionHandler(); } try { @SuppressWarnings("unchecked") final Class<? extends ExceptionHandler<AsyncLoggerConfigDisruptor.Log4jEventWrapper>> klass = (Class<? extends ExceptionHandler<AsyncLoggerConfigDisruptor.Log4jEventWrapper>>) LoaderUtil.loadClass(cls); return klass.newInstance(); } catch (final Exception ignored) { LOGGER.debug("Invalid AsyncLoggerConfig.ExceptionHandler value: error creating {}: ", cls, ignored); return new AsyncLoggerConfigDefaultExceptionHandler(); } }