List of usage examples for java.lang Class getResource
@CallerSensitive
public URL getResource(String name)
From source file:net.ymate.platform.commons.util.FileUtils.java
/** * ?????//from w ww . j av a2 s . c om * * @param clazz * @return ??? */ public static long getClassLastModifiedTimestamp(Class<?> clazz) { return getLastModifiedTimestamp(clazz.getResource(clazz.getSimpleName() + ".class").getFile()); }
From source file:net.ymate.platform.commons.util.ResourceUtils.java
/** * //from w w w. ja v a 2s .c o m * @param resourceName * @param callingClass * @return */ public static URL getResource(String resourceName, Class<?> callingClass) { URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName); if (url == null) { url = ClassUtils.getDefaultClassLoader().getResource(resourceName); } if (url == null) { url = callingClass.getResource(resourceName); if (url == null) { ClassLoader cl = callingClass.getClassLoader(); if (cl != null) { url = cl.getResource(resourceName); } } } if ((url == null) && (resourceName != null) && (((resourceName.length() == 0) || (resourceName.charAt(0) != '/')))) { return getResource('/' + resourceName, callingClass); } return url; }
From source file:nz.govt.natlib.ndha.manualdeposit.dialogs.About.java
@SuppressWarnings("unchecked") private void startup() { try {// w ww. j a v a 2s . co m try { theFormControl = new FormControl(this, theSettingsPath); } catch (Exception ex) { LOG.error("Error loading form parameters", ex); } ClassLoader cLoader = Thread.currentThread().getContextClassLoader(); Toolkit kit = Toolkit.getDefaultToolkit(); Package p = this.getClass().getPackage(); lblVersion.setText(p.getImplementationVersion()); if (new File(theLogoFileName).exists()) { theLogo = kit.getImage(theLogoFileName); } else { java.net.URL imageURL = cLoader.getResource(theLogoFileName); if (imageURL != null) { theLogo = kit.getImage(imageURL); } } if (theLogo != null) { lblBuildNo.setForeground(Color.white); lblBuildNoLabel.setForeground(Color.white); //lblHeader.setForeground(Color.white); lblVersion.setForeground(Color.white); lblVersionLabel.setForeground(Color.white); paintImage(); } //String manifestFileName = "/META-INF/MANIFEST.MF"; Class cls = this.getClass(); String className = cls.getSimpleName(); String classFileName = className + ".class"; String pathToThisClass = cls.getResource(classFileName).toString(); int mark = pathToThisClass.indexOf("!"); String pathToManifest = pathToThisClass.substring(0, mark + 1); if (!pathToManifest.equals("")) { pathToManifest += "/META-INF/MANIFEST.MF"; LOG.debug("Path to manifest: " + pathToManifest); Manifest mf = new Manifest(new URL(pathToManifest).openStream()); if (mf != null) { Attributes attr = mf.getMainAttributes(); String attrVersion = "Implementation-Version"; String attrBuild = "Implementation-Build"; String version = attr.getValue(attrVersion); String build = attr.getValue(attrBuild); this.lblVersion.setText(version); this.lblBuildNo.setText(build); } } Runtime runtime = Runtime.getRuntime(); long maxMemory = runtime.maxMemory(); long allocatedMemory = runtime.totalMemory(); long freeMemory = runtime.freeMemory(); LOG.debug("free memory: " + freeMemory / 1024); LOG.debug("allocated memory: " + allocatedMemory / 1024); LOG.debug("max memory: " + maxMemory / 1024); LOG.debug("total free memory: " + (freeMemory + (maxMemory - allocatedMemory)) / 1024); } catch (IOException ex) { ex.printStackTrace(); LOG.error(ex.getMessage(), ex); } }
From source file:org.ambraproject.configuration.ConfigurationStore.java
/** * Iterate over all the resources of the given name and add them to our root * configuration.//from w ww . j a v a 2 s . c o m * @param root the root configuration to add to * @param resource the resource to add * @throws ConfigurationException on an error in adding the new config */ public static void addResources(CombinedConfiguration root, String resource) throws ConfigurationException { Class<?> klass = ConfigurationStore.class; if (resource.startsWith("/")) { root.addConfiguration(getConfigurationFromUrl(klass.getResource(resource))); log.info("Added resource '" + resource + "' to configuration"); } else { try { Enumeration<URL> rs = klass.getClassLoader().getResources(resource); while (rs.hasMoreElements()) { URL resourceUrl = rs.nextElement(); root.addConfiguration(getConfigurationFromUrl(resourceUrl)); log.info("Added resource '" + resourceUrl + "' from path '" + resource + "' to configuration"); } } catch (IOException ioe) { throw new Error("Unexpected error loading resources", ioe); } } }
From source file:org.apache.abdera2.common.Discover.java
public static URL locateResource(String id, ClassLoader loader, Class<?> callingClass) { URL url = loader.getResource(id); if (url == null && id.startsWith("/")) url = loader.getResource(id.substring(1)); if (url == null) url = locateResource(id, Discover.class.getClassLoader(), callingClass); if (url == null && callingClass != null) url = locateResource(id, callingClass.getClassLoader(), null); if (url == null) url = callingClass.getResource(id); if ((url == null) && id.startsWith("/")) url = callingClass.getResource(id.substring(1)); return url;//from ww w . ja va2 s .c o m }
From source file:org.apache.axis2.schema.writer.JavaBeanWriter.java
/** * A bit of code from the old code generator. We are better off using the * template engines and such stuff that's already there. But the class * writers are hard to be reused so some code needs to be repeated (atleast * a bit)//ww w .j av a 2 s . c om */ private void loadTemplate() throws SchemaCompilationException { // first get the language specific property map Class<?> clazz = this.getClass(); String templateName = javaBeanTemplateName; if (templateName != null) { try { // Use URL instead of InputStream here, so that the processor may resolve // imports/includes with relative hrefs. URL xsl = clazz.getResource(templateName); templateCache = TransformerFactory.newInstance() .newTemplates(new StreamSource(xsl.toExternalForm())); templateLoaded = true; } catch (TransformerConfigurationException e) { throw new SchemaCompilationException( SchemaCompilerMessages.getMessage("schema.templateLoadException"), e); } } else { throw new SchemaCompilationException( SchemaCompilerMessages.getMessage("schema.templateNotFoundException")); } }
From source file:org.apache.click.util.ClickUtils.java
/** * Finds a resource with a given name. This method returns null if no * resource with this name is found./* www . ja v a 2 s . co m*/ * <p> * This method uses the current <tt>Thread</tt> context <tt>ClassLoader</tt> to find * the resource. If the resource is not found the class loader of the given * class is then used to find the resource. * * @param name the name of the resource * @param aClass the class lookup the resource against, if the resource is * not found using the current <tt>Thread</tt> context <tt>ClassLoader</tt>. * @return the URL of the resource if found or null otherwise */ public static URL getResource(String name, Class<?> aClass) { Validate.notNull(name, "Parameter name is null"); Validate.notNull(aClass, "Parameter aClass is null"); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); URL url = classLoader.getResource(name); if (url == null) { url = aClass.getResource(name); } return url; }
From source file:org.apache.falcon.extensions.store.AbstractTestExtensionStore.java
private String getAbsolutePath(Class resourceClass, String fileName) { return resourceClass.getResource("/" + fileName).getPath(); }
From source file:org.apache.falcon.util.ApplicationProperties.java
private InputStream checkClassPath(String propertyFileName) { InputStream resourceAsStream = null; Class clazz = ApplicationProperties.class; URL resource = clazz.getResource("/" + propertyFileName); if (resource != null) { LOG.info("Fallback to classpath for: {}", resource); resourceAsStream = clazz.getResourceAsStream("/" + propertyFileName); } else {/*from ww w .ja v a2 s. c o m*/ resource = clazz.getResource(propertyFileName); if (resource != null) { LOG.info("Fallback to classpath for: {}", resource); resourceAsStream = clazz.getResourceAsStream(propertyFileName); } } return resourceAsStream; }
From source file:org.apache.geode.util.test.TestUtil.java
/** * Return the path to a named resource. This finds the resource on the classpath using the rules * of class.getResource. For a relative path it will look in the same package as the class, for an * absolute path it will start from the base package. * //from w ww . j av a 2 s . c o m * Best practice is to put the resource in the same package as your test class and load it with * this method. * * @param clazz the class to look relative too * @param name the name of the resource, eg "cache.xml" */ public static String getResourcePath(Class<?> clazz, String name) { URL resource = clazz.getResource(name); if (resource == null) { throw new RuntimeException("Could not find resource " + name); } try { String path = resource.toURI().getPath(); if (path == null) { String filename = name.replaceFirst(".*/", ""); File tmpFile = File.createTempFile(filename, null); tmpFile.deleteOnExit(); FileUtil.copy(resource, tmpFile); return compatibleWithWindows(tmpFile.getAbsolutePath()); } return compatibleWithWindows(path); } catch (URISyntaxException | IOException e) { throw new RuntimeException("Failed getting path to resource " + name, e); } }