List of usage examples for java.lang Class getProtectionDomain
public java.security.ProtectionDomain getProtectionDomain()
From source file:org.covito.kit.utility.ClassUtil.java
public static URL getClassLocation(final String clsName) { if (clsName == null) { return null; }// w ww . j a v a2 s .c o m Class cls = null; try { cls = Class.forName(clsName); } catch (Exception e) { return null; } URL result = null; final String clsAsResource = cls.getName().replace('.', '/').concat(".class"); final ProtectionDomain pd = cls.getProtectionDomain(); // java.lang.Class contract does not specify if 'pd' can ever be null; // it is not the case for Sun's implementations, but guard against null // just in case: if (pd != null) { final CodeSource cs = pd.getCodeSource(); // 'cs' can be null depending on the classloader behavior: if (cs != null) result = cs.getLocation(); if (result != null) { // Convert a code source location into a full class file // location // for some common cases: if ("file".equals(result.getProtocol())) { try { if (result.toExternalForm().endsWith(".jar") || result.toExternalForm().endsWith(".zip")) result = new URL( "jar:".concat(result.toExternalForm()).concat("!/").concat(clsAsResource)); else if (new File(result.getFile()).isDirectory()) { result = new URL(result, clsAsResource); } } catch (MalformedURLException ignore) { } } } } if (result == null) { // Try to find 'cls' definition as a resource; this is not // documentd to be legal, but Sun's implementations seem to //allow // this: final ClassLoader clsLoader = cls.getClassLoader(); result = clsLoader != null ? clsLoader.getResource(clsAsResource) : ClassLoader.getSystemResource(clsAsResource); } return result; }
From source file:org.exnebula.bootstrap.BootConfigLocatorTest.java
private File getFileFromProtectionDomain(Class<?> targetClass) { return new File(targetClass.getProtectionDomain().getCodeSource().getLocation().getFile()); }
From source file:org.exnebula.bootstrap.BootConfigLocatorTest.java
private File getJarFileFromClass(Class<?> aClass) throws UnsupportedEncodingException { return new File( URLDecoder.decode(aClass.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8")); }
From source file:org.jajuk.util.UtilSystem.java
/** * Return url of jar we are executing.// w ww . j a v a2s .com * * This code no more work with last JRE 6 under JNLP (it returns only partial URL) * * @param cClass * * @return URL of jar we are executing */ public static URL getJarLocation(final Class<?> cClass) { URL url = cClass.getProtectionDomain().getCodeSource().getLocation(); if (UtilSystem.isUnderJNLP()) { Log.debug("JAR location: " + url.getFile()); } return url; }
From source file:org.java.plugin.standard.StandardPluginClassLoader.java
private static URL getClassBaseUrl(final Class cls) { ProtectionDomain pd = cls.getProtectionDomain(); if (pd != null) { CodeSource cs = pd.getCodeSource(); if (cs != null) { return cs.getLocation(); }/*from w ww . ja v a 2 s.c o m*/ } return null; }
From source file:org.jbpm.bpel.tools.WscompileTool.java
/** * Gets the directory, JAR file or remote URL from which the given class was loaded. * @param c the class to be located//from w w w .j a v a2 s . c om * @return the location from which the class was loaded, or <code>null</code> if the origin of * the class is unknown */ private static String getLocation(Class c) { CodeSource codeSource = c.getProtectionDomain().getCodeSource(); if (codeSource == null) { /* * The code source of a domain may be null. This is the case for classes included in the Java * platform. For example, the javax.xml.soap package is part of Java SE 6. Not much more can * be done here, so we just tell the caller the location is unknown. * * Thanks to Bernd Ruecker of Camunda GmbH for catching this one. */ return null; } URL url = codeSource.getLocation(); if ("jar".equals(url.getProtocol())) { try { JarURLConnection urlConnection = (JarURLConnection) url.openConnection(); url = urlConnection.getJarFileURL(); } catch (IOException e) { log.debug("could not open connection to " + url, e); } } if (!"file".equals(url.getProtocol())) return url.toString(); String fileName = url.getFile(); try { fileName = URLDecoder.decode(fileName, "UTF-8"); } catch (UnsupportedEncodingException e) { log.debug("UTF-8 not supported", e); } return new File(fileName).getAbsolutePath(); }
From source file:org.jfunktor.core.meta.api.MetaScanner.java
private void scanAndRegister(Class interfaceType) { for (Class impl : JMattrServiceLoader.load(interfaceType)) { impl.getProtectionDomain().getCodeSource().getLocation(); if (impl.isAnnotationPresent(Provider.class)) { //Provider annotation = (Provider) impl.getAnnotation(Provider.class); try { validateType(interfaceType, impl); //do the required binding here proc.process(impl, registry); if (log.isTraceEnabled()) { log.trace(String.format("Registered Provider %s", impl.getName())); }//from w w w . j a v a2s . c om } catch (NotAProviderException e) { log.error(String.format("Invalid Provider Definition ", impl.getName()), e); } } else { if (log.isInfoEnabled()) { log.info(String.format( "Class %s does not seem to be annotated with Provider annotations. Pls check the implementation!", impl)); } } } }
From source file:org.mousephenotype.dcc.utils.io.jar.JARUtils.java
public static <T> String getCwd(Class<T> clazz) { String path = clazz.getProtectionDomain().getCodeSource().getLocation().getPath(); return path.substring(0, path.lastIndexOf(File.separator)); }
From source file:org.mousephenotype.dcc.utils.io.jar.JARUtils.java
public static <T> URL getURL(Class<T> clazz) { return clazz.getProtectionDomain().getCodeSource().getLocation(); }
From source file:org.mule.util.ClassUtils.java
public static URL getClassPathRoot(Class clazz) { CodeSource cs = clazz.getProtectionDomain().getCodeSource(); return (cs != null ? cs.getLocation() : null); }