A filter which intercepts
IoHandler
events like Servlet
filters. Filters can be used for these purposes:
- Event logging,
- Performance measurement,
- Authorization,
- Overload control,
- Message transformation (e.g. encryption and decryption, ...),
- and many more.
Please NEVER implement your filters to wrap
IoSession
s. Users can cache the reference to the
session, which might malfunction if any filters are added or removed later.
The Life Cycle
IoFilter
s are activated only when they are inside
IoFilterChain
.
When you add an
IoFilter
to an
IoFilterChain
:
init()
is invoked by ReferenceCountingIoFilter
if
the filter is added at the first time.onPreAdd(IoFilterChain,String,IoFilter.NextFilter)
is invoked to notify
that the filter will be added to the chain.- The filter is added to the chain, and all events and I/O requests
pass through the filter from now.
onPostAdd(IoFilterChain,String,IoFilter.NextFilter)
is invoked to notify
that the filter is added to the chain.- The filter is removed from the chain if
onPostAdd(IoFilterChain,String,IoFilter.NextFilter)
threw an exception. destroy()
is also invoked by
ReferenceCountingIoFilter
if the filter is the last filter which
was added to IoFilterChain
s.
When you remove an
IoFilter
from an
IoFilterChain
:
onPreRemove(IoFilterChain,String,IoFilter.NextFilter)
is invoked to
notify that the filter will be removed from the chain.- The filter is removed from the chain, and any events and I/O requests
don't pass through the filter from now.
onPostRemove(IoFilterChain,String,IoFilter.NextFilter)
is invoked to
notify that the filter is removed from the chain.destroy()
is invoked by ReferenceCountingIoFilter
if
the removed filter was the last one.
destroy
public void destroy()
throws Exception
init
public void init()
throws Exception
onPostAdd
public void onPostAdd(IoFilterChain parent,
String name,
IoFilter.NextFilter nextFilter)
throws Exception
Invoked after this filter is added to the specified
parent.
Please note that this method can be invoked more than once if
this filter is added to more than one parents. This method is not
invoked before
init()
is invoked.
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.
onPostRemove
public void onPostRemove(IoFilterChain parent,
String name,
IoFilter.NextFilter nextFilter)
throws Exception
Invoked after this filter is removed from the specified
parent.
Please note that this method can be invoked more than once if
this filter is removed from more than one parents.
This method is always invoked before
destroy()
is invoked.
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.
onPreAdd
public void onPreAdd(IoFilterChain parent,
String name,
IoFilter.NextFilter nextFilter)
throws Exception
Invoked before this filter is added to the specified
parent.
Please note that this method can be invoked more than once if
this filter is added to more than one parents. This method is not
invoked before
init()
is invoked.
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.
onPreRemove
public void onPreRemove(IoFilterChain parent,
String name,
IoFilter.NextFilter nextFilter)
throws Exception
Invoked before this filter is removed from the specified
parent.
Please note that this method can be invoked more than once if
this filter is removed from more than one parents.
This method is always invoked before
destroy()
is invoked.
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.