java.net package - Override UDP transport

2019-09-07 00:41发布

问题:

I need to override the default implementation of DatagramSocket transport i.e., PlainDatagramSocketImpl and I found that java provides a way to do this via DatagramSocketImplFactory class. However my requirement is to use my own class for a very specific case and to use the default UDP socket transport for all other cases. Essentially adding an if-else clause in SocketFactory class which will instantiate my class if a condition is met, else instantiate the default transport.

My dilemma is that I cannot instantiate PlainDatagramSocketImpl inside my socketfactory since this class is visible only inside "java.net" factory. Another option would have been to put my SocketFactory class inside java.net package but then java does not allow it. I tried adding "permission java.lang.RuntimePermission "defineClassInPackage.java.net";" in policy file and then define my SocketFactory in java.net package but this doesn't seem to work either. Any ideas ?