Search in sources :

Example 36 with OnClassLoadEvent

use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.

the class OnClassLoadEventHandlerTest method testInitMethod.

@Test
public void testInitMethod() throws Exception {
    final ClassLoader appClassLoader = getClass().getClassLoader();
    context.checking(new Expectations() {

        {
            allowing(pluginManager).getHotswapTransformer();
            will(returnValue(hotswapTransformer));
            allowing(pluginManager).getPluginRegistry();
            will(returnValue(pluginRegistry));
            allowing(pluginRegistry).getAppClassLoader(with(any(Object.class)));
            will(returnValue(appClassLoader));
            oneOf(hotswapTransformer).registerTransformer(with(appClassLoader), with("org.hotswap.example.type"), with(any(ClassFileTransformer.class)));
        }
    });
    OnClassLoadedHandler onClassLoadedHandler = new OnClassLoadedHandler(pluginManager);
    SimplePlugin simplePlugin = new SimplePlugin();
    Method method = SimplePlugin.class.getMethod("transform");
    PluginAnnotation<OnClassLoadEvent> pluginAnnotation = new PluginAnnotation<OnClassLoadEvent>(SimplePlugin.class, simplePlugin, method.getAnnotation(OnClassLoadEvent.class), method);
    assertTrue("Init successful", onClassLoadedHandler.initMethod(pluginAnnotation));
}
Also used : Expectations(org.jmock.Expectations) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent) SimplePlugin(org.hotswap.agent.testData.SimplePlugin) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 37 with OnClassLoadEvent

use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.

the class BeansDeployerTransformer method transform.

/**
 * Basic CdiArchive transformation.
 *
 * @param clazz
 * @param classPool
 * @throws NotFoundException
 * @throws CannotCompileException
 */
@OnClassLoadEvent(classNameRegexp = "org.apache.webbeans.config.BeansDeployer")
public static void transform(CtClass clazz, ClassPool classPool) throws NotFoundException, CannotCompileException {
    StringBuilder src = new StringBuilder(" if (deployed) {");
    src.append("ClassLoader curCl = Thread.currentThread().getContextClassLoader();");
    src.append(PluginManagerInvoker.buildInitializePlugin(OwbPlugin.class, "curCl"));
    src.append(PluginManagerInvoker.buildCallPluginMethod("curCl", OwbPlugin.class, "init"));
    src.append(PluginManagerInvoker.buildCallPluginMethod("curCl", OwbPlugin.class, "registerBeansXmls", "$1.getBeanXmls()", "java.util.Set"));
    src.append("}");
    CtMethod startApplication = clazz.getDeclaredMethod("deploy");
    startApplication.insertAfter(src.toString());
    LOGGER.debug("Class '{}' patched with OwbPlugin registration.", clazz.getName());
}
Also used : OwbPlugin(org.hotswap.agent.plugin.owb.OwbPlugin) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 38 with OnClassLoadEvent

use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.

the class JettyPlugin method patchWebXmlConfiguration.

/**
 * Plugin initialization step needs to be fine tuned. It can be intialized only AFTER the classloader
 * already knows about hotswap-agent.properties file (i.e. after webapp basic path is added to the classloader),
 * but BEFORE first servlet is initialized.
 *
 * WebXmlConfiguration seems to be good place which should work in most setups. The plugin is intialized before
 * web.xml file is processed - basic paths should be known, but nothing is processed yet.
 *
 * Application classloader is processed during plugin initialization. It means that other plugins triggered
 * on plugin init should fire as well - for jetty is important core watchResources plugin, which will handle
 * extraClassPath and watchResources configuration properties (jetty fortunately depends only on basic
 * URLClassLoader behaviour which is handled by that plugin).
 */
@OnClassLoadEvent(classNameRegexp = "org.eclipse.jetty.webapp.WebXmlConfiguration")
public static void patchWebXmlConfiguration(CtClass ctClass) throws NotFoundException, CannotCompileException, ClassNotFoundException {
    try {
        // after application context initialized, but before processing started
        CtMethod doStart = ctClass.getDeclaredMethod("configure");
        // init the plugin
        String src = PluginManagerInvoker.buildInitializePlugin(JettyPlugin.class, "context.getClassLoader()");
        src += PluginManagerInvoker.buildCallPluginMethod("context.getClassLoader()", JettyPlugin.class, "init", "context", "java.lang.Object");
        doStart.insertBefore(src);
    } catch (NotFoundException e) {
        LOGGER.warning("org.eclipse.jetty.webapp.WebAppContext does not contain startContext method. Jetty plugin will be disabled.\n" + "*** This is Ok, Jetty plugin handles only special properties ***");
        return;
    }
}
Also used : NotFoundException(org.hotswap.agent.javassist.NotFoundException) CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 39 with OnClassLoadEvent

use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.

the class Log4j2Plugin method registerConfigurator.

@OnClassLoadEvent(classNameRegexp = "org.apache.logging.log4j.core.LoggerContext")
public static void registerConfigurator(ClassPool classPool, CtClass ctClass) throws NotFoundException, CannotCompileException {
    // fallback to the old version (<2.3) of Log4j2
    CtMethod m = ctClass.getDeclaredMethod("setConfiguration", new CtClass[] { classPool.get("org.apache.logging.log4j.core.config.Configuration") });
    m.insertAfter(PluginManagerInvoker.buildInitializePlugin(Log4j2Plugin.class));
    m.insertAfter(PluginManagerInvoker.buildCallPluginMethod(Log4j2Plugin.class, "init", "config", "java.lang.Object"));
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Example 40 with OnClassLoadEvent

use of org.hotswap.agent.annotation.OnClassLoadEvent in project HotswapAgent by HotswapProjects.

the class MojarraPlugin method facesConfigManagerInitialized.

@OnClassLoadEvent(classNameRegexp = "com.sun.faces.config.ConfigManager")
public static void facesConfigManagerInitialized(CtClass ctClass) throws NotFoundException, CannotCompileException {
    CtMethod init = ctClass.getDeclaredMethod("initialize");
    init.insertAfter(PluginManagerInvoker.buildInitializePlugin(MojarraPlugin.class));
    LOGGER.debug("com.sun.faces.config.ConfigManager enhanced with plugin initialization.");
}
Also used : CtMethod(org.hotswap.agent.javassist.CtMethod) OnClassLoadEvent(org.hotswap.agent.annotation.OnClassLoadEvent)

Aggregations

OnClassLoadEvent (org.hotswap.agent.annotation.OnClassLoadEvent)50 CtMethod (org.hotswap.agent.javassist.CtMethod)29 CtConstructor (org.hotswap.agent.javassist.CtConstructor)16 CtClass (org.hotswap.agent.javassist.CtClass)13 CtField (org.hotswap.agent.javassist.CtField)10 NotFoundException (org.hotswap.agent.javassist.NotFoundException)10 CannotCompileException (org.hotswap.agent.javassist.CannotCompileException)9 ExprEditor (org.hotswap.agent.javassist.expr.ExprEditor)5 IOException (java.io.IOException)4 WeldPlugin (org.hotswap.agent.plugin.weld.WeldPlugin)4 Method (java.lang.reflect.Method)3 MalformedURLException (java.net.MalformedURLException)3 URISyntaxException (java.net.URISyntaxException)3 MethodCall (org.hotswap.agent.javassist.expr.MethodCall)3 URL (java.net.URL)2 Set (java.util.Set)2 DeltaSpikePlugin (org.hotswap.agent.plugin.deltaspike.DeltaSpikePlugin)2 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1