Search in sources :

Example 1 with BytecodePreprocessor

use of com.sun.appserv.BytecodePreprocessor in project Payara by payara.

the class WebappClassLoader method addTransformer.

/**
 * Add a new ClassFileTransformer to this class loader. This transfomer should be called for
 * each class loading event.
 *
 * @param transformer new class file transformer to do byte code enhancement.
 */
public void addTransformer(final ClassFileTransformer transformer) {
    final WebappClassLoader cl = this;
    addByteCodePreprocessor(new BytecodePreprocessor() {

        /*
                 * This class adapts ClassFileTransformer to ByteCodePreprocessor that
                 * is used inside WebappClassLoader.
                 */
        public boolean initialize(Hashtable parameters) {
            return true;
        }

        public byte[] preprocess(String resourceName, byte[] classBytes) {
            try {
                // convert java/lang/Object.class to java/lang/Object
                String classname = resourceName.substring(0, // ".class" size = 6
                resourceName.length() - 6);
                byte[] newBytes = transformer.transform(cl, classname, null, null, classBytes);
                // to return non-null byte array.
                return newBytes == null ? classBytes : newBytes;
            } catch (IllegalClassFormatException e) {
                logger.logp(Level.WARNING, "WebModuleListener$InstrumentableClassLoader$BytecodePreprocessor", "preprocess", e.getMessage());
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : BytecodePreprocessor(com.sun.appserv.BytecodePreprocessor) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException)

Example 2 with BytecodePreprocessor

use of com.sun.appserv.BytecodePreprocessor in project Payara by payara.

the class WebappClassLoader method findClass.

// ---------------------------------------------------- ClassLoader Methods
/**
 * Find the specified class in our local repositories, if possible.  If
 * not found, throw <code>ClassNotFoundException</code>.
 *
 * @param name Name of the class to be loaded
 *
 * @exception ClassNotFoundException if the class was not found
 */
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
    if (logger.isLoggable(Level.FINER))
        logger.log(Level.FINER, "    findClass(" + name + ")");
    // if (securityManager != null) {
    if (securityManager != null && packageDefinitionEnabled) {
        // END PE 4989455
        int i = name.lastIndexOf('.');
        if (i >= 0) {
            try {
                if (logger.isLoggable(Level.FINER))
                    logger.log(Level.FINER, "      securityManager.checkPackageDefinition");
                securityManager.checkPackageDefinition(name.substring(0, i));
            } catch (Exception se) {
                if (logger.isLoggable(Level.FINER))
                    logger.log(Level.FINER, "      -->Exception-->ClassNotFoundException", se);
                throw new ClassNotFoundException(name, se);
            }
        }
    }
    // Ask our superclass to locate this class, if possible
    // (throws ClassNotFoundException if it is not found)
    Class<?> clazz = null;
    try {
        if (logger.isLoggable(Level.FINER))
            logger.log(Level.FINER, "      findClassInternal(" + name + ")");
        try {
            ResourceEntry entry = findClassInternal(name);
            // Create the code source object
            CodeSource codeSource = new CodeSource(entry.codeBase, entry.certificates);
            synchronized (this) {
                if (entry.loadedClass == null) {
                    /* START GlassFish [680]
                        clazz = defineClass(name, entry.binaryContent, 0,
                                entry.binaryContent.length,
                                codeSource);
                        */
                    // START GlassFish [680]
                    // We use a temporary byte[] so that we don't change
                    // the content of entry in case bytecode
                    // preprocessing takes place.
                    byte[] binaryContent = entry.binaryContent;
                    if (!byteCodePreprocessors.isEmpty()) {
                        // ByteCodePreprpcessor expects name as
                        // java/lang/Object.class
                        String resourceName = name.replace('.', '/') + ".class";
                        for (BytecodePreprocessor preprocessor : byteCodePreprocessors) {
                            binaryContent = preprocessor.preprocess(resourceName, binaryContent);
                        }
                    }
                    clazz = defineClass(name, binaryContent, 0, binaryContent.length, codeSource);
                    // END GlassFish [680]
                    entry.loadedClass = clazz;
                    entry.binaryContent = null;
                    entry.source = null;
                    entry.codeBase = null;
                    entry.manifest = null;
                    entry.certificates = null;
                } else {
                    clazz = entry.loadedClass;
                }
            }
        } catch (ClassNotFoundException cnfe) {
            if (!hasExternalRepositories) {
                throw cnfe;
            }
        } catch (UnsupportedClassVersionError ucve) {
            throw new UnsupportedClassVersionError(getString(LogFacade.UNSUPPORTED_VERSION, name, getJavaVersion()));
        } catch (AccessControlException ace) {
            if (logger.isLoggable(Level.WARNING)) {
                logger.log(Level.WARNING, LogFacade.FIND_CLASS_INTERNAL_SECURITY_EXCEPTION, new Object[] { name, ace.getMessage() });
            }
            throw new ClassNotFoundException(name, ace);
        } catch (RuntimeException rex) {
            throw rex;
        } catch (Error err) {
            throw err;
        } catch (Throwable t) {
            throw new RuntimeException(getString(LogFacade.UNABLE_TO_LOAD_CLASS, name, t.toString()), t);
        }
        if ((clazz == null) && hasExternalRepositories) {
            try {
                clazz = super.findClass(name);
            } catch (AccessControlException ace) {
                if (logger.isLoggable(Level.WARNING)) {
                    String msg = getString(LogFacade.FIND_CLASS_INTERNAL_SECURITY_EXCEPTION, new Object[] { name, ace.getMessage() });
                    logger.log(Level.WARNING, msg, ace);
                }
                throw new ClassNotFoundException(name, ace);
            } catch (RuntimeException e) {
                if (logger.isLoggable(Level.FINER))
                    logger.log(Level.FINER, "      -->RuntimeException Rethrown", e);
                throw e;
            }
        }
        if (clazz == null) {
            if (logger.isLoggable(Level.FINER))
                logger.log(Level.FINER, "    --> Returning ClassNotFoundException");
            throw new ClassNotFoundException(name);
        }
    } catch (ClassNotFoundException e) {
        if (logger.isLoggable(Level.FINER))
            logger.log(Level.FINER, "    --> Passing on ClassNotFoundException");
        throw e;
    }
    // Return the class we have located
    if (logger.isLoggable(Level.FINER))
        logger.log(Level.FINER, "      Returning class " + clazz);
    if (logger.isLoggable(Level.FINER)) {
        ClassLoader cl;
        if (securityManager != null) {
            cl = AccessController.doPrivileged(new PrivilegedGetClassLoader(clazz));
        } else {
            cl = clazz.getClassLoader();
        }
        logger.log(Level.FINER, "      Loaded by " + cl);
    }
    return (clazz);
}
Also used : NamingException(javax.naming.NamingException) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedURLException(java.net.MalformedURLException) BytecodePreprocessor(com.sun.appserv.BytecodePreprocessor) URLClassLoader(java.net.URLClassLoader) InstrumentableClassLoader(org.glassfish.api.deployment.InstrumentableClassLoader)

Aggregations

BytecodePreprocessor (com.sun.appserv.BytecodePreprocessor)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 URLClassLoader (java.net.URLClassLoader)1 NamingException (javax.naming.NamingException)1 InstrumentableClassLoader (org.glassfish.api.deployment.InstrumentableClassLoader)1