Guest User

jetty server

a guest
Mar 11th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. /*
  2.  * Copyright (c) Sergiu Giurgiu 2011.
  3.  *
  4.  * This file is part of TVMan.
  5.  *
  6.  * TVMan is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * TVMan is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with TVMan.  If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19.  
  20. package com.zergiu.tvman;
  21.  
  22. import org.eclipse.jetty.plus.jndi.Resource;
  23. import org.eclipse.jetty.server.AbstractConnector;
  24. import org.eclipse.jetty.server.Connector;
  25. import org.eclipse.jetty.server.Server;
  26. import org.eclipse.jetty.server.bio.SocketConnector;
  27. import org.eclipse.jetty.server.nio.SelectChannelConnector;
  28. import org.eclipse.jetty.server.ssl.SslConnector;
  29. import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
  30. import org.eclipse.jetty.server.ssl.SslSocketConnector;
  31. import org.eclipse.jetty.util.ssl.SslContextFactory;
  32. import org.eclipse.jetty.util.thread.QueuedThreadPool;
  33. import org.eclipse.jetty.webapp.WebAppContext;
  34. import org.quartz.SchedulerException;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37. import org.springframework.scheduling.quartz.SchedulerFactoryBean;
  38.  
  39. public class TVManWebServer {
  40.     private WebAppContext context;
  41.     private Server server;
  42.     private String serverUrl;
  43.     private boolean showSystemTray = true;
  44.     private Resource resource;
  45.     private boolean shutdown=true;
  46.    
  47.     private static Logger log=LoggerFactory.getLogger(TVManWebServer.class);
  48.    
  49.     public TVManWebServer() {
  50.  
  51.     }
  52.  
  53.     public void start() throws Exception {
  54.         TVManOptions options=TVManOptions.getInstance();
  55.         log.debug("starting the server:");
  56.         server = new Server();
  57.  
  58.         //if we ever want to run with the non-nio connector, we can
  59.         AbstractConnector connector = null;
  60.        
  61.         connector = getJettyConnector();
  62.        
  63.         setupSSLContext(connector);
  64.  
  65.         setJettyConnectorProperties(connector);
  66.  
  67.         server.setConnectors(new Connector[] { connector });
  68.  
  69.         createApplicationContext();
  70.  
  71.         resource = new Resource("jdbc/TVManDataSource",
  72.                 TVManDatasource.getDatasource(options.getDbLocation()));
  73.  
  74.         server.setHandler(context);
  75.         server.start();
  76.         boolean ssl=options.isSsl();
  77.         String protocol=ssl?"https":"http";    
  78.         serverUrl = protocol+"://" + options.getHost() + ":" + options.getPort()+ "/";
  79.  
  80.         if (showSystemTray) {
  81.             TVDBSystemTray tray = new TVDBSystemTray(this);
  82.             tray.showSystemTray();
  83.         }
  84.         shutdown=false;
  85.         log.info("Server started and is listening on "+serverUrl);     
  86.         server.join();
  87.     }
  88.  
  89.     private void createApplicationContext() {
  90.         context = new WebAppContext(getClass().getResource(
  91.                 "/com/zergiu/tvman/web").toExternalForm(), "/");
  92.         context.setConfigurationClasses(new String[] {
  93.                 "org.eclipse.jetty.webapp.WebInfConfiguration",
  94.                 "org.eclipse.jetty.webapp.WebXmlConfiguration",
  95.                // "org.eclipse.jetty.webapp.MetaInfConfiguration",
  96.                // "org.eclipse.jetty.webapp.FragmentConfiguration",
  97.                // "org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
  98.                
  99.                 // This enables the <resource-ref> section of web.xml
  100.                 "org.eclipse.jetty.plus.webapp.EnvConfiguration" });
  101.         context.setExtractWAR(false);      
  102.     }
  103.  
  104.     private void setJettyConnectorProperties(AbstractConnector connector) {
  105.         TVManOptions options=TVManOptions.getInstance();
  106.         connector.setHost(options.getHost());
  107.         connector.setPort(options.getPort());      
  108.         connector.setThreadPool(new QueuedThreadPool(options.getMaxThreads()));
  109.         connector.setMaxIdleTime(options.getMaxIdleTime());
  110.     }
  111.  
  112.     private void setupSSLContext(AbstractConnector connector) {
  113.         TVManOptions options=TVManOptions.getInstance();
  114.         if(options.isSsl() && (connector instanceof SslConnector)){
  115.             SslContextFactory factory=((SslConnector)connector).getSslContextFactory();
  116.             factory.setKeyStorePath(options.getKeystorePath());
  117.             factory.setKeyStorePassword(options.getKeystorePassword());
  118.             factory.setKeyManagerPassword(options.getKeystoreManagerPassword());
  119.             factory.setTrustStore(options.getTrustStorePath());
  120.             factory.setTrustStorePassword(options.getTrustStorePassword());
  121.         }
  122.     }
  123.  
  124.     private AbstractConnector getJettyConnector() {
  125.         AbstractConnector connector;
  126.         TVManOptions options=TVManOptions.getInstance();
  127.         if (options.isUseIO()) {
  128.             if(options.isSsl()){
  129.                 connector=new SslSocketConnector();
  130.             }else{
  131.                 connector = new SocketConnector();
  132.             }
  133.         } else {
  134.             if(options.isSsl()){
  135.                 connector=new SslSelectChannelConnector();
  136.             }else{
  137.                 connector = new SelectChannelConnector();
  138.             }
  139.         }
  140.         return connector;
  141.     }
  142.  
  143.     public void shutdown() {
  144.         if(shutdown){
  145.             return;
  146.         }
  147.         shutdownSchedulerFactory();    
  148.         shutdownJettyServer();
  149.         context = null;
  150.         server = null;
  151.         resource=null;     
  152.         ApplicationContextProvider.getInstance().setApplicationContext(null);
  153.         shutdown=true;
  154.     }
  155.  
  156.     private void shutdownJettyServer() {
  157.         context.setShutdown(true);
  158.         try{
  159.             resource.release();
  160.         }catch(Exception e){
  161.             e.printStackTrace();
  162.         }
  163.         try {          
  164.             server.stop();
  165.         } catch (Exception e) {
  166.             e.printStackTrace();
  167.         }
  168.     }
  169.  
  170.     private void shutdownSchedulerFactory() {
  171.         SchedulerFactoryBean schedulerFactory=ApplicationContextProvider.getApplicationContext().getBean(SchedulerFactoryBean.class);
  172.         if(schedulerFactory!=null){
  173.             try {
  174.                 schedulerFactory.destroy();
  175.             } catch (SchedulerException e) {
  176.                 e.printStackTrace();
  177.             }
  178.         }
  179.     }
  180.  
  181.     public String getServerUrl() {
  182.         return serverUrl;
  183.     }
  184.  
  185.     public boolean isShowSystemTray() {
  186.         return showSystemTray;
  187.     }
  188.  
  189.     public void setShowSystemTray(boolean showSystemTray) {
  190.         this.showSystemTray = showSystemTray;
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment