47

The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the
Page 2: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

The Dark Sides of Integration

Anton Arhipov JRebel, ZeroTurnaround

@antonarhipov

Page 3: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Make a change

Build, deploy,

wait

Observe results

Page 4: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Where the time goes?

Page 5: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Where the time goes?

Build Just don’t

Container start Light containers

Deployment ?

Navigation Persist sessions

Page 6: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Where the time goes?

Build Just don’t

Container start Light containers

Deployment JRebel

Navigation Persist sessions

Page 7: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

@Path(“/”) public String foo() { return “FooBar”; }

Page 8: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

@Path(“/”) public String foo() { return “FooBar”; }

@Path(“/”) public FooBar foo() { return new FooBar(); }

Page 9: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

@Path(“/”) public String foo() { return “FooBar”; }

@Path(“/foobar”) public FooBar foo() { return new FooBar(); }

Page 10: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the
Page 11: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the
Page 12: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Come To The Dark Side We have cookies

Page 13: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Java

App

Framework

Conf

XML

HTTP request

Page 14: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Java

App

Framework

Conf

XML

HTTP request

Page 15: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Java

App

Framework

Conf

XML

HTTP request

1) Stop request

Page 16: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Java

App

Framework

Conf

XML

HTTP request

2) Check resources

Page 17: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Java

App

Framework

Conf

XML

HTTP request

3) Reconfigure

Page 18: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Java

App

Framework

Conf

XML

HTTP request

4) Resume request

Page 19: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Java

App

Framework

Conf

XML

HTTP request

PROFIT! J

Page 20: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

How?

•  Load time weaving, e.g. binary patching

o_O Isn’t it dangerous??

•  Well, you have to be careful J

Page 21: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

How? •  Add –javaagent to hook into class loading

process •  Implement ClassFileTransformer •  Use bytecode manipulation libraries

(Javassist, cglib, asm) to add any custom logic

java.lang.instrument

Page 22: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

java.lang.instrument import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String args, Instrumentation inst) throws Exception { inst.addTransformer(new ClassFileTransformer { … }); } }

META-INF/MANIFEST.MF Premain-Class: Agent

java –javaagent:agent.jar …

Page 23: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

java.lang.instrument import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String args, Instrumentation inst) throws Exception { inst.addTransformer(new ClassFileTransformer { … }); } }

META-INF/MANIFEST.MF Premain-Class: Agent

java –javaagent:agent.jar …

Page 24: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

ClassFileTransformer new ClassFileTransformer() { public byte[] transform(ClassLoader loader, String className, Class<?>classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer){ ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.makeClass(new ByteArrayInputStream(classfileBuffer)); transformClass(ct, cp); return ct.toBytecode(); } }

Page 25: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

ClassFileTransformer new ClassFileTransformer() { public byte[] transform(ClassLoader loader, String className, Class<?>classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer){ ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.makeClass(new ByteArrayInputStream(classfileBuffer)); transformClass(ct, cp); return ct.toBytecode(); } }

Page 26: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

ClassFileTransformer new ClassFileTransformer() { public byte[] transform(ClassLoader loader, String className, Class<?>classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer){ ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.makeClass(new ByteArrayInputStream(classfileBuffer)); transformClass(ct, cp); return ct.toBytecode(); } }

Page 27: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

ClassFileTransformer new ClassFileTransformer() { public byte[] transform(ClassLoader loader, String className, Class<?>classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer){ ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.makeClass(new ByteArrayInputStream(classfileBuffer)); transformClass(ct, cp); return ct.toBytecode(); } }

Enter JRebel SDK

Page 28: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

JRebel

Spring plugin

Weld plugin

IntegrationFactory

ReloaderFactory

#addIntegrationProcesor (Processor)

#addClassReloadListener (Listener)

Page 29: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

JRebel SDK class MyPlugin implements Plugin { public void preinit() { IntegrationFactory.getInstance(). addIntegrationProcessor( getClass().getClassLoader(), "org.jboss.Registry", new RegistryCBP()); } }

Page 30: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

JRebel SDK class MyPlugin implements Plugin { public void preinit() { ReloaderFactory.getInstance(). addClassReloadListener( new ClassEventListener() { public void onClassEvent(int type, Class c) { } } }

Page 31: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

JRebel SDK public class RegistryCBP extends JavassistClassBytecodeProcessor { public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception { cp.importPackage("org.zeroturnaround.javarebel");

Page 32: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

JRebel SDK public class RegistryCBP extends JavassistClassBytecodeProcessor { public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception { cp.importPackage("org.zeroturnaround.javarebel");

Page 33: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

JRebel SDK public class RegistryCBP extends JavassistClassBytecodeProcessor { public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception { for (CtConstructor c : ctClass.getConstructors()) { if (c.callsSuper()) c.insertAfter("ReloaderFactory.getInstance(). ” + "addClassReloadListener($0);");

Page 34: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

JRebel SDK public class RegistryCBP extends JavassistClassBytecodeProcessor { public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) throws Exception { ctClass.addInterface(cp.get( ClassEventListener.class.getName())); ctClass.addMethod(CtNewMethod.make( "public void onClassEvent(int type, Class clazz) {"

Page 35: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Javassist

•  Bytecode manipulation made easy •  Source-level and bytecode-level API •  Uses the vocabulary of Java language •  On-the-fly compilation of the injected code •  http://www.jboss.org/javassist

Page 36: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Insert Before

CtClass clazz = ... CtMethod method = clazz.getMethod(“dispatch”, “(V)V”); m.insertBefore( “{ Reloader reloader = “ + “ReloaderFactory.getInstance();” + “reloader.checkAndReload(SomeClass.class); ” + “Config.init(); }“ );

Page 37: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Adding Interfaces ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.get("org.judcon.Alarm"); ct.addInterface(cp.get(Listener.class.getName())); ct.addMethod(CtNewMethod.make("public void fire(){ alert(); }", ct));

public interface Listener { void fire(); }

public class Alarm { void alert() {} }

Page 38: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Adding Interfaces ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.get("org.judcon.Alarm"); ct.addInterface(cp.get(Listener.class.getName())); ct.addMethod(CtNewMethod.make("public void fire(){ alert(); }", ct));

public interface Listener { void fire(); }

public class Alarm { void alert() {} }

Page 39: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Adding Interfaces ClassPool cp = ClassPool.getDefault(); CtClass ct = cp.get("org.judcon.Alarm"); ct.addInterface(cp.get(Listener.class.getName())); ct.addMethod(CtNewMethod.make("public void fire(){ alert(); }", ct));

public interface Listener { void fire(); }

public class Alarm { void alert() {} }

Page 40: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Intercept Statements

ClassPool pool = ClassPool.getDefault(); CtClass ct = pool.get("org.judcon.Config"); ct.getDeclaredMethod("process") .instrument(new ExprEditor() { public void edit(NewExpr e) throws CannotCompileException { e.replace("$_ = $proceed($$);"); } });

Page 41: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Intercept Statements

ClassPool pool = ClassPool.getDefault(); CtClass ct = pool.get("org.judcon.Config"); ct.getDeclaredMethod("process") .instrument(new ExprEditor() { public void edit(NewExpr e) throws CannotCompileException { e.replace("$_ = $proceed($$);"); } });

Page 42: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Copy Methods CtClass clazz = ... CtMethod m = clazz.getMethod("configure", "(V)V"); CtMethod copy = CtNewMethod.copy( m, "__configure", clazz, null);

… clazz.addMethod(CtNewMethod.make( "public void onClassEvent(int type, Class clazz) {" + "__configure();" + "} ");

Page 43: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Copy Methods CtClass clazz = ... CtMethod m = clazz.getMethod("configure", "(V)V"); CtMethod copy = CtNewMethod.copy( m, "__configure", clazz, null);

… clazz.addMethod(CtNewMethod.make( "public void onClassEvent(int type, Class clazz) {" + "__configure();" + "} ");

Page 44: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Copy Methods CtClass clazz = ... CtMethod m = clazz.getMethod("configure", "(V)V"); CtMethod copy = CtNewMethod.copy( m, "__configure", clazz, null);

… clazz.addMethod(CtNewMethod.make( "public void onClassEvent(int type, Class clazz) {" + "__configure();" + "} ");

Page 45: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

How To Test?

•  Unit tests are (almost) no use here •  Requires integration testing across all

platforms: – Containers / versions – JDK / versions – Framework / versions

Page 46: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Custom Dashboard

Page 47: The Dark Sides of Integration - JBoss€¦ · Anton Arhipov JRebel, ZeroTurnaround @antonarhipov . Make a change Build, deploy, wait Observe results . Where the time goes? Where the

Thank You!

Questions? @antonarhipov [email protected] http://www.jrebel.com