<commit_before>/*
 *  ____    ____    _____    ____    ___     ____ 
 * |  _ \  |  _ \  | ____|  / ___|  / _/    / ___|        Precog (R)
 * | |_) | | |_) | |  _|   | |     | |  /| | |  _         Advanced Analytics Engine for NoSQL Data
 * |  __/  |  _ <  | |___  | |___  |/ _| | | |_| |        Copyright (C) 2010 - 2013 SlamData, Inc.
 * |_|     |_| \_\ |_____|  \____|   /__/   \____|        All Rights Reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU Affero General Public License as published by the Free Software Foundation, either version 
 * 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 
 * the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.precog.shard
package desktop

import akka.actor.ActorSystem
import akka.dispatch.{ExecutionContext, Future, Promise}

import blueeyes.bkka.{FutureMonad, Stoppable}
import blueeyes.core.data.ByteChunk
import blueeyes.core.http._
import blueeyes.util.CommandLineArguments

import com.precog.common.jobs.InMemoryJobManager
import com.precog.common.accounts.StaticAccountFinder
import com.precog.common.security.StaticAPIKeyFinder
import com.precog.shard.nihdb.NIHDBQueryExecutorComponent
import com.precog.standalone._
import com.precog.ingest.{EventServiceDeps, EventService}
import com.precog.ingest.kafka.KafkaEventStore
import com.precog.util.PrecogUnit

import java.awt._
import java.awt.event._
import javax.swing._

import java.io.IOException
import java.net.{InetAddress, Socket, URL}
import java.util.Properties

import kafka.server.{KafkaConfig, KafkaServerStartable}

import org.apache.zookeeper.server._

import org.streum.configrity.Configuration

import scala.annotation.tailrec
import scala.collection.JavaConverters._

import scalaz.{EitherT, Monad}

object DesktopIngestShardServer
    extends StandaloneShardServer
    with StandaloneIngestServer
    with NIHDBQueryExecutorComponent {
  val caveatMessage = None

  val actorSystem = ActorSystem("desktopExecutorActorSystem")
  implicit val executionContext = ExecutionContext.defaultExecutionContext(actorSystem)
  implicit val M: Monad[Future] = new FutureMonad(executionContext)

  def runGUI(config: Configuration): Option[Future[PrecogUnit]] = {
    val guiNotifier = if (config[Boolean]("appwindow.enabled", false)) {
      logger.info("Starting gui window")
      // Add a window with a menu for shutdown
      val notifyArea = new JTextArea(25, 80)

      EventQueue.invokeLater(new Runnable {
        def run() {
          val precogMenu = new JMenu("Precog for Desktop")
          val quitItem = new JMenuItem("Quit", KeyEvent.VK_Q)
          quitItem.addActionListener(new ActionListener {
            def actionPerformed(ev: ActionEvent) = {
              System.exit(0)
            }
          })

          precogMenu.add(quitItem)

          val labcoatMenu = new JMenu("Labcoat")
          val launchItem = new JMenuItem("Launch", KeyEvent.VK_L)
          launchItem.addActionListener(new ActionListener {
            def actionPerformed(ev: ActionEvent) = {
              LaunchLabcoat.launchBrowser(config)
            }
          })

          labcoatMenu.add(launchItem)

          val menubar = new JMenuBar
          menubar.add(precogMenu)
          menubar.add(labcoatMenu)

          val appFrame = new JFrame("Precog for Desktop")
          appFrame.setJMenuBar(menubar)

          appFrame.addWindowListener(new WindowAdapter {
            override def windowClosed(ev: WindowEvent) = {
              System.exit(0)
            }
          })

          notifyArea.setEditable(false)
          appFrame.add(notifyArea)

          appFrame.pack()

          val iconUrl = ClassLoader.getSystemClassLoader.getResource("LargeIcon.png")
          logger.debug("Loaded icon: " + iconUrl)
          appFrame.setIconImage(Toolkit.getDefaultToolkit.getImage(iconUrl))

          appFrame.setVisible(true)
        }
      })

      Some({ (msg: String) =>
        EventQueue.invokeLater(new Runnable {
          def run() {
            notifyArea.append(msg + "\n")
          }
        })
      })
    } else {
      logger.info("Skipping gui window")
      None
    }

    guiNotifier.foreach(_("Starting internal services"))
    val zookeeper = startZookeeperStandalone(config.detach("zookeeper"))

    logger.debug("Waiting for ZK startup")

    if (!waitForPortOpen(config[Int]("zookeeper.port"), 60)) {
      throw new Exception("Timeout waiting for ZK port to open")
    }

    val kafka = startKafkaStandalone(config)

    logger.debug("Waiting for Kafka startup")

    if (!waitForPortOpen(config[Int]("kafka.port"), 60)) {
      throw new Exception("Time out waiting on kafka port to open")<commit_msg>Set Desktop app frame to close on exit<commit_after>/*
 *  ____    ____    _____    ____    ___     ____ 
 * |  _ \  |  _ \  | ____|  / ___|  / _/    / ___|        Precog (R)
 * | |_) | | |_) | |  _|   | |     | |  /| | |  _         Advanced Analytics Engine for NoSQL Data
 * |  __/  |  _ <  | |___  | |___  |/ _| | | |_| |        Copyright (C) 2010 - 2013 SlamData, Inc.
 * |_|     |_| \_\ |_____|  \____|   /__/   \____|        All Rights Reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU Affero General Public License as published by the Free Software Foundation, either version 
 * 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 
 * the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.precog.shard
package desktop

import akka.actor.ActorSystem
import akka.dispatch.{ExecutionContext, Future, Promise}

import blueeyes.bkka.{FutureMonad, Stoppable}
import blueeyes.core.data.ByteChunk
import blueeyes.core.http._
import blueeyes.util.CommandLineArguments

import com.precog.common.jobs.InMemoryJobManager
import com.precog.common.accounts.StaticAccountFinder
import com.precog.common.security.StaticAPIKeyFinder
import com.precog.shard.nihdb.NIHDBQueryExecutorComponent
import com.precog.standalone._
import com.precog.ingest.{EventServiceDeps, EventService}
import com.precog.ingest.kafka.KafkaEventStore
import com.precog.util.PrecogUnit

import java.awt._
import java.awt.event._
import javax.swing._

import java.io.IOException
import java.net.{InetAddress, Socket, URL}
import java.util.Properties

import kafka.server.{KafkaConfig, KafkaServerStartable}

import org.apache.zookeeper.server._

import org.streum.configrity.Configuration

import scala.annotation.tailrec
import scala.collection.JavaConverters._

import scalaz.{EitherT, Monad}

object DesktopIngestShardServer
    extends StandaloneShardServer
    with StandaloneIngestServer
    with NIHDBQueryExecutorComponent {
  val caveatMessage = None

  val actorSystem = ActorSystem("desktopExecutorActorSystem")
  implicit val executionContext = ExecutionContext.defaultExecutionContext(actorSystem)
  implicit val M: Monad[Future] = new FutureMonad(executionContext)

  def runGUI(config: Configuration): Option[Future[PrecogUnit]] = {
    val guiNotifier = if (config[Boolean]("appwindow.enabled", false)) {
      logger.info("Starting gui window")
      // Add a window with a menu for shutdown
      val notifyArea = new JTextArea(25, 80)

      EventQueue.invokeLater(new Runnable {
        def run() {
          val precogMenu = new JMenu("Precog for Desktop")
          val quitItem = new JMenuItem("Quit", KeyEvent.VK_Q)
          quitItem.addActionListener(new ActionListener {
            def actionPerformed(ev: ActionEvent) = {
              System.exit(0)
            }
          })

          precogMenu.add(quitItem)

          val labcoatMenu = new JMenu("Labcoat")
          val launchItem = new JMenuItem("Launch", KeyEvent.VK_L)
          launchItem.addActionListener(new ActionListener {
            def actionPerformed(ev: ActionEvent) = {
              LaunchLabcoat.launchBrowser(config)
            }
          })

          labcoatMenu.add(launchItem)

          val menubar = new JMenuBar
          menubar.add(precogMenu)
          menubar.add(labcoatMenu)

          val appFrame = new JFrame("Precog for Desktop")
          appFrame.setJMenuBar(menubar)

          appFrame.addWindowListener(new WindowAdapter {
            override def windowClosed(ev: WindowEvent) = {
              System.exit(0)
            }
          })

          notifyArea.setEditable(false)
          appFrame.add(notifyArea)

          appFrame.pack()

          val iconUrl = ClassLoader.getSystemClassLoader.getResource("LargeIcon.png")
          logger.debug("Loaded icon: " + iconUrl)
          appFrame.setIconImage(Toolkit.getDefaultToolkit.getImage(iconUrl))

          appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

          appFrame.setVisible(true)
        }
      })

      Some({ (msg: String) =>
        EventQueue.invokeLater(new Runnable {
          def run() {
            notifyArea.append(msg + "\n")
          }
        })
      })
    } else {
      logger.info("Skipping gui window")
      None
    }

    guiNotifier.foreach(_("Starting internal services"))
    val zookeeper = startZookeeperStandalone(config.detach("zookeeper"))

    logger.debug("Waiting for ZK startup")

    if (!waitForPortOpen(config[Int]("zookeeper.port"), 60)) {
      throw new Exception("Timeout waiting for ZK port to open")
    }

    val kafka = startKafkaStandalone(config)

    logger.debug("Waiting for Kafka startup")

    if (!waitForPortOpen(config[Int]("kafka.port"), 60)) {
      throw new Exception("Time out waiting on kafka port to open")
{'fraction_non_alphanumeric': 0.09492316669908578, 'fraction_numerical': 0.0036957790313168646, 'mean_word_length': 2.8716114457831323, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '534871', 'n_tokens_mistral': 3138, 'n_tokens_neox': 2990, 'n_words': 723}