本文整理汇总了Java中java.security.CodeSource类的典型用法代码示例。如果您正苦于以下问题:Java CodeSource类的具体用法?Java CodeSource怎么用?Java CodeSource使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodeSource类属于java.security包,在下文中一共展示了CodeSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getClasspathForClass
import java.security.CodeSource; //导入依赖的package包/类
public static File getClasspathForClass(Class<?> targetClass) {
URI location;
try {
CodeSource codeSource = targetClass.getProtectionDomain().getCodeSource();
if (codeSource != null && codeSource.getLocation() != null) {
location = codeSource.getLocation().toURI();
if (location.getScheme().equals("file")) {
return new File(location);
}
}
if (targetClass.getClassLoader() != null) {
String resourceName = targetClass.getName().replace('.', '/') + ".class";
URL resource = targetClass.getClassLoader().getResource(resourceName);
if (resource != null) {
return getClasspathForResource(resource, resourceName);
}
}
throw new GradleException(String.format("Cannot determine classpath for class %s.", targetClass.getName()));
} catch (URISyntaxException e) {
throw UncheckedException.throwAsUncheckedException(e);
}
}
示例2: getJavacApiJarClasspath
import java.security.CodeSource; //导入依赖的package包/类
private static ClassPath getJavacApiJarClasspath() {
Reference<ClassPath> r = javacApiClasspath;
ClassPath res = r.get();
if (res != null) {
return res;
}
if (r == NONE) {
return null;
}
CodeSource codeSource = Modifier.class.getProtectionDomain().getCodeSource();
URL javacApiJar = codeSource != null ? codeSource.getLocation() : null;
if (javacApiJar != null) {
Logger.getLogger(DeclarativeHintsParser.class.getName()).log(Level.FINE, "javacApiJar={0}", javacApiJar);
File aj = FileUtil.archiveOrDirForURL(javacApiJar);
if (aj != null) {
res = ClassPathSupport.createClassPath(FileUtil.urlForArchiveOrDir(aj));
javacApiClasspath = new WeakReference<>(res);
return res;
}
}
javacApiClasspath = NONE;
return null;
}
示例3: getCodeBase
import java.security.CodeSource; //导入依赖的package包/类
/**
* 得到类所在地址,可以是文件,也可以是jar包
*
* @param cls
* the cls
* @return the code base
*/
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();
}
示例4: getInstanceAdapterClass
import java.security.CodeSource; //导入依赖的package包/类
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;
}
示例5: getContext
import java.security.CodeSource; //导入依赖的package包/类
private static AccessControlContext getContext(File[] dirs)
throws IOException
{
PathPermissions perms =
new PathPermissions(dirs);
ProtectionDomain domain = new ProtectionDomain(
new CodeSource(perms.getCodeBase(),
(java.security.cert.Certificate[]) null),
perms);
AccessControlContext acc =
new AccessControlContext(new ProtectionDomain[] { domain });
return acc;
}
示例6: getPermissions
import java.security.CodeSource; //导入依赖的package包/类
/**
* Get the Permissions for a CodeSource. If this instance
* of WebappClassLoaderBase is for a web application context,
* add read FilePermission or JndiPermissions for the base
* directory (if unpacked),
* the context URL, and jar file resources.
*
* @param codeSource where the code was loaded from
* @return PermissionCollection for CodeSource
*/
@Override
protected PermissionCollection getPermissions(CodeSource codeSource) {
String codeUrl = codeSource.getLocation().toString();
PermissionCollection pc;
if ((pc = loaderPC.get(codeUrl)) == null) {
pc = super.getPermissions(codeSource);
if (pc != null) {
Iterator<Permission> perms = permissionList.iterator();
while (perms.hasNext()) {
Permission p = perms.next();
pc.add(p);
}
loaderPC.put(codeUrl,pc);
}
}
return (pc);
}
示例7: checkCerts
import java.security.CodeSource; //导入依赖的package包/类
private void checkCerts(String name, CodeSource cs) {
int i = name.lastIndexOf('.');
String pname = (i == -1) ? "" : name.substring(0, i);
Certificate[] certs = null;
if (cs != null) {
certs = cs.getCertificates();
}
Certificate[] pcerts = null;
if (parallelLockMap == null) {
synchronized (this) {
pcerts = package2certs.get(pname);
if (pcerts == null) {
package2certs.put(pname, (certs == null? nocerts:certs));
}
}
} else {
pcerts = ((ConcurrentHashMap<String, Certificate[]>)package2certs).
putIfAbsent(pname, (certs == null? nocerts:certs));
}
if (pcerts != null && !compareCerts(pcerts, certs)) {
throw new SecurityException("class \""+ name +
"\"'s signer information does not match signer information of other classes in the same package");
}
}
示例8: check
import java.security.CodeSource; //导入依赖的package包/类
@Override
public boolean check(Permission permission) {
if (!Globals.IS_SECURITY_ENABLED) {
return true;
}
Policy currentPolicy = Policy.getPolicy();
if (currentPolicy != null) {
ResourceEntry entry = findResourceInternal("/", "/", false);
if (entry != null) {
CodeSource cs = new CodeSource(entry.codeBase, (java.security.cert.Certificate[]) null);
PermissionCollection pc = currentPolicy.getPermissions(cs);
if (pc.implies(permission)) {
return true;
}
}
}
return false;
}
示例9: getPermissions
import java.security.CodeSource; //导入依赖的package包/类
/**
* Get the Permissions for a CodeSource. If this instance of
* WebappClassLoaderBase is for a web application context, add read
* FilePermission or JndiPermissions for the base directory (if unpacked),
* the context URL, and jar file resources.
*
* @param codeSource
* where the code was loaded from
* @return PermissionCollection for CodeSource
*/
@Override
protected PermissionCollection getPermissions(CodeSource codeSource) {
String codeUrl = codeSource.getLocation().toString();
PermissionCollection pc;
if ((pc = loaderPC.get(codeUrl)) == null) {
pc = super.getPermissions(codeSource);
if (pc != null) {
Iterator<Permission> perms = permissionList.iterator();
while (perms.hasNext()) {
Permission p = perms.next();
pc.add(p);
}
loaderPC.put(codeUrl, pc);
}
}
return (pc);
}
示例10: getPermissions
import java.security.CodeSource; //导入依赖的package包/类
/**
* Get the Permissions for a CodeSource. If this instance
* of WebappClassLoader is for a web application context,
* add read FilePermission or JndiPermissions for the base
* directory (if unpacked),
* the context URL, and jar file resources.
*
* @param codeSource where the code was loaded from
* @return PermissionCollection for CodeSource
*/
protected PermissionCollection getPermissions(CodeSource codeSource) {
String codeUrl = codeSource.getLocation().toString();
PermissionCollection pc;
if ((pc = (PermissionCollection)loaderPC.get(codeUrl)) == null) {
pc = super.getPermissions(codeSource);
if (pc != null) {
Iterator perms = permissionList.iterator();
while (perms.hasNext()) {
Permission p = (Permission)perms.next();
pc.add(p);
}
loaderPC.put(codeUrl,pc);
}
}
return (pc);
}
示例11: getApplicationDirectory
import java.security.CodeSource; //导入依赖的package包/类
public static String getApplicationDirectory()
{
String jarDir = null;
try
{
CodeSource codeSource = MiscUtils.class.getProtectionDomain().getCodeSource();
File jarFile = new File(URLDecoder.decode(codeSource.getLocation().toURI().getPath(), "UTF-8"));
jarDir = jarFile.getParentFile().getPath();
} catch (URISyntaxException | UnsupportedEncodingException ex)
{
ex.printStackTrace();
}
return jarDir + "/";
}
示例12: getBaseLocatoin
import java.security.CodeSource; //导入依赖的package包/类
public static URL getBaseLocatoin(Class clazz) {
if (clazz == null) {
return null;
}
CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
if (codeSource != null) {
URL locationURL = codeSource.getLocation();
String location = locationURL.toString();
int i = location.indexOf("!/");
if (i == -1) {
return locationURL;
} else {
try {
return new URL(location.substring(0, i));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
} else {
return null;
}
}
示例13: getCodeSource
import java.security.CodeSource; //导入依赖的package包/类
/**
* Get the code source file or class path of the Class passed in.
*
* @param clazz
* Class to find.
* @return Jar file name or class path.
*/
public static String getCodeSource(Class<?> clazz) {
ProtectionDomain protectionDomain = clazz.getProtectionDomain();
if (protectionDomain == null || protectionDomain.getCodeSource() == null) {
return null;
}
CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
URL location = codeSource.getLocation();
if (location == null) {
return null;
}
String path = codeSource.getLocation().toExternalForm();
if (path.endsWith(".jar") && path.contains("/")) {
return path.substring(path.lastIndexOf('/') + 1);
}
return path;
}
示例14: getCodeSources
import java.security.CodeSource; //导入依赖的package包/类
CodeSource[] getCodeSources(URL url) {
ensureInitialization();
if (jv != null) {
return jv.getCodeSources(this, url);
}
/*
* JAR file has no signed content. Is there a non-signing
* code source?
*/
Enumeration<String> unsigned = unsignedEntryNames();
if (unsigned.hasMoreElements()) {
return new CodeSource[]{JarVerifier.getUnsignedCS(url)};
} else {
return null;
}
}
示例15: getPluginPermissions
import java.security.CodeSource; //导入依赖的package包/类
@Test
public void getPluginPermissions() throws Exception {
CodeSource cs = mock(CodeSource.class);
ProtectionDomain domain = new ProtectionDomain(cs, null, new PluginClassLoader(new URL[0]), null);
SandboxSecurityPolicy policy = new SandboxSecurityPolicy();
PermissionCollection permissions = policy.getPermissions(domain);
Assert.assertFalse(permissions.elements().hasMoreElements());
}