Fixes #292 - NPE in SslConnectionFactory newConnection.

ConnectionFactories may be added after the connector is started.
As such there is always the possibility that creating a new
connection fails because there is no available ConnectionFactory for
that protocol.

Rather than failing with an IllegalStateException instead of a
NullPointerException, we now check at connector start whether the
SslConnectionFactory is properly configured.
This should catch 99% of the cases, where the connector is
misconfigured, reporting the error earlier and explicitly
(connector does not start) rather later and hidden (connection cannot
 be created).
This commit is contained in:
Simone Bordet 2016-10-17 19:14:01 +02:00
parent ad8bdde4f3
commit be93a1ff31
3 changed files with 21 additions and 0 deletions

View File

@ -256,6 +256,14 @@ public abstract class AbstractConnector extends ContainerLifeCycle implements Co
_defaultConnectionFactory = getConnectionFactory(_defaultProtocol);
if(_defaultConnectionFactory==null)
throw new IllegalStateException("No protocol factory for default protocol: "+_defaultProtocol);
SslConnectionFactory ssl = getConnectionFactory(SslConnectionFactory.class);
if (ssl != null)
{
String next = ssl.getNextProtocol();
ConnectionFactory cf = getConnectionFactory(next);
if (cf == null)
throw new IllegalStateException("No protocol factory for SSL next protocol: " + next);
}
super.doStart();

View File

@ -61,6 +61,11 @@ public class SslConnectionFactory extends AbstractConnectionFactory
return _sslContextFactory;
}
public String getNextProtocol()
{
return _nextProtocol;
}
@Override
protected void doStart() throws Exception
{

View File

@ -185,6 +185,14 @@ public class SslConnectionFactoryTest
Assert.assertEquals(0, history.size());
}
@Test(expected = IllegalStateException.class)
public void testServerWithoutHttpConnectionFactory() throws Exception
{
_server.stop();
Assert.assertNotNull(_connector.removeConnectionFactory(HttpVersion.HTTP_1_1.asString()));
_server.start();
}
private String getResponse(String host, String cn) throws Exception
{
String response = getResponse(host, host, cn);