Name: jl125535 Date: 02/14/2003
FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)
FULL OPERATING SYSTEM VERSION : Microsoft Windows XP
[Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
I need to be able to report statistics on the amount of data
being sent over a socket by my application. With the old IO
classes this was easy to do because you could simply "wrap"
a Socket with a class that would record the reads and writes.
In NIO there is no way to do this. Following is an e-mail I
send to "###@###.###" that will help to explain and
the response will also be helpful...
------------------------------
> Date: Fri, 12 Jul 2002 20:50:03 -0500
> I'm wondering if the access levels might be a little too tight on
> the New IO API's. I'm trying to wrap a SocketChannel so that I can
> keep track of how much data is transmitted. This is proving to be
> remarkably difficult to do due to the method access levels and use of
> "final".
>
> Normally, I'm a big proponent of making security as tight as
> possible, but I really can't find a way to do what I need. Here are
> the problems I hit:
>
> Simply wrapping SocketChannel:
> - Registration with selectors won't work because you'll get an
> IllegalSelectorException.
> - I can't override register(...) because it's final.
>
> So, I tried to provide a SelectorProvider and Selector that wrapped
> the defaults. But, register(Selector,int,Object) is protected in
> AbstractSelectableChannel, so I can't return a wrapped implementation
> that can call to the real channel. To clarify, here's what I'm trying
> to do:
>
>> public class StatWrapperSelector extends AbstractSelector {
>> private AbstractSelector engine;
>>
>> /** Creates a new instance of StatWrapperSelector */
>> public StatWrapperSelector( AbstractSelector engine ) {
>> super( engine.provider() );
>>
>> this.engine = engine;
>> }
>>
>> ....
>>
>> protected SelectionKey register(AbstractSelectableChannel ch, int
>> ops, Object att) {
>> return engine.register( ( ( StatSocketChannelWrapper ) ch ).sock,
>> ops, att );
>> }
>
> The line in red won't work because it's protected.
>
> The other way I could think of to do this is with a
> java.lang.reflect.Proxy, but that won't work because there aren't
> interfaces for everyone.
>
> So, unless there's some way to do this that I'm missing (which is very
> possible... there aren't many examples out yet), it doesn't appear
> that there's a way to do this.
>
> Any suggestions?
Sorry, but there's currently no way to do this. Even if we
removed the various final and protected keywords, your
derived channel would most likely not work with a selector
as intended since the selector/channel implementations are
so tightly bound together.
During the NIO design process we considered defining a
SelectableChannelProxy class that would support this sort of
pattern, but it proved too difficult to implement within our
time constraints. We might revisit this idea in the 1.5
release.
- Senior Staff Engineer 901 San Antonio Road
Core Java Platform Group Palo Alto, CA 94303
Java Software xxx-xxx-xxxx
Sun Microsystems, Inc. ###@###.###
-----------------------------------
There are many reasons in my application that the
SelectableChannelProxy mentioned would be very helpful, so I
would ask that it be implemented.
REPRODUCIBILITY :
This bug can be reproduced always.
CUSTOMER WORKAROUND :
None using NIO.
(Review ID: 166198)
======================================================================
- duplicates
-
JDK-6331719 (se) Application should be able to extend default selector provider
- Closed