190 likes | 355 Views
ECOOP 2002. 2. Introduction. Uses of unanticipated dynamic adaptation:Long running applicationsApplications which must adapt to a changing environmentApplications for which source code is not available?Unanticipated"No preparatory modifications to source or compiled code?Dynamic": Insertion
E N D
1. Supporting Unanticipated Dynamic Adaptation of Application Behaviour Barry Redmond and Vinny Cahill
Distributed Systems Group
Trinity College, Dublin
2. ECOOP 2002 2 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
3. ECOOP 2002 3 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
4. ECOOP 2002 4 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() { … } }
5. ECOOP 2002 5 Implement the New Behaviour 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
6. ECOOP 2002 6 Create a Protocol Declare that our new class is for handling method invocation:protocol SyncProtocol { reify Invocation: Sync;}
Use the Iguana/J protocol compiler to convert this to a Java class file
7. ECOOP 2002 7 Associate the New Behaviour 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 Stackand all their existing and future instances
8. ECOOP 2002 8 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);
9. ECOOP 2002 9 Organisation Uanticipated adaptation
Dynamic adaptation
10. ECOOP 2002 10 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)); } }
11. ECOOP 2002 11 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) { … }}
12. ECOOP 2002 12 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);
13. ECOOP 2002 13 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);}
14. ECOOP 2002 14 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
15. ECOOP 2002 15 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
16. ECOOP 2002 16 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
17. ECOOP 2002 17 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
18. ECOOP 2002 18 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
19. ECOOP 2002 19 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
20. ECOOP 2002 20 Ireland for the World Cup!