List of usage examples for java.lang Class getResource
@CallerSensitive
public URL getResource(String name)
From source file:freemarker.test.servlet.WebAppTestCase.java
@SuppressFBWarnings(value = "UI_INHERITANCE_UNSAFE_GETRESOURCE", justification = "By design relative to subclass") private String getWebAppDirURL(String webAppName) throws IOException { final URL webXmlURL; {//from ww w.ja v a 2 s. co m final String relResPath = "webapps/" + webAppName + "/WEB-INF/web.xml"; Class<?> baseClass = this.getClass(); findWebXmlURL: do { URL r = baseClass.getResource(relResPath); if (r != null) { webXmlURL = r; break findWebXmlURL; } baseClass = baseClass.getSuperclass(); if (!WebAppTestCase.class.isAssignableFrom(baseClass)) { throw new IOException("Can't find test class relative resource: " + relResPath); } } while (true); } try { return webXmlURL.toURI().resolve("..").toString(); } catch (URISyntaxException e) { throw new RuntimeException("Failed to get grandparent URL for " + webXmlURL, e); } }
From source file:com.taobao.adfs.util.Utilities.java
public static File getLibPath() { Class<?> clazz = Utilities.class; String pathOfClassInPackage = "/" + clazz.getName().replaceAll("\\.", "/") + ".class"; URL urlOfClass = clazz.getResource("/" + clazz.getName().replaceAll("\\.", "/") + ".class"); if (urlOfClass == null) return null; String classOfPath = urlOfClass.getPath().substring(0, urlOfClass.getPath().lastIndexOf(pathOfClassInPackage)); if (urlOfClass.getProtocol().equals("jar")) classOfPath = new File(classOfPath.substring(5, classOfPath.length() - 1)).getParent(); return new File(classOfPath); }
From source file:net.sf.profiler4j.agent.Config.java
private String detectInstallDirUrl() { Class c = getClass(); String cn = "/" + c.getName().replace('.', '/') + ".class"; URL url = c.getResource(cn); String s = url.toString();/*w ww. j a v a 2s .c o m*/ int pos = s.lastIndexOf('!'); s = s.substring(0, pos); pos = s.lastIndexOf('/'); s = s.substring(4, pos); return s; }
From source file:net.sf.freecol.FreeCol.java
/** * Get the JarURLConnection from a class. * * @return The <code>JarURLConnection</code>. *//* w w w . j a v a2 s .c o m*/ private static JarURLConnection getJarURLConnection(Class c) throws IOException { String resourceName = "/" + c.getName().replace('.', '/') + ".class"; URL url = c.getResource(resourceName); return (JarURLConnection) url.openConnection(); }
From source file:com.jwebmp.core.FileTemplates.java
/** * Returns the template as a string/* w w w .jav a2 s .c o m*/ * * @param referenceClass * The class to reference to locate the file * @param templateName * The file without .min.js or .js attached to it * @param fileName * * @return The string for the file */ public static StringBuilder getFileTemplate(Class referenceClass, String templateName, String fileName, boolean alwaysRefresh) { StringBuilder template = TemplateScripts.get(templateName); if (template == null || alwaysRefresh) { try { String templateFileName = fileName; if (!(fileName.contains(".html") || fileName.contains(".htm") || fileName.contains(".js") || fileName.contains(".css") || fileName.contains(".min") || fileName.contains(".txt"))) { templateFileName += ".js"; } if (templateFileName.endsWith(".min")) { templateFileName = templateFileName + ".js"; } String contents = IOUtils.toString(referenceClass.getResourceAsStream(templateFileName), StaticStrings.UTF8_CHARSET); setTemplateScript(templateName, new StringBuilder(contents)); } catch (FileNotFoundException ex) { LOG.log(Level.SEVERE, "[Error]-[unable to find template file];[TemplateFile]-[" + templateName + "];[TemplatePath]-[" + referenceClass.getResource(templateName).getPath() + "]", ex); } catch (IOException ex) { LOG.log(Level.SEVERE, "Unable to read file contents jwangular template File", ex); } catch (NullPointerException npe) { LOG.log(Level.SEVERE, "template file [" + fileName + "(.js)] not found.", npe); } catch (Exception npe) { LOG.log(Level.SEVERE, "Exception Rendering Template", npe); } } return TemplateScripts.get(templateName); }
From source file:com.obergner.hzserver.ServerInfo.java
private Manifest loadServerManifest() throws MalformedURLException, IOException { final Class<?> clazz = getClass(); final String className = clazz.getSimpleName() + ".class"; final String classPath = clazz.getResource(className).toString(); if (!classPath.startsWith("jar")) { // Class not from JAR -> probably running in a unit test context return new Manifest(getClass().getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF")); }//from w w w . j a v a 2 s .c om final String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF"; return new Manifest(new URL(manifestPath).openStream()); }
From source file:atunit.spring.SpringContainer.java
protected void loadBeanDefinitions(Class<?> testClass, BeanDefinitionRegistry registry) { XmlBeanDefinitionReader xml = new XmlBeanDefinitionReader(registry); String resourceName = testClass.getSimpleName() + ".xml"; Context ctxAnno = testClass.getAnnotation(Context.class); if (ctxAnno != null) { resourceName = ctxAnno.value();//from w w w .j av a 2 s . com } URL xmlUrl = testClass.getResource(resourceName); if (xmlUrl != null) { xml.loadBeanDefinitions(new UrlResource(xmlUrl)); } else if (ctxAnno != null) { // is this the appropriate exception here? throw new ApplicationContextException("Could not find context file named " + resourceName); } }
From source file:com.liferay.frontend.js.loader.modules.extender.internal.JSLoaderModulesServletTest.java
protected URL getResource(String name) { Class<?> clazz = getClass(); return clazz.getResource(name); }
From source file:org.callimachusproject.rewrite.RewriteAdvice.java
private String getSystemId(Method m) { if (m.isAnnotationPresent(Iri.class)) return m.getAnnotation(Iri.class).value(); Class<?> dclass = m.getDeclaringClass(); String mame = m.getName();//from w w w. j ava 2 s . c o m if (dclass.isAnnotationPresent(Iri.class)) { String url = dclass.getAnnotation(Iri.class).value(); if (url.indexOf('#') >= 0) return url.substring(0, url.indexOf('#') + 1) + mame; return url + "#" + mame; } String name = dclass.getSimpleName() + ".class"; URL url = dclass.getResource(name); if (url != null) return url.toExternalForm() + "#" + mame; return "java:" + dclass.getName() + "#" + mame; }
From source file:org.chorusbdd.chorus.spring.SpringContextInjector.java
public void injectSpringContext(Object handler, FeatureToken featureToken, String contextFileName) { Class handlerClass = handler.getClass(); //check for a 'Configuration:' specific Spring context if (featureToken.isConfiguration()) { if (contextFileName.endsWith(".xml")) { String tmp = String.format("%s-%s.xml", contextFileName.substring(0, contextFileName.length() - 4), featureToken.getConfigurationName()); URL url = handlerClass.getResource(tmp); if (url != null) { contextFileName = tmp;//from www.j a v a 2 s . co m } } else { log.warn("Unexpected suffix for Spring config file (should end with .xml) : " + contextFileName); } } URL url = handlerClass.getResource(contextFileName); //this spring context may have been already loaded for another handler instance - if so, reuse it AbstractApplicationContext springContext = getExistingContextByUrl(url); //not already created, new one up if (springContext == null) { springContext = createNewContext(handler, contextFileName, handlerClass, url); } //FileSystemXmlApplicationContext springContext = new FileSystemXmlApplicationContext(url.toExternalForm()); injectResourceFields(springContext, handler); }