Exemplo n.º 1
0
  public void applyHandle(Boolean warDeployFlag) {
    if (StringUtils.isBlank(webDefault)) {
      webDefault = "org.eclipse.jetty.webapp.webdefault.xml";
    }

    // contexts handler
    ContextHandlerCollection contexthandler = new ContextHandlerCollection();

    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath(contextPath);
    webapp.setDefaultsDescriptor(webDefault);
    webapp.setTempDirectory(new File(warTmp));

    // webapp.setParentLoaderPriority(true);
    if (!warDeployFlag) {
      webapp.setResourceBase(resourceBase);
      webapp.setDescriptor(webXmlPath);
    } else {
      webapp.setWar(warPath);
    }

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[] {"index.html"});
    resource_handler.setResourceBase(resourceBase);

    contexthandler.setHandlers(
        new Handler[] {
          webapp, resource_handler, new DefaultHandler(), new StatisticsHandler(), new GzipHandler()
        });

    super.setHandler(contexthandler);
  }
Exemplo n.º 2
0
 private static WebAppContext loadContext(String webappDirLocation, String webXmlLocation) {
   WebAppContext context = new WebAppContext();
   context.setContextPath(getContext());
   context.setDescriptor(webXmlLocation);
   context.setResourceBase(webappDirLocation);
   context.setParentLoaderPriority(true);
   return context;
 }
Exemplo n.º 3
0
  public static void main(String[] args) throws Exception {
    Server server = new Server(8080);

    WebAppContext context = new WebAppContext();
    context.setDescriptor("src/main/webapp/WEB-INF/web.xml");
    context.setResourceBase("src/main/webapp/");
    context.setContextPath("/");
    context.setParentLoaderPriority(true);
    server.setHandler(context);
    server.start();
  }
Exemplo n.º 4
0
  public static void main(String[] args) throws Exception {
    Server server = new Server(8080);

    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setResourceBase("webapp");
    webAppContext.setDescriptor("webapp/WEB-INF/web.xml");
    webAppContext.setContextPath("/");
    // server.setHandler(webAppContext);
    server.setHandler(new HelloWorld());

    server.start();
    server.join();
  }
Exemplo n.º 5
0
 public void start(int port) throws Exception {
   LOG.warn("Port used: " + port + " location " + WEBAPP_LOCATION + " " + databaseInfo.toString());
   server = new Server(port);
   WebAppContext root = new WebAppContext();
   root.setContextPath("/");
   root.setDescriptor(WEBAPP_LOCATION + "/WEB-INF/web.xml");
   root.setResourceBase(WEBAPP_LOCATION);
   root.setParentLoaderPriority(true);
   root.setAttribute(ConsoleFilter.DATABASE_ATTRIBUTE, databaseInfo);
   setupRequestLimits(root, REQUEST_TIME_LIMIT, MAX_OPS_LIMIT);
   server.setHandler(root);
   server.start();
 }
  public static void main(String[] args) throws Exception {
    System.out.println("Starting Frontend...");
    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setContextPath("/");
    webAppContext.setResourceBase("webapp");
    webAppContext.setDescriptor("webapp/WEB-INF/web.xml");

    Server server = new Server(8080);
    server.setHandler(webAppContext);

    server.start();
    System.out.println("Started Frontend.");
    server.join();
  }
Exemplo n.º 7
0
  public static void main(String[] args) throws Exception {
    String host = null;
    int port = 8080;
    String contextPath = "/";
    boolean forceHttps = false;

    for (String arg : args) {
      if (arg.startsWith("--") && arg.contains("=")) {
        String[] dim = arg.split("=");
        if (dim.length >= 2) {
          if (dim[0].equals("--host")) {
            host = dim[1];
          } else if (dim[0].equals("--port")) {
            port = Integer.parseInt(dim[1]);
          } else if (dim[0].equals("--prefix")) {
            contextPath = dim[1];
          } else if (dim[0].equals("--https") && (dim[1].equals("1") || dim[1].equals("true"))) {
            forceHttps = true;
          } else if (dim[0].equals("--gitbucket.home")) {
            System.setProperty("gitbucket.home", dim[1]);
          }
        }
      }
    }

    Server server = new Server();

    HttpsSupportConnector connector = new HttpsSupportConnector(forceHttps);
    if (host != null) {
      connector.setHost(host);
    }
    connector.setMaxIdleTime(1000 * 60 * 60);
    connector.setSoLingerTime(-1);
    connector.setPort(port);
    server.addConnector(connector);

    WebAppContext context = new WebAppContext();
    ProtectionDomain domain = JettyLauncher.class.getProtectionDomain();
    URL location = domain.getCodeSource().getLocation();

    context.setContextPath(contextPath);
    context.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml");
    context.setServer(server);
    context.setWar(location.toExternalForm());

    server.setHandler(context);
    server.start();
    server.join();
  }
Exemplo n.º 8
0
 public void start(int port) throws Exception {
   LOG.warn("Port used: " + port + " location " + WEBAPP_LOCATION + " " + databaseInfo.toString());
   server = new Server(port);
   WebAppContext root = new WebAppContext();
   root.setContextPath("/");
   root.setDescriptor(WEBAPP_LOCATION + "/WEB-INF/web.xml");
   root.setResourceBase(WEBAPP_LOCATION);
   root.setParentLoaderPriority(true);
   root.setAttribute(BackendFilter.DATABASE_ATTRIBUTE, databaseInfo);
   //        setupRequestLimits(root, REQUEST_TIME_LIMIT, MAX_OPS_LIMIT);
   final HandlerList handlers = new HandlerList();
   final Handler resourceHandler = createResourceHandler("/console_assets", WEBAPP_LOCATION);
   handlers.setHandlers(new Handler[] {resourceHandler, root});
   server.setHandler(handlers);
   server.start();
 }
  public static void main(String[] args) throws Exception {

    Server server = new Server(8080);
    String dir =
        "/home/gpt/Desarrollo/freedomotic/framework/freedomotic/plugins/devices/es.gpulido.webserver/data/webapps/gwt_client";
    WebAppContext context = new WebAppContext();

    context.setDescriptor(dir + "/WEB-INF/web.xml");
    context.setResourceBase(
        "/home/gpt/Desarrollo/freedomotic/framework/freedomotic/plugins/devices/es.gpulido.webserver/data/webapps/gwt_client");
    context.setContextPath("/");
    context.setParentLoaderPriority(true);

    server.setHandler(context);
    server.start();
  }
Exemplo n.º 10
0
  /** Starts the server at this base directory and port */
  public RealVRaptor(File base, int port) {
    this.server = new Server(port);
    WebAppContext context = new WebAppContext();

    context.setDescriptor(base.getAbsolutePath() + "/WEB-INF/web.xml");
    context.setResourceBase(base.getAbsolutePath());
    context.setContextPath("/");
    context.setParentLoaderPriority(true);
    LOG.info("Loading port " + port + " with vraptor context at " + base.getAbsolutePath());

    server.setHandler(context);

    try {
      server.start();
      this.port = port;
      LOG.info("VRaptor context started");
    } catch (Exception e) {
      throw new VRaptorException("Unable to start vraptor", e);
    }
  }
Exemplo n.º 11
0
  public static void main(String[] args) throws Exception {
    String webappDirLocation = "src/main/webapp/";

    String webPort = System.getenv("PORT");
    if (webPort == null || webPort.isEmpty()) {
      webPort = "8080";
    }

    Server server = new Server(Integer.valueOf(webPort));
    WebAppContext root = new WebAppContext();

    root.setContextPath("/");
    root.setDescriptor(webappDirLocation + "/WEB-INF/web.xml");
    root.setResourceBase(webappDirLocation);
    root.setParentLoaderPriority(true);

    server.setHandler(root);
    server.start();
    server.join();
  }
  @Test
  public void testCluster() throws Exception {

    String projectBaseDirectory = System.getProperty("user.dir");

    //
    // Create master node
    //
    Server masterServer = new Server(8080);

    WebAppContext masterContext = new WebAppContext();
    masterContext.setDescriptor(projectBaseDirectory + "/target/vaporware/WEB-INF/web.xml");
    masterContext.setResourceBase(projectBaseDirectory + "/target/vaporware");
    masterContext.setContextPath("/");
    masterContext.setConfigurations(
        new Configuration[] {
          new WebInfConfiguration(),
          new WebXmlConfiguration(),
          new MetaInfConfiguration(),
          new FragmentConfiguration(),
          new EnvConfiguration(),
          new PlusConfiguration(),
          new AnnotationConfiguration(),
          new JettyWebXmlConfiguration(),
          new TagLibConfiguration()
        });
    masterContext.setParentLoaderPriority(true);

    masterServer.setHandler(masterContext);
    masterServer.start();
    // masterServer.join();

    //
    // Create slave node
    //
    Server slaveServer = new Server(8181);

    WebAppContext slaveContext = new WebAppContext();
    slaveContext.setDescriptor(projectBaseDirectory + "/target/vaporware/WEB-INF/web-slave.xml");
    slaveContext.setResourceBase(projectBaseDirectory + "/target/vaporware");
    slaveContext.setContextPath("/");
    slaveContext.setConfigurations(
        new Configuration[] {
          new WebInfConfiguration(),
          new WebXmlConfiguration(),
          new MetaInfConfiguration(),
          new FragmentConfiguration(),
          new EnvConfiguration(),
          new PlusConfiguration(),
          new AnnotationConfiguration(),
          new JettyWebXmlConfiguration(),
          new TagLibConfiguration()
        });
    slaveContext.setParentLoaderPriority(true);

    slaveServer.setHandler(slaveContext);
    slaveServer.start();
    // slaveServer.join();

    // Try to let the user terminate the Jetty server instances gracefully.  This won't work in an
    // environment like Eclipse, if
    // console input can't be received.  However, even in that that case you will be able to kill
    // the Maven process without
    // a Java process lingering around (as would be the case if you used "Sever.join()" to pause
    // execution of this thread).
    System.out.println("PRESS <ENTER> TO HALT SERVERS (or kill the Maven process)...");
    Scanner scanner = new Scanner(System.in);
    String line = scanner.nextLine();
    System.out.println(line);
    scanner.close();
    masterServer.stop();
    slaveServer.stop();
    System.out.println("Servers halted");
  }
  public ServerMain(String configDir) throws Exception {

    config = Util.getConfig(configDir);
    // make sure we have all the correct dirs and files now
    ensureInstall();

    logger.info("Freeboard starting....");

    // do we have a USB drive connected?
    logger.info("USB drive " + Util.getUSBFile());

    // create a new Camel Main so we can easily start Camel
    Main main = new Main();

    // enable hangup support which mean we detect when the JVM terminates, and stop Camel graceful
    main.enableHangupSupport();

    NavDataWebSocketRoute route = new NavDataWebSocketRoute(config);
    // must do this early!
    CamelContextFactory.setContext(route);
    // web socket on port 9090
    logger.info("  Websocket port:" + config.getProperty(Constants.WEBSOCKET_PORT));
    route.setPort(Integer.valueOf(config.getProperty(Constants.WEBSOCKET_PORT)));

    // are we running demo?
    logger.info("  Serial url:" + config.getProperty(Constants.SERIAL_URL));
    route.setSerialUrl(config.getProperty(Constants.SERIAL_URL));

    // add our routes to Camel
    main.addRouteBuilder(route);

    Connector connector = new SelectChannelConnector();
    logger.info("  Webserver http port:" + config.getProperty(Constants.HTTP_PORT));
    connector.setPort(Integer.valueOf(config.getProperty(Constants.HTTP_PORT)));

    // virtual hosts
    String virtualHosts = config.getProperty(Constants.VIRTUAL_URL);
    server = new Server();
    server.addConnector(connector);

    // serve mapcache
    ServletContextHandler mapContext = new ServletContextHandler();
    logger.info("  Mapcache url:" + config.getProperty(Constants.MAPCACHE));
    mapContext.setContextPath(config.getProperty(Constants.MAPCACHE));
    logger.info("  Mapcache resource:" + config.getProperty(Constants.MAPCACHE_RESOURCE));
    mapContext.setResourceBase(config.getProperty(Constants.MAPCACHE_RESOURCE));
    ServletHolder mapHolder = mapContext.addServlet(DefaultServlet.class, "/*");
    mapHolder.setInitParameter("cacheControl", "max-age=3600,public");

    // serve tracks
    ServletContextHandler trackContext = new ServletContextHandler();
    logger.info("  Tracks url:" + config.getProperty(Constants.TRACKS));
    trackContext.setContextPath(config.getProperty(Constants.TRACKS));
    logger.info("  Tracks resource:" + config.getProperty(Constants.TRACKS_RESOURCE));
    trackContext.setResourceBase(config.getProperty(Constants.TRACKS_RESOURCE));
    trackContext.addServlet(DefaultServlet.class, "/*");

    if (StringUtils.isNotBlank(virtualHosts)) {
      mapContext.setVirtualHosts(virtualHosts.split(","));
      trackContext.setVirtualHosts(virtualHosts.split(","));
    }
    HandlerList handlers = new HandlerList();
    handlers.addHandler(mapContext);
    handlers.addHandler(trackContext);

    // serve freeboard
    WebAppContext wac = new WebAppContext();
    logger.info("  Freeboard resource:" + config.getProperty(Constants.FREEBOARD_RESOURCE));
    wac.setWar(config.getProperty(Constants.FREEBOARD_RESOURCE));
    wac.setDefaultsDescriptor(
        config.getProperty(Constants.FREEBOARD_RESOURCE) + "WEB-INF/webdefault.xml");

    wac.setDescriptor(config.getProperty(Constants.FREEBOARD_RESOURCE) + "WEB-INF/web.xml");
    logger.info("  Freeboard url:" + config.getProperty(Constants.FREEBOARD_URL));
    wac.setContextPath(config.getProperty(Constants.FREEBOARD_URL));
    wac.setServer(server);
    wac.setParentLoaderPriority(true);
    wac.setVirtualHosts(null);
    if (StringUtils.isNotBlank(virtualHosts)) {
      wac.setVirtualHosts(virtualHosts.split(","));
    }
    handlers.addHandler(wac);

    server.setHandler(handlers);
    server.start();

    // and run, which keeps blocking until we terminate the JVM (or stop CamelContext)
    main.run();

    // so now shutdown serial reader and server

    route.stopSerial();
    server.stop();
    System.exit(0);
  }
    public void configureWebApp() throws Exception {
      // TODO turn this around and let any context.xml file get applied first, and have the
      // properties override
      _webApp.setContextPath(_contextPath);

      // osgi Enterprise Spec r4 p.427
      _webApp.setAttribute(OSGiWebappConstants.OSGI_BUNDLECONTEXT, _bundle.getBundleContext());

      String overrideBundleInstallLocation =
          (String) _properties.get(OSGiWebappConstants.JETTY_BUNDLE_INSTALL_LOCATION_OVERRIDE);
      File bundleInstallLocation =
          (overrideBundleInstallLocation == null
              ? BundleFileLocatorHelperFactory.getFactory()
                  .getHelper()
                  .getBundleInstallLocation(_bundle)
              : new File(overrideBundleInstallLocation));
      URL url = null;
      Resource rootResource =
          Resource.newResource(
              BundleFileLocatorHelperFactory.getFactory()
                  .getHelper()
                  .getLocalURL(bundleInstallLocation.toURI().toURL()));
      // try and make sure the rootResource is useable - if its a jar then make it a jar file url
      if (rootResource.exists()
          && !rootResource.isDirectory()
          && !rootResource.toString().startsWith("jar:")) {
        Resource jarResource = JarResource.newJarResource(rootResource);
        if (jarResource.exists() && jarResource.isDirectory()) rootResource = jarResource;
      }

      // if the path wasn't set or it was ., then it is the root of the bundle's installed location
      if (_webAppPath == null || _webAppPath.length() == 0 || ".".equals(_webAppPath)) {
        url = bundleInstallLocation.toURI().toURL();
      } else {
        // Get the location of the root of the webapp inside the installed bundle
        if (_webAppPath.startsWith("/") || _webAppPath.startsWith("file:")) {
          url = new File(_webAppPath).toURI().toURL();
        } else if (bundleInstallLocation != null && bundleInstallLocation.isDirectory()) {
          url = new File(bundleInstallLocation, _webAppPath).toURI().toURL();
        } else if (bundleInstallLocation != null) {
          Enumeration<URL> urls =
              BundleFileLocatorHelperFactory.getFactory()
                  .getHelper()
                  .findEntries(_bundle, _webAppPath);
          if (urls != null && urls.hasMoreElements()) url = urls.nextElement();
        }
      }

      if (url == null) {
        throw new IllegalArgumentException(
            "Unable to locate "
                + _webAppPath
                + " in "
                + (bundleInstallLocation != null
                    ? bundleInstallLocation.getAbsolutePath()
                    : "unlocated bundle '" + _bundle.getSymbolicName() + "'"));
      }

      // Sets the location of the war file
      // converts bundleentry: protocol if necessary
      _webApp.setWar(
          BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(url).toString());

      // Set up what has been configured on the provider
      _webApp.setParentLoaderPriority(isParentLoaderPriority());
      _webApp.setExtractWAR(isExtract());
      if (getConfigurationClasses() != null)
        _webApp.setConfigurationClasses(getConfigurationClasses());
      else _webApp.setConfigurationClasses(__defaultConfigurations);

      if (getDefaultsDescriptor() != null) _webApp.setDefaultsDescriptor(getDefaultsDescriptor());

      // Set up configuration from manifest headers
      // extra classpath
      String tmp = (String) _properties.get(OSGiWebappConstants.JETTY_EXTRA_CLASSPATH);
      if (tmp != null) _webApp.setExtraClasspath(tmp);

      // web.xml
      tmp = (String) _properties.get(OSGiWebappConstants.JETTY_WEB_XML_PATH);
      if (tmp != null && tmp.trim().length() != 0) {
        File webXml = getFile(tmp, bundleInstallLocation);
        if (webXml != null && webXml.exists()) _webApp.setDescriptor(webXml.getAbsolutePath());
      }

      // webdefault.xml
      tmp = (String) _properties.get(OSGiWebappConstants.JETTY_DEFAULT_WEB_XML_PATH);
      if (tmp != null) {
        File defaultWebXml = getFile(tmp, bundleInstallLocation);
        if (defaultWebXml != null) {
          if (defaultWebXml.exists())
            _webApp.setDefaultsDescriptor(defaultWebXml.getAbsolutePath());
          else LOG.warn(defaultWebXml.getAbsolutePath() + " does not exist");
        }
      }

      // Handle Require-TldBundle
      // This is a comma separated list of names of bundles that contain tlds that this webapp uses.
      // We add them to the webapp classloader.
      String requireTldBundles = (String) _properties.get(OSGiWebappConstants.REQUIRE_TLD_BUNDLE);
      String pathsToTldBundles = getPathsToRequiredBundles(requireTldBundles);

      // make sure we provide access to all the jetty bundles by going
      // through this bundle.
      OSGiWebappClassLoader webAppLoader =
          new OSGiWebappClassLoader(
              _serverWrapper.getParentClassLoaderForWebapps(), _webApp, _bundle);

      if (pathsToTldBundles != null) webAppLoader.addClassPath(pathsToTldBundles);
      _webApp.setClassLoader(webAppLoader);

      // apply any META-INF/context.xml file that is found to configure
      // the webapp first
      applyMetaInfContextXml(rootResource);

      // pass the value of the require tld bundle so that the TagLibOSGiConfiguration
      // can pick it up.
      _webApp.setAttribute(OSGiWebappConstants.REQUIRE_TLD_BUNDLE, requireTldBundles);

      // Set up some attributes
      // rfc66
      _webApp.setAttribute(
          OSGiWebappConstants.RFC66_OSGI_BUNDLE_CONTEXT, _bundle.getBundleContext());

      // spring-dm-1.2.1 looks for the BundleContext as a different attribute.
      // not a spec... but if we want to support
      // org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext
      // then we need to do this to:
      _webApp.setAttribute(
          "org.springframework.osgi.web." + BundleContext.class.getName(),
          _bundle.getBundleContext());

      // also pass the bundle directly. sometimes a bundle does not have a
      // bundlecontext.
      // it is still useful to have access to the Bundle from the servlet
      // context.
      _webApp.setAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE, _bundle);
    }