use of java.security.ProtectionDomain in project cglib by cglib.
the class AbstractClassGenerator method generate.
protected Class generate(ClassLoaderData data) {
Class gen;
Object save = CURRENT.get();
CURRENT.set(this);
try {
ClassLoader classLoader = data.getClassLoader();
if (classLoader == null) {
throw new IllegalStateException("ClassLoader is null while trying to define class " + getClassName() + ". It seems that the loader has been expired from a weak reference somehow. " + "Please file an issue at cglib's issue tracker.");
}
synchronized (classLoader) {
String name = generateClassName(data.getUniqueNamePredicate());
data.reserveName(name);
this.setClassName(name);
}
if (attemptLoad) {
try {
gen = classLoader.loadClass(getClassName());
return gen;
} catch (ClassNotFoundException e) {
// ignore
}
}
byte[] b = strategy.generate(this);
String className = ClassNameReader.getClassName(new ClassReader(b));
ProtectionDomain protectionDomain = getProtectionDomain();
synchronized (classLoader) {
// just in case
if (protectionDomain == null) {
gen = ReflectUtils.defineClass(className, b, classLoader);
} else {
gen = ReflectUtils.defineClass(className, b, classLoader, protectionDomain);
}
}
return gen;
} catch (RuntimeException e) {
throw e;
} catch (Error e) {
throw e;
} catch (Exception e) {
throw new CodeGenerationException(e);
} finally {
CURRENT.set(save);
}
}
use of java.security.ProtectionDomain in project elasticsearch by elastic.
the class ESPolicyUnitTests method testNullCodeSource.
/**
* Test policy with null codesource.
* <p>
* This can happen when restricting privileges with doPrivileged,
* even though ProtectionDomain's ctor javadocs might make you think
* that the policy won't be consulted.
*/
public void testNullCodeSource() throws Exception {
assumeTrue("test cannot run with security manager", System.getSecurityManager() == null);
// create a policy with AllPermission
Permission all = new AllPermission();
PermissionCollection allCollection = all.newPermissionCollection();
allCollection.add(all);
ESPolicy policy = new ESPolicy(allCollection, Collections.emptyMap(), true);
// restrict ourselves to NoPermission
PermissionCollection noPermissions = new Permissions();
assertFalse(policy.implies(new ProtectionDomain(null, noPermissions), new FilePermission("foo", "read")));
}
use of java.security.ProtectionDomain in project elasticsearch by elastic.
the class ESPolicyUnitTests method testNullLocation.
/**
* test with null location
* <p>
* its unclear when/if this happens, see https://bugs.openjdk.java.net/browse/JDK-8129972
*/
public void testNullLocation() throws Exception {
assumeTrue("test cannot run with security manager", System.getSecurityManager() == null);
PermissionCollection noPermissions = new Permissions();
ESPolicy policy = new ESPolicy(noPermissions, Collections.emptyMap(), true);
assertFalse(policy.implies(new ProtectionDomain(new CodeSource(null, (Certificate[]) null), noPermissions), new FilePermission("foo", "read")));
}
use of java.security.ProtectionDomain in project jetty.project by eclipse.
the class TypeUtil method getLoadedFrom.
/* ------------------------------------------------------------ */
public static Resource getLoadedFrom(Class<?> clazz) {
ProtectionDomain domain = clazz.getProtectionDomain();
if (domain != null) {
CodeSource source = domain.getCodeSource();
if (source != null) {
URL location = source.getLocation();
if (location != null)
return Resource.newResource(location);
}
}
String rname = clazz.getName().replace('.', '/') + ".class";
ClassLoader loader = clazz.getClassLoader();
URL url = (loader == null ? ClassLoader.getSystemClassLoader() : loader).getResource(rname);
if (url != null) {
try {
return Resource.newResource(URIUtil.getJarSource(url.toString()));
} catch (Exception e) {
LOG.debug(e);
}
}
return null;
}
use of java.security.ProtectionDomain in project jetty.project by eclipse.
the class WebAppClassLoaderTest method testClassFileTranslations.
@Test
public void testClassFileTranslations() throws Exception {
final List<Object> results = new ArrayList<Object>();
_loader.addTransformer(new ClassFileTransformer() {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
results.add(loader);
byte[] b = new byte[classfileBuffer.length];
for (int i = 0; i < classfileBuffer.length; i++) b[i] = (byte) (classfileBuffer[i] ^ 0xff);
return b;
}
});
_loader.addTransformer(new ClassFileTransformer() {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
results.add(className);
byte[] b = new byte[classfileBuffer.length];
for (int i = 0; i < classfileBuffer.length; i++) b[i] = (byte) (classfileBuffer[i] ^ 0xff);
return b;
}
});
_context.setParentLoaderPriority(false);
assertCanLoadClass("org.acme.webapp.ClassInJarA");
assertCanLoadClass("org.acme.webapp.ClassInJarB");
assertCanLoadClass("org.acme.other.ClassInClassesC");
assertCanLoadClass("java.lang.String");
assertCantLoadClass("org.eclipse.jetty.webapp.Configuration");
assertThat("Classname Results", results, contains(_loader, "org.acme.webapp.ClassInJarA", _loader, "org.acme.webapp.ClassInJarB", _loader, "org.acme.other.ClassInClassesC"));
}
Aggregations