Java Code Examples for java.security.ProtectionDomain#getCodeSource()

The following examples show how to use java.security.ProtectionDomain#getCodeSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClassUtils.java    From sofa-ark with Apache License 2.0 6 votes vote down vote up
public static String getCodeBase(Class<?> cls) {

        if (cls == null) {
            return null;
        }
        ProtectionDomain domain = cls.getProtectionDomain();
        if (domain == null) {
            return null;
        }
        CodeSource source = domain.getCodeSource();
        if (source == null) {
            return null;
        }
        URL location = source.getLocation();
        if (location == null) {
            return null;
        }
        return location.getFile();
    }
 
Example 2
Source File: BaseExecutableArchiveLauncher.java    From sofa-ark with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the executable file archive
 * @return executable file archive
 * @throws Exception
 */
protected ExecutableArchive createArchive() throws Exception {
    ProtectionDomain protectionDomain = getClass().getProtectionDomain();
    CodeSource codeSource = protectionDomain.getCodeSource();
    URI location = (codeSource == null ? null : codeSource.getLocation().toURI());
    String path = (location == null ? null : location.getSchemeSpecificPart());
    if (path == null) {
        throw new IllegalStateException("Unable to determine code source archive");
    }
    File root = new File(path);
    if (!root.exists()) {
        throw new IllegalStateException("Unable to determine code source archive from " + root);
    }
    return root.isDirectory() ? new ExecutableArkBizJar(new ExplodedArchive(root))
        : new ExecutableArkBizJar(new JarFileArchive(root), root.toURI().toURL());
}
 
Example 3
Source File: WebApplication.java    From TrackRay with GNU General Public License v3.0 6 votes vote down vote up
protected static Archive createArchive(Class clazz) throws Exception {
	ProtectionDomain protectionDomain = clazz.getProtectionDomain();
	CodeSource codeSource = protectionDomain.getCodeSource();
	URI location = codeSource != null ? codeSource.getLocation().toURI() : null;
	String path = location != null ? location.getSchemeSpecificPart() : null;
	if (path == null) {
		throw new IllegalStateException("Unable to determine code source archive");
	} else {
		File root = new File(path);
		if (!root.exists()) {
			throw new IllegalStateException("Unable to determine code source archive from " + root);
		} else {
			return (Archive)(root.isDirectory() ? new ExplodedArchive(root) : new JarFileArchive(root));
		}
	}
}
 
Example 4
Source File: ProfilerImpl.java    From jsonde with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void describeRedefinableClass(long classId, Class clazz) {

    DescribeClassMessage describeClassMessage = new DescribeClassMessage(classId, true);

    describeClassMessage.setClassLoaderId(
            classLoaderIdGenerator.getId(clazz.getClassLoader())
    );

    ProtectionDomain protectionDomain = clazz.getProtectionDomain();
    if (null != protectionDomain) {
        CodeSource codeSource = protectionDomain.getCodeSource();
        if (null != codeSource) {
            URL codeLocation = codeSource.getLocation();
            describeClassMessage.setCodeLocation(codeLocation.toExternalForm());
        }
    }

    networkServer.sendMessage(describeClassMessage);

}
 
Example 5
Source File: JavaAdapterFactory.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
private StaticClass getInstanceAdapterClass(final ProtectionDomain protectionDomain) {
    CodeSource codeSource = protectionDomain.getCodeSource();
    if(codeSource == null) {
        codeSource = MINIMAL_PERMISSION_DOMAIN.getCodeSource();
    }
    StaticClass instanceAdapterClass = instanceAdapters.get(codeSource);
    if(instanceAdapterClass != null) {
        return instanceAdapterClass;
    }
    // Any "unknown source" code source will default to no permission domain.
    final ProtectionDomain effectiveDomain = codeSource.equals(MINIMAL_PERMISSION_DOMAIN.getCodeSource()) ?
            MINIMAL_PERMISSION_DOMAIN : protectionDomain;

    instanceAdapterClass = instanceAdapterGenerator.generateClass(commonLoader, effectiveDomain);
    final StaticClass existing = instanceAdapters.putIfAbsent(codeSource, instanceAdapterClass);
    return existing == null ? instanceAdapterClass : existing;
}
 
Example 6
Source File: ClassUtils.java    From confucius-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Get {@link Class}'s code source location URL
 *
 * @param type
 * @return If , return <code>null</code>.
 * @throws NullPointerException
 *         If <code>type</code> is <code>null</code> , {@link NullPointerException} will be thrown.
 */
public static URL getCodeSourceLocation(Class<?> type) throws NullPointerException {

    URL codeSourceLocation = null;
    ClassLoader classLoader = type.getClassLoader();

    if (classLoader == null) { // Bootstrap ClassLoader or type is primitive or void
        String path = findClassPath(type);
        if (StringUtils.isNotBlank(path)) {
            try {
                codeSourceLocation = new File(path).toURI().toURL();
            } catch (MalformedURLException ignored) {
                codeSourceLocation = null;
            }
        }
    } else {
        ProtectionDomain protectionDomain = type.getProtectionDomain();
        CodeSource codeSource = protectionDomain == null ? null : protectionDomain.getCodeSource();
        codeSourceLocation = codeSource == null ? null : codeSource.getLocation();
    }
    return codeSourceLocation;
}
 
Example 7
Source File: JavaAdapterFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private StaticClass getInstanceAdapterClass(final ProtectionDomain protectionDomain) {
    CodeSource codeSource = protectionDomain.getCodeSource();
    if(codeSource == null) {
        codeSource = MINIMAL_PERMISSION_DOMAIN.getCodeSource();
    }
    StaticClass instanceAdapterClass = instanceAdapters.get(codeSource);
    if(instanceAdapterClass != null) {
        return instanceAdapterClass;
    }
    // Any "unknown source" code source will default to no permission domain.
    final ProtectionDomain effectiveDomain = codeSource.equals(MINIMAL_PERMISSION_DOMAIN.getCodeSource()) ?
            MINIMAL_PERMISSION_DOMAIN : protectionDomain;

    instanceAdapterClass = instanceAdapterGenerator.generateClass(commonLoader, effectiveDomain);
    final StaticClass existing = instanceAdapters.putIfAbsent(codeSource, instanceAdapterClass);
    return existing == null ? instanceAdapterClass : existing;
}
 
Example 8
Source File: ReflectUtils.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
public static String getCodeBase(Class<?> cls) {
    if (cls == null)
        return null;
    ProtectionDomain domain = cls.getProtectionDomain();
    if (domain == null)
        return null;
    CodeSource source = domain.getCodeSource();
    if (source == null)
        return null;
    URL location = source.getLocation();
    if (location == null)
        return null;
    return location.getFile();
}
 
Example 9
Source File: ClassLoader.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
private void postDefineClass(Class<?> c, ProtectionDomain pd)
{
    if (pd.getCodeSource() != null) {
        Certificate certs[] = pd.getCodeSource().getCertificates();
        if (certs != null)
            setSigners(c, certs);
    }
}
 
Example 10
Source File: ReflectUtils.java    From jsongood with Apache License 2.0 5 votes vote down vote up
public static String getCodeBase(Class<?> cls) {
    if (cls == null)
        return null;
    ProtectionDomain domain = cls.getProtectionDomain();
    if (domain == null)
        return null;
    CodeSource source = domain.getCodeSource();
    if (source == null)
        return null;
    URL location = source.getLocation();
    if (location == null)
        return null;
    return location.getFile();
}
 
Example 11
Source File: ClassLoader.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
private void postDefineClass(Class<?> c, ProtectionDomain pd)
{
    if (pd.getCodeSource() != null) {
        Certificate certs[] = pd.getCodeSource().getCertificates();
        if (certs != null)
            setSigners(c, certs);
    }
}
 
Example 12
Source File: LangUtils.java    From pixymeta-android with Eclipse Public License 1.0 5 votes vote down vote up
/** Java language specific classes return null cSource */
public static URL getLoadedClassLocation(Class<?> cls) {
	ProtectionDomain pDomain = cls.getProtectionDomain();
	CodeSource cSource = pDomain.getCodeSource();
	URL loc = (cSource==null)?null:cSource.getLocation(); 
	
	return loc; 
}
 
Example 13
Source File: ReflectUtils.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public static String getCodeBase(Class<?> cls) {
	if (cls == null)
		return null;
	ProtectionDomain domain = cls.getProtectionDomain();
	if (domain == null)
		return null;
	CodeSource source = domain.getCodeSource();
	if (source == null)
		return null;
	URL location = source.getLocation();
	if (location == null)
		return null;
	return location.getFile();
}
 
Example 14
Source File: ExtendedAnnotationTest.java    From annotation-tools with MIT License 5 votes vote down vote up
public String nameExpected(String className) {
  Class cls = this.getClass();
  ProtectionDomain pr = cls.getProtectionDomain();
  CodeSource cs = pr.getCodeSource();
  URL loc = cs.getLocation();
  String s = loc.toString();
  if(s.startsWith("file:")) {
    s = s.substring(5);
  }
  // in the default asmx ant script, class will be run from something like
  // asmx/output/... so just cut things off at asmx
  s = s.substring(0, s.lastIndexOf("asmx")+4);
  return s+"/test/conform/cases/"+className+".expected";
}
 
Example 15
Source File: MCRServletContainerInitializer.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private static String getSource(final Class<? extends MCRServletContainerInitializer> clazz) {
    if (clazz == null) {
        return null;
    }
    ProtectionDomain protectionDomain = clazz.getProtectionDomain();
    CodeSource codeSource = protectionDomain.getCodeSource();
    if (codeSource == null) {
        LogManager.getLogger().warn("Cannot get CodeSource.");
        return null;
    }
    URL location = codeSource.getLocation();
    String fileName = location.getFile();
    File sourceFile = new File(fileName);
    return sourceFile.getName();
}
 
Example 16
Source File: ClassLoader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void postDefineClass(Class<?> c, ProtectionDomain pd)
{
    if (pd.getCodeSource() != null) {
        Certificate certs[] = pd.getCodeSource().getCertificates();
        if (certs != null)
            setSigners(c, certs);
    }
}
 
Example 17
Source File: ClassLoader.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private String defineClassSourceLocation(ProtectionDomain pd)
{
    CodeSource cs = pd.getCodeSource();
    String source = null;
    if (cs != null && cs.getLocation() != null) {
        source = cs.getLocation().toString();
    }
    return source;
}
 
Example 18
Source File: ClassLoader.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
private void postDefineClass(Class<?> c, ProtectionDomain pd)
{
    if (pd.getCodeSource() != null) {
        Certificate certs[] = pd.getCodeSource().getCertificates();
        if (certs != null)
            setSigners(c, certs);
    }
}
 
Example 19
Source File: ClassLoader.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private String defineClassSourceLocation(ProtectionDomain pd)
{
    CodeSource cs = pd.getCodeSource();
    String source = null;
    if (cs != null && cs.getLocation() != null) {
        source = cs.getLocation().toString();
    }
    return source;
}
 
Example 20
Source File: PluginSandboxPolicy.java    From rapidminer-studio with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Checks whether the given domain is unknown. In that case, restrict everything!
 *
 * @param domain
 *            the domain in question, may be {@code null}
 * @return {@code true} if the domain is unknown; {@code false} otherwise
 *
 */
private static boolean isUnknown(ProtectionDomain domain) {
	return domain == null || domain.getCodeSource() == null;
}