Home » Java » Setting the default SocketFactory

Setting the default SocketFactory

Posted by: admin December 28, 2021 Leave a comment

Questions:

I recently migrated my web services from Axis to CXF (2.1). It appears that while Axis needed the “axis.socketSecureFactory” set, CXF grabs the default. How do I set the default SocketFactory in Java to my own implementation (such as a property)?

What I am not looking to do is to set the default SocketFactory properties like setting the property for my truststore, keystore and passwords.

Answers:

If we are talking about the standard sockets then Socket.setSocketImplFactory(SocketImplFactory) is used to change factory for ordinary sockets and ServerSocket.setSocketFactory(SocketImplFactory) for server sockets.

If you are using these, then take note of the fact that you can only set these factories once.

Note the java socket implementation defaults to using SocksSocketImpl (a package visible class in java.net) if the it discovers that the factory is null in case you want a factory that can switch pack to the standard java implementation.

###

According to the OpenJDK7 source code for javax.net.SocketFactory, the Sun JVM is hard coded to return a javax.net.DefaultSocketFactory for getDefault();

Edit: However, the default SSLSocketFactory can be set by the security property ssl.SocketFactory.provider