private String readVersionFromManifest(Class<?> api) throws IOException { String className = api.getSimpleName() + ".class"; URL resource = api.getResource(className); log.trace("get resource for {} -> {} -> {}", new Object[] {className, pathOf(api), resource}); if (resource == null) return null; String version = null; String classPath = resource.toString(); if (classPath.startsWith("jar:")) version = version(classPath.substring(0, classPath.lastIndexOf("!") + 1)); if (version == null && classPath.endsWith(pathOf(api) + ".class")) version = version(classPath.substring(0, classPath.length() - pathOf(api).length() - 7)); if (version == null && classPath.contains("/WEB-INF/")) version = version(classPath.substring(0, classPath.lastIndexOf("/WEB-INF/"))); log.debug("version {} for {} from {}.", version, api, classPath); return version; }
private static InputSource getConfigSource(Class pAppClass) throws DataNotFoundException { URL resource = pAppClass.getResource(CONFIG_FILE_NAME); String resourceFileName = resource.toExternalForm(); File resourceFile = new File(resourceFileName); InputStream configResourceStream = pAppClass.getResourceAsStream(CONFIG_FILE_NAME); if (null == configResourceStream) { throw new DataNotFoundException( "unable to find XML configuration file resource: " + CONFIG_FILE_NAME + " for class: " + pAppClass.getName()); } InputSource inputSource = new InputSource(configResourceStream); if (!resourceFile.exists()) { inputSource.setSystemId(resourceFileName); } return (inputSource); }
private static long getLastModified(Class cls) { try { String shortName = cls.getName().substring(1 + cls.getName().lastIndexOf('.')); java.net.URL url = cls.getResource(shortName + ".class"); String file = url.getFile(); // System.out.println("url="+url); if ("file".equals(url.getProtocol())) { // example: // file='/home/cliffwd/cvs/dublin/nb_all/schema2beans/rt/src/org/netbeans/modules/schema2beans/GenBeans.class' String result = file.substring(0, file.length() - cls.getName().length() - 6); return new File(file).lastModified(); } else if ("jar".equals(url.getProtocol())) { // example: file = 'jar:/usr/local/j2sdkee1.3.1/lib/j2ee.jar!/org/w3c/dom/Node.class' String jarFile = file.substring(file.indexOf(':') + 1); jarFile = jarFile.substring(0, jarFile.indexOf('!')); // System.out.println("jarFile="+jarFile); return new File(jarFile).lastModified(); } return url.openConnection().getDate(); } catch (java.io.IOException e) { return 0; } }
public static File fileForClass(Class clazz) throws IOException { URL url = clazz.getResource(simpleName(clazz) + ".class"); return new File(url.getFile()); }
private File findJarFile(Class targetClass) { String absolutePath = targetClass.getResource('/' + targetClass.getName().replace(".", "/") + ".class").getPath(); String jarPath = absolutePath.substring("file:".length(), absolutePath.lastIndexOf("!")); return new File(jarPath); }
/** * get the URL of a file from a class relative path. * * @param relToThisClass the class location is prepend to the relImageName to an absolute path. * @param relFileName the relative text file path. * @return the url of this file. */ public static URL getURL(Class relToThisClass, String resourceName) { URL url = relToThisClass.getResource(resourceName); return url; }