private URL urlByResourceName(StatusManager sm, String resourceName) { sm.add(new InfoStatus("Searching for [" + resourceName + "]", this)); URL url = Loader.getResource(resourceName, Loader.getTCL()); if (url != null) { return url; } return Loader.getResourceBySelfClassLoader(resourceName); }
/** * Instantiate the context selector class designated by the user. The selector * must have a constructor taking a LoggerContext instance as an argument. * * @param defaultLoggerContext * @param contextSelectorStr * @return an instance of the designated context selector class * @throws ClassNotFoundException * @throws SecurityException * @throws NoSuchMethodException * @throws IllegalArgumentException * @throws InstantiationException * @throws IllegalAccessException * @throws InvocationTargetException */ static ContextSelector dynamicalContextSelector(LoggerContext defaultLoggerContext, String contextSelectorStr) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Class<?> contextSelectorClass = Loader.loadClass(contextSelectorStr); Constructor cons = contextSelectorClass.getConstructor(new Class[] { LoggerContext.class }); return (ContextSelector) cons.newInstance(defaultLoggerContext); }
public URL findURLOfDefaultConfigurationFile(boolean updateStatus) { ClassLoader myClassLoader = Loader.getClassLoaderOfObject(this); URL url = findConfigFileURLFromSystemProperties(myClassLoader, updateStatus); if (url != null) { return url; } url = getResource(TEST_AUTOCONFIG_FILE, myClassLoader, updateStatus); if (url != null) { return url; } url = getResource(GROOVY_AUTOCONFIG_FILE, myClassLoader, updateStatus); if (url != null) { return url; } return getResource(AUTOCONFIG_FILE, myClassLoader, updateStatus); }
/** * Attempt to find a resource by using the classloader that loaded this class, * namely Loader.class. * * @param resource * @return */ public static URL getResourceBySelfClassLoader(String resource) { return getResource(resource, getClassLoaderOfClass(Loader.class)); }
URL resourceAsURL(String resourceAttribute) { URL url = Loader.getResourceBySelfClassLoader(resourceAttribute); if (url == null) { if (!optional) { String errMsg = "Could not find resource corresponding to [" + resourceAttribute + "]"; addError(errMsg); } return null; } else return url; }
private URL getResource(String filename, ClassLoader myClassLoader, boolean updateStatus) { URL url = Loader.getResource(filename, myClassLoader); if (updateStatus) { statusOnResourceSearch(filename, myClassLoader, url); } return url; }
/** * If running under JDK 1.2 load the specified class using the * <code>Thread</code> <code>contextClassLoader</code> if that fails try * Class.forname. Under JDK 1.1 only Class.forName is used. */ public static Class<?> loadClass(String clazz) throws ClassNotFoundException { // Just call Class.forName(clazz) if we are running under JDK 1.1 // or if we are instructed to ignore the TCL. if (ignoreTCL) { return Class.forName(clazz); } else { try { return getTCL().loadClass(clazz); } catch (Throwable e) { // we reached here because tcl was null or because of a // security exception, or because clazz could not be loaded... // In any case we now try one more time return Class.forName(clazz); } } } }
private static ClassLoader getServiceLoaderClassLoader() { return testServiceLoaderClassLoader == null ? Loader.getClassLoaderOfClass(EnvUtil.class) : testServiceLoaderClassLoader; }
private void multiplicityWarning(String resourceName, ClassLoader classLoader) { Set<URL> urlSet = null; StatusManager sm = loggerContext.getStatusManager(); try { urlSet = Loader.getResources(resourceName, classLoader); } catch (IOException e) { sm.add(new ErrorStatus("Failed to get url list for resource [" + resourceName + "]", loggerContext, e)); } if (urlSet != null && urlSet.size() > 1) { sm.add(new WarnStatus("Resource [" + resourceName + "] occurs multiple times on the classpath.", loggerContext)); for (URL url : urlSet) { sm.add(new WarnStatus("Resource [" + resourceName + "] occurs at [" + url.toString() + "]", loggerContext)); } } } }
/** * Attempt to find a resource by using the classloader that loaded this class, * namely Loader.class. * * @param resource the resource name to look for * @return resource URL */ public static URL getResourceBySelfClassLoader(String resource) { return getResource(resource, getClassLoaderOfClass(Loader.class)); }
url = Loader.getResourceBySelfClassLoader(location); throw new MalformedURLException("path is required"); url = Loader.getResourceBySelfClassLoader(path);
private URL findConfigFileURLFromSystemProperties(ClassLoader classLoader, boolean updateStatus) { String logbackConfigFile = OptionHelper.getSystemProperty(CONFIG_FILE_PROPERTY); if (logbackConfigFile != null) { URL result = null; try { result = new URL(logbackConfigFile); return result; } catch (MalformedURLException e) { // so, resource is not a URL: // attempt to get the resource from the class path result = Loader.getResource(logbackConfigFile, classLoader); if (result != null) { return result; } File f = new File(logbackConfigFile); if (f.exists() && f.isFile()) { try { result = f.toURI().toURL(); return result; } catch (MalformedURLException e1) { } } } finally { if (updateStatus) { statusOnResourceSearch(logbackConfigFile, classLoader, result); } } } return null; }
/** * If running under JDK 1.2 load the specified class using the * <code>Thread</code> <code>contextClassLoader</code> if that fails try * Class.forname. Under JDK 1.1 only Class.forName is used. */ public static Class loadClass(String clazz) throws ClassNotFoundException { // Just call Class.forName(clazz) if we are running under JDK 1.1 // or if we are instructed to ignore the TCL. if (ignoreTCL) { return Class.forName(clazz); } else { try { return getTCL().loadClass(clazz); } catch (Throwable e) { // we reached here because tcl was null or because of a // security exception, or because clazz could not be loaded... // In any case we now try one more time return Class.forName(clazz); } } } }
static public boolean isGroovyAvailable() { ClassLoader classLoader = Loader.getClassLoaderOfClass(EnvUtil.class); try { Class<?> bindingClass = classLoader.loadClass("groovy.lang.Binding"); return (bindingClass != null); } catch (ClassNotFoundException e) { return false; } }
private void multiplicityWarning(String resourceName, ClassLoader classLoader) { Set<URL> urlSet = null; StatusManager sm = loggerContext.getStatusManager(); try { urlSet = Loader.getResources(resourceName, classLoader); } catch (IOException e) { sm.add(new ErrorStatus("Failed to get url list for resource [" + resourceName + "]", loggerContext, e)); } if (urlSet != null && urlSet.size() > 1) { sm.add(new WarnStatus("Resource [" + resourceName + "] occurs multiple times on the classpath.", loggerContext)); for (URL url : urlSet) { sm.add(new WarnStatus("Resource [" + resourceName + "] occurs at [" + url.toString() + "]", loggerContext)); } } } }
private URL urlByResourceName(StatusManager sm, String resourceName) { sm.add(new InfoStatus("Searching for [" + resourceName + "]", this)); URL url = Loader.getResource(resourceName, Loader.getTCL()); if (url != null) { return url; } return Loader.getResourceBySelfClassLoader(resourceName); }
/** * Attempt to find a resource by using the classloader that loaded this class, * namely Loader.class. * * @param resource * @return */ public static URL getResourceBySelfClassLoader(String resource) { return getResource(resource, getClassLoaderOfClass(Loader.class)); }
private <T> T getEnhancer(String enhancerClassName) { try { Class<T> clz = (Class<T>) Loader.loadClass(enhancerClassName.trim()); return clz.newInstance(); } catch (Exception ex) { // If we cannot create the enhancer we fallback to null } return null; }
public static Class<?> loadClass(String clazz, Context context) throws ClassNotFoundException { ClassLoader cl = getClassLoaderOfObject(context); return cl.loadClass(clazz); }
/** * Returns the string "true" if the {@link #setResource(String) resource} specified by the * user is available on the class path, "false" otherwise. * * @return "true"|"false" depending on the availability of resource on the classpath */ public String getPropertyValue() { if (OptionHelper.isEmpty(resourceStr)) { addError("The \"resource\" property must be set."); return null; } URL resourceURL = Loader.getResourceBySelfClassLoader(resourceStr); return booleanAsStr(resourceURL != null); }