List of usage examples for java.lang Class getProtectionDomain
public java.security.ProtectionDomain getProtectionDomain()
From source file:com.streamsets.pipeline.stage.destination.mapreduce.MapreduceUtils.java
/** * Returns path to the jar containing location to given class. *//*from w w w.j a va 2s .c o m*/ public static String jarForClass(Class klass) { return klass.getProtectionDomain().getCodeSource().getLocation().toString(); }
From source file:edu.cornell.med.icb.util.VersionUtils.java
/** * Gets the Implementation-Version attribute from the manifest of the jar file a class * is loaded from./* w w w . ja v a 2s . c o m*/ * @param clazz The class to get the version for * @return The value of the Implementation-Version attribute or "UNKNOWN" if the * jar file cannot be read. */ public static String getImplementationVersion(final Class<?> clazz) { String version; try { final String classContainer = clazz.getProtectionDomain().getCodeSource().getLocation().toString(); final URL manifestUrl = new URL("jar:" + classContainer + "!/" + JarFile.MANIFEST_NAME); final Manifest manifest = new Manifest(manifestUrl.openStream()); final Attributes attributes = manifest.getMainAttributes(); version = attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION); } catch (Exception e) { // pretty much any error here is ok since we may not even have a jar to read from version = "UNKNOWN"; } if (LOG.isDebugEnabled()) { LOG.debug(Attributes.Name.IMPLEMENTATION_VERSION + ": " + version); } return StringUtils.defaultString(version); }
From source file:uk.dsxt.voting.common.utils.PropertiesHelper.java
public static File getParentFile(Class cls) { return new File(cls.getProtectionDomain().getCodeSource().getLocation().getPath()).getParentFile(); }
From source file:com.navercorp.pinpoint.bootstrap.AgentDirBaseClassPathResolverTest.java
private static String getClassLocation(Class<?> clazz) { CodeSource codeSource = clazz.getProtectionDomain().getCodeSource(); URL location = codeSource.getLocation(); logger.debug("codeSource.getLocation:{}", location); File file = FileUtils.toFile(location); return file.getPath(); }
From source file:com.arrow.acs.ManifestUtils.java
public static Manifest readManifest(Class<?> clazz) { String method = "readManifest"; String jarFile = null;/* w ww.ja v a 2 s . co m*/ String path = clazz.getProtectionDomain().getCodeSource().getLocation().toString(); for (String token : path.split("/")) { token = token.replace("!", "").toLowerCase().trim(); if (token.endsWith(".jar")) { jarFile = token; break; } } LOGGER.logInfo(method, "className: %s, path: %s, jarFile: %s", clazz.getName(), path, jarFile); InputStream is = null; try { if (!StringUtils.isEmpty(jarFile)) { Enumeration<URL> enumeration = Thread.currentThread().getContextClassLoader() .getResources(JarFile.MANIFEST_NAME); while (enumeration.hasMoreElements()) { URL url = enumeration.nextElement(); for (String token : url.toString().split("/")) { token = token.replace("!", "").toLowerCase(); if (token.equals(jarFile)) { LOGGER.logInfo(method, "loading manifest from: %s", url.toString()); return new Manifest(is = url.openStream()); } } } } else { URL url = new URL(path + "/META-INF/MANIFEST.MF"); LOGGER.logInfo(method, "loading manifest from: %s", url.toString()); return new Manifest(is = url.openStream()); } } catch (IOException e) { } finally { IOUtils.closeQuietly(is); } LOGGER.logError(method, "manifest file not found for: %s", clazz.getName()); return null; }
From source file:by.stub.cli.CommandLineIntepreter.java
/** * Returns stubby4j JAR path, relative to current execution directory under local filesystem based * on the given class executed within the JAR. * * @param theclass Class executed within the JAR. The class discloses JAR's current location * @return relative stubby4j JAR path//w w w . j a va 2s.c o m */ public static String getCurrentJarLocation(final Class theclass) { final URL location = theclass.getProtectionDomain().getCodeSource().getLocation(); final String jarAbsolutePath = new File(location.getFile()).getAbsolutePath(); final String jar = jarAbsolutePath.replaceAll(System.getProperty("user.dir") + "/", ""); if (StringUtils.toLower(jar).endsWith(".jar")) { return jar; } return "stubby4j-x.x.x-SNAPSHOT.jar"; }
From source file:com.intuit.autumn.utils.PropertyFactory.java
private static void fromJar(final String propertyResourceName, @Nullable final Class base, final Properties properties) { if (base == null) { return;/*from w w w .j av a 2s . c o m*/ } CodeSource src = base.getProtectionDomain().getCodeSource(); if (src == null) { return; } Properties propertiesFromJar = getPropertyFromJar(base, propertyResourceName.substring(1), src); for (Entry<Object, Object> entry : propertiesFromJar.entrySet()) { if (!properties.containsKey(entry.getKey())) { LOGGER.debug("overriding key: {}, newValue: {}, originalValue: {}", new Object[] { entry.getKey(), propertiesFromJar, entry.getValue() }); properties.setProperty((String) entry.getKey(), (String) entry.getValue()); } } }
From source file:utils.TestUtils.java
private static File codeSourceDir(Class<?> clazz) { URI uri = null;//from w w w . j ava 2 s. c o m try { uri = clazz.getProtectionDomain().getCodeSource().getLocation().toURI(); File jarOrDir = new File(uri); return jarOrDir.isDirectory() ? jarOrDir : jarOrDir.getParentFile(); } catch (URISyntaxException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Gets the base location of the given class. * <p>/* w w w .ja v a2 s.c o m*/ * If the class is directly on the file system (e.g., * "/path/to/my/package/MyClass.class") then it will return the base directory * (e.g., "file:/path/to"). * </p> * <p> * If the class is within a JAR file (e.g., * "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the * path to the JAR (e.g., "file:/path/to/my-jar.jar"). * </p> * * @param c The class whose location is desired. * @see FileUtils#urlToFile(URL) to convert the result to a {@link File}. */ public static URL getLocation(final Class<?> c) { if (c == null) return null; // could not load the class // try the easy way first try { final URL codeSourceLocation = c.getProtectionDomain().getCodeSource().getLocation(); if (codeSourceLocation != null) return codeSourceLocation; } catch (SecurityException e) { // NB: Cannot access protection domain. } catch (NullPointerException e) { // NB: Protection domain or code source is null. } // NB: The easy way failed, so we try the hard way. We ask for the class // itself as a resource, then strip the class's path from the URL string, // leaving the base path. // get the class's raw resource path final URL classResource = c.getResource(c.getSimpleName() + ".class"); if (classResource == null) return null; // cannot find class resource final String url = classResource.toString(); final String suffix = c.getCanonicalName().replace('.', '/') + ".class"; if (!url.endsWith(suffix)) return null; // weird URL // strip the class's path from the URL string final String base = url.substring(0, url.length() - suffix.length()); String path = base; // remove the "jar:" prefix and "!/" suffix, if present if (path.startsWith("jar:")) path = path.substring(4, path.length() - 2); try { return new URL(path); } catch (MalformedURLException e) { e.printStackTrace(); return null; } }
From source file:com.chaschev.install.InstallMojo.java
public static File getJarByClass(Class<?> aClass) { return new File(aClass.getProtectionDomain().getCodeSource().getLocation().getFile()); }