<commit_before>/*
 * gretty
 *
 * Copyright 2013  <NAME>.
 *
 * See the file "license.txt" for copying and usage permission.
 */
package org.akhikhl.gretty

import org.gradle.api.*
import org.gradle.api.plugins.*
import org.gradle.api.tasks.*
import org.gradle.api.tasks.bundling.*

final class GrettyRunner {

  public static void run(Map params = [:]) {

    Project project = params.project

    def urls = []

    urls.addAll project.configurations.grettyConfig.collect { it.toURI().toURL() }
    if(params.inplace) {
      urls.addAll project.sourceSets.main.runtimeClasspath.files.collect { it.toURI().toURL() }
      // ATTENTION: order of overlays is important!
      for(String overlay in project.gretty.overlays.reverse())
        urls.addAll project.project(overlay).sourceSets.main.runtimeClasspath.files.collect { it.toURI().toURL() }
    }
    ClassLoader classLoader = new URLClassLoader(urls as URL[])

    def helper = classLoader.findClass('org.akhikhl.gretty.GrettyHelper')

    def server = helper.createServer()
    server.setConnectors helper.createConnectors(project.gretty.port)

    def context = helper.createWebAppContext()
    helper.setClassLoader context, classLoader

    String realm = project.gretty.realm
    String realmConfigFile = project.gretty.realmConfigFile
    if(realmConfigFile && !new File(realmConfigFile).isAbsolute())
      realmConfigFile = "${project.webAppDir.absolutePath}/${realmConfigFile}"
    if(!realm || !realmConfigFile)
      for(def overlay in project.gretty.overlays.reverse()) {
        overlay = project.project(overlay)
        if(overlay.gretty.realm && overlay.gretty.realmConfigFile) {
          realm = overlay.gretty.realm
          realmConfigFile = overlay.gretty.realmConfigFile
          if(realmConfigFile && !new File(realmConfigFile).isAbsolute())
            realmConfigFile = "${overlay.webAppDir.absolutePath}/${realmConfigFile}"
          break
        }
      }
    if(realm && realmConfigFile)
      context.getSecurityHandler().setLoginService(helper.createLoginService(realm, realmConfigFile))

    String contextPath = project.gretty.contextPath
    if(!contextPath)
      for(def overlay in project.gretty.overlays.reverse()) {
        overlay = project.project(overlay)
        if(overlay.gretty.contextPath) {
          contextPath = overlay.gretty.contextPath
          break
        }
      }
    contextPath = contextPath ?: "/${project.name}"
    context.setContextPath contextPath

    for(def overlay in project.gretty.overlays) {
      overlay = project.project(overlay)
      for(def e in overlay.gretty.initParameters) {
        def paramValue = e.value
        if(paramValue instanceof Closure)
          paramValue = paramValue()
        context.setInitParameter e.key, paramValue
      }
    }
    for(def e in project.gretty.initParameters) {
      def paramValue = e.value
      if(paramValue instanceof Closure)
        paramValue = paramValue()
      context.setInitParameter e.key, paramValue
    }

    if(params.inplace)
      context.setResourceBase "${project.buildDir}/webapp"
    else
      context.setWar project.tasks.war.archivePath.toString()

    context.setServer server
    server.setHandler context

    server.start()

    boolean interactive = params.interactive

    System.out.println 'Jetty server started.'
    System.out.println 'You can see web-application in browser under the address:'
    System.out.println "http://localhost:${project.gretty.port}${contextPath}"
    for(def overlay in project.gretty.overlays) {
      overlay = project.project(overlay)
      overlay.gretty.onStart.each { onStart ->
        if(onStart instanceof Closure)
          onStart()
      }
    }
    project.gretty.onStart.each { onStart ->
      if(onStart instanceof Closure)
        onStart()
    }
    if(interactive)
      System.out.println 'Press any key to stop the jetty server.'
    else
      System.out.println 'Enter \'gradle jettyStop\' to stop the jetty server.'
    System.out.println()

    Thread monitor = new JettyMonitorThread(project.gretty.servicePort, server)
    monitor.start()

    if(interactive) {
      System.in.read()
      if(monitor.running)
        sendServiceCommand project.gretty.servicePort, 'stop'
    }

    server.join()

    System.out.println 'Jetty server stopped.'
    project.gretty.onStop.each { onStop ->
      if(onStop instanceof Closure)
        onStop()
    }
    for(def overlay in project.gretty.overlays.reverse()) {
      overlay = project.project(overlay)
      overlay.gretty.onStop.each { onStop ->
        if(onStop instanceof Closure)
          onStop()
      }
    }
  }

  public static void sendServiceCommand(int servicePort, String command) {
    Socket s = new Socket(InetAddress.getByName('127.0.0.1'), servicePort)
    try {
      OutputStream out = s.getOutputStream()
      System.out.println "Sending command: ${command}"
      out.write(("${command}\n").getBytes())
      out.flush()
    } finally {
      s.close()
    }
  }
}
<commit_msg>gretty-plugin: removed redundant local variable 'interactive'<commit_after>/*
 * gretty
 *
 * Copyright 2013  <NAME>.
 *
 * See the file "license.txt" for copying and usage permission.
 */
package org.akhikhl.gretty

import org.gradle.api.*
import org.gradle.api.plugins.*
import org.gradle.api.tasks.*
import org.gradle.api.tasks.bundling.*

final class GrettyRunner {

  public static void run(Map params = [:]) {

    Project project = params.project

    def urls = []

    urls.addAll project.configurations.grettyConfig.collect { it.toURI().toURL() }
    if(params.inplace) {
      urls.addAll project.sourceSets.main.runtimeClasspath.files.collect { it.toURI().toURL() }
      // ATTENTION: order of overlays is important!
      for(String overlay in project.gretty.overlays.reverse())
        urls.addAll project.project(overlay).sourceSets.main.runtimeClasspath.files.collect { it.toURI().toURL() }
    }
    ClassLoader classLoader = new URLClassLoader(urls as URL[])

    def helper = classLoader.findClass('org.akhikhl.gretty.GrettyHelper')

    def server = helper.createServer()
    server.setConnectors helper.createConnectors(project.gretty.port)

    def context = helper.createWebAppContext()
    helper.setClassLoader context, classLoader

    String realm = project.gretty.realm
    String realmConfigFile = project.gretty.realmConfigFile
    if(realmConfigFile && !new File(realmConfigFile).isAbsolute())
      realmConfigFile = "${project.webAppDir.absolutePath}/${realmConfigFile}"
    if(!realm || !realmConfigFile)
      for(def overlay in project.gretty.overlays.reverse()) {
        overlay = project.project(overlay)
        if(overlay.gretty.realm && overlay.gretty.realmConfigFile) {
          realm = overlay.gretty.realm
          realmConfigFile = overlay.gretty.realmConfigFile
          if(realmConfigFile && !new File(realmConfigFile).isAbsolute())
            realmConfigFile = "${overlay.webAppDir.absolutePath}/${realmConfigFile}"
          break
        }
      }
    if(realm && realmConfigFile)
      context.getSecurityHandler().setLoginService(helper.createLoginService(realm, realmConfigFile))

    String contextPath = project.gretty.contextPath
    if(!contextPath)
      for(def overlay in project.gretty.overlays.reverse()) {
        overlay = project.project(overlay)
        if(overlay.gretty.contextPath) {
          contextPath = overlay.gretty.contextPath
          break
        }
      }
    contextPath = contextPath ?: "/${project.name}"
    context.setContextPath contextPath

    for(def overlay in project.gretty.overlays) {
      overlay = project.project(overlay)
      for(def e in overlay.gretty.initParameters) {
        def paramValue = e.value
        if(paramValue instanceof Closure)
          paramValue = paramValue()
        context.setInitParameter e.key, paramValue
      }
    }
    for(def e in project.gretty.initParameters) {
      def paramValue = e.value
      if(paramValue instanceof Closure)
        paramValue = paramValue()
      context.setInitParameter e.key, paramValue
    }

    if(params.inplace)
      context.setResourceBase "${project.buildDir}/webapp"
    else
      context.setWar project.tasks.war.archivePath.toString()

    context.setServer server
    server.setHandler context

    server.start()

    System.out.println 'Jetty server started.'
    System.out.println 'You can see web-application in browser under the address:'
    System.out.println "http://localhost:${project.gretty.port}${contextPath}"
    for(def overlay in project.gretty.overlays) {
      overlay = project.project(overlay)
      overlay.gretty.onStart.each { onStart ->
        if(onStart instanceof Closure)
          onStart()
      }
    }
    project.gretty.onStart.each { onStart ->
      if(onStart instanceof Closure)
        onStart()
    }
    if(params.interactive)
      System.out.println 'Press any key to stop the jetty server.'
    else
      System.out.println 'Enter \'gradle jettyStop\' to stop the jetty server.'
    System.out.println()

    Thread monitor = new JettyMonitorThread(project.gretty.servicePort, server)
    monitor.start()

    if(params.interactive) {
      System.in.read()
      if(monitor.running)
        sendServiceCommand project.gretty.servicePort, 'stop'
    }

    server.join()

    System.out.println 'Jetty server stopped.'
    project.gretty.onStop.each { onStop ->
      if(onStop instanceof Closure)
        onStop()
    }
    for(def overlay in project.gretty.overlays.reverse()) {
      overlay = project.project(overlay)
      overlay.gretty.onStop.each { onStop ->
        if(onStop instanceof Closure)
          onStop()
      }
    }
  }

  public static void sendServiceCommand(int servicePort, String command) {
    Socket s = new Socket(InetAddress.getByName('127.0.0.1'), servicePort)
    try {
      OutputStream out = s.getOutputStream()
      System.out.println "Sending command: ${command}"
      out.write(("${command}\n").getBytes())
      out.flush()
    } finally {
      s.close()
    }
  }
}

{'fraction_non_alphanumeric': 0.10969793322734499, 'fraction_numerical': 0.001987281399046105, 'mean_word_length': 2.9486072969792074, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '681002', 'n_tokens_mistral': 3047, 'n_tokens_neox': 2996, 'n_words': 592}