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

org.zeroturnaround.javarebel.integration.sap.SapResourceLoaderCBP Maven / Gradle / Ivy

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

import org.zeroturnaround.bundled.javassist.ClassPool;
import org.zeroturnaround.bundled.javassist.CtClass;
import org.zeroturnaround.bundled.javassist.CtConstructor;
import org.zeroturnaround.bundled.javassist.CtMethod;
import org.zeroturnaround.bundled.javassist.CtNewMethod;
import org.zeroturnaround.bundled.javassist.NotFoundException;
import org.zeroturnaround.javarebel.Integration;
import org.zeroturnaround.javarebel.Logger;
import org.zeroturnaround.javarebel.LoggerFactory;
import org.zeroturnaround.javarebel.integration.generic.FindResourceClassResourceSource;
import org.zeroturnaround.javarebel.integration.support.JavassistClassBytecodeProcessor;


/**
 * Bytecode processor for the SAP WAS class loader integration.
 * @see Integration
 * @see FindResourceClassResourceSource
 * @see org.zeroturnaround.javarebel.integration.generic.FindClassClassBytecodeProcessor
 *
 * @author Rolf Kulemann (RoKu)
 */
public class SapResourceLoaderCBP extends JavassistClassBytecodeProcessor {
  private Logger log = LoggerFactory.getInstance();

  public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception {
    cp.importPackage("java.util");
    cp.importPackage("java.net"); 
    cp.importPackage("org.zeroturnaround.javarebel");
    cp.importPackage("org.zeroturnaround.javarebel.support");
    cp.importPackage("org.zeroturnaround.javarebel.integration.util"); 
    cp.importPackage("org.zeroturnaround.javarebel.integration.sap"); 

    ctClass.addInterface(cp.get(SapResourceSource.class.getName()));
    
    CtConstructor[] constructors = ctClass.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++) {
      CtConstructor constructor = constructors[i];
      constructor.insertAfter(
          "IntegrationFactory.getInstance().registerClassLoader($0, new SapClassResourceSourceImpl((SapResourceSource) $0));");
    }
    
    CtMethod m = ctClass.getDeclaredMethod("loadLocalClass");        

    m.insertBefore(
        "{ " +
        "synchronized ($0) {" + 
        "    Class result =" + 
        "      findLoadedClass($1);" + 
        "    if (result != null)" + 
        "      return result;" + 
        "    result = " + 
        "      org.zeroturnaround.javarebel.IntegrationFactory.getInstance()" + 
        "        .findReloadableClass($0, $1);" + 
        "    if (result != null)" + 
        "      return result;" + 
    "}}");

    try {
      m = ctClass.getDeclaredMethod("getLocalResource", cp.get(new String[] {String.class.getName()}));
      m.insertBefore(
          "{" + 
          "  org.zeroturnaround.javarebel.Integration integration = org.zeroturnaround.javarebel.IntegrationFactory.getInstance();" +
          "  if (integration.isResourceReplaced($0, $1))" +
          "    return integration.findResource($0, $1);" + 
      "}");
    }
    catch (NotFoundException e) {
      // skip
    }
    
    try {
      m = ctClass.getDeclaredMethod("getLocalResourceAsStream", cp.get(new String[] {String.class.getName()}));
      m.insertBefore(
          "{" + 
          "  org.zeroturnaround.javarebel.Integration integration = org.zeroturnaround.javarebel.IntegrationFactory.getInstance();" +
          "  if (integration.isResourceReplaced($0, $1))" +
          "    return ResourceUtil.asInputStream(integration.findResource($0, $1));" + 
      "}");
    }
    catch (NotFoundException e) {
      // skip
    }

    try {
      m = ctClass.getDeclaredMethod("findLocalResources", cp.get(new String[] {String.class.getName()}));
      m.insertBefore(
          "{" + 
          "  org.zeroturnaround.javarebel.Integration integration = org.zeroturnaround.javarebel.IntegrationFactory.getInstance();" +
          "  if (integration.isResourceReplaced($0, $1))" +
          "    return integration.findResources($0, $1);" + 
      "}");
    }
    catch (NotFoundException e) {
      // skip
    }
    
    ctClass.addMethod(CtNewMethod.make(
        "public Resource getRebelLocalResource(String resourcePath) {" +
        "  URL url = getLocalResource(resourcePath);" +
        "  if (url == null) return null;" +
        "  return ResourceUtil.asResource(url);" +
        "}", ctClass));
    
    ctClass.addMethod(CtNewMethod.make("public Resource[] getRebelLocalResources(String resourcePath) {"
        + " URL[] urls = ResourceUtil.toURLs(findLocalResources(resourcePath));\n"
        + " Resource[] result = new Resource[urls.length];\n" + 
          "    for (int i = 0; i < urls.length; i++) {\n" + 
          "      result[i] = ResourceUtil.asResource(urls[i]);\n" +  
          "    }\n" + 
          "    return result;" +
        "}", ctClass));
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy