I'm trying to use a custom SocketImpl with SSL in Java. Because of that I need to set ServerSocket's socket factory. I now noticed that it's static, which creates some hassle for me as a I want to supply it with some paramaters that differs between in each ServerSocket-instance. Does anyone know the rationale behind making it static? To me it feels like an unnecessary constraint that only introduces more global state in your app.
Update There seems to be some confusion on why this is a hassle. The problem this creates is that it forces me to use the same factory across the entire application. What if I want to use the default SocketImpl in one place and a custom one in another? That can't be done without resorting to some ugly reflection-hacks, because the factory can't be changed once it has been set. I also can't make my factory create default implementation because SocksSocketImpl is package private.
SocketImpl is an abstract class that provides an interface for customizing socket implementations. Subclasses of SocketImpl are not intended to be explicitly instantiated. Instead, they are created by a SocketImplFactory. The motivation for this design is to allow you to change the default type of socket used by an entire application by calling Socket.setSocketImplFactory(), making it unnecessary to rewrite all of your networking code. Custom sockets are necessary for features such as SSL and firewall tunneling. Unfortunately, the global nature of setSocketImplFactory() is rather limiting, as it doesn't allow you to use multiple types of sockets within a single application. You can get around this by subclassing Socket and using the protected Socket(SocketImpl) constructor that was introduced in JDK 1.1.
Reference:http://www.devx.com/tips/Tip/26363