Version: 29.10.2014 Architecture and Design of Distributed Dependable Systems TI-ARDI POSA2: Interceptor Architectural Pattern
Abstract The Interceptor architectural pattern allows services to be added transparently to a framework and triggered automatically when certain events occur 2
Context Developing frameworks that can be extended transparently 3
Problem Frameworks cannot anticipate all the services they must offer to users Framework developers must address the following three forces: A framework should allow integration of additional services These integrations should not affect existing framework components Applications using a framework may need to monitor and control its behavior 4
Solution Allow applications to register out-of-band services with the framework via predefined interfaces Let the framework trigger these services automatically when certain events occur Open the frameworks implementation for the out-of-band services allowing the services to access and control certain aspects of the framework s behavior 5
Application +do_work() * Concrete Interceptor +event1_callback() +event2_callback() Interceptor Structure FW «register» «remove» Interceptor +event1_callback() +event2_callback() * «uses» Concrete Framework +event() +access_internals() +service() 1 «creates» Context +set_value() +get_value() +consume_service() 1 * Dispatcher List of Interceptors +dispatch() +register() +remove() +iterate_list() 6
:Application Dynamics :Concrete Framework ci:concrete Interceptor register(ci) :Dispatcher co:context object dispatch(co) event() get_value() consume_ service() event1_callback(co) access_internals() service() iterate_ list() 7
Implementation Steps 1. Model the internal behavior of the concrete framework (e.g. as a state machine) 2. Identify and model interception points 3. Specify the context objects 4. Specify the interceptors 5. Specify the dispatchers 6. Implement the callback mechanisms in the concrete framework 7. Implement the concrete interceptors 8
State Chart for Client ORB Framework Initializing shutdown() Binding Idle bindrequest() Proxy Creation Connecting [ok] methodrequest() [already bound] Bind Error [not ok] Sending requests and receiving replies Transmission Error [not ok] [ok] Marshaling Demarshaling [ok] Delivering [ok] Receiving result 9
2. Identify and model Interception Points 2.1 Identify concrete framework state transitions subject to interceptions (=interception points) 2.2 Partition interception points into reader and writer sets 2.3 Integrate the interception points into the state machine model - by introducing intermediary states 2.4 Partition the interception points into disjoint interception groups - e.g. OutRequest, InRequest, InReply 10
2.1 Example of Interception Points Interception Points Shut-down Binding PreMarshalRequest PostMarshalRequest PreMarshalReply PostMarshalReply Reader/Writer Reader Reader Reader+Writer Reader Reader Reader+Writer 11
1 main() Client ClientRequest Interceptor +onpremarshalrequest() +onpostmarshalrequest() ClientRequest Interceptor «register» «remove» +onpremarshalrequest() +onpostmarshalrequest() Example «uses» «uses» Concrete Framework +event() +access_internals() +service() 1 1 «creates» * 1 ClientRequest Unmarshaled Request Marshaled Request 1 Dispatcher +dispatchclient RequestPreMarshal() +register() +remove() +iterate_list() 12
3. Specify the Context Objects - Example public interface UnmarshaledRequest // Context object { // JAVA public String gethost(); public void sethost(string host); public long getport(); public void setport(long newport); public String getobjname(); public void setobjname(string newname); public String getmethod(); public void setmethod(string name); public Enumeration getparameters(); public Object getarg(long i); // get i_th arg public void setarg(long i, Object o); // set i_th arg public void addinfo(object info); // } 13
4. Specify the Interceptors - Example Interceptor interface for Request interception group: public interface ClientRequestInterceptor { public void onpremarshalrequest(unmarshaledrequest context); public void onpostmarshalrequest(marshaledrequest context); } Each interception point in the group results in a separate hook method 14
6. Implement the Callback mechanism public class ClientRequestDispatcher { // JAVA Code // public void dispatchclientrequestinterceptorpremarshal( UnmarshaledRequest context) { Vector interceptors; synchronized (this) { interceptors= (Vector) interceptors_.clone(); } for (int i=0; i < interceptors.size(); i++) { ClientRequestInterceptor ic= (ClientRequestInterceptor) interceptors.elementat(i); // dispatch callback hook method ic.onpremarshalrequest(context); } } // } 15
7. Implement the Concrete Interceptor public class Client { // JAVA Code static final void main (string args[]) { ClientRequestInterceptor myinterceptor = // use an anonymous inner class new ClientRequestInterceptor() { public void onpremarshalrequest( UnmarshaledRequest context) { System.out.println(context.getObj() + called ); // } public void onpostmarshalrequest( MarshaledRequest context) { /*.. */ } }; ClientRequestDispatcher.theInstance().register(myInterceptor); // do normal work } // end main } 16
Load Balancing Example :Client 1. invoke: x.f() 5. request() 6. call operation: f() ORB A x:x Client ORB x:x 2. dispatch 4. use(a) ORB B ORB Load Table A: 10 % B: 25 % C: 90 % :RequestInterceptor 3. lookup ORB C x:x 17
Variants: Interceptor Proxy (Delegator) Client Machine Server Machine configure Client call intercept and delegate :Proxy Server «creates» Concrete Framework :Server «creates» 18
Variants: Implicit Interceptor Registration Dynamic load of interceptors Strategy 1: search and load interceptor libraries from predefined locations Strategy 2: using a run-time configuration mechanism i.e. Component Configurator pattern Interpretation of a script file, defining which interceptors to link, where to find the DLLs and how to initialize them in the framework 19
Known Uses Component-based application servers EJB, CORBA Components, COM+ CORBA implementations (TAO, Orbix) OMG s CORBA Portable Interceptor specification Fault-tolerant ORB frameworks Web browsers allow plug-ins for specific media types The dynamictao reflective ORB Using component configurators 20
Component-based Application Servers Client call read configuration Configuration Interface (Proxy) Interceptor delegate Use services Transaction Component Security Persistence Lifecycle_ Event() Service() Container (Concrete Framework) 21
Interceptor Benefits Extensibility and flexibility Separation of concerns Support for monitoring and control of frameworks Layer symmetry Reusability 22
Interceptor Liabilities Complex design issues Malicious or erroneous interceptors Potential interception cascades 23
Relation to other POSA2 patterns 24