Supporting Unanticipated Dynamic Adaptation of Application Behaviour Barry Redmond and Vinny Cahill Distributed Systems Group Trinity College, Dublin
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Introduction Uses of unanticipated dynamic adaptation: –Long running applications –Applications which must adapt to a changing environment –Applications for which source code is not available “Unanticipated” –No preparatory modifications to source or compiled code “Dynamic”: –Insertion and removal of new behaviour at runtime Usually implemented for interpreted languages –MetaXa, Guaraná, PROSE, MetaclassTalk, Iguana/J
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Iguana/J An architecture to support unanticipated dynamic adaptation of Java programs Implemented as –An extended JVM –A class library Provides a strong separation between new and existing behaviours –New behaviour is reusable in compiled form Uses reflective techniques –Some reflective terms
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP A Simple Example Add synchronisation to this class –while it is running –without access to the source code public class Stack { … public void push(Object o) { … } public Object pop() { … } }
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Implement the New Behaviour 1.Create a class to implement the new behaviour –Extend Iguana/J library class MExecute public class Sync extends MExecute { public Object execute( Object targ, Object[] args, Method m) { synchronized(targ) { return(proceed(targ, args, m)); } } } This is called a metaobject class
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Create a Protocol 2.Declare that our new class is for handling method invocation: protocol SyncProtocol { reify Invocation: Sync; } 3.Use the Iguana/J protocol compiler to convert this to a Java class file
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Associate the New Behaviour 4.Associate the new behaviour with the Stack class: Meta.associate(“Stack”, “SyncProtocol”); Affects all existing and future instances of Stack Association is inherited –Affects all subclasses of Stack and all their existing and future instances
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Associating with One Object Apply new behaviour to a single object: Object x = new Object(); … Meta.associate(x, “SyncProtocol”); Remove new behaviour: Meta.reset(x);
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Organisation Uanticipated adaptation Dynamic adaptation JVM Iguana/J extension Application classes Protocol compiler Protocol declarations Metaobject classes Protocol classes Iguana/J classes
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP A More Focused Solution Apply synchronisation to selected methods of Stack: public class Sync extends MExecute { public Object execute( Object targ, Object[] args, Method m) { String s = m.getName(); if(s.equals(“push”) || s.equals(“pop”)) { synchronized(targ) { return(proceed(targ, args, m)); } } else return(proceed(targ, args, m)); } }
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP A Reusable Solution public class Sync extends MExecute { private String[] names; public Sync(String[] n) { names = n;// Accept & store parameters } public Object execute( Object targ, Object[] args, Method m) { if(match(m.getName())) { synchronized(targ) { return(proceed(targ, args, m)); } } else return(proceed(targ, args, m)); } // Check for match with an element of names[]: private boolean match(String s) { … } }
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Applying the Reusable Solution Add parameters to the protocol declaration: protocol SyncProt(String[] mNames) { reify Invocation: Sync(mNames); } Provide parameter values at association time: Object[] params = new Object[1]; params[0] = new String[] {“push”, “pop”}; Meta.associate(“Stack”, “SyncProt”, params);
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Context Sensitivity Allow calls only from a specified class: public class Guard extends MExecute { private String name; public Guard(String c) { name = c & “.*”; } public Object execute( Object targ, Object[] args, Method m) { if(getCallStack().containsCallTo(name)) return(proceed(targ, args, m)); else throw new RuntimeException(); } } protocol GuardProt(String cName) { reify Invocation: Guard(cName); }
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Deriving New Behaviour Protocols may be derived from other protocols protocol NewP(String c, String[] m): GuardProt(c), SyncProt(m) { reify Invocation: Verbose; } Automatic composition of metaobject classes for each category (no overriding) Composition is a simple chain SyncProt NewP GuardProt
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Restrictions on Association The protocol associated with a class must be equal to or derived from the protocol associated with its superclass Every class includes all new behaviour applied to any superclass The protocol associated with an object must be equal to or derived from the protocol associated with its class Every object includes all new behaviour applied to its class
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Inheritance and Derivation Association is inherited (B) Inheritance can be overridden by explicit association (C) –P2 must be derived from P1 New behaviour cannot be inadvertantly omitted from subclasses CP2 AP1 Explicit association BP1 Inherited association
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Implementation Interception mechanism is an extended JVM Prototype extends Sun JDK1.3 using the JIT Interface JIT Interface supports: –Close integration with the JVM –Insertion of hooks before the application loads –Interception of all class loading –Access to internal JVM data structures Interception is by dynamically changing invoker functions and altering bytecode
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Performance No effect on classes and objects with no associated metaobject protocol No effect on non-intercepted operations Prototype: Overhead of 24 times for intercepted method invocation –Affects invocation only, not method execution –Performance of other dynamic architectures: 40 times for Guaraná and 70 times for PROSE
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Conclusion The Iguana/J architecture supports: Dynamic insertion/removal of new behaviour Unanticipated change without source code Inheritance of new behaviour Strong separation Context sensitivity Reusable new behaviour
Supporting Unanticipated Dynamic Adaptation of Application Behaviour ECOOP Ireland for the World Cup!