Java Code Examples for org.mortbay.jetty.Connector#setPort()

The following examples show how to use org.mortbay.jetty.Connector#setPort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpServerImpl.java    From reef with Apache License 2.0 6 votes vote down vote up
private Server tryPort(final int portNumber) throws Exception {
  Server srv = new Server();
  final Connector connector = new SocketConnector();
  connector.setHost(this.hostAddress);
  connector.setPort(portNumber);
  srv.addConnector(connector);
  try {
    srv.start();
    LOG.log(Level.INFO, "Jetty Server started with port: {0}", portNumber);
  } catch (final BindException ex) {
    srv = null;
    LOG.log(Level.FINEST, "Cannot use host: {0},port: {1}. Will try another",
        new Object[] {this.hostAddress, portNumber});
  }
  return srv;
}
 
Example 2
Source File: HttpServer2.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Open the main listener for the server
 * @throws Exception
 */
void openListeners() throws Exception {
  for (Connector listener : listeners) {
    if (listener.getLocalPort() != -1) {
      // This listener is either started externally or has been bound
      continue;
    }
    int port = listener.getPort();
    while (true) {
      // jetty has a bug where you can't reopen a listener that previously
      // failed to open w/o issuing a close first, even if the port is changed
      try {
        listener.close();
        listener.open();
        LOG.info("Jetty bound to port " + listener.getLocalPort());
        break;
      } catch (BindException ex) {
        if (port == 0 || !findPort) {
          BindException be = new BindException("Port in use: "
              + listener.getHost() + ":" + listener.getPort());
          be.initCause(ex);
          throw be;
        }
      }
      // try the next port number
      listener.setPort(++port);
      Thread.sleep(100);
    }
  }
}
 
Example 3
Source File: HttpServer2.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Open the main listener for the server
 * @throws Exception
 */
void openListeners() throws Exception {
  for (Connector listener : listeners) {
    if (listener.getLocalPort() != -1) {
      // This listener is either started externally or has been bound
      continue;
    }
    int port = listener.getPort();
    while (true) {
      // jetty has a bug where you can't reopen a listener that previously
      // failed to open w/o issuing a close first, even if the port is changed
      try {
        listener.close();
        listener.open();
        LOG.info("Jetty bound to port " + listener.getLocalPort());
        break;
      } catch (BindException ex) {
        if (port == 0 || !findPort) {
          BindException be = new BindException("Port in use: "
              + listener.getHost() + ":" + listener.getPort());
          be.initCause(ex);
          throw be;
        }
      }
      // try the next port number
      listener.setPort(++port);
      Thread.sleep(100);
    }
  }
}
 
Example 4
Source File: AbstractServerMojo.java    From opoopress with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param host localhost, 127.0.0.1 or 0.0.0.0.
 * @param port port
 * @return jetty connector
 */
protected Connector createConnector(String host, int port){
    Connector connector = new SelectChannelConnector();
    if(host != null){
        connector.setHost(host);
    }
    connector.setPort( port );
    connector.setMaxIdleTime(MAX_IDLE_TIME);
    return connector;
}
 
Example 5
Source File: RestServer.java    From hraven with Apache License 2.0 5 votes vote down vote up
@Override
protected void startUp() throws Exception {
  // setup the jetty config
  ServletHolder sh = new ServletHolder(ServletContainer.class);
  sh.setInitParameter("com.sun.jersey.config.property.packages", "com.twitter.hraven.rest");
  sh.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");

  server = new Server();

  Connector connector = new SelectChannelConnector();
  connector.setPort(this.port);
  connector.setHost(address);

  server.addConnector(connector);

  // TODO: in the future we may want to provide settings for the min and max threads
  // Jetty sets the default max thread number to 250, if we don't set it.
  //
  QueuedThreadPool threadPool = new QueuedThreadPool();
  server.setThreadPool(threadPool);

  server.setSendServerVersion(false);
  server.setSendDateHeader(false);
  server.setStopAtShutdown(true);
  // set up context
  Context context = new Context(server, "/", Context.SESSIONS);
  context.addServlet(sh, "/*");

  // start server
  server.start();
}
 
Example 6
Source File: HbaseRestLocalCluster.java    From hadoop-mini-clusters with Apache License 2.0 4 votes vote down vote up
@Override
public void start() throws Exception {
    VersionInfo.logVersion();
    Configuration conf = builder.getHbaseConfiguration();

    conf.set("hbase.rest.port", hbaseRestPort.toString());
    conf.set("hbase.rest.readonly", (hbaseRestReadOnly == null) ? "true" : hbaseRestReadOnly.toString());
    conf.set("hbase.rest.info.port", (hbaseRestInfoPort == null) ? "8085" : hbaseRestInfoPort.toString());
    String hbaseRestHost = (this.hbaseRestHost == null) ? "0.0.0.0" : this.hbaseRestHost;

    Integer hbaseRestThreadMax = (this.hbaseRestThreadMax == null) ? 100 : this.hbaseRestThreadMax;
    Integer hbaseRestThreadMin = (this.hbaseRestThreadMin == null) ? 2 : this.hbaseRestThreadMin;

    UserProvider userProvider = UserProvider.instantiate(conf);
    Pair<FilterHolder, Class<? extends ServletContainer>> pair = loginServerPrincipal(userProvider, conf);
    FilterHolder authFilter = pair.getFirst();
    Class<? extends ServletContainer> containerClass = pair.getSecond();
    RESTServlet.getInstance(conf, userProvider);

    // set up the Jersey servlet container for Jetty
    ServletHolder sh = new ServletHolder(containerClass);
    sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", ResourceConfig.class.getCanonicalName());
    sh.setInitParameter("com.sun.jersey.config.property.packages", "jetty");
    ServletHolder shPojoMap = new ServletHolder(containerClass);
    Map<String, String> shInitMap = sh.getInitParameters();
    for (Map.Entry<String, String> e : shInitMap.entrySet()) {
        shPojoMap.setInitParameter(e.getKey(), e.getValue());
    }
    shPojoMap.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");

    // set up Jetty and run the embedded server

    server = new Server();

    Connector connector = new SelectChannelConnector();
    if (conf.getBoolean(RESTServer.REST_SSL_ENABLED, false)) {
        SslSelectChannelConnector sslConnector = new SslSelectChannelConnector();
        String keystore = conf.get(RESTServer.REST_SSL_KEYSTORE_STORE);
        String password = HBaseConfiguration.getPassword(conf, RESTServer.REST_SSL_KEYSTORE_PASSWORD, null);
        String keyPassword = HBaseConfiguration.getPassword(conf, RESTServer.REST_SSL_KEYSTORE_KEYPASSWORD, password);
        sslConnector.setKeystore(keystore);
        sslConnector.setPassword(password);
        sslConnector.setKeyPassword(keyPassword);
        connector = sslConnector;
    }
    connector.setPort(hbaseRestPort);
    connector.setHost(hbaseRestHost);
    connector.setHeaderBufferSize(8192);


    server.addConnector(connector);

    QueuedThreadPool threadPool = new QueuedThreadPool(hbaseRestThreadMax);
    threadPool.setMinThreads(hbaseRestThreadMin);
    server.setThreadPool(threadPool);

    server.setSendServerVersion(false);
    server.setSendDateHeader(false);
    server.setStopAtShutdown(true);
    // set up context
    Context context = new Context(server, "/", Context.SESSIONS);
    context.addServlet(shPojoMap, "/status/cluster");
    context.addServlet(sh, "/*");
    if (authFilter != null) {
        context.addFilter(authFilter, "/*", 1);
    }

    HttpServerUtil.constrainHttpMethods(context);

    // Put up info server.
    int port = (hbaseRestInfoPort == null) ? 8085 : hbaseRestInfoPort;
    if (port >= 0) {
        conf.setLong("startcode", System.currentTimeMillis());
        String a = hbaseRestHost;
        infoServer = new InfoServer("rest", a, port, false, conf);
        infoServer.setAttribute("hbase.conf", conf);
        infoServer.start();
    }
    // start server
    server.start();
}
 
Example 7
Source File: Main.java    From stanbol-freeling with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    CommandLineParser parser = new PosixParser();
    CommandLine line = parser.parse(options, args);
    args = line.getArgs();
    if(line.hasOption('h')){
        printHelp();
        System.exit(0);
    }
    //Parse the Freeling parameter and init the Freeling instance
    String sharedFolder = System.getenv(ENV_FREELING_SHARED_FOLDER);
    sharedFolder = System.getProperty(PROPERTY_FREELING_SHARED_FOLDER, sharedFolder);
    sharedFolder = line.getOptionValue('s', sharedFolder);
    if(sharedFolder == null){
        System.err.println("The Freeling shared Folder MUST BE set! \n");
        printHelp();
        System.exit(0);
    }
    File shared = new File(sharedFolder);
    if(!shared.isDirectory()){
        System.err.println("The configured Freeling shared folder '"
            + sharedFolder + "' is not a directory!\n");
        System.exit(1);
    }
    String configFolder = FilenameUtils.concat(sharedFolder, "config");
    configFolder = System.getProperty(PROPERTY_FREELING_CONFIG_FOLDER, configFolder);
    configFolder = line.getOptionValue('c', configFolder);
    File config = new File(configFolder);
    if(!config.isDirectory()){
        System.err.println("The configured Freeling config folder '"
            + configFolder + "' is not a directory!\n");
        System.exit(1);
    }
    String nativeLib = FilenameUtils.concat(sharedFolder, Freeling.DEFAULT_FREELING_LIB_PATH);
    if(new File(Freeling.DEFAULT_FREELING_LIB_PATH).isFile()){
        nativeLib = Freeling.DEFAULT_FREELING_LIB_PATH;
    }
    nativeLib = line.getOptionValue('l', nativeLib);
    if(!new File(nativeLib).isFile()){
        System.err.println("The configured Freeling native lib '"
                + nativeLib + "' is not a file!\n");
        System.exit(1);
    }
    Freeling freeling = new Freeling(
        config.getPath(), Freeling.DEFAULT_CONFIGURATION_FILENAME_SUFFIX, 
        shared.getPath(), nativeLib, Freeling.DEFAULT_FREELING_LOCALE, 
        getInt(line, 'i', DEFAULT_INIT_THREADS), 
        getInt(line, 'm', DEFAULT_MAX_POOL_SIZE), 
        getInt(line, 'q', DEFAULT_MIN_QUEUE_SIZE));
    
    
    //init the Jetty Server
    Server server = new Server();
    Connector con = new SelectChannelConnector();
    //we need the port
    con.setPort(getInt(line,'p',DEFAULT_PORT));
    server.addConnector(con);

    //init the Servlet and the ServletContext
    Context context = new Context(server, "/", Context.SESSIONS);
    ServletHolder holder = new ServletHolder(RestServlet.class);
    holder.setInitParameter("javax.ws.rs.Application", FreelingApplication.class.getName());
    context.addServlet(holder, "/*");
    
    //now initialise the servlet context
    context.setAttribute(Constants.SERVLET_ATTRIBUTE_CONTENT_ITEM_FACTORY, 
        lookupService(ContentItemFactory.class));
    context.setAttribute(Constants.SERVLET_ATTRIBUTE_FREELING, freeling);
    context.setAttribute(Constants.SERVLET_ATTRIBUTE_MAX_RESOURCE_WAIT_TIEM, 
        getLong(line,'w',Constants.DEFAULT_RESOURCE_WAIT_TIME));
    //Freeling
    
    server.start();
    try {
        server.join();
    }catch (InterruptedException e) {
    }
    System.err.println("Shutting down Freeling");
    freeling.close();
}