use of java.security.CodeSource in project streamsx.topology by IBMStreams.
the class DependencyResolver method addClassDependency.
public void addClassDependency(Class<?> clazz) {
CodeSource source = clazz.getProtectionDomain().getCodeSource();
if (source == null)
return;
Path absolutePath = null;
try {
absolutePath = Paths.get(source.getLocation().toURI()).toAbsolutePath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
globalDependencies.add(absolutePath);
}
use of java.security.CodeSource in project jdk8u_jdk by JetBrains.
the class FactoryURLClassLoader method defineClass.
/*
* Defines a Class using the class bytes obtained from the specified
* Resource. The resulting Class must be resolved before it can be
* used.
*/
private Class<?> defineClass(String name, Resource res) throws IOException {
long t0 = System.nanoTime();
int i = name.lastIndexOf('.');
URL url = res.getCodeSourceURL();
if (i != -1) {
String pkgname = name.substring(0, i);
// Check if package already loaded.
Manifest man = res.getManifest();
definePackageInternal(pkgname, man, url);
}
// Now read the class bytes and define the class
java.nio.ByteBuffer bb = res.getByteBuffer();
if (bb != null) {
// Use (direct) ByteBuffer:
CodeSigner[] signers = res.getCodeSigners();
CodeSource cs = new CodeSource(url, signers);
sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
return defineClass(name, bb, cs);
} else {
byte[] b = res.getBytes();
// must read certificates AFTER reading bytes.
CodeSigner[] signers = res.getCodeSigners();
CodeSource cs = new CodeSource(url, signers);
sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0);
return defineClass(name, b, 0, b.length, cs);
}
}
use of java.security.CodeSource in project jdk8u_jdk by JetBrains.
the class MethodUtil method defineClass.
/*
* Define the proxy classes
*/
private Class<?> defineClass(String name, URL url) throws IOException {
byte[] b = getBytes(url);
CodeSource cs = new CodeSource(null, (java.security.cert.Certificate[]) null);
if (!name.equals(TRAMPOLINE)) {
throw new IOException("MethodUtil: bad name " + name);
}
return defineClass(name, b, 0, b.length, cs);
}
use of java.security.CodeSource in project jdk8u_jdk by JetBrains.
the class LoaderHandler method getLoaderAccessControlContext.
/**
* Return the access control context that a loader for the given
* codebase URL path should execute with.
*/
private static AccessControlContext getLoaderAccessControlContext(URL[] urls) {
/*
* The approach used here is taken from the similar method
* getAccessControlContext() in the sun.applet.AppletPanel class.
*/
// begin with permissions granted to all code in current policy
PermissionCollection perms = java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<PermissionCollection>() {
public PermissionCollection run() {
CodeSource codesource = new CodeSource(null, (java.security.cert.Certificate[]) null);
Policy p = java.security.Policy.getPolicy();
if (p != null) {
return p.getPermissions(codesource);
} else {
return new Permissions();
}
}
});
// createClassLoader permission needed to create loader in context
perms.add(new RuntimePermission("createClassLoader"));
// add permissions to read any "java.*" property
perms.add(new java.util.PropertyPermission("java.*", "read"));
// add permissions reuiqred to load from codebase URL path
addPermissionsForURLs(urls, perms, true);
/*
* Create an AccessControlContext that consists of a single
* protection domain with only the permissions calculated above.
*/
ProtectionDomain pd = new ProtectionDomain(new CodeSource((urls.length > 0 ? urls[0] : null), (java.security.cert.Certificate[]) null), perms);
return new AccessControlContext(new ProtectionDomain[] { pd });
}
use of java.security.CodeSource in project JMRI by JMRI.
the class FileUtilSupport method getJmriJarFile.
/**
* Get the JMRI distribution jar file.
*
* @return the JAR file containing the JMRI library or null if not running
* from a JAR file
*/
public JarFile getJmriJarFile() {
if (jarPath == null) {
CodeSource sc = FileUtilSupport.class.getProtectionDomain().getCodeSource();
if (sc != null) {
jarPath = sc.getLocation().toString();
if (jarPath.startsWith("jar:file:")) {
// 9 = length of jar:file:
jarPath = jarPath.substring(9, jarPath.lastIndexOf("!"));
} else {
log.info("Running from classes not in jar file.");
// set to empty String to bypass search
jarPath = "";
return null;
}
log.debug("jmri.jar path is {}", jarPath);
}
if (jarPath == null) {
log.error("Unable to locate jmri.jar");
// set to empty String to bypass search
jarPath = "";
return null;
}
}
if (!jarPath.isEmpty()) {
try {
return new JarFile(jarPath);
} catch (IOException ex) {
log.error("Unable to open jmri.jar", ex);
return null;
}
}
return null;
}
Aggregations