InterceptorCS562 Spring 2002
Jan 29 2002
Anand KrishnanMorgan DetersVenkita Subramonian
Discussion Topics
• Interceptor definition• Motivating Examples• Problem definition and solutions• Implementation details• Scope for Generic Programming• Relationship with other patterns
Interceptor
• Architectural pattern• Allows services to be transparently added to a
framework• Services triggered automatically when certain
events occur
Problem
• Problem – How to integrate out-of-band tasks with in-band tasks
transparently ?
Example 1 – Java event modelpublic class Demo extends JApplet { public void init() { bHandler = new ButtonHandler(); button = new JButton("Click Me"); getContentPane().add(button,
BorderLayout.CENTER); button.addActionListener(bHandler); } public class ButtonHandler
implements ActionListenervoid actionPerformed(ActionEvent e) { Toolkit.getDefaultToolkit().beep(); }
}
AppletFramework
Button clicked
ActionListener
Some action
ActionEvent
Example 2 – Unix signals
void catch_int(int sig_num) {signal(SIGINT, catch_int); printf("Don't do that");fflush(stdout);
}main() {
…..signal(SIGINT, catch_int); for ( ;; )
pause();}
OSFramework
Ctrl + C
signalhandler
Some action
Signal info
Example 3 - Database Triggers
CREATE TRIGGER print_salary_changesBEFORE UPDATE ON emp FOR EACH ROW WHEN (new.empno > 0) DECLARE sal_diff number; BEGIN
sal_diff := new.sal - old.sal; dbms_output.put('Old salary: ' || :old.sal); dbms_output.put(' New salary: ' || :new.sal); dbms_output.put(' Difference ' || sal_diff);
END;
UPDATE emp SET sal = sal + 500.00 WHERE deptno = 10
Trigger will fire once for each row that isupdated, and it prints the new and oldsalaries, and the difference.
DatabaseFramework
Update Request
BeforeUpdateTrigger
AfterUpdateTrigger
Result
Example 4 – CORBA ORB
• In-band tasks– Connection management
with peer ORB– Request/reply
marshaling/demarshaling– Demultiplexing requests
to objects• Out-of-band tasks
– Added at interceptor points
– Security, Fault Tolerance
ORB ORB
SecurityService
FaultTolerance
Service
Solution • Problem
– How to integrate out-of-band tasks with in-band tasks transparently ?
• Solution– Out-of-band tasks
• register with framework via specific interfaces• get triggered by framework on certain events• are provided access to framework internals via specific
interfaces
UML Class Diagram
implement *
implement z4
Application
Concrete Interceptor
Interceptor
UML Class Diagram
implement *
implement
Application
Concrete Interceptor
Interceptor
UML Class Diagram
register
implement *
implement
Application
Concrete Interceptor
InterceptorDispatcher
UML Class Diagram
register
implement *
event /
trigger
implement
Application
Concrete Interceptor
InterceptorDispatcher
ConcreteFramework
UML Class Diagram
register
instantiate implement *
event /
trigger
implement
Application
Concrete Interceptor
InterceptorDispatcher
ConcreteFramework
ContextObject(CO)
UML Class Diagram
register
instantiate implement *
event /
trigger
*notify (CO)
implement
Application
Concrete Interceptor
InterceptorDispatcher
ConcreteFramework
ContextObject(CO)
UML Class Diagram
register
instantiate implement *
event /
trigger
*notify (CO)
(CO) 1..* implement
Application
Concrete Interceptor
InterceptorDispatcher
ConcreteFramework
ContextObject(CO)
UML Class Diagram
Application
Concrete Interceptor
InterceptorDispatcher
ConcreteFramework
ContextObject(CO)
Implementation
• Model Internal Behavior of Concrete Framework • Identify and model interception points
– Identify concrete framework state transitions– Partition interception points into reader / writer sets – Integrate them into state machine model– Partition them into disjoint interception groups
• Specify the context object– context object semantics– number of context object types– strategy for passing context objects to interceptors
• Per-registration / Per-event
Implementation
• Specify the interceptors• Specify the dispatchers
– interceptor registration interface– dispatcher callback interface
• Implement CallBack mechanisms in Concrete Framework
• Implement Concrete Interceptors
Interceptor Themes• A priori knowledge of engineering context
– Have you covered all the bases?• Flexibility
– How much influence can the Interceptor have?• Performance
– How simple can the Interceptor model be?• "in-band" vs. "out-of-band“
– Are the concerns that Interceptors realize orthogonal?
Generic Interceptors?
• Templatization of context object?– Use context_traits to achieve compile-time
negotiation of dispatcher & interceptor
Interceptors as a Language Construct
• Interception becomes 1st class language concern• Interception points are language constructs
– Variable mutation, method execution, object instantiation, etc...
• Concrete Framework need not be specially prepared for extension– This is a major consequence
• Aspect-Oriented Programming (AOP)
Interceptors and AOP• Join Points
– Language constructs (execution/data, static/dynamic)
– Correspond to interception points• Aspect
– Collection of (possibly abstract) Interceptors– Specifies the join points to operate on– Attach advice to a particular place in execution (or
piece of data) in the system, reflectively reason about its place in the system
Interceptors as a Language Construct:Examples
• Component Configurator– Infrequently-used features (e.g., backward-compatible
protocol support) can be loaded, unloaded without the application being aware of it
• Customization– Quality of Service– Fault-Tolerance
+ Separation of Concerns
+ (Future) Flexibility
+ Reusability/Portability
− Efficiency/Heterogenity
− Evil Interceptors
Consequences
Interceptor Variants• Bind Interceptor - Fault Tolerance• Client Request Interceptor - Load Balancing• Interceptor Proxy / Delegator - Customized
Service• Portable Interceptor• Strategies
– Single Interceptor Per Dispatcher– Interceptor Factory– Implicit Interceptor Registration
• Component Configurator Pattern• similar (partly) to AOP -- AOP languages can differ• load from a library at runtime, Concrete Framework creates
the Interceptors
Relationships to Other Patterns
• Chain of Responsibility (single handler)• Observer/Publisher-Subscriber (notification)• Template Method (localized interception)
– Interceptor reaches across multiple layers• Pipes & Filters• Reflection