<commit_before>/*
 * Copyright 2012 ios-driver committers.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package org.uiautomation.ios.server;

import java.net.InetSocketAddress;

import javax.servlet.Servlet;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.uiautomation.ios.exceptions.IOSAutomationSetupException;
import org.uiautomation.ios.server.instruments.SessionsManager;
import org.uiautomation.ios.server.servlet.IOSServlet;
import org.uiautomation.ios.server.servlet.UIAScriptProxyRegister;
import org.uiautomation.ios.server.servlet.UIAScriptServlet;

public class IOSServer {

  private final String IDE_CLASS = "org.uiautomation.ios.ide.IDEServlet";

  public static final String SESSIONS_MGR = SessionsManager.class.getName();
  public static final String SERVER_CONFIG = IOSServerConfiguration.class.getName();
  private final Server server;
  public static int port;
  public static final boolean debugMode = true;
  private IOSServerConfiguration config;


  public static void main(String[] args) throws Exception {
    IOSServerConfiguration command_line_configuration = IOSServerConfiguration.create(args);
    IOSServer server = new IOSServer(command_line_configuration);
    server.start();
  }

  public void init(IOSServerConfiguration config) throws IOSAutomationSetupException {
    ServletContextHandler servletContextHandler =
        new ServletContextHandler(server, "/wd/hub", true, false);
    servletContextHandler.addServlet(UIAScriptProxyRegister.class, "/uiascriptproxy/register/*");
    servletContextHandler.addServlet(UIAScriptServlet.class, "/uiascriptproxy/*");
    servletContextHandler.addServlet(IOSServlet.class, "/*");

    Class<Servlet> ide = getIDEServlet();
    if (ide != null) {
      servletContextHandler.addServlet(ide, "/ide/*");
    }

    servletContextHandler.setAttribute(SESSIONS_MGR, new SessionsManager());
    servletContextHandler.setAttribute(SERVER_CONFIG, this.config);
    Runtime.getRuntime().addShutdownHook(new Thread() {
      public void run() {
        try {
          server.stop();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });

  }

  public void start() throws Exception {
    init(config);
    if (!server.isRunning()) {
      server.start();
    }
  }


  public void stop() throws Exception {
    server.stop();
  }

  public IOSServer(IOSServerConfiguration config) throws IOSAutomationSetupException {
    this.config = config;
    this.port = config.getPort();
    server = new Server(new InetSocketAddress("0.0.0.0", config.getPort()));
  }

  private Class<Servlet> getIDEServlet() {
    try {
      Class<?> ide = Class.forName(IDE_CLASS);
      Class<Servlet> c = (Class<Servlet>) ide;
      return c;<commit_msg>goinf back to server config<commit_after>/*
 * Copyright 2012 ios-driver committers.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package org.uiautomation.ios.server;

import java.net.InetSocketAddress;

import javax.servlet.Servlet;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.uiautomation.ios.exceptions.IOSAutomationSetupException;
import org.uiautomation.ios.server.grid.RegistrationRequest;
import org.uiautomation.ios.server.instruments.SessionsManager;
import org.uiautomation.ios.server.servlet.IOSServlet;
import org.uiautomation.ios.server.servlet.UIAScriptProxyRegister;
import org.uiautomation.ios.server.servlet.UIAScriptServlet;

import com.beust.jcommander.JCommander;

public class IOSServer {

  private final String IDE_CLASS = "org.uiautomation.ios.ide.IDEServlet";

  public static final String SESSIONS_MGR = SessionsManager.class.getName();
  public static final String SERVER_CONFIG = IOSServerConfiguration.class.getName();
  private final Server server;
  public static int port;
  public static final boolean debugMode = true;
  private IOSServerConfiguration options;


  public static void main(String[] args) throws Exception {
    IOSServerConfiguration options = new IOSServerConfiguration();
    new JCommander(options, args);

    IOSServer server = new IOSServer(options);
    server.start();
    if (options.getRegistrationURL() != null) {
      RegistrationRequest request =
          new RegistrationRequest(options.getRegistrationURL(), options.getHost(),
              options.getPort(), options.getAbsoluteAppPath());
      request.registerToHub();
    }



  }

  public void init() throws IOSAutomationSetupException {
    ServletContextHandler servletContextHandler =
        new ServletContextHandler(server, "/wd/hub", true, false);
    servletContextHandler.addServlet(UIAScriptProxyRegister.class, "/uiascriptproxy/register/*");
    servletContextHandler.addServlet(UIAScriptServlet.class, "/uiascriptproxy/*");
    servletContextHandler.addServlet(IOSServlet.class, "/*");

    Class<Servlet> ide = getIDEServlet();
    if (ide != null) {
      servletContextHandler.addServlet(ide, "/ide/*");
    }

    servletContextHandler.setAttribute(SESSIONS_MGR, new SessionsManager());
    servletContextHandler.setAttribute(SERVER_CONFIG, this.options);
    Runtime.getRuntime().addShutdownHook(new Thread() {
      public void run() {
        try {
          server.stop();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });

  }

  public void start() throws Exception {
    init();
    if (!server.isRunning()) {
      server.start();
    }
  }


  public void stop() throws Exception {
    server.stop();
  }

  public IOSServer(IOSServerConfiguration options) throws IOSAutomationSetupException {
    this.options = options;
    this.port = options.getPort();
    server = new Server(new InetSocketAddress("0.0.0.0", options.getPort()));
  }

  private Class<Servlet> getIDEServlet() {
    try {
      Class<?> ide = Class.forName(IDE_CLASS);
      Class<Servlet> c = (Class<Servlet>) ide;
      return c;
{'fraction_non_alphanumeric': 0.1004585841215248, 'fraction_numerical': 0.0034393809114359416, 'mean_word_length': 4.075636363636364, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3108455', 'n_tokens_mistral': 2018, 'n_tokens_neox': 1937, 'n_words': 499}