Upgrade Jfinal built-in jetty to 9 version, and support SSL

Source: Internet
Author: User

Jfinal built-in Jetty8 has basically met the development needs. Since I am a version control, I want to run it directly with the built-in jetty in some small projects.

The built-in jetty is then upgraded to 9. The Jetty9 box jetty8 varies greatly. So it is almost necessary to rewrite the Com.jfinal.server.JettyServer Dostart method. A jar package that needs to be introduced into Jetty9.

Latest Jetty 9.3.0.v20150612 minimum requirements java8

The code is as follows:

/** * copyright  (c)  2011-2015, James Zhan  Giampo   ([email protected]).  * * Licensed under the Apache License, Version 2.0  (The   "License"); * you may not use this file except in  Compliance with the license. * you may obtain a copy of  the license at * *      http://www.apache.org/licenses /license-2.0 * * unless required by applicable law or agreed  to in writing, software * distributed under the license is  distributed on an  "As is"  basis, * without warranties or  conditions of any kind, either express or implied. * see  the license for&nbsP;the specific language governing permissions and * limitations under  the License. */package com.jfinal.server;import java.io.File;import  java.io.ioexception;import java.net.datagramsocket;import java.net.serversocket;import  java.util.enumset;import javax.servlet.dispatchertype;import  Org.eclipse.jetty.server.httpconfiguration;import org.eclipse.jetty.server.httpconnectionfactory;import  org.eclipse.jetty.server.securerequestcustomizer;import org.eclipse.jetty.server.server;import  org.eclipse.jetty.server.serverconnector;import org.eclipse.jetty.server.sessionmanager;import  org.eclipse.jetty.server.SslConnectionFactory;import  org.eclipse.jetty.server.session.hashsessionmanager;import  Org.eclipse.jetty.server.session.sessionhandler;import org.eclipse.jetty.servlet.filterholder;import  org.eclipse.jetty.util.ssl.sslcontextfactory;import org.eClipse.jetty.webapp.webappcontext;import com.jfinal.core.const;import com.jfinal.kit.filekit;import  com.jfinal.kit.pathkit;import com.jfinal.kit.strkit;/** * jettyserver is used  to config and start jetty web server. * Jetty version  8.1.8 */class jettyserver implements iserver {private string webappdir; Private int port;private string context;private int scanintervalseconds;private  boolean running = false;private Server server;private WebAppContext  webapp;private string keystorepath = null;private string keystorepassword =  null;private String keyManagerPassword = null; Jettyserver (string webappdir, int port, string context, int  Scanintervalseconds)  {if  (webappdir == null) THROW&NBSp;new illegalstateexception ("invalid webappdir of web server: "  +  Webappdir);if  (port < 0 | |  port > 65536) throw new illegalargumentexception ("Invalid port of  web server:  " + port);if  (Strkit.isblank (context)) throw new  IllegalStateException ("invalid context of web server: "  + context); this.webappdir = webappdir;this.port = port;this.context = context; This.scanintervalseconds = scanintervalseconds;} Jettyserver (String webappdir, int port, string context, string keystorepath , String keystorepassword,string keymanagerpassword)  {this (webappdir,port,context,0); this.keymanagerpassword = keymanagerpassword;this.keystorepassword = keystorepassword; This.keystorepath = keystorepath;} Public void stArt ()  {if  (!running)  {try {dostart ();}  catch  (exception e)  {e.printstacktrace ();} Running = true;}} Public void stop ()  {if  (running)  {try {webapp.stop (); Server.stop ();}  catch  (exception e)  {e.printstacktrace ();} Running = false;}} Private void dostart ()  {if  (!available (port)) throw new illegalstateexception (" port:  " + port + "  already in use! "); Deletesessiondata (); System.out.println ("starting jfinal "  + const.jfinal_version);server = new  Server ();//httl configuration. if (Null == this.keystorepath) {httpconfiguration http_config = new  Httpconfiguration ();        // http connector         serverconnector connector = new serverconnector (server , New httpconNectionfactory (Http_config)); Connector.setreuseaddress (true); Connector.setidletimeout (30000); Connector.setPort (port); Server.addconnector (connector);} else{//https  configuration httpconfiguration https_config = new httpconfiguration (); https_ Config.setsecurescheme ("https"); Https_config.setsecureport (port); Https_config.setoutputbuffersize (32768); https _config.addcustomizer (New securerequestcustomizer ()); Sslcontextfactory sslcontextfactory = new sslcontextfactory ();         sslcontextfactory.setkeystorepath (This.keystorepath);         sslcontextfactory.setkeystorepassword (This.keystorepassword);         sslcontextfactory.setkeymanagerpassword (This.keymanagerpassword);         serverconnector httpsconnector = new serverconnector (server,                  new sslconnectionfactory (SslContextFactory, "http/1.1"),                 new  Httpconnectionfactory (Https_config));         httpsconnector.setport ( Port);         httpsconnector.setidletimeout (500000);         server.addconnector (httpsconnector);} Webapp = new webappcontext ();    /** *  increased gzip support  */ Filterholder  fh = new filterholder (); fh.setasyncsupported (true); Fh.setClassName (" Org.eclipse.jetty.servlets.GzipFilter "); Fh.setinitparameter (" Mimetypes ", " Text/html,text/plain,text/xml, Text/css,text/javascript,application/javascript,image/gif,image/png "); Enumset<dispatchertype> set = enumset.noneof (Dispatchertype.class); Set.add ( Dispatchertype.requEST); Set.add (Dispatchertype.forward); Set.add (dispatchertype.include); Set.add (Dispatchertype.error); Set.add ( Dispatchertype.async); Webapp.addfilter (fh,  "/*",  set); Webapp.setcontextpath (context); Webapp.setresourcebase (Webappdir); webapp.setmaxformcontentsize (81920000); Webapp.getinitparams (). Put (" Org.eclipse.jetty.servlet.Default.dirAllowed ", " false "); Webapp.getinitparams (). Put (" Org.eclipse.jetty.servlet.Default.useFileMappedBuffer ", " true "); Webapp.getinitparams (). Put (" Org.eclipse.jetty.server.Request.maxFormContentSize ", "-1 ");p ersistsession (WEBAPP); Server.sethandler ( WEBAPP); Changeclassloader (WEBAPP);// configurescannerif  (scanintervalseconds > 0)   {Scanner scanner = new scanner (Pathkit.getrootclasspath (),  scanintervalseconds)   {Public void onchange ()  {try {system.err.println ("\NLOADING&NBSP;CHANGES&NBSP; ..."); Webapp.stop (); Jfinalclassloader loader = new jfinalclassloAder (Webapp, getclasspath ()); Webapp.setclassloader (loader); Webapp.start (); System.err.println ("Loading complete.");  catch  (exception e)  {system.err.println ("Error reconfiguring/restarting webapp  after change in watched files "); E.printstacktrace ();}}; System.out.println ("starting scanner at interval of "  + scanintervalseconds  +  " seconds."); Scanner.start ();} Try {system.out.println ("starting web server on port: "  + port); Server.start (); System.out.println ("starting complete. welcome to the jfinal world :)"); Server.join ();}  catch  (exception e)  {e.printstacktrace (); System.exit (100);} return;} @SuppressWarnings ("Resource") Private void changeclassloader (Webappcontext webapp)  {try  {string classpath = getclasspath (); jfinalclassloader wacl =&Nbsp;new jfinalclassloader (Webapp, classpath); Wacl.addclasspath (ClassPath);}  catch  (ioexception e)  {e.printstacktrace ();}} Private string getclasspath ()  {return system.getproperty ("Java.class.path");} Private void deletesessiondata ()  {try {filekit.delete (New file (GetStoreDir ())); catch  (exception e)  {}}private string getstoredir ()  {string storedir =  pathkit.getwebrootpath ()  +  "/.. /.. /session_data " + context;if  (" \ \ ". Equals (File.separator)) storedir =  Storedir.replaceall ("/",  "\\\\"); return storedir;} Private void persistsession (Webappcontext webapp)  {String storeDir =  Getstoredir (); Sessionmanager sm = webapp.getsessionhandler (). Getsessionmanager ();if  (sm instanceof  hashsessionmanager)  {try {((Hashsessionmanager) SM). Setstoredirectory (New File (storeDir));}  catch  (ioexception e)  {e.printstacktrace ();} return ;} Hashsessionmanager hsm = new hashsessionmanager (); Try {hsm.setstoredirectory (new  file (Storedir));}  catch  (ioexception e)  {e.printstacktrace ();} Sessionhandler sh = new sessionhandler (); Sh.setsessionmanager (HSM); Webapp.setsessionhandler (SH);} Private static boolean available (Int port)  {if  (port <= 0)  { Throw new illegalargumentexception ("invalid start port: "  + port); Serversocket ss = null;datagramsocket ds = null;try {ss = new  serversocket (port); Ss.setreuseaddress (True);d s = new datagramsocket (port); Ds.setreuseaddress (true); return true;}  catch  (ioexception e)  {} finally {if  (ds != null)  {ds.close ();} if  (ss != null)  {tRy {ss.close ();}  catch  (ioexception e)  {// should not be thrown, just detect  port available.}}} Return false;}}

In order to support SSL also need to modify the Com.jfinal.core.JFinal implementation, add the corresponding method.

public static void Startsslserver (string webappdir, int port, string context, String keystorepath,string Keystorepassword , String keymanagerpassword) {server = Serverfactory.gethttpsserver (Webappdir, port, context, Keystorepath, Keystorepassword, Keymanagerpassword); Server.start ();}

Jetty-all

http://repo1.maven.org/maven2/org/eclipse/jetty/aggregate/jetty-all/


Upgrade jfinal built-in jetty to version 9, and support SSL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.