コード例 #1
0
  /**
   * Starts the web server on the default {@link #PORT}. The given resourceBase is used to be the
   * ROOT directory that serves the default context.
   *
   * <p><b>Don't forget to stop the returned HttpServer after the test</b>
   *
   * @param resourceBase the base of resources for the default context
   * @param classpath additional classpath entries to add (may be null)
   * @param servlets map of {String, Class} pairs: String is the path spec, while class is the class
   * @throws Exception if the test fails
   */
  protected void startWebServer(
      final String resourceBase,
      final String[] classpath,
      final Map<String, Class<? extends Servlet>> servlets)
      throws Exception {
    if (server_ != null) {
      throw new IllegalStateException("startWebServer() can not be called twice");
    }
    server_ = new Server(PORT);

    final WebAppContext context = new WebAppContext();
    context.setContextPath("/");
    context.setResourceBase(resourceBase);

    for (final Map.Entry<String, Class<? extends Servlet>> entry : servlets.entrySet()) {
      final String pathSpec = entry.getKey();
      final Class<? extends Servlet> servlet = entry.getValue();
      context.addServlet(servlet, pathSpec);
    }
    final WebAppClassLoader loader = new WebAppClassLoader(context);
    if (classpath != null) {
      for (final String path : classpath) {
        loader.addClassPath(path);
      }
    }
    context.setClassLoader(loader);
    server_.setHandler(context);
    server_.start();
  }
コード例 #2
0
  /**
   * Starts the web server on the default {@link #PORT}. The given resourceBase is used to be the
   * ROOT directory that serves the default context.
   *
   * <p><b>Don't forget to stop the returned HttpServer after the test</b>
   *
   * @param resourceBase the base of resources for the default context
   * @param classpath additional classpath entries to add (may be null)
   * @throws Exception if the test fails
   */
  protected void startWebServer(final String resourceBase, final String[] classpath)
      throws Exception {
    if (server_ != null) {
      throw new IllegalStateException("startWebServer() can not be called twice");
    }
    server_ = new Server(PORT);

    final WebAppContext context = new WebAppContext();
    context.setContextPath("/");
    context.setResourceBase(resourceBase);
    final WebAppClassLoader loader = new WebAppClassLoader(context);
    if (classpath != null) {
      for (final String path : classpath) {
        loader.addClassPath(path);
      }
    }
    context.setClassLoader(loader);
    server_.setHandler(context);
    server_.start();
  }
コード例 #3
0
  /**
   * Starts the web server on the default {@link #PORT}. The given resourceBase is used to be the
   * ROOT directory that serves the default context.
   *
   * <p><b>Don't forget to stop the returned HttpServer after the test</b>
   *
   * @param resourceBase the base of resources for the default context
   * @throws Exception if the test fails
   */
  protected void startWebServer(final String resourceBase) throws Exception {
    if (server_ != null) {
      throw new IllegalStateException("startWebServer() can not be called twice");
    }
    server_ = new Server(PORT);

    final WebAppContext context = new WebAppContext();
    context.setContextPath("/");
    context.setResourceBase(resourceBase);

    final ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase(resourceBase);
    resourceHandler.getMimeTypes().addMimeMapping("js", "application/javascript");

    final HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {resourceHandler, context});
    server_.setHandler(handlers);
    server_.setHandler(resourceHandler);
    server_.start();
  }
コード例 #4
0
  public LiquidityServer() throws IOException {
    server = new Server(valueOf(getenv("PORT")));

    AnnotationConfigApplicationContext applicationContext =
        new AnnotationConfigApplicationContext();
    applicationContext.register(ProductionContext.class);
    applicationContext.refresh();

    URL resource = this.getClass().getClassLoader().getResource("webapp");
    String webapp = resource.toExternalForm();

    WebAppContext handler = new WebAppContext(webapp, CONTEXT);
    handler.setAttribute(
        WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
    handler.setResourceBase(webapp);

    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] {handler, new DefaultHandler()});
    server.addHandler(handlers);
  }
コード例 #5
0
  @SuppressWarnings("unchecked")
  public static void startup(String args[]) {
    try {
      Server server = new Server();
      System.err.println("mycat init ... ");

      int httpPort = 7066;
      int httpsPort = -1;
      String contextPath = "/";

      String keyStorePath = null;
      String keyStorePassword = null;

      String updateServer = null;

      boolean disableUpdateCenterSwitch = false;
      boolean skipInitSetup = false;

      for (int i = 0; i < args.length; ++i) {
        String portStr;
        if (args[i].startsWith("--httpPort=")) {
          portStr = args[i].substring("--httpPort=".length());
          httpPort = Integer.parseInt(portStr);
        }

        if (args[i].startsWith("--httpsPort=")) {
          portStr = args[i].substring("--httpsPort=".length());
          httpsPort = Integer.parseInt(portStr);
        }

        if (args[i].startsWith("--httpsKeyStore=")) {
          keyStorePath = args[i].substring("--httpsKeyStore=".length());
        }

        if (args[i].startsWith("--httpsKeyStorePassword="******"--httpsKeyStorePassword="******"--prefix=")) {
          String prefix = args[i].substring("--prefix=".length());
          if (prefix.startsWith("/")) contextPath = prefix;
          else {
            contextPath = "/" + prefix;
          }
        }
        if (args[i].startsWith("--updateServer=")) {
          updateServer = args[i].substring("--updateServer=".length());
        }

        if (args[i].startsWith("--disableUpdateCenterSwitch")) {
          disableUpdateCenterSwitch = true;
        }

        if (args[i].startsWith("--skipInitSetup")) {
          skipInitSetup = true;
        }
      }

      List connectors = new ArrayList();
      if (httpPort != -1) {
        SelectChannelConnector httpConnector = new SelectChannelConnector();
        httpConnector.setPort(httpPort);
        connectors.add(httpConnector);
      }

      if (httpsPort != -1) {
        SslSocketConnector httpsConnector = new SslSocketConnector();
        httpsConnector.setPort(httpsPort);
        if (keyStorePath != null) {
          httpsConnector.setKeystore(keyStorePath);
        }
        if (keyStorePassword != null) {
          httpsConnector.setKeyPassword(keyStorePassword);
        }
        connectors.add(httpsConnector);
      }

      server.setConnectors((Connector[]) connectors.toArray(new Connector[connectors.size()]));

      ProtectionDomain protectionDomain = Executable.class.getProtectionDomain();
      URL location = protectionDomain.getCodeSource().getLocation();
      String url = java.net.URLDecoder.decode(location.getPath(), "utf-8");
      System.out.println(url);

      File tempDir = new File(getHomeDir(), "war");
      tempDir.mkdirs();

      WebAppContext webapp = new WebAppContext();
      webapp.setContextPath("/mycat"); //
      webapp.setDescriptor(url + "/WEB-INF/web.xml");
      webapp.setResourceBase(url);
      webapp.setTempDirectory(tempDir);

      server.setHandler(webapp);

      System.err.println("mycat start success,port: " + httpsPort);
      server.start();
      server.join();

    } catch (Exception e) {
      System.exit(-1);
    }
  }
コード例 #6
0
ファイル: SenseiServerBuilder.java プロジェクト: sguo/sensei
  public Server buildHttpRestServer() throws Exception {
    int port = _senseiConf.getInt(SERVER_BROKER_PORT);

    String webappPath =
        _senseiConf.getString(SERVER_BROKER_WEBAPP_PATH, "sensei-core/src/main/webapp");

    Server server = new Server();

    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setName("Sensei Broker(jetty) threads");
    threadPool.setMinThreads(_senseiConf.getInt(SERVER_BROKER_MINTHREAD, 20));
    threadPool.setMaxThreads(_senseiConf.getInt(SERVER_BROKER_MAXTHREAD, 50));
    threadPool.setMaxIdleTimeMs(_senseiConf.getInt(SERVER_BROKER_MAXWAIT, 2000));
    // threadPool.start();
    server.setThreadPool(threadPool);

    logger.info("request threadpool started.");
    SelectChannelConnector connector = new SelectChannelConnector();
    connector.setPort(port);
    server.addConnector(connector);

    DefaultSenseiJSONServlet senseiServlet = new DefaultSenseiJSONServlet();
    ServletHolder senseiServletHolder = new ServletHolder(senseiServlet);

    SenseiHttpInvokerServiceServlet springServlet = new SenseiHttpInvokerServiceServlet();
    ServletHolder springServletHolder = new ServletHolder(springServlet);

    AgentServlet jmxServlet = new AgentServlet();
    ServletHolder jmxServletHolder = new ServletHolder(jmxServlet);

    WebAppContext senseiApp = new WebAppContext();
    senseiApp.addFilter(GzipFilter.class, "/" + SENSEI_CONTEXT_PATH + "/*", 1);

    // HashMap<String, String> initParam = new HashMap<String, String>();
    // if (_senseiConfFile != null) {
    // logger.info("Broker Configuration file: "+_senseiConfFile.getAbsolutePath());
    // initParam.put("config.file", _senseiConfFile.getAbsolutePath());
    // }
    // senseiApp.setInitParams(initParam);
    senseiApp.setAttribute("sensei.search.configuration", _senseiConf);
    senseiApp.setAttribute(
        SenseiConfigServletContextListener.SENSEI_CONF_PLUGIN_REGISTRY, pluginRegistry);
    senseiApp.setAttribute(
        "sensei.search.version.comparator",
        _gateway != null ? _gateway.getVersionComparator() : ZoieConfig.DEFAULT_VERSION_COMPARATOR);

    PartitionedLoadBalancerFactory<String> routerFactory =
        pluginRegistry.getBeanByFullPrefix(
            SenseiConfParams.SERVER_SEARCH_ROUTER_FACTORY, PartitionedLoadBalancerFactory.class);
    if (routerFactory == null) {
      routerFactory = new SenseiPartitionedLoadBalancerFactory(50);
    }

    senseiApp.setAttribute("sensei.search.router.factory", routerFactory);
    senseiApp.addEventListener(new SenseiConfigServletContextListener());
    senseiApp.addServlet(senseiServletHolder, "/" + SENSEI_CONTEXT_PATH + "/*");
    senseiApp.setResourceBase(webappPath);
    senseiApp.addServlet(springServletHolder, "/sensei-rpc/SenseiSpringRPCService");
    senseiApp.addServlet(jmxServletHolder, "/admin/jmx/*");

    server.setHandler(senseiApp);
    server.setStopAtShutdown(true);

    return server;
  }