<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.flink.api.scala

import java.io.{BufferedReader, File, FileOutputStream}

import org.apache.flink.api.java.{JarHelper, ScalaShellRemoteEnvironment}
import org.apache.flink.util.AbstractID

import scala.tools.nsc.interpreter._


class FlinkILoop(
    val host: String,
    val port: Int,
    val externalJars: Option[Array[String]],
    in0: Option[BufferedReader],
    out0: JPrintWriter)
  extends ILoopCompat(in0, out0) {

  def this(host: String,
           port: Int,
           externalJars: Option[Array[String]],
           in0: BufferedReader, 
           out: JPrintWriter){
    this(host: String, port: Int, externalJars, Some(in0), out)
  }

  def this(host: String, port: Int, externalJars: Option[Array[String]]){
    this(host: String, port: Int, externalJars, None, new JPrintWriter(Console.out, true))
  }
  
  def this(host: String, port: Int, in0: BufferedReader, out: JPrintWriter){
    this(host: String, port: Int, None, in0: BufferedReader, out: JPrintWriter)
  }

  // remote environment
  private val remoteEnv: ScalaShellRemoteEnvironment = {
    // allow creation of environments
    ScalaShellRemoteEnvironment.resetContextEnvironments()
    
    // create our environment that submits against the cluster (local or remote)
    val remoteEnv = new ScalaShellRemoteEnvironment(host, port, this)
    
    // prevent further instantiation of environments
    ScalaShellRemoteEnvironment.disableAllContextAndOtherEnvironments()
    
    remoteEnv
  }

  // local environment
  val scalaEnv: ExecutionEnvironment = {
    val scalaEnv = new ExecutionEnvironment(remoteEnv)
    scalaEnv
  }

  /**
   * creates a temporary directory to store compiled console files
   */
  private val tmpDirBase: File = {
    // get unique temporary folder:
    val abstractID: String = new AbstractID().toString
    val tmpDir: File = new File(
      System.getProperty("java.io.tmpdir"),
      "scala_shell_tmp-" + abstractID)
    if (!tmpDir.exists) {
      tmpDir.mkdir
    }
    tmpDir
  }

  // scala_shell commands
  private val tmpDirShell: File = {
    new File(tmpDirBase, "scala_shell_commands")
  }

  // scala shell jar file name
  private val tmpJarShell: File = {
    new File(tmpDirBase, "scala_shell_commands.jar")
  }

  private val packageImports = Seq[String](
    "org.apache.flink.api.scala._",
    "org.apache.flink.api.common.functions._"
  )

  override def createInterpreter(): Unit = {
    super.createInterpreter()

    addThunk {
      intp.beQuietDuring {
        // import dependencies
        intp.interpret("import " + packageImports.mkString(", "))

        // set execution environment
        intp.bind("env", this.scalaEnv)
      }
    }
  }

  /**
   * Packages the compiled classes of the current shell session into a Jar file for execution<commit_msg>[FLINK-2774] [scala shell] Extended default imports for ScalaShell<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.flink.api.scala

import java.io.{BufferedReader, File, FileOutputStream}

import org.apache.flink.api.java.{JarHelper, ScalaShellRemoteEnvironment}
import org.apache.flink.util.AbstractID

import scala.tools.nsc.interpreter._


class FlinkILoop(
    val host: String,
    val port: Int,
    val externalJars: Option[Array[String]],
    in0: Option[BufferedReader],
    out0: JPrintWriter)
  extends ILoopCompat(in0, out0) {

  def this(host: String,
           port: Int,
           externalJars: Option[Array[String]],
           in0: BufferedReader, 
           out: JPrintWriter){
    this(host: String, port: Int, externalJars, Some(in0), out)
  }

  def this(host: String, port: Int, externalJars: Option[Array[String]]){
    this(host: String, port: Int, externalJars, None, new JPrintWriter(Console.out, true))
  }
  
  def this(host: String, port: Int, in0: BufferedReader, out: JPrintWriter){
    this(host: String, port: Int, None, in0: BufferedReader, out: JPrintWriter)
  }

  // remote environment
  private val remoteEnv: ScalaShellRemoteEnvironment = {
    // allow creation of environments
    ScalaShellRemoteEnvironment.resetContextEnvironments()
    
    // create our environment that submits against the cluster (local or remote)
    val remoteEnv = new ScalaShellRemoteEnvironment(host, port, this)
    
    // prevent further instantiation of environments
    ScalaShellRemoteEnvironment.disableAllContextAndOtherEnvironments()
    
    remoteEnv
  }

  // local environment
  val scalaEnv: ExecutionEnvironment = {
    val scalaEnv = new ExecutionEnvironment(remoteEnv)
    scalaEnv
  }

  /**
   * creates a temporary directory to store compiled console files
   */
  private val tmpDirBase: File = {
    // get unique temporary folder:
    val abstractID: String = new AbstractID().toString
    val tmpDir: File = new File(
      System.getProperty("java.io.tmpdir"),
      "scala_shell_tmp-" + abstractID)
    if (!tmpDir.exists) {
      tmpDir.mkdir
    }
    tmpDir
  }

  // scala_shell commands
  private val tmpDirShell: File = {
    new File(tmpDirBase, "scala_shell_commands")
  }

  // scala shell jar file name
  private val tmpJarShell: File = {
    new File(tmpDirBase, "scala_shell_commands.jar")
  }

  private val packageImports = Seq[String](
    "org.apache.flink.core.fs._",
    "org.apache.flink.core.fs.local._",
    "org.apache.flink.api.common.io._",
    "org.apache.flink.api.common.aggregators._",
    "org.apache.flink.api.common.accumulators._",
    "org.apache.flink.api.common.distributions._",
    "org.apache.flink.api.common.operators._",
    "org.apache.flink.api.common.operators.base.JoinOperatorBase.JoinHint",
    "org.apache.flink.api.common.functions._",
    "org.apache.flink.api.java.io._",
    "org.apache.flink.api.java.aggregation._",
    "org.apache.flink.api.java.functions._",
    "org.apache.flink.api.java.operators._",
    "org.apache.flink.api.java.sampling._",
    "org.apache.flink.api.scala._",
    "org.apache.flink.api.scala.utils._"
  )

  override def createInterpreter(): Unit = {
    super.createInterpreter()

    addThunk {
      intp.beQuietDuring {
        // import dependencies
        intp.interpret("import " + packageImports.mkString(", "))

        // set execution environment
        intp.bind("env", this.scalaEnv)
      }
    }
  }

  /**
   * Packages the compiled classes of the current shell session into a Jar file for execution
{'fraction_non_alphanumeric': 0.09933525649065596, 'fraction_numerical': 0.003511852502194908, 'mean_word_length': 3.343137254901961, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4674592', 'n_tokens_mistral': 2390, 'n_tokens_neox': 2337, 'n_words': 686}