protected URL getURL(String name) { return ClassLoaderUtil.getResource(name, getClass()); } }
/** * This is a convenience method to load a resource as a stream. * * The algorithm used to find the resource is given in getResource() * * @param resourceName The name of the resource to load * @param callingClass The Class object of the calling object */ public static InputStream getResourceAsStream(String resourceName, Class callingClass) { URL url = getResource(resourceName, callingClass); try { return (url != null) ? url.openStream() : null; } catch (IOException e) { return null; } }
/** * This is a convenience method to load a resource as a stream. * * The algorithm used to find the resource is given in getResource() * * @param resourceName The name of the resource to load * @param callingClass The Class object of the calling object */ public static InputStream getResourceAsStream(String resourceName, Class callingClass) { URL url = getResource(resourceName, callingClass); try { return (url != null) ? url.openStream() : null; } catch (IOException e) { return null; } }
private Element loadConfigXml(final String configFileLocation) throws SAXException, IOException, ParserConfigurationException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final URL fileUrl = ClassLoaderUtil.getResource(configFileLocation, getClass()); if (fileUrl == null) { throw new IllegalArgumentException("No such XML file: " + configFileLocation); } // Parse document final Document doc = factory.newDocumentBuilder().parse(fileUrl.toString()); return doc.getDocumentElement(); }
private Document parseActionsXmlFile(final String actionsXmlFile) { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); URL fileUrl = ClassLoaderUtil.getResource(actionsXmlFile + ".xml", this.getClass()); if (fileUrl == null) { fileUrl = ClassLoaderUtil.getResource("/" + actionsXmlFile + ".xml", this.getClass()); } if (fileUrl == null) { throw new IllegalArgumentException("No such XML file:/" + actionsXmlFile + ".xml"); } try { return factory.newDocumentBuilder().parse(fileUrl.toString()); } catch (final SAXException e) { throw new RuntimeException(e); } catch (final IOException e) { throw new RuntimeException(e); } catch (final ParserConfigurationException e) { throw new RuntimeException(e); } }
/** * Load a given resource. * * This method will try to load the resource using the following methods (in order): * <ul> * <li>From Thread.currentThread().getContextClassLoader() * <li>From ClassLoaderUtil.class.getClassLoader() * <li>callingClass.getClassLoader() * </ul> * * @param resourceName The name of the resource to load * @param callingClass The Class object of the calling object */ public static URL getResource(String resourceName, Class callingClass) { URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName); if (url == null) { url = ClassLoaderUtil.class.getClassLoader().getResource(resourceName); } if (url == null) { ClassLoader cl = callingClass.getClassLoader(); if (cl != null) { url = cl.getResource(resourceName); } } if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) { return getResource('/' + resourceName, callingClass); } return url; }
/** * Load a given resource. * * This method will try to load the resource using the following methods (in order): * <ul> * <li>From Thread.currentThread().getContextClassLoader() * <li>From ClassLoaderUtil.class.getClassLoader() * <li>callingClass.getClassLoader() * </ul> * * @param resourceName The name of the resource to load * @param callingClass The Class object of the calling object */ public static URL getResource(String resourceName, Class callingClass) { URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName); if (url == null) { url = ClassLoaderUtil.class.getClassLoader().getResource(resourceName); } if (url == null) { ClassLoader cl = callingClass.getClassLoader(); if (cl != null) { url = cl.getResource(resourceName); } } if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) { return getResource('/' + resourceName, callingClass); } return url; }
@Nonnull public static XmlJohnsonConfig fromFile(@Nonnull String fileName) { URL url = ClassLoaderUtil.getResource(checkNotNull(fileName, "fileName"), XmlJohnsonConfig.class); if (url != null) { LOG.debug("Loading {} from classpath at {}", fileName, url); fileName = url.toString(); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(fileName); return fromDocument(document); } catch (IOException e) { throw new ConfigurationJohnsonException("Failed to parse [" + fileName + "]; the file could not be read", e); } catch (ParserConfigurationException e) { throw new ConfigurationJohnsonException("Failed to parse [" + fileName + "]; JVM configuration is invalid", e); } catch (SAXException e) { throw new ConfigurationJohnsonException("Failed to parse [" + fileName + "]; XML is not well-formed", e); } }
URL fileUrl = ClassLoaderUtil.getResource(fileName, clazz);
URL fileUrl = ClassLoaderUtil.getResource(configFileLocation, this.getClass()); fileUrl = ClassLoaderUtil.getResource("/" + configFileLocation, this.getClass());
URL fileUrl = ClassLoaderUtil.getResource(fileName, clazz);