Package ch.hortis.sonar.application

Source Code of ch.hortis.sonar.application.JettyEmbedder

/**
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar. Sonar is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Sonar; if not, write to the Free Software Foundation, Inc., 51 Franklin
* Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.application;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandler;
import org.mortbay.jetty.handler.ResourceHandler;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.xml.XmlConfiguration;

public class JettyEmbedder {
  private static final String STATIC_RESOURCE_BASE = "../lib/sonar-web/public/";

  public void start(int port) throws Exception {
    final Server server = new Server();
    Connector connector = new SelectChannelConnector();
    connector.setPort( port );
    server.setConnectors( new Connector[]{connector} );

    // optimization : jetty serves static content (images, CSS, js) before propogating the
    // request to ruby on rails (goldspike).
    // This allows to set Cache-Control=Public to activate browser cache
    addStaticHandler(server, "images");
    addStaticHandler(server, "javascripts");
    addStaticHandler(server, "stylesheets");

    WebAppContext webappcontext = new WebAppContext();
    XmlConfiguration configuration = new XmlConfiguration( JettyEmbedder.class.getResourceAsStream( "/jetty-env.xml" ) );
    configuration.configure( webappcontext );
    server.addHandler( webappcontext );
    server.start();

    Runtime.getRuntime().addShutdownHook( new Thread() {
      public void run() {
        try {
          server.stop();
        } catch ( Exception e ) {
          System.err.println("Error occured during jetty shutdown : " + e.getMessage() );
        }
      }
    });
    System.out.println( "Jetty server started on port " +port);
  }

  private void addStaticHandler(Server server, String contextPath) {
    ContextHandler staticHandler = new ContextHandler();
    staticHandler.setContextPath("/" + contextPath);
    staticHandler.setResourceBase(STATIC_RESOURCE_BASE + contextPath);
    ResourceHandler staticResourceHandler = new ResourceHandler();
    staticResourceHandler.setCacheControl("Public,max-age=604800");
    staticHandler.addHandler(staticResourceHandler);
    server.addHandler( staticHandler );
  }
}
TOP

Related Classes of ch.hortis.sonar.application.JettyEmbedder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.