Migrating Jetty 6.1 to 9.3 [message #1790515] |
Tue, 12 June 2018 09:07 |
vibhas karn Messages: 2 Registered: June 2018 |
Junior Member |
|
|
Hi All need few suggestion while migrating our legacy application which has in-built jetty server 6.1.
I am trying to migrate to Jetty 9.3, yes after long gap and after decided to move to JAVA 8. While changing few of the files I encounter piece of code written in jetty 6.1.
Code:
@Override
public void initialize(final ServiceConfiguration genericConfig, final Controller controller, final int serviceId,
final ServiceLock lock) throws Exception {
if (genericConfig instanceof JettyServerConfiguration) {
configuration = (JettyServerConfiguration) genericConfig;
} else {
configuration = XmlConfigurable.createInstance(JettyServerConfiguration.class,
genericConfig.getXmlConfigElement());
}
server = new Server();
log.info("jetty version = " + Server.getVersion()); //frozen
maxWaitForSlave = getConfiguration().getMaxWaitForSlave();
final boolean debug = getConfiguration().getMortBayDebug();
log.info("mortbay debug = '" + debug + "'"); //frozen
org.mortbay.log.Log.getLog().setDebugEnabled(debug);
// Configure http
final boolean httpEnabled = getConfiguration().getHttpEnabled();
if (httpEnabled) {
// Setup http connector as nio or socket.
final boolean nio = getConfiguration().getNioEnabled();
Connector connector;
if (nio) {
connector = new SelectChannelConnector();
} else {
connector = new SocketConnector();
}
final int mainPort = getConfiguration().getHttpPort();
log.info("adding default connector on port '" + mainPort + "'"); //frozen
connector.setPort(mainPort);
server.addConnector(connector);
}
// Configure SSL
final boolean sslEnabled = getConfiguration().getSslEnabled();
if (sslEnabled) {
final int sslPort = getConfiguration().getSslPort();
final String sslKeyStore = getConfiguration().getSslKeyStore();
final String sslPassword = getConfiguration().getSslPassword();
final String sslKeyPassword = getConfiguration().getSslKeyPassword();
final String sslTrustPassword = getConfiguration().getSslTrustPassword();
//final boolean nio = configuration.getBooleanValue("NioEnabled", false); //frozen
//if(nio) {
//sslConnector = new SslSelectChannelConnector(); available in jetty 7
//} else {
final SslSocketConnector sslConnector = new SslSocketConnector();
sslConnector.setKeystore(sslKeyStore);
sslConnector.setTruststore(sslKeyStore);
sslConnector.setPassword(sslPassword);
sslConnector.setKeyPassword(sslKeyPassword);
sslConnector.setTrustPassword(sslTrustPassword);
sslConnector.setPort(sslPort);
log.info("adding ssl connector on port '" + sslPort + "'"); //frozen
server.addConnector(sslConnector);
//}
}
// Check we had 1 connector else the server is useless
if (server.getConnectors().length == 0) {
throw new FileNotFoundException("No connectors registered. Please see HttpEnable or SslEnable XML tags."); //frozen
}
// Configure the handlers
final HandlerCollection handlers = new HandlerCollection();
for (final WebAppContext webAppContext : getConfiguration().getWebAppContexts()) {
log.info("Adding WebAppContext " + webAppContext.getWar() + " at " + webAppContext.getContextPath()); //frozen
handlers.addHandler(webAppContext);
}
final boolean accessLogEnabled = getConfiguration().getLogEnabled();
if (accessLogEnabled) {
final RequestLogHandler requestLogHandler = new RequestLogHandler();
final File logDir = ServiceUtilities.getLogDirectory();
if (!logDir.exists()) {
logDir.mkdirs();
}
final File logFile = new File(getConfiguration().getLogFormat());
if (!logFile.getParentFile().exists()) {
logFile.getParentFile().mkdirs();
}
final NCSARequestLog requestLog = new NCSARequestLog(getConfiguration().getLogFormat());
requestLog.setRetainDays(getConfiguration().getLogRetain());
requestLog.setAppend(getConfiguration().getLogAppend());
requestLog.setExtended(getConfiguration().getLogExtended());
requestLog.setLogTimeZone(getConfiguration().getLogTz());
requestLog.setLogLatency(getConfiguration().getLogLatency());
requestLogHandler.setRequestLog(requestLog);
handlers.addHandler(requestLogHandler);
}
handlers.addHandler(new DefaultHandler());
server.setHandler(handlers);
server.setUserRealms(new UserRealm[] { new OSMUserRealm() });
JettyServerInfo.install(server);
super.initialize(configuration, controller, serviceId, lock);
}
After reading many docs , I have re-writen it like:
@Override
public void initialize(final ServiceConfiguration genericConfig, final Controller controller, final int serviceId,
final ServiceLock lock) throws Exception {
if (genericConfig instanceof JettyServerConfiguration) {
configuration = (JettyServerConfiguration) genericConfig;
} else {
configuration = XmlConfigurable.createInstance(JettyServerConfiguration.class,
genericConfig.getXmlConfigElement());
}
server = new Server();
log.info("jetty version = " + Server.getVersion()); //frozen
maxWaitForSlave = getConfiguration().getMaxWaitForSlave();
final boolean debug = getConfiguration().getMortBayDebug();
log.info("mortbay debug = '" + debug + "'"); //frozen
org.eclipse.jetty.util.log.Log.getLog().setDebugEnabled(debug);
// Configure http
final boolean httpEnabled = getConfiguration().getHttpEnabled();
if (httpEnabled) {
// Setup http connector as nio or socket.
final boolean nio = getConfiguration().getNioEnabled();
ServerConnector connector; //modified for jetty 9.3
if (nio) {// Need to remove if else condition as jetty 9.3 has no specific option
connector = new ServerConnector(server);//modified for jetty 9.3
} else {
connector = new ServerConnector(server);//modified for jetty 9.3
}
final int mainPort = getConfiguration().getHttpPort();
log.info("adding default connector on port '" + mainPort + "'"); //frozen
connector.setPort(mainPort);
server.addConnector(connector);
}
// Configure SSL
final boolean sslEnabled = getConfiguration().getSslEnabled();
if (sslEnabled) {
final int sslPort = getConfiguration().getSslPort();
final String sslKeyStore = getConfiguration().getSslKeyStore();
final String sslPassword = getConfiguration().getSslPassword();
final String sslKeyPassword = getConfiguration().getSslKeyPassword();
final String sslTrustPassword = getConfiguration().getSslTrustPassword();
//Added for Jetty 9.3
final KeyStore trustKeyStore=KeyStore.getInstance(getConfiguration().getSslKeyStore());
//final boolean nio = configuration.getBooleanValue("NioEnabled", false); //frozen
//if(nio) {
//sslConnector = new SslSelectChannelConnector(); available in jetty 7
//} else {
/* final SslSocketConnector sslConnector = new SslSocketConnector();
sslConnector.setKeystore(sslKeyStore);
sslConnector.setTruststore(sslKeyStore);
sslConnector.setPassword(sslPassword);
sslConnector.setKeyPassword(sslKeyPassword);
sslConnector.setTrustPassword(sslTrustPassword);
sslConnector.setPort(sslPort);*/
//Commented old SSLConnector code for jetty 6.2
//Re-writing code for Jetty 9.3
SslContextFactory theSSLFactory = new SslContextFactory();
theSSLFactory.setKeyStorePath(sslKeyStore);
theSSLFactory.setKeyManagerPassword(sslPassword);
theSSLFactory.setKeyStorePassword(sslKeyPassword);
theSSLFactory.setTrustStore(trustKeyStore);
theSSLFactory.setTrustStorePassword(sslTrustPassword);
ServerConnector serverConnector= new ServerConnector(server, new SslConnectionFactory(theSSLFactory,"http/1.1"));
serverConnector.setPort(sslPort);
log.info("adding ssl connector on port '" + sslPort + "'"); //frozen
server.addConnector(serverConnector);
//}
}
// Check we had 1 connector else the server is useless
if (server.getConnectors().length == 0) {
throw new FileNotFoundException("No connectors registered. Please see HttpEnable or SslEnable XML tags."); //frozen
}
// Configure the handlers
final HandlerCollection handlers = new HandlerCollection();
for (final WebAppContext webAppContext : getConfiguration().getWebAppContexts()) {
log.info("Adding WebAppContext " + webAppContext.getWar() + " at " + webAppContext.getContextPath()); //frozen
handlers.addHandler(webAppContext);
}
final boolean accessLogEnabled = getConfiguration().getLogEnabled();
if (accessLogEnabled) {
final RequestLogHandler requestLogHandler = new RequestLogHandler();
final File logDir = ServiceUtilities.getLogDirectory();
if (!logDir.exists()) {
logDir.mkdirs();
}
final File logFile = new File(getConfiguration().getLogFormat());
if (!logFile.getParentFile().exists()) {
logFile.getParentFile().mkdirs();
}
final NCSARequestLog requestLog = new NCSARequestLog(getConfiguration().getLogFormat());
requestLog.setRetainDays(getConfiguration().getLogRetain());
requestLog.setAppend(getConfiguration().getLogAppend());
requestLog.setExtended(getConfiguration().getLogExtended());
requestLog.setLogTimeZone(getConfiguration().getLogTz());
requestLog.setLogLatency(getConfiguration().getLogLatency());
requestLogHandler.setRequestLog(requestLog);
handlers.addHandler(requestLogHandler);
}
handlers.addHandler(new DefaultHandler());
server.setHandler(handlers);
server.setUserRealms(new UserRealm[] { new OSMUserRealm() });
JettyServerInfo.install(server);
super.initialize(configuration, controller, serviceId, lock);
}
Now I have few doubts and quries:
1. For the line of code supporting jetty 6.1.
if (nio) {
connector = new SelectChannelConnector(server);
} else {
connector = new SocketConnector(server);
}
I modifies it to :
if (nio) {// Need to remove if else condition as jetty 9.3 has no specific option
connector = new ServerConnector(server);//modified for jetty 9.3
} else {
connector = new ServerConnector(server);//modified for jetty 9.3
}
which I will remove to single line like:
connector = new ServerConnector(server);
As Jetty 9.3 has no specific connector like jetty 6.1 has. Is the above change is fine? As Jetty 9.3 has a single selector-based non-blocking I/O connector.
So not sure if I need any perticular implementation for SocketConnector or only ServerConnector is fine.
2. For the ssl enabled condition:
I have changed the lines for:
final KeyStore trustKeyStore=KeyStore.getInstance(getConfiguration().getSslKeyStore());
Hope it is correct.
But I am not sure if the ssl conversion is correct for the below line as I don't understand the use of "http/1.1" use in SslConnectionFactory constructor.
ServerConnector serverConnector= new ServerConnector(server, new SslConnectionFactory(theSSLFactory,"http/1.1"));
3. And the Last one is:
server.setUserRealms(new UserRealm[] { new OSMUserRealm() });
I didn't find any alternative method in jetty 9.3 for UserRealm, any suggestion how can I re-write this line of code?
|
|
|
Powered by
FUDForum. Page generated in 0.07231 seconds