下面列出了java.security.ProtectionDomain#getCodeSource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
@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);
}
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;
}
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;
}
/**
* 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;
}
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));
}
}
}
/**
* 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());
}
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();
}
private String defineClassSourceLocation(ProtectionDomain pd)
{
CodeSource cs = pd.getCodeSource();
String source = null;
if (cs != null && cs.getLocation() != null) {
source = cs.getLocation().toString();
}
return source;
}
private void postDefineClass(Class<?> c, ProtectionDomain pd)
{
if (pd.getCodeSource() != null) {
Certificate certs[] = pd.getCodeSource().getCertificates();
if (certs != null)
setSigners(c, certs);
}
}
private String defineClassSourceLocation(ProtectionDomain pd)
{
CodeSource cs = pd.getCodeSource();
String source = null;
if (cs != null && cs.getLocation() != null) {
source = cs.getLocation().toString();
}
return source;
}
private void postDefineClass(Class<?> c, ProtectionDomain pd)
{
if (pd.getCodeSource() != null) {
Certificate certs[] = pd.getCodeSource().getCertificates();
if (certs != null)
setSigners(c, certs);
}
}
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();
}
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";
}
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();
}
/** 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;
}
private void postDefineClass(Class<?> c, ProtectionDomain pd)
{
if (pd.getCodeSource() != null) {
Certificate certs[] = pd.getCodeSource().getCertificates();
if (certs != null)
setSigners(c, certs);
}
}
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();
}
private void postDefineClass(Class<?> c, ProtectionDomain pd)
{
if (pd.getCodeSource() != null) {
Certificate certs[] = pd.getCodeSource().getCertificates();
if (certs != null)
setSigners(c, certs);
}
}
/**
* 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;
}