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

org.zeroturnaround.javarebel.integration.glassfish.GlassfishWebappClassLoaderCBP Maven / Gradle / Ivy

The newest version!
package org.zeroturnaround.javarebel.integration.glassfish;

import org.zeroturnaround.bundled.javassist.CannotCompileException;
import org.zeroturnaround.bundled.javassist.ClassPool;
import org.zeroturnaround.bundled.javassist.CtClass;
import org.zeroturnaround.bundled.javassist.CtConstructor;
import org.zeroturnaround.bundled.javassist.NotFoundException;
import org.zeroturnaround.javarebel.integration.generic.FindClassClassBytecodeProcessor;

/**
 * Transforms org.glassfish.web.loader.WebappClassLoader.
 * 
 * @see FindClassClassBytecodeProcessor
 * 
 * @author Rein Raudjärv
 */
public class GlassfishWebappClassLoaderCBP extends FindClassClassBytecodeProcessor {

  public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception {
    CtConstructor[] constructors = ctClass.getConstructors();
    for (int i = 0; i < constructors.length; i++) {
      CtConstructor constructor = constructors[i];
      constructor.insertAfter("$0.started = true;");
    }
    
    super.process(cp, cl, ctClass);
    
    for (int i = 0; i < constructors.length; i++) {
      CtConstructor constructor = constructors[i];
      constructor.insertAfter("$0.started = false;");
    }
    
    ctClass.getDeclaredMethod("findLoadedResource").setBody(
        "{ " +
        "  java.net.URL url = findResource($1);" +
        "  try {" +
        "    return url != null ? url.openStream() : null;" +
        "  } catch (java.io.IOException e) {" +
        "    return null;" +
        "  }" +
        "}");
    
    cp.importPackage("org.zeroturnaround.javarebel");
    
    processReinit(cp, ctClass);
    processStop(ctClass);
  }

  private void processReinit(ClassPool cp, CtClass ctClass) throws CannotCompileException {
    try {
      ctClass.getDeclaredMethod("addRepository", cp.get(new String[] { "java.net.URL" })).insertBefore(
          "{" +
          "  IntegrationFactory.getInstance().reinitializeClassLoader($0);" +
          "}");
    } catch (NotFoundException e) {
    }
    try {
      ctClass.getDeclaredMethod("addRepository", cp.get(new String[] { "java.lang.String", "java.io.File" })).insertBefore(
          "{" +
          "  IntegrationFactory.getInstance().reinitializeClassLoader($0);" +
          "}");
    } catch (NotFoundException e) {
    }
    try {
      ctClass.getDeclaredMethod("addJar").insertBefore(
          "{" +
          "  IntegrationFactory.getInstance().reinitializeClassLoader($0);" +
          "}");
    } catch (NotFoundException e) {
    }
  }

  private void processStop(CtClass ctClass) throws CannotCompileException {
    try {
      ctClass.getDeclaredMethod("stop").insertBefore(
          "{" +
          "  IntegrationFactory.getInstance().unregisterClassLoader($0);" +
          "}");
    } catch (NotFoundException e) {
    }
  }
  
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy