<commit_before>require 'pathname'
require 'net/http'

module TorqueSpec
  def deploy(*paths)
    metaclass = class << self; self; end
    metaclass.send(:define_method, :deploy_paths) do
      paths.map {|p| Pathname.new(p).absolute? ? p : File.join(TorqueSpec.knob_root, p) }
    end
  end

  class << self
    attr_accessor :host, :port, :knob_root, :jboss_home, :jboss_conf, :jvm_args, :max_heap, :lazy
    def configure
      yield self
    end
    def jvm_args
      max_heap ? @jvm_args.sub(/-Xmx\w+/, "-Xmx#{max_heap}") : @jvm_args
    end
  end
end

# Default configuration options
TorqueSpec.configure do |config|
  config.lazy = true
  config.host = 'localhost'
  config.port = 8080
  config.jboss_home = ENV['JBOSS_HOME']
  config.jboss_conf = 'default'
  config.jvm_args = "-Xmx1024m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSClassUnloadingEnabled -Djruby_home.env.ignore=true -Dgem.path=default"
end


module TorqueSpec
  class Server
    
    def start(opts={})
      return if TorqueSpec.lazy and ready?
      wait = opts[:wait].to_i
      raise "JBoss is already running" if ready?
      cmd = command
      @process = IO.popen( cmd )
      Thread.new(@process) { |console| while(console.gets); end }
      %w{ INT TERM KILL }.each { |signal| trap(signal) { stop } }
      puts "#{cmd}\npid=#{@process.pid}"
      wait > 0 ? wait_for_ready(wait) : @process.pid
    end

    def deploy(url)
      t0 = Time.now
      puts "#{url}"
      success?( deployer( 'redeploy', url ) )
      puts "  deployed in #{(Time.now - t0).to_i}s"
    end

    def undeploy(url)
      success?( deployer( 'undeploy', url ) )
      puts "  undeployed #{url.split('/')[-1]}"
    end

    def stop
      return if TorqueSpec.lazy
      if @process
        Process.kill("INT", @process.pid) 
        @process = nil
        puts "JBoss stopped"
      end
    end

    def ready?
      response = jmx_console( :action => 'inspectMBean', :name => 'jboss.system:type=Server' )
      "True" == response.match(/>Started<.*?<pre>\s+^(\w+)/m)[1]
    rescue
      false
    end

    def wait_for_ready(timeout)<commit_msg>Shutdown cleanly -- hopefully -- on windows.<commit_after>require 'pathname'
require 'net/http'

module TorqueSpec
  def deploy(*paths)
    metaclass = class << self; self; end
    metaclass.send(:define_method, :deploy_paths) do
      paths.map {|p| Pathname.new(p).absolute? ? p : File.join(TorqueSpec.knob_root, p) }
    end
  end

  class << self
    attr_accessor :host, :port, :knob_root, :jboss_home, :jboss_conf, :jvm_args, :max_heap, :lazy
    def configure
      yield self
    end
    def jvm_args
      max_heap ? @jvm_args.sub(/-Xmx\w+/, "-Xmx#{max_heap}") : @jvm_args
    end
  end
end

# Default configuration options
TorqueSpec.configure do |config|
  config.lazy = true
  config.host = 'localhost'
  config.port = 8080
  config.jboss_home = ENV['JBOSS_HOME']
  config.jboss_conf = 'default'
  config.jvm_args = "-Xmx1024m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSClassUnloadingEnabled -Djruby_home.env.ignore=true -Dgem.path=default"
end


module TorqueSpec
  class Server
    
    def start(opts={})
      return if TorqueSpec.lazy and ready?
      wait = opts[:wait].to_i
      raise "JBoss is already running" if ready?
      cmd = command
      @process = IO.popen( cmd )
      Thread.new(@process) { |console| while(console.gets); end }
      %w{ INT TERM KILL }.each { |signal| trap(signal) { stop } }
      puts "#{cmd}\npid=#{@process.pid}"
      wait > 0 ? wait_for_ready(wait) : @process.pid
    end

    def deploy(url)
      t0 = Time.now
      puts "#{url}"
      success?( deployer( 'redeploy', url ) )
      puts "  deployed in #{(Time.now - t0).to_i}s"
    end

    def undeploy(url)
      success?( deployer( 'undeploy', url ) )
      puts "  undeployed #{url.split('/')[-1]}"
    end

    def stop
      return if TorqueSpec.lazy
      if @process
        unless clean_stop
          puts "Unable to shutdown JBoss cleanly, interrupting process"
          Process.kill("INT", @process.pid) 
        end
        @process = nil
        puts "JBoss stopped"
      end
    end

    def clean_stop
      success?( jmx_console( :action     => 'invokeOpByName', 
                             :name       => 'jboss.system:type=Server', 
                             :methodName => 'shutdown' ) )
    end

    def ready?
      response = jmx_console( :action => 'inspectMBean', :name => 'jboss.system:type=Server' )
      "True" == response.match(/>Started<.*?<pre>\s+^(\w+)/m)[1]
    rescue
      false
    end

    def wait_for_ready(timeout)
{'fraction_non_alphanumeric': 0.13449913644214162, 'fraction_numerical': 0.0069084628670120895, 'mean_word_length': 2.6394344069128044, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '3206599', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1635, 'n_words': 375}