All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.obermuhlner.scriptengine.java.MemoryClassLoader Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package ch.obermuhlner.scriptengine.java;

import java.net.*;
import java.security.*;
import java.security.cert.Certificate;
import java.util.Map;

/**
 * A {@link ClassLoader} that loads classes from memory.
 */
public class MemoryClassLoader extends ClassLoader {

    /**
     * URL used to identify the {@link CodeSource} of the {@link ProtectionDomain} used by this class loader.
     *
     * This is useful to identify classes loaded by this class loader in a policy file.
     * 
grant codeBase "jrt:/ch.obermuhlner.scriptengine.java/memory-class" {
    permission java.lang.RuntimePermission "exitVM";
};
     * 
*/ public static final String MEMORY_CLASS_URL = "jrt:/ch.obermuhlner.scriptengine.java/memory-class"; private ProtectionDomain protectionDomain; private Map mapClassBytes; /** * Creates a {@link MemoryClassLoader}. * * @param mapClassBytes the map of class names to compiled classes * @param parent the parent {@link ClassLoader} */ public MemoryClassLoader(Map mapClassBytes, ClassLoader parent) { super(parent); this.mapClassBytes = mapClassBytes; try { URL url = new URL(MEMORY_CLASS_URL); CodeSource codeSource = new CodeSource(url, (Certificate[]) null); protectionDomain = new ProtectionDomain(codeSource, null, this, new Principal[0]); } catch (MalformedURLException e) { throw new RuntimeException(e); } } @Override public Class loadClass(String name) throws ClassNotFoundException { byte[] bytes = mapClassBytes.get(name); if (bytes == null) { return super.loadClass(name); } return defineClass(name, bytes, 0, bytes.length, protectionDomain); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy