<commit_before>require 'torquebox/option_utils'

module TorqueBox
  module Web
    class Server
      include TorqueBox::OptionUtils

      WB = org.projectodd.wunderboss.WunderBoss
      WBWeb = org.projectodd.wunderboss.web.Web
      java_import org.projectodd.wunderboss.rack.RackHandler

      attr_accessor :web_component

      DEFAULT_CREATE_OPTIONS = {
        :host => 'localhost',
        :port => 8080,
        :auto_start => true
      }
      def self.find_or_create(name, options={})
        new(name, options)
      end

      def self.run(name, options={})
        create_options = options.reject { |k, v| DEFAULT_MOUNT_OPTIONS.has_key?(k) }
        server = new(name, create_options)
        mount_options = options.reject { |k, v| DEFAULT_CREATE_OPTIONS.has_key?(k) }
        server.mount(mount_options)
        server
      end

      DEFAULT_MOUNT_OPTIONS = {
        :root => '.',
        :context_path => '/',
        :static_dir => 'public',
        :init => nil,
        :destroy => nil,
        :rackup => 'config.ru',
        :rack_app => nil
      }

      def mount(options={})
        options = DEFAULT_MOUNT_OPTIONS.merge(options)
        validate_options(options, enum_to_set(WBWeb::RegisterOption) + DEFAULT_MOUNT_OPTIONS.keys)
        @logger.debugf("Mounting context path %s with options %s on TorqueBox::Web::Server '%s'",
                       options[:context_path], options.inspect, @web_component.name)
        if options[:rack_app].nil?
          require 'rack'
          rackup = File.join(options[:root], options[:rackup])
          options[:rack_app] = Rack::Builder.parse_file(rackup).first
        end
        if options[:init]
          options[:init] = options[:init].to_java(java.lang.Runnable)
        end
        handler = RackHandler.new(options[:rack_app], options[:context_path])
        register_options = extract_options(options, WBWeb::RegisterOption)
        @logger.tracef("Registering handler at context path %s", options[:context_path])
        @web_component.register_handler(handler, register_options)
        handler
      end

      def unmount(context_path)
        @web_component.unregister(context_path)
      end

      DEFAULT_MOUNT_SERVLET_OPTIONS = {
        :context_path => '/'
      }

      def mount_servlet(servlet, options={})
        options = DEFAULT_MOUNT_SERVLET_OPTIONS.merge(options);
        validate_options(options, enum_to_set(WBWeb::RegisterOption) + DEFAULT_MOUNT_SERVLET_OPTIONS.keys)
        @logger.debugf("Mounting servlet %s with options %s on TorqueBox::Web::Server '%s'",
                       servlet, options.inspect, @web_component.name)
        register_options = extract_options(options, WBWeb::RegisterOption)
        @web_component.register_servlet(servlet, register_options)
      end

      def start
        @logger.infof("Starting TorqueBox::Web::Server '%s'",
                      @web_component.name)
        @web_component.start
      end

      def stop
        @logger.infof("Stopping TorqueBox::Web::Server '%s'",
                      @web_component.name)
        @web_component.stop
      end


      protected

      def initialize(name, options={})
        @logger = WB.logger('TorqueBox::Web::Server')
        options = DEFAULT_CREATE_OPTIONS.merge(options)
        validate_options(options, enum_to_set(WBWeb::CreateOption) + DEFAULT_CREATE_OPTIONS.keys)
        create_options = extract_options(options, WBWeb::CreateOption)
        web = WB.find_or_create_component(WBWeb.java_class, name,
                                          create_options)
        @logger.debugf("TorqueBox::Web::Server '%s' has component %s",
                       name, web)
        @web_component = web
      end

    end
  end
end
<commit_msg>We don't need to include the CreateOptions since the opts are all listed in the defaults.<commit_after>require 'torquebox/option_utils'

module TorqueBox
  module Web
    class Server
      include TorqueBox::OptionUtils

      WB = org.projectodd.wunderboss.WunderBoss
      WBWeb = org.projectodd.wunderboss.web.Web
      java_import org.projectodd.wunderboss.rack.RackHandler

      attr_accessor :web_component

      DEFAULT_CREATE_OPTIONS = {
        :host => 'localhost',
        :port => 8080,
        :auto_start => true
      }
      def self.find_or_create(name, options={})
        new(name, options)
      end

      def self.run(name, options={})
        create_options = options.reject { |k, v| DEFAULT_MOUNT_OPTIONS.has_key?(k) }
        server = new(name, create_options)
        mount_options = options.reject { |k, v| DEFAULT_CREATE_OPTIONS.has_key?(k) }
        server.mount(mount_options)
        server
      end

      DEFAULT_MOUNT_OPTIONS = {
        :root => '.',
        :context_path => '/',
        :static_dir => 'public',
        :init => nil,
        :destroy => nil,
        :rackup => 'config.ru',
        :rack_app => nil
      }

      def mount(options={})
        options = DEFAULT_MOUNT_OPTIONS.merge(options)
        validate_options(options, enum_to_set(WBWeb::RegisterOption) + DEFAULT_MOUNT_OPTIONS.keys)
        @logger.debugf("Mounting context path %s with options %s on TorqueBox::Web::Server '%s'",
                       options[:context_path], options.inspect, @web_component.name)
        if options[:rack_app].nil?
          require 'rack'
          rackup = File.join(options[:root], options[:rackup])
          options[:rack_app] = Rack::Builder.parse_file(rackup).first
        end
        if options[:init]
          options[:init] = options[:init].to_java(java.lang.Runnable)
        end
        handler = RackHandler.new(options[:rack_app], options[:context_path])
        register_options = extract_options(options, WBWeb::RegisterOption)
        @logger.tracef("Registering handler at context path %s", options[:context_path])
        @web_component.register_handler(handler, register_options)
        handler
      end

      def unmount(context_path)
        @web_component.unregister(context_path)
      end

      DEFAULT_MOUNT_SERVLET_OPTIONS = {
        :context_path => '/'
      }

      def mount_servlet(servlet, options={})
        options = DEFAULT_MOUNT_SERVLET_OPTIONS.merge(options);
        validate_options(options, enum_to_set(WBWeb::RegisterOption) + DEFAULT_MOUNT_SERVLET_OPTIONS.keys)
        @logger.debugf("Mounting servlet %s with options %s on TorqueBox::Web::Server '%s'",
                       servlet, options.inspect, @web_component.name)
        register_options = extract_options(options, WBWeb::RegisterOption)
        @web_component.register_servlet(servlet, register_options)
      end

      def start
        @logger.infof("Starting TorqueBox::Web::Server '%s'",
                      @web_component.name)
        @web_component.start
      end

      def stop
        @logger.infof("Stopping TorqueBox::Web::Server '%s'",
                      @web_component.name)
        @web_component.stop
      end


      protected

      def initialize(name, options={})
        @logger = WB.logger('TorqueBox::Web::Server')
        options = DEFAULT_CREATE_OPTIONS.merge(options)
        validate_options(options, DEFAULT_CREATE_OPTIONS.keys)
        create_options = extract_options(options, WBWeb::CreateOption)
        web = WB.find_or_create_component(WBWeb.java_class, name,
                                          create_options)
        @logger.debugf("TorqueBox::Web::Server '%s' has component %s",
                       name, web)
        @web_component = web
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.10760249436115166, 'fraction_numerical': 0.0010614302772986599, 'mean_word_length': 2.5373064289066165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '796065', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2182, 'n_words': 402}