Ejemplo n.º 1
0
  /**
   * Start Jetty web server
   *
   * @param version of bigloupe-chart
   * @return
   * @throws Exception
   */
  private Server startWebServer(String version) throws Exception {
    if (!cmd.hasOption(OPTION_NO_WEBSERVER)) {

      applyOptionWithWebServer();

      Server server = new Server(getPortWebServer());

      WebAppContext root = new WebAppContext();
      root.setContextPath("/");

      if (cmd.hasOption(OPTION_WEBSERVER_WEBROOT)) {
        String webRoot = cmd.getOptionValue(OPTION_WEBSERVER_WEBROOT);
        Resource resource = FileResource.newResource(webRoot);
        root.setBaseResource(resource);

      } else {
        String webFiles = "bigloupe-chart-" + version + "-webapp.war";
        File fileWebApp = new File(webFiles);
        if (!fileWebApp.exists()) {
          if (version.equals("'undefined'")) {
            Resource resource = FileResource.newResource("src/main/webapp");
            root.setBaseResource(resource);
            root.setDefaultsDescriptor("./etc/webdefault.xml");
            logger.info(
                "Embedded webServer started with base resource "
                    + resource.getFile().getAbsolutePath());
          } else {
            logger.info(webFiles + " file not available");
            logger.info("Embedded webServer will be not started");
            return null;
          }
        } else {
          root.setWar(fileWebApp.getAbsolutePath());
        }
      }

      File tmp = new File("tmp");
      if (!tmp.exists()) tmp.mkdir();
      root.setTempDirectory(tmp);

      ContextHandlerCollection contexts = new ContextHandlerCollection();
      Handler handlerHawtIO = addWebApplicationHawtIO();
      if (handlerHawtIO != null) contexts.setHandlers(new Handler[] {root, handlerHawtIO});
      else contexts.setHandlers(new Handler[] {root});
      server.setHandler(contexts);

      server.start();
      addWebServerJMXSupport(server);
      return server;
    } else {
      applyOptionWithoutWebServer();
      return null;
    }
  }
 public void visitMetaInfResource(WebAppContext context, Resource dir) {
   Collection<Resource> metaInfResources =
       (Collection<Resource>) context.getAttribute(MetaInfConfiguration.METAINF_RESOURCES);
   if (metaInfResources == null) {
     metaInfResources = new HashSet<Resource>();
     context.setAttribute(MetaInfConfiguration.METAINF_RESOURCES, metaInfResources);
   }
   metaInfResources.add(dir);
   // also add to base resource of webapp
   Resource[] collection = new Resource[metaInfResources.size() + 1];
   int i = 0;
   collection[i++] = context.getBaseResource();
   for (Resource resource : metaInfResources) collection[i++] = resource;
   context.setBaseResource(new ResourceCollection(collection));
 }
Ejemplo n.º 3
0
  static WebAppContext newWebAppContext() throws MalformedURLException {
    final WebAppContext handler = new WebAppContext();
    handler.setContextPath("/");
    handler.setBaseResource(Resource.newClassPathResource("/tomcat_service"));
    handler.setClassLoader(
        new URLClassLoader(
            new URL[] {
              Resource.newClassPathResource("/tomcat_service/WEB-INF/lib/hello.jar")
                  .getURI()
                  .toURL()
            },
            JettyService.class.getClassLoader()));

    handler.addBean(new ServletContainerInitializersStarter(handler), true);
    handler.setAttribute(
        "org.eclipse.jetty.containerInitializers",
        Collections.singletonList(new ContainerInitializer(new JettyJasperInitializer(), null)));
    return handler;
  }
Ejemplo n.º 4
0
  private WebAppContext buildWebAppContext(String[] args, Enviroment env) {

    AnnotationConfigWebApplicationContext applicationContext =
        new AnnotationConfigWebApplicationContext();
    applicationContext.register(WebContext.class);

    WebAppContext handler = new WebAppContext();
    handler.setContextPath(CONTEXT_NAME);
    handler.setDisplayName(DISPLAY_NAME);

    String[] resources = null;
    resources = new String[] {"./WebContent"};

    handler.setWelcomeFiles(new String[] {"index.html"});
    handler.setInitParameter("useFileMappedBuffer", "false");
    handler.setBaseResource(new ResourceCollection(resources));
    handler.setResourceAlias("/WEB-INF/classes/", "/classes/");

    this.appendListeners(applicationContext, handler);
    this.appendSpringDispatcherServlet(applicationContext, handler);

    applicationContext.close();
    return handler;
  }
Ejemplo n.º 5
0
  public void startWebSocketServer(final Injector injector) {
    httpServer = new Server();

    List<Connector> connectors = getSelectChannelConnectors(httpAddresses);
    if (connectors.isEmpty()) {
      LOG.severe("No valid http end point address provided!");
    }
    for (Connector connector : connectors) {
      httpServer.addConnector(connector);
    }
    final WebAppContext context = new WebAppContext();

    context.setParentLoaderPriority(true);

    if (jettySessionManager != null) {
      // This disables JSessionIDs in URLs redirects
      // see:
      // http://stackoverflow.com/questions/7727534/how-do-you-disable-jsessionid-for-jetty-running-with-the-eclipse-jetty-maven-plu
      // and:
      // http://jira.codehaus.org/browse/JETTY-467?focusedCommentId=114884&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-114884
      jettySessionManager.setSessionIdPathParameterName(null);

      context.getSessionHandler().setSessionManager(jettySessionManager);
    }
    final ResourceCollection resources = new ResourceCollection(resourceBases);
    context.setBaseResource(resources);

    addWebSocketServlets();

    try {
      final Injector parentInjector = injector;

      final ServletModule servletModule = getServletModule(parentInjector);

      ServletContextListener contextListener =
          new GuiceServletContextListener() {

            private final Injector childInjector =
                parentInjector.createChildInjector(servletModule);

            @Override
            protected Injector getInjector() {
              return childInjector;
            }
          };

      context.addEventListener(contextListener);
      context.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
      context.addFilter(GzipFilter.class, "/webclient/*", EnumSet.allOf(DispatcherType.class));
      String[] hosts = new String[httpAddresses.length];
      for (int i = 0; i < httpAddresses.length; i++) {
        hosts[i] = httpAddresses[i].getHostName();
      }
      context.addVirtualHosts(hosts);
      httpServer.setHandler(context);

      httpServer.start();
      restoreSessions();

    } catch (Exception e) { // yes, .start() throws "Exception"
      LOG.severe("Fatal error starting http server.", e);
      return;
    }
    LOG.fine("WebSocket server running.");
  }