1

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 ?

user207421
  • 305,947
  • 44
  • 307
  • 483
user572964
  • 600
  • 5
  • 12
  • If you only have one case why are you even considering this over-intrusive architecture? – user207421 Feb 10 '15 at 20:48
  • I am trying to modify the behavior of an existing application. Unfortunately I cannot change the original code. This application opens UDP sockets for various purposes, one of them for SNMP communication. Now I need to route this SNMP packets over SSH. The only way to achieve this in this case is to provide my implementation of DatagramSocket transport. – user572964 Feb 11 '15 at 14:06
  • I have the same problem, and in my case, the library changes the recv code to use recvmsg to be able to access control messages to get extra information from received packets. – mirabilos Oct 14 '20 at 23:29

0 Answers0