【发布时间】:2018-06-13 18:05:27
【问题描述】:
大家好,在迁移我们的旧版应用程序时需要一些建议 内置jetty server 6.1。
我正在尝试迁移到 Jetty 9.3,是的,在很长一段时间之后和之后 决定迁移到 JAVA 8。虽然更改了我遇到的一些文件 用 jetty 6.1 编写的一段代码。
代码:
@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);
}
// See http://docs.codehaus.org/display/JETTY/Logging+Requests
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);
}
根据下面给出的建议,我将其重写为:
@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);
//Re-writing code for Jetty 9.3
final int mainPort = getConfiguration().getHttpPort();//8580
final int sslPort = getConfiguration().getSslPort(); //8581
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());
ClassLoader cl = JettyServer.class.getClassLoader();// Get the class loader for my current class which is JettyServer
String keystoreResource = "ssl/keystore";
URL f = cl.getResource(keystoreResource);
if (f == null)
{
throw new RuntimeException("Unable to find " + keystoreResource);
}
// Setup HTTP Connector
HttpConfiguration httpConf = new HttpConfiguration();
httpConf.setSecurePort(mainPort);
httpConf.setSecureScheme("https");
// Establish the HTTP ServerConnector
ServerConnector httpConnector = new ServerConnector(server,
new HttpConnectionFactory(httpConf));
httpConnector.setPort(mainPort);
server.addConnector(httpConnector);
// Setup SSL
SslContextFactory theSSLFactory = new SslContextFactory();
theSSLFactory.setKeyStorePath(f.toExternalForm()); //replaced for--> theSSLFactory.setKeyStorePath(sslKeyStore);
theSSLFactory.setKeyManagerPassword(sslPassword); // or this one ? seems hardcoded --> sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
theSSLFactory.setKeyStorePassword(sslKeyPassword); // or this one ? seems hardcoded --> sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
theSSLFactory.setTrustStore(trustKeyStore);
theSSLFactory.setTrustStorePassword(sslTrustPassword);
// Setup HTTPS Configuration
HttpConfiguration httpsConf = new HttpConfiguration(httpConf);
httpsConf.addCustomizer(new SecureRequestCustomizer()); // adds ssl info to request object
// Establish the HTTPS ServerConnector
ServerConnector httpsConnector = new ServerConnector(server,
new SslConnectionFactory(theSSLFactory,"http/1.1"),
new HttpConnectionFactory(httpsConf));
httpsConnector.setPort(sslPort);
log.info("adding ssl connector on port '" + sslPort + "'"); //frozen
server.addConnector(httpsConnector);
// 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);
}
// See http://docs.codehaus.org/display/JETTY/Logging+Requests
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);
}
我仍然不确定如何处理这条线。
server.setUserRealms(new UserRealm[] { new OSMUserRealm() });
我没有在 jetty 9.3 中为 UserRealm 找到任何替代方法,任何 建议我如何重写这行代码?
【问题讨论】:
标签: embedded-jetty