Reputation: 26405
This code has been taken from org.glassfish.jersey.grizzly2
project, like the method name indicate, createHttpServer
should be responsible "only" for creating and returning an instance of the HttpServer
class, I just wonder why should the HttpServer.start
call be encapsulated in such way ?
public static HttpServer createHttpServer(final URI uri,
final GrizzlyHttpContainer handler,
final boolean secure,
final SSLEngineConfigurator sslEngineConfigurator
final boolean start) {
final String host = (uri.getHost() == null) ? NetworkListener.DEFAULT_NETWORK_HOST : uri.getHost();
final int port = (uri.getPort() == -1) ? DEFAULT_HTTP_PORT : uri.getPort();
final NetworkListener listener = new NetworkListener("grizzly", host, port);
listener.setSecure(secure);
if (sslEngineConfigurator != null) {
listener.setSSLEngineConfig(sslEngineConfigurator);
}
final HttpServer server = new HttpServer();
server.addListener(listener);
// Map the path to the processor.
final ServerConfiguration config = server.getServerConfiguration();
if (handler != null) {
config.addHttpHandler(handler, uri.getPath());
}
config.setPassTraceRequest(true);
if (start) {
try {
// Start the server.
server.start();
} catch (IOException ex) {
throw new ProcessingException(LocalizationMessages.FAILED_TO_START_SERVER(ex.getMessage()), ex);
}
}
return server;
}
Upvotes: 0
Views: 94
Reputation: 7057
Single responsibility principle in wiki says
Every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class.
SRP is intended for loose coupling and robustness. It definitely helps developers in maintaining the same while keeping it well functioning.
So had it been some internal method or class, I would have agreed.
The design goals of public API are completely different.
Hope this helps.
Upvotes: 1
Reputation: 1221
The only advantage I see is that the user has to write less code. I totally disagree with this practice. If it says "create", then it should only create. Anyway, as far as its clearly specified in the documntation, it shoul be "ok" to do that... It's not the worst violation of the SRP I have seen...
Upvotes: 0