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));
}
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());
}
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;
}
}
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"));
}
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.");
}
Aggregations