java.io.StringWriter Scala Examples
The following examples show how to use java.io.StringWriter.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: DotRenderer.scala From reftree with GNU General Public License v3.0 | 5 votes |
package reftree.render import reftree.dot.Graph import java.io.StringWriter import java.nio.charset.StandardCharsets import java.nio.file.Path import scala.sys.process.{Process, BasicIO} object DotRenderer { case class RenderingException(message: String) extends Exception(message) def render( graph: Graph, output: Path, options: RenderingOptions, format: String ): Unit = { val args = Seq( "-K", "dot", "-T", format, s"-Gdpi=${options.density}", "-o", output.toString ) val process = Process("dot", args) val error = new StringWriter val io = BasicIO.standard { stream ⇒ stream.write(graph.encode.getBytes(StandardCharsets.UTF_8)) stream.close() }.withError(BasicIO.processFully(error)) (process run io).exitValue() if (error.toString.nonEmpty) throw RenderingException(error.toString) () } }
Example 2
Source File: SchemaSpec.scala From comet-data-pipeline with Apache License 2.0 | 5 votes |
package com.ebiznext.comet.schema.model import java.io.{InputStream, StringWriter} import com.ebiznext.comet.TestHelper import com.ebiznext.comet.schema.handlers.SchemaHandler class SchemaSpec extends TestHelper { new WithSettings() { val schemaHandler = new SchemaHandler(storageHandler) "Attribute type" should "be valid" in { val stream: InputStream = getClass.getResourceAsStream("/sample/default.yml") val lines = scala.io.Source.fromInputStream(stream).getLines().mkString("\n") val types = mapper.readValue(lines, classOf[Types]) val attr = Attribute( "attr", "invalid-type", // should raise error non existent type Some(true), true, Some( PrivacyLevel("MD5") ) // Should raise an error. Privacy cannot be applied on types other than string ) attr.checkValidity(schemaHandler) shouldBe Left(List("Invalid Type invalid-type")) } "Attribute privacy" should "appliable to any type" in { val attr = Attribute( "attr", "long", Some(true), true, Some( PrivacyLevel("ApproxLong(20)") ) // Should raise an error. Privacy cannot be applied on types other than stringsettings = settings ) attr.checkValidity(schemaHandler) shouldBe Right(true) } "Sub Attribute" should "be present for struct types only" in { val attr = Attribute( "attr", "long", Some(true), true, Some( PrivacyLevel("ApproxLong(20)") ), // Should raise an error. Privacy cannot be applied on types other than string attributes = Some(List[Attribute]()) ) val expectedErrors = List( "Attribute Attribute(attr,long,Some(true),true,Some(ApproxLong(20)),None,None,None,Some(List()),None,None,None) : Simple attributes cannot have sub-attributes", "Attribute Attribute(attr,long,Some(true),true,Some(ApproxLong(20)),None,None,None,Some(List()),None,None,None) : when present, attributes list cannot be empty." ) attr.checkValidity(schemaHandler) shouldBe Left(expectedErrors) } "Position serialization" should "output all fields" in { val yml = loadTextFile(s"/expected/yml/position_serialization_${versionSuffix}.yml") val attr = Attribute("hello", position = Some(Position(1, 2))) val writer = new StringWriter() mapper.writer().writeValue(writer, attr) logger.info("--" + writer.toString + "--") logger.info("++" + yml + "++") writer.toString.trim should equal(yml) } "Default value for an attribute" should "only be used for non obligatory fields" in { val requiredAttribute = Attribute("requiredAttribute", "long", required = true, default = Some("10")) requiredAttribute.checkValidity(schemaHandler) shouldBe Left( List( s"attribute with name ${requiredAttribute.name}: default value valid for optional fields only" ) ) val optionalAttribute = Attribute("optionalAttribute", "long", required = false, default = Some("10")) optionalAttribute.checkValidity(schemaHandler) shouldBe Right(true) } } }
Example 3
Source File: StringReaderSuite.scala From functadelic with MIT License | 5 votes |
package stagedparsec import lms._ import lms.testutil.FileDiffSpec import scala.lms.common._ import scala.lms.internal.Effects import java.io.PrintWriter import java.io.StringWriter import java.io.FileOutputStream trait StringReaderProg extends StringReaderOps with MiscOps { //print reader.first def testFirst(in: Rep[Array[Char]]): Rep[Char] = { val rdr: Rep[StringReader] = StringReader(in) rdr.first } def testAtEnd(in: Rep[Array[Char]], offset: Rep[Int]): Rep[Boolean] = { val rdr: Rep[StringReader] = StringReader(in, offset) rdr.atEnd } //compute rdr.rest and print first def testRest(in: Rep[Array[Char]]): Rep[Char] = { val rdr: Rep[StringReader] = StringReader(in) val rst = rdr.rest rst.first } def testIteration(in: Rep[Array[Char]]): Rep[Unit] = { val rdr = StringReader(in) rdr.foreach { c => println(c) } } } class StringReaderSuite extends FileDiffSpec { val prefix = "test-out/" def `StringReader generate code with no diff` = { withOutFile(prefix + "stringreader") { new StringReaderProg with StringReaderOpsExpOpt with MiscOpsExp with SeqOpsExp with MyScalaCompile { self => val codegen = new ScalaGenStringReaderOps with ScalaGenMiscOps { val IR: self.type = self } codegen.emitSource(testFirst _, "testFirst", new java.io.PrintWriter(System.out)) codegen.reset val testcFirst = compile(testFirst) scala.Console.println(testcFirst("hello".toArray)) codegen.reset codegen.emitSource2(testAtEnd _, "testAtEnd", new java.io.PrintWriter(System.out)) codegen.reset val testcAtEnd = compile2(testAtEnd) scala.Console.println(testcAtEnd("hello".toArray, 0)) scala.Console.println(testcAtEnd("hello".toArray, 6)) codegen.reset codegen.emitSource(testRest _, "testRest", new java.io.PrintWriter(System.out)) codegen.reset val testcRest = compile(testRest) scala.Console.println(testcRest("hello".toArray)) codegen.reset codegen.emitSource(testIteration _, "testIteration", new java.io.PrintWriter(System.out)) codegen.reset val testcIteration = compile(testIteration) testcIteration("hello".toArray) codegen.reset } } assertFileEqualsCheck(prefix + "stringreader") } }
Example 4
Source File: RecParsersSuite.scala From functadelic with MIT License | 5 votes |
package stagedparsec import lms._ import lms.testutil.FileDiffSpec import scala.lms.common._ import scala.lms.internal.Effects import java.io.PrintWriter import java.io.StringWriter import java.io.FileOutputStream trait RecParsersProg extends CharParsers with Functions { import Parser._ with SeqOpsExp with MyScalaCompile { self => val codegen = new ScalaGenCharParsers with ScalaGenStruct with ScalaGenIfThenElse { val IR: self.type = self } codegen.emitSource(recNumber _, "recNumber", new java.io.PrintWriter(System.out)) codegen.reset val testcRecNumber = compile(recNumber) scala.Console.println(testcRecNumber("12345".toArray)) codegen.reset } assertFileEqualsCheck(prefix + "rec-parser") } } }
Example 5
Source File: GoldenGateStage.scala From midas with BSD 3-Clause "New" or "Revised" License | 5 votes |
// See LICENSE for license details. package midas.stage import firrtl.AnnotationSeq import firrtl.options.{Phase, PhaseManager, PreservesAll, Shell, Stage, StageMain} import firrtl.options.phases.DeletedWrapper import firrtl.options.Viewer.view import java.io.{StringWriter, PrintWriter} class GoldenGateStage extends Stage with PreservesAll[Phase] { val shell: Shell = new Shell("goldengate") with GoldenGateCli private val phases: Seq[Phase] = Seq( new GoldenGateGetIncludes, new firrtl.stage.phases.AddDefaults, new firrtl.stage.phases.AddImplicitEmitter, new firrtl.stage.phases.Checks, new firrtl.stage.phases.AddCircuit, new firrtl.stage.phases.AddImplicitOutputFile, new midas.stage.GoldenGateCompilerPhase, new firrtl.stage.phases.WriteEmitted ) .map(DeletedWrapper(_)) def run(annotations: AnnotationSeq): AnnotationSeq = phases.foldLeft(annotations)((a, f) => f.transform(a)) } object GoldenGateMain extends StageMain(new GoldenGateStage)
Example 6
Source File: PlatformMapping.scala From midas with BSD 3-Clause "New" or "Revised" License | 5 votes |
// See LICENSE for license details. package midas package passes import firrtl._ import firrtl.annotations.{CircuitName} import firrtl.ir._ import firrtl.Mappers._ import Utils._ import java.io.{File, FileWriter, StringWriter} private[passes] class PlatformMapping( target: String, dir: File) (implicit param: freechips.rocketchip.config.Parameters) extends firrtl.Transform { def inputForm = LowForm def outputForm = HighForm override def name = "[MIDAS] Platform Mapping" private def dumpHeader(c: platform.PlatformShim) { def vMacro(arg: (String, Long)): String = s"`define ${arg._1} ${arg._2}\n" val csb = new StringBuilder csb append "#ifndef __%s_H\n".format(target.toUpperCase) csb append "#define __%s_H\n".format(target.toUpperCase) c.genHeader(csb, target) csb append "#endif // __%s_H\n".format(target.toUpperCase) val vsb = new StringBuilder vsb append "`ifndef __%s_H\n".format(target.toUpperCase) vsb append "`define __%s_H\n".format(target.toUpperCase) c.headerConsts map vMacro addString vsb vsb append "`endif // __%s_H\n".format(target.toUpperCase) val ch = new FileWriter(new File(dir, s"${target}-const.h")) val vh = new FileWriter(new File(dir, s"${target}-const.vh")) try { ch write csb.result vh write vsb.result } finally { ch.close vh.close csb.clear vsb.clear } } def initStmt(sim: String)(s: Statement): Statement = s match { case s: WDefInstance if s.name == "sim" && s.module == "SimBox" => s.copy(module = sim) // replace TargetBox with the actual sim module case s => s map initStmt(sim) } def init(info: Info, sim: String)(m: DefModule) = m match { case m: Module if m.name == "FPGATop" => val body = initStmt(sim)(m.body) Some(m.copy(info = info, body = body)) case m: Module => Some(m) case m: ExtModule => None } def linkCircuits(parent: Circuit, child: Circuit): Circuit = { parent.copy(modules = child.modules ++ (parent.modules flatMap init(child.info, child.main))) } def execute(c: CircuitState) = { val sim = c.circuit match { case w: WCircuit => w.sim } lazy val shim = param(Platform) match { case Zynq => new platform.ZynqShim(sim) case F1 => new platform.F1Shim(sim) } val shimCircuit = chisel3.Driver.elaborate(() => shim) val chirrtl = Parser.parse(chisel3.Driver.emit(shimCircuit)) val shimAnnos = shimCircuit.annotations.map(_.toFirrtl) val transforms = Seq(new Fame1Instances, new PreLinkRenaming(Namespace(c.circuit))) val shimCircuitState = new LowFirrtlCompiler().compile(CircuitState(chirrtl, ChirrtlForm, shimAnnos), transforms) // Rename the annotations from the inner module, which are using an obselete CircuitName val renameMap = RenameMap( Map(CircuitName(c.circuit.main) -> Seq(CircuitName(shimCircuitState.circuit.main)))) dumpHeader(shim) c.copy(circuit = linkCircuits(shimCircuitState.circuit, c.circuit), annotations = shimCircuitState.annotations ++ c.annotations, renames = Some(renameMap)) } }
Example 7
Source File: EnsureNoTargetIO.scala From midas with BSD 3-Clause "New" or "Revised" License | 5 votes |
// See LICENSE for license details. package midas.passes import midas.widgets.BridgeAnnotation import midas.passes.fame.{PromoteSubmodule, PromoteSubmoduleAnnotation, FAMEChannelConnectionAnnotation} import firrtl._ import firrtl.annotations._ import firrtl.ir._ import firrtl.Mappers._ import firrtl.transforms.TopWiring.{TopWiringAnnotation, TopWiringTransform, TopWiringOutputFilesAnnotation} import firrtl.passes.wiring.{Wiring, WiringInfo} import Utils._ import scala.collection.mutable import java.io.{File, FileWriter, StringWriter} // Ensures that there are no dangling IO on the target. All I/O coming off the DUT must be bound // to an Bridge BlackBox private[passes] class EnsureNoTargetIO extends firrtl.Transform { def inputForm = HighForm def outputForm = HighForm override def name = "[MIDAS] Ensure No Target IO" def execute(state: CircuitState): CircuitState = { val topName = state.circuit.main val topModule = state.circuit.modules.find(_.name == topName).get val nonClockPorts = topModule.ports.filter(_.tpe != ClockType) if (!nonClockPorts.isEmpty) { val exceptionMessage = """ Your target design has dangling IO. You must bind the following top-level ports to an Bridge BlackBox: """ + nonClockPorts.map(_.name).mkString("\n") throw new Exception(exceptionMessage) } state } }
Example 8
Source File: LogTestListener.scala From hail with MIT License | 5 votes |
package is.hail import java.io.{PrintWriter, StringWriter} import is.hail.utils._ import org.testng.{ITestContext, ITestListener, ITestResult} class LogTestListener extends ITestListener { def testString(result: ITestResult): String = { s"${ result.getTestClass.getName }.${ result.getMethod.getMethodName }" } def onTestStart(result: ITestResult) { info(s"starting test ${ testString(result) }...") } def onTestSuccess(result: ITestResult) { info(s"test ${ testString(result) } SUCCESS") } def onTestFailure(result: ITestResult) { val cause = result.getThrowable if (cause != null) { val sw = new StringWriter() val pw = new PrintWriter(sw) cause.printStackTrace(pw) info(s"Exception:\n$sw") } info(s"test ${ testString(result) } FAILURE\n") } def onTestSkipped(result: ITestResult) { info(s"test ${ testString(result) } SKIPPED") } def onTestFailedButWithinSuccessPercentage(result: ITestResult) { } def onStart(context: ITestContext) { } def onFinish(context: ITestContext) { } }
Example 9
Source File: ExceptionStacktraceToString.scala From graphcool-framework with Apache License 2.0 | 5 votes |
package cool.graph.util.exceptions import java.io.{PrintWriter, StringWriter} object ExceptionStacktraceToString { implicit class ThrowableStacktraceExtension(t: Throwable) { def stackTraceAsString: String = ExceptionStacktraceToString(t) } def apply(t: Throwable): String = { val sw = new StringWriter() val pw = new PrintWriter(sw) t.printStackTrace(pw) sw.toString() } }
Example 10
Source File: PrometheusRouting.scala From vinyldns with Apache License 2.0 | 5 votes |
package vinyldns.api.route import java.io.StringWriter import akka.http.scaladsl.model._ import akka.http.scaladsl.server.Directives import io.prometheus.client.CollectorRegistry import io.prometheus.client.exporter.common.TextFormat import scala.collection.JavaConverters._ trait PrometheusRoute extends Directives { def collectorRegistry: CollectorRegistry private val `text/plain; version=0.0.4; charset=utf-8` = ContentType { MediaType.customWithFixedCharset( "text", "plain", HttpCharsets.`UTF-8`, params = Map("version" -> "0.0.4") ) } def renderMetrics(registry: CollectorRegistry, names: Set[String]): String = { val writer = new StringWriter() TextFormat.write004(writer, registry.filteredMetricFamilySamples(names.toSet.asJava)) writer.toString } val prometheusRoute = (get & path("metrics" / "prometheus") & parameter('name.*)) { names => val content = renderMetrics(collectorRegistry, names.toSet) complete { HttpResponse(entity = HttpEntity(`text/plain; version=0.0.4; charset=utf-8`, content)) } } }
Example 11
Source File: HatServerProvider.scala From HAT2.0 with GNU Affero General Public License v3.0 | 5 votes |
package org.hatdex.hat.resourceManagement import java.io.StringWriter import java.security.interfaces.RSAPublicKey import javax.inject.{ Inject, Named, Singleton } import akka.actor.ActorRef import akka.pattern.ask import akka.util.Timeout import com.mohiva.play.silhouette.api.services.DynamicEnvironmentProviderService import org.bouncycastle.util.io.pem.{ PemObject, PemWriter } import org.hatdex.hat.api.service.RemoteExecutionContext import org.hatdex.hat.resourceManagement.actors.HatServerProviderActor import org.hatdex.hat.utils.LoggingProvider import play.api.cache.{ AsyncCacheApi, NamedCache } import play.api.Configuration import play.api.mvc.Request import scala.concurrent.Future import scala.concurrent.duration._ trait HatServerProvider extends DynamicEnvironmentProviderService[HatServer] { def retrieve[B](request: Request[B]): Future[Option[HatServer]] def retrieve(hatAddress: String): Future[Option[HatServer]] def toString(publicKey: RSAPublicKey): String = { val pemObject = new PemObject("PUBLIC KEY", publicKey.getEncoded) val stringPemWriter = new StringWriter() val pemWriter: PemWriter = new PemWriter(stringPemWriter) pemWriter.writeObject(pemObject) pemWriter.flush() val pemPublicKey = stringPemWriter.toString pemPublicKey } } @Singleton class HatServerProviderImpl @Inject() ( configuration: Configuration, @NamedCache("hatserver-cache") cache: AsyncCacheApi, loggingProvider: LoggingProvider, @Named("hatServerProviderActor") serverProviderActor: ActorRef)( implicit val ec: RemoteExecutionContext) extends HatServerProvider { private val logger = loggingProvider.logger(this.getClass) def retrieve[B](request: Request[B]): Future[Option[HatServer]] = { val hatAddress = request.host //.split(':').headOption.getOrElse(request.host) retrieve(hatAddress) } implicit val timeout: Timeout = configuration.get[FiniteDuration]("resourceManagement.serverProvisioningTimeout") implicit val serverInfoTimeout: Duration = configuration.get[FiniteDuration]("resourceManagement.serverIdleTimeout") def retrieve(hatAddress: String): Future[Option[HatServer]] = { cache.get[HatServer](s"server:$hatAddress") .flatMap { case Some(server) => Future.successful(Some(server)) case _ => (serverProviderActor ? HatServerProviderActor.HatServerRetrieve(hatAddress)) map { case server: HatServer => logger.debug(s"Got back server $server") cache.set(s"server:$hatAddress", server, serverInfoTimeout) Some(server) case error: HatServerDiscoveryException => logger.warn(s"Got back error $error") throw error case message => logger.warn(s"Unknown message $message from HAT Server provider actor") val error = new HatServerDiscoveryException("Unknown message") throw error } recoverWith { case e => logger.warn(s"Error while retrieving HAT $hatAddress info: ${e.getMessage}") val error = new HatServerDiscoveryException("HAT Server info retrieval failed", e) throw error } } } }
Example 12
Source File: ThrowableImplicits.scala From ForestFlow with Apache License 2.0 | 5 votes |
package ai.forestflow.utils import java.io.{PrintWriter, StringWriter} import scala.language.implicitConversions object ThrowableImplicits { implicit def Throwable2StackTraceString(ex: Throwable): String = { val sw = new StringWriter() val pw = new PrintWriter(sw) ex.printStackTrace(pw) sw.toString } implicit class PrintableThrowable(ex: Throwable) { def printableStackTrace: String = Throwable2StackTraceString(ex) } }
Example 13
Source File: RewriterBase.scala From apalache with Apache License 2.0 | 5 votes |
package at.forsyte.apalache.tla.bmcmt import java.io.{PrintWriter, StringWriter} import at.forsyte.apalache.tla.bmcmt.types.eager.TrivialTypeFinder import at.forsyte.apalache.tla.lir.convenience.tla import org.scalatest.{BeforeAndAfterEach, FunSuite} class RewriterBase extends FunSuite with BeforeAndAfterEach { protected var solverContext: SolverContext = new PreproSolverContext(new Z3SolverContext()) protected var arena: Arena = Arena.create(solverContext) override def beforeEach() { solverContext = new PreproSolverContext(new Z3SolverContext(debug = true)) arena = Arena.create(solverContext) } override def afterEach() { solverContext.dispose() } protected def create(): SymbStateRewriterAuto = { new SymbStateRewriterAuto(solverContext) } protected def createWithoutCache(): SymbStateRewriter = { new SymbStateRewriterImpl(solverContext, new TrivialTypeFinder()) } protected def assertUnsatOrExplain(rewriter: SymbStateRewriter, state: SymbState): Unit = { assertOrExplain("UNSAT", rewriter, state, !solverContext.sat()) } protected def assumeTlaEx(rewriter: SymbStateRewriter, state: SymbState): SymbState = { val nextState = rewriter.rewriteUntilDone(state.setTheory(BoolTheory())) solverContext.assertGroundExpr(nextState.ex) assert(solverContext.sat()) nextState } protected def assertTlaExAndRestore(rewriter: SymbStateRewriter, state: SymbState): Unit = { rewriter.push() val nextState = rewriter.rewriteUntilDone(state.setTheory(BoolTheory())) assert(solverContext.sat()) rewriter.push() solverContext.assertGroundExpr(nextState.ex) assert(solverContext.sat()) rewriter.pop() rewriter.push() solverContext.assertGroundExpr(tla.not(nextState.ex)) assertUnsatOrExplain(rewriter, nextState) rewriter.pop() rewriter.pop() } private def assertOrExplain(msg: String, rewriter: SymbStateRewriter, state: SymbState, outcome: Boolean): Unit = { if (!outcome) { val writer = new StringWriter() new SymbStateDecoder(solverContext, rewriter).dumpArena(state, new PrintWriter(writer)) solverContext.log(writer.getBuffer.toString) solverContext.push() // push and pop flush the log output solverContext.pop() fail("Expected %s, check log.smt for explanation".format(msg)) } } }
Example 14
Source File: HugeRoundTripBench.scala From xml-lens with MIT License | 5 votes |
package pl.msitko.xml.bench import java.io.{File, StringWriter} import java.nio.file.Paths import java.util.concurrent.TimeUnit import org.openjdk.jmh.annotations._ import pl.msitko.xml.parsing.XmlParser import pl.msitko.xml.printing.XmlPrinter import scala.xml.XML @BenchmarkMode(Array(Mode.AverageTime)) @OutputTimeUnit(TimeUnit.MICROSECONDS) @State(Scope.Benchmark) class HugeRoundTripBench { import HugeRoundTripBench._ @Benchmark def roundTripWithLens = { val parsed = XmlParser.parsePath(path).right.get XmlPrinter.print(parsed) } @Benchmark def roundtripWithStd = { val xml = XML.loadFile(file) val writer = new StringWriter XML.write(writer, xml, "UTF-8", true, null) writer.toString } } object HugeRoundTripBench { // BEWARE: that file is not included in the repo because of its huge size // you can download some big XMLs at https://dumps.wikimedia.org/enwiki/ val path = Paths.get("src", "main", "resources", "enwiki-20180420-pages-articles26.xml") val file = { // BEWARE: that file is not included in the repo because of its huge size // you can download some big XMLs at https://dumps.wikimedia.org/enwiki/ val p = List("src", "main", "resources", "enwiki-20180420-pages-articles26.xml").mkString(File.separator) new File(p) } }
Example 15
Source File: PrintBench.scala From xml-lens with MIT License | 5 votes |
package pl.msitko.xml.bench import java.io.StringWriter import java.util.concurrent.TimeUnit import org.openjdk.jmh.annotations._ import pl.msitko.xml.entities.XmlDocument import pl.msitko.xml.parsing.XmlParser import pl.msitko.xml.printing.XmlPrinter import scala.xml.{Elem, XML} object PrintBenchParams { val lensElement: XmlDocument = XmlParser.parse(SmallRoundtrip.example.input).right.get val stdElement: Elem = XML.loadString(SmallRoundtrip.example.input) } @BenchmarkMode(Array(Mode.AverageTime)) @OutputTimeUnit(TimeUnit.MICROSECONDS) @State(Scope.Benchmark) class PrintBench { import PrintBenchParams._ @Benchmark def printWithLens: String = { XmlPrinter.print(lensElement) } @Benchmark def prettyPrintWithLens: String = { XmlPrinter.print(lensElement) } @Benchmark def prettyPrintWithStd: String = { val writer = new StringWriter XML.write(writer, stdElement, "UTF-8", true, null) writer.toString } }
Example 16
Source File: SimpleTransformationStd.scala From xml-lens with MIT License | 5 votes |
package pl.msitko.xml.bench import java.io.StringWriter import scala.xml.{Elem, Text, XML} object SimpleTransformationStd extends SimpleTransformation { def transform(el: Elem): Elem = { if(el.child.size == 1) { val replaceWith = el.child.head match { case t: Text => Text(t.text.toUpperCase) case a => a } el.copy(child = List(replaceWith)) } else { el } } override def transform(input: String): String = { val xml = XML.loadString(input) val transformed = xml.map { case el: Elem if el.label == "a" => el.copy(child = el.child.flatMap { case el: Elem if el.label == "interesting" => el.copy(child = el.child.flatMap { case el: Elem if el.label == "special" => transform(el) case a => a }) case a => a }) case a => a } val writer = new StringWriter XML.write(writer, transformed.head, "UTF-8", true, null) writer.toString } }
Example 17
Source File: InstrumentedInputs.scala From scastie with Apache License 2.0 | 5 votes |
package com.olegych.scastie.instrumentation import java.io.{PrintWriter, StringWriter} import java.time.Instant import com.olegych.scastie.api._ import scala.meta.parsers.Parsed case class InstrumentationFailureReport(message: String, line: Option[Int]) { def toProgress(snippetId: SnippetId): SnippetProgress = { SnippetProgress.default.copy( ts = Some(Instant.now.toEpochMilli), snippetId = Some(snippetId), compilationInfos = List(Problem(Error, line, message)) ) } } object InstrumentedInputs { def apply(inputs0: Inputs): Either[InstrumentationFailureReport, InstrumentedInputs] = { if (inputs0.isWorksheetMode) { val instrumented = Instrument(inputs0.code, inputs0.target).map { instrumentedCode => inputs0.copy(code = instrumentedCode) } instrumented match { case Right(inputs) => success(inputs) case Left(error) => import InstrumentationFailure._ error match { case HasMainMethod => Right(InstrumentedInputs(inputs0.copy(_isWorksheetMode = false), isForcedProgramMode = true)) case UnsupportedDialect => Left(InstrumentationFailureReport("This Scala target does not have a worksheet mode", None)) case ParsingError(Parsed.Error(pos, message, _)) => val lineOffset = Instrument.getParsingLineOffset(inputs0) Left(InstrumentationFailureReport(message, Some(pos.startLine + lineOffset))) case InternalError(exception) => val errors = new StringWriter() exception.printStackTrace(new PrintWriter(errors)) val fullStack = errors.toString Left(InstrumentationFailureReport(fullStack, None)) } } } else { success(inputs0) } } private def success(inputs: Inputs): Either[InstrumentationFailureReport, InstrumentedInputs] = { Right(InstrumentedInputs(inputs, isForcedProgramMode = false)) } } case class InstrumentedInputs( inputs: Inputs, isForcedProgramMode: Boolean )
Example 18
Source File: HasGeometry.scala From recogito2 with Apache License 2.0 | 5 votes |
package services import com.vividsolutions.jts.geom.{Coordinate, Envelope, Geometry} import java.io.StringWriter import org.geotools.geojson.geom.GeometryJSON import play.api.libs.json._ trait HasGeometry { protected val DECIMAL_PRECISION = 12 implicit val geometryFormat: Format[Geometry] = Format( JsPath.read[JsValue].map { json => try { val geom = new GeometryJSON(DECIMAL_PRECISION).read(Json.stringify(json)) if (geom != null && geom.isEmpty) null else geom // Some Java legacy... } catch { case t: Throwable => null } }, Writes[Geometry] { geom => val writer = new StringWriter() new GeometryJSON(DECIMAL_PRECISION).write(geom, writer) Json.parse(writer.toString) } ) implicit val envelopeFormat: Format[Envelope] = Format( (JsPath \ "coordinates").read[JsArray].map { js => val points = js.value.map(_.as[JsArray].value.map(_.as[Double])) val topLeft = points(0) val bottomRight = points(1) new Envelope(topLeft(0), bottomRight(0), bottomRight(1), topLeft(1)) }, Writes[Envelope] { e => Json.obj( "type" -> "envelope", // A special geo_shape type supported by ElasticSearch "coordinates" -> Seq(Seq(e.getMinX, e.getMaxY), Seq(e.getMaxX, e.getMinY))) } ) implicit val coordinateFormat: Format[Coordinate] = Format( JsPath.read[JsArray].map { json => val lon = json.value(0).as[Double] val lat = json.value(1).as[Double] new Coordinate(lon, lat) }, Writes[Coordinate] { c => Json.toJson(Seq(c.x, c.y)) } ) } trait HasGeometrySafe extends HasGeometry { private def safeGeometry(json: JsValue): JsObject = JsObject( Json.obj( "type" -> (json \ "type").get, "coordinates" -> (json \ "coordinates").asOpt[JsArray], "geometries" -> (json \ "geometries").asOpt[JsArray].map(_.value.map(safeGeometry)) ).fields.filter(_._2 match { // Filter out undefined props case JsNull => false case _ => true }) ) implicit override val geometryFormat: Format[Geometry] = Format( JsPath.read[JsValue].map { json => new GeometryJSON(DECIMAL_PRECISION).read(Json.stringify(safeGeometry(json))) }, Writes[Geometry] { geom => val writer = new StringWriter() new GeometryJSON(DECIMAL_PRECISION).write(geom, writer) Json.parse(writer.toString) } ) }
Example 19
Source File: ParadoxLogger.scala From paradox with Apache License 2.0 | 5 votes |
package com.lightbend.paradox import java.io.{ PrintWriter, StringWriter } import scala.collection.immutable.StringOps trait ParadoxLogger { def debug(t: Throwable): Unit = { // we provide our own implementation because sbt doesn't offer any exception logging at debug val writer = new StringWriter() t.printStackTrace(new PrintWriter(writer)) new StringOps(writer.toString).lines.foreach(debug(_)) } def debug(msg: => String): Unit def info(msg: => String): Unit def warn(msg: => String): Unit def error(msg: => String): Unit } object NullLogger extends ParadoxLogger { override def debug(msg: => String): Unit = () override def info(msg: => String): Unit = () override def warn(msg: => String): Unit = () override def error(msg: => String): Unit = () } object PrintlnLogger extends ParadoxLogger { override def debug(msg: => String): Unit = println(s"[debug] $msg") override def info(msg: => String): Unit = println(s"[info] $msg") override def warn(msg: => String): Unit = println(s"[warn] $msg") override def error(msg: => String): Unit = println(s"[error] $msg") }
Example 20
Source File: DirectoryViewer.scala From peregrine with Apache License 2.0 | 5 votes |
package io.peregrine import com.github.mustachejava.DefaultMustacheFactory import java.io.{StringWriter, File} import com.twitter.util.Try object DirectoryViewer { // these are passed to the mustache view case class ListingView(root: FileView, files: Array[FileView], directories: Array[FileView]) case class FileView(relativeName: String, shortName: String) def getListing(directory: File): Try[String] = { // there's a lot of io and untyped computation in this function. It's best to // err on the side of safety and wrap everything in a Try comprehension val mustacheFactory = new DefaultMustacheFactory val output = new StringWriter for { reader <- Try(mustacheFactory.getReader("directory_browser.mustache")) mustache <- Try(mustacheFactory.compile(reader, "directory_browser")) scope <- Try(renderView(directory)) () <- Try(mustache.execute(output, scope).flush()) } yield output.toString } val absoluteAssetPath = new File(config.docRoot() + config.assetPath()).getAbsolutePath private def fileToView(f: File): FileView = { val relativeName = f.getAbsolutePath.replace(absoluteAssetPath, "") val name = f.getName FileView(relativeName, name) } def renderView(directory: File): ListingView = { val directories = directory.listFiles.filter(x => x != null && x.isDirectory).map(fileToView) val files = directory.listFiles.filter(x => x != null && x.isFile).map(fileToView) ListingView(fileToView(directory), files, directories) } }
Example 21
Source File: PropertiesApi.scala From iep-apps with Apache License 2.0 | 5 votes |
package com.netflix.iep.archaius import java.io.StringWriter import java.util.Properties import akka.actor.ActorRefFactory import akka.http.scaladsl.model.HttpCharsets import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.model.HttpEntity import akka.http.scaladsl.model.HttpRequest import akka.http.scaladsl.model.HttpResponse import akka.http.scaladsl.model.MediaTypes import akka.http.scaladsl.model.StatusCodes import akka.http.scaladsl.server.Route import com.netflix.atlas.akka.CustomDirectives._ import com.netflix.atlas.akka.WebApi import com.netflix.atlas.json.Json import com.netflix.frigga.Names class PropertiesApi( val propContext: PropertiesContext, implicit val actorRefFactory: ActorRefFactory ) extends WebApi { def routes: Route = { endpointPath("api" / "v1" / "property") { get { parameter("asg") { asg => extractRequest { request => val cluster = Names.parseName(asg).getCluster if (propContext.initialized) { val props = propContext.getClusterProps(cluster) complete(encode(request, props)) } else { complete(HttpResponse(StatusCodes.ServiceUnavailable)) } } } } } } private def encode(request: HttpRequest, props: List[PropertiesApi.Property]): HttpResponse = { val useJson = request.headers.exists(h => h.is("accept") && h.value == "application/json") if (useJson) { HttpResponse( StatusCodes.OK, entity = HttpEntity(MediaTypes.`application/json`, Json.encode(props)) ) } else { val ps = new Properties props.foreach { p => ps.setProperty(p.key, p.value) } val writer = new StringWriter() ps.store(writer, s"count: ${ps.size}") writer.close() val entity = HttpEntity(MediaTypes.`text/plain`.toContentType(HttpCharsets.`UTF-8`), writer.toString) HttpResponse(StatusCodes.OK, entity = entity) } } } object PropertiesApi { case class Property(id: String, cluster: String, key: String, value: String, timestamp: Long) }
Example 22
Source File: VCFHeaderWriter.scala From glow with Apache License 2.0 | 5 votes |
package htsjdk.variant.variantcontext.writer import java.io.{StringWriter, Writer} import htsjdk.variant.vcf.VCFHeader object VCFHeaderWriter { // Shim into default visibility VCFWriter class def writeHeader( header: VCFHeader, writer: Writer, versionLine: String, streamNameForError: String): VCFHeader = { VCFWriter.writeHeader(header, writer, versionLine, streamNameForError) } def writeHeaderAsString(header: VCFHeader): String = { val writer = new StringWriter() writeHeader(header, writer, VCFWriter.getVersionLine, "headerBuffer") writer.toString } }
Example 23
Source File: ThreadPoolSchedulerProvider.scala From scala-game-library with MIT License | 5 votes |
package sgl.util import java.util.concurrent.Executors import java.io.{StringWriter, PrintWriter} import scala.collection.mutable.Queue trait ThreadPoolSchedulerProvider extends SchedulerProvider { this: LoggingProvider => private implicit val Tag = Logger.Tag("threadpool-scheduler") class ThreadPoolScheduler extends Scheduler { private val pool = Executors.newFixedThreadPool(4) private val tasks: Queue[ChunkedTask] = new Queue private val taskQueueLock = new Object private var r1: ChunksRunner = null private var r2: ChunksRunner = null private var r3: ChunksRunner = null private var r4: ChunksRunner = null override def schedule(task: ChunkedTask): Unit = { taskQueueLock.synchronized { tasks.enqueue(task) } } def shutdown(): Unit = { pool.shutdown() // Need to check for null because we could have skipped resume. if(r1 != null) r1.shouldStop = true if(r2 != null) r2.shouldStop = true if(r3 != null) r3.shouldStop = true if(r4 != null) r4.shouldStop = true } // Simple Runnable class that picks up the first available ChunkedTask and // run one chunk of it. // Note that if there is only one ChunkedTask in the queue, there will only // be one busy Thread at a time as ChunkedTask are assumed to be sequentials. // In order to optimize the use of the thread pool, one should try to split // parallel work into several independent ChunkedTask. class ChunksRunner extends Runnable { var shouldStop = false override def run(): Unit = { while(!shouldStop) { val task = taskQueueLock.synchronized { if(tasks.isEmpty) { None } else { Some(tasks.dequeue()) } } task match { case None => Thread.sleep(50) case Some(task) => { logger.debug("Executing some ChunkedTask from the task queue.") try { task.doRun(5l) if(task.status != ChunkedTask.Completed) taskQueueLock.synchronized { tasks.enqueue(task) } } catch { case (e: Throwable) => { logger.error(s"Unexpected error while executing task ${task.name}: ${e.getMessage}") val sw = new StringWriter() val pw = new PrintWriter(sw, true) e.printStackTrace(pw) logger.error(sw.toString) } } } } } } } } override val Scheduler = new ThreadPoolScheduler }
Example 24
Source File: StaxEncoder.scala From scalaz-deriving with GNU Lesser General Public License v3.0 | 5 votes |
// Copyright: 2017 - 2020 Sam Halliday // License: http://www.gnu.org/licenses/lgpl-3.0.en.html package xmlformat package stax import java.io.StringWriter import java.util.regex.Pattern import javax.xml.stream.{ XMLOutputFactory, XMLStreamWriter } import scalaz._, Scalaz._ import com.ctc.wstx.stax.WstxOutputFactory object StaxEncoder { // must not escape the code in this module private[this] val factory = new ThreadLocal[XMLOutputFactory] { override def initialValue: WstxOutputFactory = { val f = new com.ctc.wstx.stax.WstxOutputFactory f.configureForSpeed() f.getConfig.doSupportNamespaces(false) f } } def encode(t: XTag): String = { val output = new StringWriter val x = factory.get.createXMLStreamWriter(output) x.writeStartDocument() writeTag(x, t, 0) x.writeEndDocument() output.toString() } private[this] def writeTag(x: XMLStreamWriter, t: XTag, level: Int): Unit = { x.writeCharacters("\n") x.writeCharacters(" " * 2 * level) x.writeStartElement(t.name) t.attrs.toList.foreach { a => x.writeAttribute(a.name, a.value.text) } t.children.toList.foreach { c => writeTag(x, c, level + 1) } t.body.toList.foreach { s => if (t.children.nonEmpty) { x.writeCharacters("\n") x.writeCharacters(" " * 2 * (level + 1)) } if (!containsXmlEntities(s.text)) x.writeCharacters(s.text) else { val clean = if (!s.text.contains("]]>")) s.text else s.text.replace("]]>", "]]]]><![CDATA[>") x.writeCData(clean) } } if (t.children.nonEmpty) { x.writeCharacters("\n") x.writeCharacters(" " * 2 * level) } x.writeEndElement() } private[this] val entities = Pattern.compile("""("|&|'|<|>)""") def containsXmlEntities(input: String): Boolean = entities.matcher(input).find() }
Example 25
Source File: SparkSvc.scala From Mastering-Spark-for-Data-Science with MIT License | 5 votes |
package svc import java.io.StringWriter import java.net.{HttpURLConnection, URL} import com.typesafe.config.ConfigFactory import io.gzet.recommender.Node import org.apache.commons.io.IOUtils import play.api.Logger import play.api.libs.json._ class SparkSvc() { val config = ConfigFactory.load() val host = config.getString("spark.job.server.host") val port = config.getInt("spark.job.server.port") val timeout = config.getInt("spark.job.server.timeout") val appName = config.getString("spark.job.server.app") val context = config.getString("spark.job.server.context") val indexJob = config.getString("spark.job.index") val playlistJob = config.getString("spark.job.playlist") val playlistRecommendJob = config.getString("spark.job.personalized.playlist") private def getConnection(endpoint: String, params: Option[String]) = { try { val url = new URL(endpoint) val connection = url.openConnection().asInstanceOf[HttpURLConnection] connection.setDoOutput(true) connection.setRequestMethod("POST") connection.setRequestProperty("Accept", "application/json") if(params.isDefined){ val os = connection.getOutputStream os.write(params.get.getBytes()) os.flush() os.close() } val inputStream = connection.getInputStream val writer = new StringWriter() IOUtils.copy(inputStream, writer, "UTF-8") val ret = writer.toString Json.parse(ret) } catch { case e: Exception => throw new Exception("Job Failed: " + e.getMessage) } } private def parseResponse(json: JsValue) : String = { val jobId = (json \ "result" \ "jobId").asOpt[String] if(jobId.isDefined){ s"Job submitted [${jobId.get}]" } else { val message = (json \ "result" \ "message").asOpt[String] if(message.isDefined){ throw new Exception(s"Job failed: ${message.get}") } throw new Exception("Could not find Spark job id") } } def index(path: String): String = { Logger.info("Submitting INDEX job") val url = s"http://$host:$port/jobs?appName=$appName&classPath=$indexJob&context=$context" val params = "input.dir=\"" + path + "\"" val json = getConnection(url, Some(params)) parseResponse(json) } def playlist() = { Logger.info("Submitting PLAYLIST job") val url = s"http://$host:$port/jobs?appName=$appName&classPath=$playlistJob&context=$context" val json = getConnection(url, None) parseResponse(json) } def playlist(id: Long) = { Logger.info("Submitting RECOMMEND job") val url = s"http://$host:$port/jobs?appName=$appName&classPath=$playlistRecommendJob&context=$context&sync=true&timeout=$timeout" val params = s"song.id=$id" val json: JsValue = getConnection(url, Some(params)) val array = (json \ "result").as[Array[String]] array.map({line => val Array(id, pr, song) = line.split(",").take(3) Node(id.toLong, song, pr.toDouble) }).toList } }
Example 26
Source File: NERServiceSpec.scala From recogito2 with Apache License 2.0 | 5 votes |
package transform.ner import java.io.File import org.specs2.mutable._ import org.specs2.runner._ import org.junit.runner._ import org.joox.JOOX._ import org.pelagios.recogito.sdk.ner.EntityType import play.api.test._ import play.api.test.Helpers._ import scala.concurrent.duration._ import scala.io.Source import org.codehaus.plexus.util.StringInputStream import java.io.StringWriter import java.io.BufferedWriter @RunWith(classOf[JUnitRunner]) class NERServiceSpec extends Specification { def parsePlaintext() = { val TEST_TEXT = Source.fromFile("test/resources/transform/ner/text-for-ner-01.txt").getLines().mkString("\n") NERService.parseText(TEST_TEXT, None) } def enrichTEI() = { val TEST_TEI = new File("test/resources/transform/ner/tei-for-ner.tei.xml") val writer = new StringWriter() NERService.enrichTEI(TEST_TEI, None, Some(new BufferedWriter(writer))) $(writer.toString) } "The NER text parse function" should { "detect 8 Named Entites in the test text" in { val entities = parsePlaintext() entities.size must equalTo (8) } "detect 3 Locations - Pylos, Sparta and Ithaca" in { val entities = parsePlaintext() val locations = entities.filter(_.entityType == EntityType.LOCATION).map(_.chars) locations.size must equalTo(3) locations must contain("Pylos") locations must contain("Sparta") locations must contain("Ithaca") } "detect 1 date" in { val entities = parsePlaintext() entities.filter(_.entityType.equals(EntityType.DATE)).size must equalTo(1) } "detect 4 persons - Ulysses (2x), Penelope and Telemachus" in { val entities = parsePlaintext() val persons = entities.filter(_.entityType == EntityType.PERSON).map(_.chars) persons.size must equalTo(4) persons must contain("Penelope") persons must contain("Telemachus") persons.filter(_.equals("Ulysses")).size must equalTo(2) } "retain correct char offsets for each entity" in { val TEST_TEXT = Source.fromFile("test/resources/transform/ner/text-for-ner-01.txt").getLines().mkString("\n") val entities = parsePlaintext() entities.map(e => { val snippetFromSourceFile = TEST_TEXT.substring(e.charOffset, e.charOffset + e.chars.size) snippetFromSourceFile must equalTo(e.chars) }) } } "The NER TEI enrichment function" should { "insert 11 placeName tags" in { val enriched = enrichTEI() enriched.find("placeName").size must equalTo(11) } "insert 24 persName tags" in { val enriched = enrichTEI() enriched.find("persName").size must equalTo(24) } } }
Example 27
Source File: RuntimeError.scala From scastie with Apache License 2.0 | 5 votes |
package com.olegych.scastie.api import java.io.{PrintWriter, StringWriter} import play.api.libs.json._ case class RuntimeError( message: String, line: Option[Int], fullStack: String ) object RuntimeError { implicit val formatRuntimeError: OFormat[RuntimeError] = Json.format[RuntimeError] def wrap[T](in: => T): Either[Option[RuntimeError], T] = { try { Right(in) } catch { case ex: Exception => Left(RuntimeError.fromThrowable(ex, fromScala = false)) } } def fromThrowable(t: Throwable, fromScala: Boolean = true): Option[RuntimeError] = { def search(e: Throwable) = { e.getStackTrace .find( trace => if (fromScala) trace.getFileName == "main.scala" && trace.getLineNumber != -1 else true ) .map(v => (e, Some(v.getLineNumber))) } def loop(e: Throwable): Option[(Throwable, Option[Int])] = { val s = search(e) if (s.isEmpty) if (e.getCause != null) loop(e.getCause) else Some((e, None)) else s } loop(t).map { case (err, line) => val errors = new StringWriter() t.printStackTrace(new PrintWriter(errors)) val fullStack = errors.toString RuntimeError(err.toString, line, fullStack) } } } object RuntimeErrorWrap { implicit val formatRuntimeErrorWrap: OFormat[RuntimeErrorWrap] = Json.format[RuntimeErrorWrap] } case class RuntimeErrorWrap(error: Option[RuntimeError])
Example 28
Source File: PackagePlatformExtensions.scala From qamr with MIT License | 5 votes |
package qamr.util import java.io.StringWriter import java.io.PrintWriter import scala.util.{Try, Success, Failure} import com.typesafe.scalalogging.Logger trait PackagePlatformExtensions { implicit class RichTry[A](val t: Try[A]) { def toOptionLogging(logger: Logger): Option[A] = t match { case Success(a) => Some(a) case Failure(e) => val sw = new StringWriter() val pw = new PrintWriter(sw, true) e.printStackTrace(pw) logger.error(e.getLocalizedMessage + "\n" + sw.getBuffer.toString) None } } }
Example 29
Source File: MetricsEndpointSpec.scala From prometheus-akka-http with MIT License | 5 votes |
package com.lonelyplanet.prometheus.api import java.io.StringWriter import akka.http.scaladsl.model.HttpCharsets import akka.http.scaladsl.testkit.ScalatestRouteTest import com.lonelyplanet.prometheus.Utils._ import io.prometheus.client.exporter.common.TextFormat import io.prometheus.client.{CollectorRegistry, Histogram} import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers import scala.util.Random class MetricsEndpointSpec extends AnyFlatSpec with Matchers with ScalatestRouteTest { "Metrics endpoint" should "return the correct media type and charset" in { val api = createEndpoint(CollectorRegistry.defaultRegistry) Get("/metrics") ~> api.routes ~> check { mediaType.subType shouldBe "plain" mediaType.isText shouldBe true mediaType.params shouldBe Map("version" -> "0.0.4") charset shouldBe HttpCharsets.`UTF-8` } } it should "return serialized metrics in the prometheus text format" in { val registry = new CollectorRegistry() val api = createEndpoint(registry) val hist = Histogram.build().name(RandomTestName).help(RandomTestHelp).linearBuckets(0, 1, 10).register(registry) hist.observe(Math.abs(Random.nextDouble())) Get("/metrics") ~> api.routes ~> check { val resp = responseAs[String] val writer = new StringWriter() TextFormat.write004(writer, registry.metricFamilySamples()) resp shouldBe writer.toString } } private val RandomTestName = generateRandomStringOfLength(16) private val RandomTestHelp = generateRandomStringOfLength(16) private def createEndpoint(collectorRegistry: CollectorRegistry) = { new MetricsEndpoint(collectorRegistry) } }
Example 30
Source File: MetricFamilySamplesEntity.scala From prometheus-akka-http with MIT License | 5 votes |
package com.lonelyplanet.prometheus.api import java.io.{StringWriter, Writer} import java.util import akka.http.scaladsl.marshalling.{ToEntityMarshaller, Marshaller} import akka.http.scaladsl.model._ import io.prometheus.client.Collector.MetricFamilySamples import io.prometheus.client.CollectorRegistry import io.prometheus.client.exporter.common.TextFormat case class MetricFamilySamplesEntity(samples: util.Enumeration[MetricFamilySamples]) object MetricFamilySamplesEntity { private val mediaTypeParams = Map("version" -> "0.0.4") private val mediaType = MediaType.customWithFixedCharset("text", "plain", HttpCharsets.`UTF-8`, params = mediaTypeParams) def fromRegistry(collectorRegistry: CollectorRegistry): MetricFamilySamplesEntity = { MetricFamilySamplesEntity(collectorRegistry.metricFamilySamples()) } def toPrometheusTextFormat(e: MetricFamilySamplesEntity): String = { val writer: Writer = new StringWriter() TextFormat.write004(writer, e.samples) writer.toString } implicit val metricsFamilySamplesMarshaller: ToEntityMarshaller[MetricFamilySamplesEntity] = { Marshaller.withFixedContentType(mediaType) { s => HttpEntity(mediaType, toPrometheusTextFormat(s)) } } }
Example 31
Source File: DriverInitContainerBootstrapStep.scala From Spark-2.3.1 with Apache License 2.0 | 5 votes |
package org.apache.spark.deploy.k8s.submit.steps import java.io.StringWriter import java.util.Properties import io.fabric8.kubernetes.api.model.{ConfigMap, ConfigMapBuilder, ContainerBuilder, HasMetadata} import org.apache.spark.deploy.k8s.Config._ import org.apache.spark.deploy.k8s.KubernetesUtils import org.apache.spark.deploy.k8s.submit.KubernetesDriverSpec import org.apache.spark.deploy.k8s.submit.steps.initcontainer.{InitContainerConfigurationStep, InitContainerSpec} private[spark] class DriverInitContainerBootstrapStep( steps: Seq[InitContainerConfigurationStep], configMapName: String, configMapKey: String) extends DriverConfigurationStep { override def configureDriver(driverSpec: KubernetesDriverSpec): KubernetesDriverSpec = { var initContainerSpec = InitContainerSpec( properties = Map.empty[String, String], driverSparkConf = Map.empty[String, String], initContainer = new ContainerBuilder().build(), driverContainer = driverSpec.driverContainer, driverPod = driverSpec.driverPod, dependentResources = Seq.empty[HasMetadata]) for (nextStep <- steps) { initContainerSpec = nextStep.configureInitContainer(initContainerSpec) } val configMap = buildConfigMap( configMapName, configMapKey, initContainerSpec.properties) val resolvedDriverSparkConf = driverSpec.driverSparkConf .clone() .set(INIT_CONTAINER_CONFIG_MAP_NAME, configMapName) .set(INIT_CONTAINER_CONFIG_MAP_KEY_CONF, configMapKey) .setAll(initContainerSpec.driverSparkConf) val resolvedDriverPod = KubernetesUtils.appendInitContainer( initContainerSpec.driverPod, initContainerSpec.initContainer) driverSpec.copy( driverPod = resolvedDriverPod, driverContainer = initContainerSpec.driverContainer, driverSparkConf = resolvedDriverSparkConf, otherKubernetesResources = driverSpec.otherKubernetesResources ++ initContainerSpec.dependentResources ++ Seq(configMap)) } private def buildConfigMap( configMapName: String, configMapKey: String, config: Map[String, String]): ConfigMap = { val properties = new Properties() config.foreach { entry => properties.setProperty(entry._1, entry._2) } val propertiesWriter = new StringWriter() properties.store(propertiesWriter, s"Java properties built from Kubernetes config map with name: $configMapName " + s"and config map key: $configMapKey") new ConfigMapBuilder() .withNewMetadata() .withName(configMapName) .endMetadata() .addToData(configMapKey, propertiesWriter.toString) .build() } }
Example 32
Source File: CornichonError.scala From cornichon with Apache License 2.0 | 5 votes |
package com.github.agourlay.cornichon.core import java.io.{ PrintWriter, StringWriter } import cats.data.EitherT import cats.syntax.either._ import cats.instances.future._ import scala.concurrent.{ ExecutionContext, Future } import scala.util.control.NoStackTrace trait CornichonError { def baseErrorMessage: String val causedBy: List[CornichonError] = Nil lazy val renderedMessage: String = { if (causedBy.isEmpty) baseErrorMessage else s"""$baseErrorMessage |caused by: |${causedBy.iterator.map(c => c.renderedMessage).mkString("\nand\n")}""".stripMargin } def toException = CornichonException(renderedMessage) } object CornichonError { def genStacktrace(exception: Throwable): String = { val sw = new StringWriter() val pw = new PrintWriter(sw) exception.printStackTrace(pw) sw.toString } def fromString(error: String): CornichonError = BasicError(error) def fromThrowable(exception: Throwable): CornichonError = StepExecutionError(exception) def catchThrowable[A](f: => A): Either[CornichonError, A] = Either.catchNonFatal(f).leftMap(fromThrowable) implicit class fromEither[A](e: Either[CornichonError, A]) { def valueUnsafe: A = e.fold(e => throw e.toException, identity) def futureEitherT(implicit ec: ExecutionContext): EitherT[Future, CornichonError, A] = EitherT.fromEither[Future](e) } } case class StepExecutionError(e: Throwable) extends CornichonError { lazy val baseErrorMessage = s"exception thrown ${CornichonError.genStacktrace(e)}" } case class BasicError(error: String) extends CornichonError { lazy val baseErrorMessage = error } case class CornichonException(m: String) extends Exception with NoStackTrace { override def getMessage = m }
Example 33
Source File: LogEvent.scala From mist with Apache License 2.0 | 5 votes |
package io.hydrosphere.mist.core.logging import java.io.{PrintWriter, StringWriter} import java.time.format.DateTimeFormatter import java.time._ case class LogEvent( from: String, message: String, timeStamp: Long, level: Int, errTrace: Option[String]) { def mkString: String = { val date = formatDate val error = errTrace.map(s => "\n" + s).getOrElse("") s"${typedLevel.name} $date [$from] $message$error" } private def formatDate: String = { val inst = Instant.ofEpochMilli(timeStamp) val date = LocalDateTime.ofInstant(inst, ZoneOffset.UTC) DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(date) } def typedLevel: Level = Level.fromInt(level) } object LogEvent { def mkDebug(from: String, message: String, ts: Long = mkTimestamp): LogEvent = nonFatal(Level.Debug, from, message, ts) def mkInfo(from: String, message: String, ts: Long = mkTimestamp): LogEvent = nonFatal(Level.Info, from, message, ts) def mkWarn(from: String, message: String, ts: Long = mkTimestamp): LogEvent = nonFatal(Level.Warn, from, message, ts) def mkError(from: String, message: String, t: Throwable): LogEvent = mkError(from, message, Some(t)) def mkError(from: String, message: String, optT: Option[Throwable] = None, ts: Long = mkTimestamp): LogEvent = { val errTrace = optT.map(ex => { val writer = new StringWriter() ex.printStackTrace(new PrintWriter(writer)) writer.toString }) LogEvent(from, message, ts, Level.Error.value, errTrace) } def nonFatal(level: Level, from: String, message: String, ts: Long): LogEvent = LogEvent(from, message, ts, level.value, None) private def mkTimestamp: Long = LocalDateTime.now(ZoneOffset.UTC).toInstant(ZoneOffset.UTC).toEpochMilli }
Example 34
Source File: JsonEncoderSpec.scala From logback-json-logger with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.logging import java.io.{PrintWriter, StringWriter} import java.net.InetAddress import ch.qos.logback.classic.Level import ch.qos.logback.classic.spi.{ILoggingEvent, ThrowableProxy} import ch.qos.logback.core.ContextBase import org.apache.commons.lang3.time.FastDateFormat import org.mockito.Mockito.when import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import org.scalatestplus.mockito.MockitoSugar import play.api.libs.json.{JsLookupResult, Json} import scala.collection.JavaConverters._ class JsonEncoderSpec extends AnyWordSpec with Matchers with MockitoSugar { "Json-encoded message" should { "contain all required fields" in { val jsonEncoder = new JsonEncoder() val event = mock[ILoggingEvent] when(event.getTimeStamp).thenReturn(1) when(event.getLevel).thenReturn(Level.INFO) when(event.getThreadName).thenReturn("my-thread") when(event.getFormattedMessage).thenReturn("my-message") when(event.getLoggerName).thenReturn("logger-name") when(event.getMDCPropertyMap).thenReturn(Map("myMdcProperty" -> "myMdcValue").asJava) val testException = new Exception("test-exception") val stringWriter = new StringWriter() testException.printStackTrace(new PrintWriter(stringWriter)) when(event.getThrowableProxy).thenReturn(new ThrowableProxy(testException)) jsonEncoder.setContext { val ctx = new ContextBase() ctx.putProperty("myKey", "myValue") ctx } val result = new String(jsonEncoder.encode(event), "UTF-8") val resultAsJson = Json.parse(result) (resultAsJson \ "app").asString shouldBe "my-app-name" (resultAsJson \ "hostname").asString shouldBe InetAddress.getLocalHost.getHostName (resultAsJson \ "timestamp").asString shouldBe FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSSZZ").format(1) (resultAsJson \ "message").asString shouldBe "my-message" (resultAsJson \ "exception").asString should include("test-exception") (resultAsJson \ "exception").asString should include("java.lang.Exception") (resultAsJson \ "exception").asString should include(stringWriter.toString) (resultAsJson \ "logger").asString shouldBe "logger-name" (resultAsJson \ "thread").asString shouldBe "my-thread" (resultAsJson \ "level").asString shouldBe "INFO" (resultAsJson \ "mykey").asString shouldBe "myValue" (resultAsJson \ "mymdcproperty").asString shouldBe "myMdcValue" } } implicit class JsLookupResultOps(jsLookupResult: JsLookupResult) { def asString: String = jsLookupResult.get.as[String] } }
Example 35
Source File: HydraJsonSupport.scala From hydra with Apache License 2.0 | 5 votes |
package hydra.core.marshallers import java.io.{PrintWriter, StringWriter} import java.util.UUID import akka.actor.ActorPath import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.model.StatusCode import hydra.common.util.Resource._ import org.joda.time.DateTime import org.joda.time.format.ISODateTimeFormat import spray.json.{JsString, _} import scala.util.{Failure, Success, Try} implicit def tryWriter[R: JsonWriter]: RootJsonWriter[Try[R]] = new RootJsonWriter[Try[R]] { override def write(responseTry: Try[R]): JsValue = { responseTry match { case Success(r) => JsObject("success" -> r.toJson) case Failure(t) => JsObject("failure" -> t.toJson) } } } implicit object StreamTypeFormat extends RootJsonFormat[StreamType] { def read(json: JsValue): StreamType = json match { case JsString("Notification") => Notification case JsString("History") => History case JsString("CurrentState") => CurrentState case JsString("Telemetry") => Telemetry case _ => { import scala.reflect.runtime.{universe => ru} val tpe = ru.typeOf[StreamType] val clazz = tpe.typeSymbol.asClass throw new DeserializationException( s"expected a streamType of ${clazz.knownDirectSubclasses}, but got $json" ) } } def write(obj: StreamType): JsValue = { JsString(obj.toString) } } implicit val genericErrorFormat = jsonFormat2(GenericError) implicit val topicCreationMetadataFormat = jsonFormat8(TopicMetadataRequest) implicit val genericSchemaFormat = jsonFormat2(GenericSchema) } case class GenericError(status: Int, errorMessage: String) case class TopicMetadataRequest( schema: JsObject, streamType: StreamType, derived: Boolean, deprecated: Option[Boolean], dataClassification: String, contact: String, additionalDocumentation: Option[String], notes: Option[String] ) case class GenericSchema(name: String, namespace: String) { def subject = s"$namespace.$name" } sealed trait StreamType case object Notification extends StreamType case object CurrentState extends StreamType case object History extends StreamType case object Telemetry extends StreamType
Example 36
Source File: LogicalPlanPrinter.scala From airframe with Apache License 2.0 | 5 votes |
package wvlet.airframe.sql.model import java.io.{PrintWriter, StringWriter} import wvlet.log.LogSupport import wvlet.airframe.sql.model.LogicalPlan.EmptyRelation object LogicalPlanPrinter extends LogSupport { def print(m: LogicalPlan): String = { val s = new StringWriter() val p = new PrintWriter(s) print(m, p, 0) p.close() s.toString } def print(m: LogicalPlan, out: PrintWriter, level: Int): Unit = { m match { case EmptyRelation => // print nothing case _ => val ws = " " * level val inputAttr = m.inputAttributes.mkString(", ") val outputAttr = m.outputAttributes.mkString(", ") val attr = m.expressions.map(_.toString) val prefix = s"${ws}[${m.modelName}](${inputAttr}) => (${outputAttr})" attr.length match { case 0 => out.println(prefix) case _ => out.println(s"${prefix}") val attrWs = " " * (level + 1) val attrStr = attr.map(x => s"${attrWs}- ${x}").mkString("\n") out.println(attrStr) } for (c <- m.children) { print(c, out, level + 1) } } } }
Example 37
Source File: ScalaStanBaseSpec.scala From ScalaStan with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.cibo.scalastan import java.io.{PrintWriter, StringWriter} import com.cibo.scalastan.ast.StanParameterDeclaration import com.cibo.scalastan.run.{StanCompiler, StanRunner} import org.scalatest.{FunSpec, Matchers} trait ScalaStanBaseSpec extends FunSpec with Matchers { case class MockRunner(data: Vector[Map[String, Double]] = Vector.empty) extends StanRunner { private val results = scala.collection.mutable.ArrayBuffer[(String, Vector[Vector[String]])]() private def set(prefix: String, values: Any, mapping: Map[String, Double]): Map[String, Double] = { values match { case s: Seq[_] => s.zipWithIndex.foldLeft(mapping) { case (m, (v, i)) => set(s"$prefix.${i + 1}", v, m) } case _ => mapping + (prefix -> values.asInstanceOf[Double]) } } def set[T <: StanType](decl: StanParameterDeclaration[T], values: Seq[T#SCALA_TYPE]): MockRunner = { require(data.isEmpty || data.length == values.length) val dataBefore = if (data.isEmpty) values.map(_ => Map[String, Double]("lp__" -> 1)) else data val prefix = decl.emit val newData = values.zip(dataBefore).map { case (v, d) => set(prefix, v, d) } copy(data = newData.toVector) } def run(model: CompiledModel, chains: Int, seed: Int, cache: Boolean, method: RunMethod.Method): StanResults = { val mappedData: Map[String, Vector[Vector[Double]]] = data.flatten.groupBy(_._1).mapValues { grouped => val iterations = grouped.map { case (k, v) => v } Vector.fill(chains)(iterations) } StanResults(mappedData, Vector.empty, model, method) } } implicit object MockCompiler extends StanCompiler { def compile(model: StanModel): CompiledModel = CompiledModel( model = model, runner = MockRunner() ) } private def removeSpaces(str: String): String = str.replaceAllLiterally(" ", "").replaceAllLiterally("\n", "") private def removeNumbers(str: String): String = str.replaceAll("[0-9]+", "#") private def compare(actual: String, template: String, originalTemplate: String = ""): Boolean = { val fixedActual = removeNumbers(removeSpaces(actual)) val fixedExpected = removeNumbers(removeSpaces(template)) fixedActual.contains(fixedExpected) } def check(actual: String, template: String): Unit = { withClue(s"actual:\n$actual\nexpected:\n$template\n") { compare(actual, template) shouldBe true } } // Compare the code output of the model with a template. // Spaces are ignored and "#" in the template matches any integer. def checkCode(model: StanModel, template: String): Unit = { val sw = new StringWriter() model.emit(new PrintWriter(sw)) sw.close() check(sw.toString, template) } }
Example 38
Source File: SHASpec.scala From ScalaStan with BSD 3-Clause "New" or "Revised" License | 5 votes |
package com.cibo.scalastan import java.io.StringWriter class SHASpec extends ScalaStanBaseSpec { describe("SHA") { it("returns the expected result") { // Validated using "echo -n test | shasum" SHA.hash("test") shouldBe "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" } } describe("ShaWriter") { it("returns the expected result after close") { val sw = new StringWriter() val shaWriter = ShaWriter(sw) shaWriter.write("test\n") shaWriter.close() sw.toString shouldBe "test\n" shaWriter.sha.digest shouldBe "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83" } it("returns the expected result after flush") { val sw = new StringWriter() val shaWriter = ShaWriter(sw) shaWriter.write("test\n") shaWriter.flush() sw.toString shouldBe "test\n" shaWriter.sha.digest shouldBe "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83" } } }
Example 39
Source File: ResponseHelper.scala From ledger-manager-chrome with MIT License | 5 votes |
package co.ledger.wallet.core.net import java.io.{ByteArrayOutputStream, StringWriter} import java.nio.charset.Charset import org.json.{JSONArray, JSONObject} import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scala.scalajs.js import scala.util.{Failure, Success} object ResponseHelper { implicit class ResponseFuture(f: Future[HttpClient#Response]) { def json: Future[(JSONObject, HttpClient#Response)] = { f.string.map { case (body, response) => (new JSONObject(body), response) } } def jsonArray: Future[(JSONArray, HttpClient#Response)] = { f.string.map { case (body, response) => (new JSONArray(body), response) } } def string: Future[(String, HttpClient#Response)] = { f.bytes.map { case (body, response) => val writer = new StringWriter(body.length) body foreach {(char) => writer.append(char.toChar) } (writer.toString, response) } } def bytes: Future[(Array[Byte], HttpClient#Response)] = { f.map { response => val input = response.body val output = new ByteArrayOutputStream() val buffer = new Array[Byte](4096) var read = 0 while ({read = input.read(buffer); read} > 0) { output.write(buffer, 0, read) } val result = output.toByteArray input.close() output.close() (result, response) } } def noResponseBody: Future[HttpClient#Response] = { f.andThen { case Success(response) => response.body.close() response case Failure(cause) => throw cause } } } }
Example 40
Source File: UpdateChecker.scala From kotlin-plugin with MIT License | 5 votes |
package kotlin import java.io.{InputStreamReader, BufferedReader, StringWriter} import argonaut._, Argonaut._ import scala.concurrent.Future import scala.util.{Failure, Success} object UpdateChecker { import scala.concurrent.ExecutionContext.Implicits.global type Result = (Set[String],String) type Callback[A] = Either[Throwable,Result] => A def apply[A](user: String, repo: String, name: String)(result: Callback[A]): Unit = { val bintray = new java.net.URL( s"https://api.bintray.com/packages/$user/$repo/$name") Future { val uc = bintray.openConnection() val in = new BufferedReader(new InputStreamReader(uc.getInputStream, "utf-8")) try { val sw = new StringWriter val buf = Array.ofDim[Char](8192) Stream.continually(in.read(buf, 0, 8192)) takeWhile ( _ != -1) foreach (sw.write(buf, 0, _)) sw.toString } finally { in.close() } } onComplete { case Success(json) => val decoded = json.decode[PackageInfo] val res: Either[Throwable, Result] = decoded match { case Left(Left(str)) => Left(new IllegalArgumentException(str)) case Left(Right(cursorHistory)) => Left(new IllegalArgumentException(cursorHistory._1)) case Right(packageInfo) => Right(packageInfo.versions.toSet -> packageInfo.version) } result(res) case Failure(t) => result(Left(t)) } } implicit def PackageInfoCodecJson: CodecJson[PackageInfo] = casecodec3( PackageInfo.apply, PackageInfo.unapply)("name", "latest_version", "versions") case class PackageInfo(name: String, version: String, versions: List[String]) }
Example 41
Source File: PrometheusMarshallers.scala From akka-http-metrics with Apache License 2.0 | 5 votes |
package fr.davit.akka.http.metrics.prometheus.marshalling import java.io.StringWriter import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller} import akka.http.scaladsl.model.{ContentType, HttpCharsets, HttpEntity, MediaTypes} import fr.davit.akka.http.metrics.prometheus.PrometheusRegistry import io.prometheus.client.exporter.common.TextFormat trait PrometheusMarshallers { val PrometheusContentType: ContentType = { MediaTypes.`text/plain` withParams Map("version" -> "0.0.4") withCharset HttpCharsets.`UTF-8` } implicit val marshaller: ToEntityMarshaller[PrometheusRegistry] = { Marshaller.opaque { registry => val output = new StringWriter() try { TextFormat.write004(output, registry.underlying.metricFamilySamples) HttpEntity(output.toString).withContentType(PrometheusContentType) } finally { output.close() } } } } object PrometheusMarshallers extends PrometheusMarshallers
Example 42
Source File: DropwizardMarshallers.scala From akka-http-metrics with Apache License 2.0 | 5 votes |
package fr.davit.akka.http.metrics.dropwizard.marshalling import java.io.StringWriter import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller} import akka.http.scaladsl.model.{ContentTypes, HttpEntity} import com.fasterxml.jackson.databind.ObjectMapper import fr.davit.akka.http.metrics.dropwizard.DropwizardRegistry trait DropwizardMarshallers { implicit val registryToEntityMarshaller: ToEntityMarshaller[DropwizardRegistry] = { val writer = new ObjectMapper().writer() Marshaller.opaque { registry => val output = new StringWriter() try { writer.writeValue(output, registry.underlying) HttpEntity(output.toString).withContentType(ContentTypes.`application/json`) } finally { output.close() } } } } object DropwizardMarshallers extends DropwizardMarshallers
Example 43
Source File: MustacheMergeTool.scala From rug with GNU General Public License v3.0 | 5 votes |
package com.atomist.project.common.template import java.io.StringWriter import com.atomist.source.{ArtifactSource, FileArtifact, StringFileArtifact} import com.github.mustachejava.DefaultMustacheFactory import scala.collection.JavaConverters._ object MustacheMergeTool { val SupportedExtensions = Set( ".scaml", ".mustache" ) } class MustacheMergeTool(templateContent: ArtifactSource) extends MergeTool { import MustacheMergeTool._ private val mf = new DefaultMustacheFactory(new ArtifactSourceBackedMustacheResolver(templateContent.underPath(".atomist/templates"))) override def isTemplate(path: String): Boolean = SupportedExtensions.exists(extension => path.endsWith(extension)) override def mergeToFile(context: MergeContext, path: String): FileArtifact = templateContent.findFile(path) match { case None => throw new IllegalArgumentException(s"Template '$path' not found") case Some(template) => val templateOutput = new StringWriter val filePath = mergeString(context, toInPlaceFilePath(path)).replace(":", "/") val mustache = mf.compile(template.content) mustache.execute(templateOutput, context.map.asJava) StringFileArtifact(filePath, templateOutput.toString, template.mode, template.uniqueId) } override def mergeString(context: MergeContext, templateString: String): String = { val templateOutput = new StringWriter val mustache = mf.compile(templateString) mustache.execute(templateOutput, context.map.asJava) templateOutput.toString } private[template] def toInPlaceFilePath(path: String): String = { val extension = SupportedExtensions.find(extension => path.endsWith(s"_$extension")) if (extension.isDefined) path.dropRight(s"_${extension.get}".length) else path } }
Example 44
Source File: package.scala From temperature-machine with Apache License 2.0 | 5 votes |
package bad.robot import java.io.{File, PrintWriter, StringWriter} import cats.effect.IO import cats.syntax.either._ import io.circe._ import org.http4s.circe.{jsonEncoderWithPrinterOf, jsonOf} import io.circe.parser._ import org.http4s.{EntityDecoder, EntityEncoder} import scalaz.\/ import scalaz.syntax.std.either._ package object temperature { type Degrees = Int // NB not implicit as this seems to clash with http4s implicits that are kicking around def jsonDecoder[A: Decoder]: EntityDecoder[IO, A] = jsonOf[IO, A] def jsonEncoder[A: Encoder]: EntityEncoder[IO, A] = jsonEncoderWithPrinterOf(spaces2PlatformSpecific) def encode[A: Encoder](a: A): Json = Encoder[A].apply(a) // deprecated def decodeAsDisjunction[A: Decoder](value: String): temperature.Error \/ A = { decode(value) .leftMap(error => ParseError(error.getMessage)) .disjunction } def stackTraceOf(error: Throwable): String = { val writer = new StringWriter() error.printStackTrace(new PrintWriter(writer)) writer.toString } private val eol = sys.props("line.separator") val spaces2PlatformSpecific = Printer( preserveOrder = true , dropNullValues = false , indent = " " , lbraceLeft = "" , lbraceRight = eol , rbraceLeft = eol , rbraceRight = "" , lbracketLeft = "" , lbracketRight = eol , rbracketLeft = eol , rbracketRight = "" , lrbracketsEmpty = "" , arrayCommaLeft = "" , arrayCommaRight = eol , objectCommaLeft = "" , objectCommaRight = eol , colonLeft = " " , colonRight = " " ) implicit class JsonOps(json: Json) { def spaces2ps: String = spaces2PlatformSpecific.pretty(json) } implicit class FileOps(file: File) { def /(child: String): File = new File(file, child) } }
Example 45
Source File: SequenceSink.scala From eel-sdk with Apache License 2.0 | 5 votes |
package io.eels.component.sequence import java.io.StringWriter import com.univocity.parsers.csv.{CsvWriter, CsvWriterSettings} import io.eels.{Row, Sink, SinkWriter} import io.eels.schema.StructType import org.apache.hadoop.conf.Configuration import org.apache.hadoop.fs.Path import org.apache.hadoop.io.{BytesWritable, IntWritable, SequenceFile} case class SequenceSink(path: Path)(implicit conf: Configuration) extends Sink { override def open(schema: StructType): SinkWriter = new SequenceSinkWriter(schema, path) class SequenceSinkWriter(schema: StructType, path: Path) extends SinkWriter { val writer = SequenceFile.createWriter(conf, SequenceFile.Writer.file(path), SequenceFile.Writer.keyClass(classOf[IntWritable]), SequenceFile.Writer.valueClass(classOf[BytesWritable]) ) val key = new IntWritable(0) val headers = valuesToCsv(schema.fieldNames()) writer.append(key, new BytesWritable(headers.getBytes)) override def close(): Unit = writer.close() override def write(row: Row): Unit = { this.synchronized { val csv = valuesToCsv(row.values) writer.append(key, new BytesWritable(csv.getBytes())) key.set(key.get() + 1) } } private def valuesToCsv(values: Seq[Any]): String = { val swriter = new StringWriter() val csv = new CsvWriter(swriter, new CsvWriterSettings()) csv.writeRow(values.map { case null => null case other => other.toString }: _*) csv.close() swriter.toString().trim() } } }
Example 46
Source File: PersistentActorWithNotifications.scala From reliable-http-client with Apache License 2.0 | 5 votes |
package rhttpc.akkapersistence.impl import java.io.{PrintWriter, StringWriter} import akka.actor.{ActorLogging, ActorRef} import akka.persistence._ private[akkapersistence] trait PersistentActorWithNotifications { this: AbstractSnapshotter with ActorLogging => override def persistenceId: String = SnapshotsRegistry.persistenceId(persistenceCategory, id) protected def persistenceCategory: String protected def id: String private var listenersForSnapshotSave: Map[Long, RecipientWithMsg] = Map.empty protected def deleteSnapshotsLogging(): Unit = { deleteSnapshotsLogging(None) } private def deleteSnapshotsLogging(maxSequenceNr: Option[Long]): Unit = { log.debug(s"Deleting all snapshots for $persistenceId until (inclusive): $maxSequenceNr...") deleteSnapshots(SnapshotSelectionCriteria(maxSequenceNr = maxSequenceNr.getOrElse(Int.MaxValue))) } protected def saveSnapshotNotifying(snapshot: Any, sequenceNr: Long, listener: Option[RecipientWithMsg]): Unit = { log.debug(s"Saving snapshot for $persistenceId with sequenceNr: $sequenceNr: $snapshot ...") listener.foreach { listener => listenersForSnapshotSave += sequenceNr -> listener } saveSnapshotWithSeqNr(snapshot, sequenceNr) } protected val handleSnapshotEvents: Receive = { case SaveSnapshotSuccess(metadata) => log.debug(s"State saved for: $metadata") deleteSnapshotsLogging(Some(metadata.sequenceNr-1)) replyToListenerForSaveIfWaiting(metadata) case DeleteSnapshotsSuccess(criteria) => log.debug(s"Snapshots with criteria: $criteria deleted") case SaveSnapshotFailure(metadata, cause) => log.error(cause, s"State save failure for: $metadata") case DeleteSnapshotsFailure(criteria, cause) => log.warning(s"Delete snapshots with criteria failure: $criteria.\nError: ${printStackTrace(cause)}") } private def printStackTrace(cause: Throwable): String = { val stringWriter = new StringWriter() val printWriter = new PrintWriter(stringWriter) cause.printStackTrace(printWriter) stringWriter.toString } private def replyToListenerForSaveIfWaiting(metadata: SnapshotMetadata): Unit = { listenersForSnapshotSave.get(metadata.sequenceNr).foreach { listener => listener.reply() listenersForSnapshotSave -= metadata.sequenceNr } } } class RecipientWithMsg(recipient: ActorRef, msg: Any) { def reply() = recipient ! msg }
Example 47
Source File: TypeExpansionHarness.scala From Converter with GNU General Public License v3.0 | 5 votes |
package org.scalablytyped.converter.internal package ts import java.io.StringWriter import com.olvind.logging import com.olvind.logging.Logger import org.scalablytyped.converter.Selection import org.scalablytyped.converter.internal.importer.Phase1ReadTypescript import org.scalablytyped.converter.internal.ts.parser.TsParser import org.scalablytyped.converter.internal.ts.transforms.SetCodePath import scala.reflect.ClassTag import scala.util.control.NonFatal trait TypeExpansionHarness { val parser = new TsParser(None) val libName = TsIdentLibrarySimple("testing") def Transformations(logger: Logger[Unit]): List[TsParsedFile => TsParsedFile] = Phase1ReadTypescript.Pipeline( scope = TsTreeScope(libName, pedantic = true, Map(), logger), libName = libName, expandTypeMappings = Selection.All, involvesReact = true, ) def run(input: String): TsParsedFile = { val parsed = parser(input).get val logger = logging.stringWriter() val withCodePath = SetCodePath.visitTsParsedFile(CodePath.HasPath(libName, TsQIdent.empty))(parsed) try Transformations(logger.void).foldLeft(withCodePath) { case (acc, f) => f(acc) } catch { case NonFatal(th) => println(logger.underlying.toString) throw th } } implicit class Extractor(x: TsParsedFile) { def extract[T: ClassTag](name: String): T = x.membersByName.get(TsIdentSimple(name)) match { case Some(ofName) => val ofType = ofName.collectFirst { case x: T => x } ofType match { case Some(found) => found case None => sys.error(s"Could not find ${implicitly[ClassTag[T]].runtimeClass} among $ofType") } case None => sys.error(s"No member with name $name among ${x.membersByName.keys}") } } }
Example 48
Source File: package.scala From Converter with GNU General Public License v3.0 | 5 votes |
package com.olvind import java.io.{StringWriter, Writer} import com.olvind.logging.Logger.{AppendableLogger, Stored, StoringLogger, WriterLogger} import fansi.Str package object logging { type Ctx = Map[Str, Str] private[logging] val emptyContext: Ctx = Map.empty def stdout: Logger[Unit] = appendable(System.out).void def appendable[A <: Appendable]( appendable: A, pattern: Pattern = Pattern.default, ctx: Ctx = emptyContext, ): Logger[A] = new AppendableLogger(appendable, pattern, ctx) def writer[W <: Writer]( writer: W = System.out, pattern: Pattern = Pattern.default, ctx: Ctx = emptyContext, ): Logger[W] = new WriterLogger(new AppendableLogger(writer, pattern, ctx)) def stringWriter(pattern: Pattern = Pattern.default, ctx: Ctx = emptyContext): Logger[StringWriter] = writer(new StringWriter) def storing(ctx: Ctx = emptyContext): Logger[Array[Stored]] = new StoringLogger(new Logger.Store, ctx) }
Example 49
Source File: Pattern.scala From Converter with GNU General Public License v3.0 | 5 votes |
package com.olvind.logging import java.io.{File, PrintWriter, StringWriter} import fansi.{Color, EscapeAttr, Str} import sourcecode.Text trait Pattern { def apply[T: Formatter](t: => Text[T], throwable: Option[Throwable], metadata: Metadata, ctx: Ctx): Str } object Pattern { def prefixFor(l: LogLevel): String = f"[${l.name.value}%-5s]" @inline def colorFor(l: LogLevel): EscapeAttr = l.level match { case LogLevel.trace.level => Color.Reset case LogLevel.debug.level => Color.Green case LogLevel.info.level => Color.Blue case LogLevel.warn.level => Color.Yellow case LogLevel.error.level => Color.Red } @inline def subtleColorFor(l: LogLevel): EscapeAttr = l.level match { case LogLevel.trace.level => Color.Reset case LogLevel.debug.level => Color.LightGreen case LogLevel.info.level => Color.LightBlue case LogLevel.warn.level => Color.LightYellow case LogLevel.error.level => Color.LightRed } def formatThrowable(th: Throwable): String = { val sw = new StringWriter() val pw = new PrintWriter(sw) th.printStackTrace(pw) sw.toString } object default extends Pattern { override def apply[T: Formatter](t: => Text[T], throwable: Option[Throwable], m: Metadata, ctx: Ctx): Str = { val Color = colorFor(m.logLevel) val Subtle = subtleColorFor(m.logLevel) val source = if (t.source.startsWith("\"") || t.source.startsWith("s\"")) "" else t.source Str.join( Color(prefixFor(m.logLevel)), " ", Subtle(m.instant.toString), " ", Subtle(Formatter(new File(m.file.value))), ":", Subtle(Formatter(m.line.value)), " ", Color(source), " ", Color(Formatter(t.value)), " ", Subtle(Formatter(ctx)), throwable match { case None => "" case Some(th) => Subtle(formatThrowable(th)) }, ) } } }
Example 50
Source File: package.scala From zio-metrics with Apache License 2.0 | 5 votes |
package zio.metrics.prometheus import zio.{ Has, Layer, Task, ZLayer } import io.prometheus.client.CollectorRegistry import io.prometheus.client.exporter.{ HTTPServer, PushGateway } import io.prometheus.client.bridge.Graphite import io.prometheus.client.exporter.common.TextFormat import io.prometheus.client.exporter.HttpConnectionFactory import io.prometheus.client.exporter.BasicAuthHttpConnectionFactory import io.prometheus.client.hotspot.DefaultExports import java.net.InetSocketAddress import java.io.StringWriter package object exporters { type Exporters = Has[Exporters.Service] object Exporters { trait Service { def http(r: CollectorRegistry, port: Int): Task[HTTPServer] def graphite(r: CollectorRegistry, host: String, port: Int, intervalSeconds: Int): Task[Thread] def pushGateway( r: CollectorRegistry, hots: String, port: Int, jobName: String, user: Option[String], password: Option[String], httpConnectionFactory: Option[HttpConnectionFactory] ): Task[Unit] def write004(r: CollectorRegistry): Task[String] def initializeDefaultExports(r: CollectorRegistry): Task[Unit] } val live: Layer[Nothing, Exporters] = ZLayer.succeed(new Service { def http(r: CollectorRegistry, port: Int): zio.Task[HTTPServer] = Task { new HTTPServer(new InetSocketAddress(port), r) } def graphite(r: CollectorRegistry, host: String, port: Int, intervalSeconds: Int): Task[Thread] = Task { val g = new Graphite(host, port) g.start(r, intervalSeconds) } def pushGateway( r: CollectorRegistry, host: String, port: Int, jobName: String, user: Option[String], password: Option[String], httpConnectionFactory: Option[HttpConnectionFactory] ): Task[Unit] = Task { val pg = new PushGateway(s"$host:$port") if (user.isDefined) for { u <- user p <- password } yield pg.setConnectionFactory(new BasicAuthHttpConnectionFactory(u, p)) else if (httpConnectionFactory.isDefined) for { conn <- httpConnectionFactory } yield pg.setConnectionFactory(conn) pg.pushAdd(r, jobName) } def write004(r: CollectorRegistry): Task[String] = Task { val writer = new StringWriter TextFormat.write004(writer, r.metricFamilySamples) writer.toString } def initializeDefaultExports(r: CollectorRegistry): Task[Unit] = Task(DefaultExports.initialize()) }) def stopHttp(server: HTTPServer): Task[Unit] = Task(server.stop()) } }
Example 51
Source File: SerializedException.scala From ArchiveSpark with MIT License | 5 votes |
package org.archive.archivespark.util import java.io.{PrintWriter, StringWriter} case class SerializedException (exception: String, message: String, stackTrace: String) { def print(): Unit = { println(exception + ": " + message) println(stackTrace) } } object SerializedException { def apply(ex: Exception): SerializedException = { val stackTrace = new StringWriter() ex.printStackTrace(new PrintWriter(stackTrace)) new SerializedException(ex.getClass.getCanonicalName, ex.getMessage, stackTrace.toString) } }
Example 52
Source File: Interpreter.scala From sjsonnet with Apache License 2.0 | 5 votes |
package sjsonnet import java.io.{PrintWriter, StringWriter} import fastparse.Parsed import sjsonnet.Expr.Params class Interpreter(parseCache: collection.mutable.Map[String, fastparse.Parsed[(Expr, Map[String, Int])]], extVars: Map[String, ujson.Value], tlaVars: Map[String, ujson.Value], wd: Path, importer: (Path, String) => Option[(Path, String)], preserveOrder: Boolean = false) { val evaluator = new Evaluator( parseCache, extVars, wd, importer, preserveOrder ) def interpret(txt: String, path: Path): Either[String, ujson.Value] = { interpret0(txt, path, ujson.Value) } def interpret0[T](txt: String, path: Path, visitor: upickle.core.Visitor[T, T]): Either[String, T] = { for{ res <- parseCache.getOrElseUpdate(txt, fastparse.parse(txt, Parser.document(_))) match{ case f @ Parsed.Failure(l, i, e) => Left("Parse error: " + f.trace().msg) case Parsed.Success(r, index) => Right(r) } (parsed, nameIndices) = res _ = evaluator.loadedFileContents(path) = txt res0 <- try Right( evaluator.visitExpr(parsed)( Std.scope(nameIndices.size + 1), new FileScope(path, nameIndices) ) ) catch{case e: Throwable => val s = new StringWriter() val p = new PrintWriter(s) e.printStackTrace(p) p.close() Left(s.toString.replace("\t", " ")) } res = res0 match{ case f: Val.Func => f.copy(params = Params(f.params.args.map{ case (k, default, i) => (k, tlaVars.get(k) match{ case None => default case Some(v) => Some(Materializer.toExpr(v)) }, i) })) case x => x } json <- try Right(Materializer.apply0(res, visitor)(evaluator)) catch{ case Error.Delegate(msg) => Left(msg) case e: Throwable => val s = new StringWriter() val p = new PrintWriter(s) e.printStackTrace(p) p.close() Left(s.toString.replace("\t", " ")) } } yield json } }
Example 53
Source File: Utilities.scala From project-matt with MIT License | 5 votes |
package org.datafy.aws.app.matt.extras import org.apache.tika.Tika import org.apache.tika.metadata.Metadata import java.io.{BufferedInputStream, IOException, InputStream, StringWriter} import java.util.zip.GZIPInputStream import org.xml.sax.SAXException import org.apache.tika.exception.TikaException import org.apache.tika.metadata.serialization.JsonMetadata import org.apache.tika.parser.{AutoDetectParser, ParseContext} import org.apache.tika.parser.pkg.CompressorParser import org.apache.tika.sax.BodyContentHandler object Utilities { private val MAX_STRING_LENGTH = 2147483647 private val tika = new Tika() tika.setMaxStringLength(MAX_STRING_LENGTH) @throws(classOf[IOException]) @throws(classOf[SAXException]) @throws(classOf[TikaException]) def getParsePlainStream(inputStream: InputStream): String = { val autoDetectParser = new AutoDetectParser() val bodyContentHandler = new BodyContentHandler(MAX_STRING_LENGTH) val fileMetadata = new Metadata() if (inputStream.read() == -1) { return "Could not scan inputStream less than 0 bytes" } autoDetectParser.parse(inputStream, bodyContentHandler, fileMetadata) bodyContentHandler.toString } @throws(classOf[IOException]) @throws(classOf[SAXException]) @throws(classOf[TikaException]) def getParseCompressedStream(inputStream: InputStream) = { var inputStream = myStream if(!inputStream.markSupported()) { inputStream = new BufferedInputStream(inputStream) } inputStream.mark(2) var magicBytes = 0 try { magicBytes = inputStream.read() & 0xff | ((inputStream.read() << 8) & 0xff00) inputStream.reset() } catch { case ioe: IOException => ioe.printStackTrace() } magicBytes == GZIPInputStream.GZIP_MAGIC } } case class And[A]( p1: A=>Boolean, p2: A=>Boolean ) extends (A=>Boolean) { def apply( a: A ) = p1(a) && p2(a) } case class Or[A]( p1: A=>Boolean, p2: A=>Boolean ) extends (A=>Boolean) { def apply( a: A ) = p1(a) || p2(a) }
Example 54
Source File: MetricsService.scala From finagle-prometheus with MIT License | 5 votes |
package com.samstarling.prometheusfinagle.metrics import java.io.StringWriter import com.twitter.finagle.Service import com.twitter.finagle.http.{Request, Response, Status} import com.twitter.util.Future import io.prometheus.client.CollectorRegistry import io.prometheus.client.exporter.common.TextFormat class MetricsService(registry: CollectorRegistry) extends Service[Request, Response] { override def apply(request: Request): Future[Response] = { val writer = new StringWriter TextFormat.write004(writer, registry.metricFamilySamples()) val response = Response(request.version, Status.Ok) response.setContentString(writer.toString) Future(response) } }
Example 55
Source File: GatherActor.scala From typebus with MIT License | 5 votes |
package io.surfkit.typebus.actors import java.io.{PrintWriter, StringWriter} import java.time.Instant import java.util.UUID import akka.actor._ import akka.cluster.Cluster import akka.util.Timeout import io.surfkit.typebus.bus.Publisher import io.surfkit.typebus.{AvroByteStreams, ByteStreamReader, ByteStreamWriter} import io.surfkit.typebus.event._ import scala.reflect.ClassTag import scala.util.Try object GatherActor{ //def props[T, U](producer: Publisher[T], timeout: Timeout, writer: ByteStreamWriter[T], reader: ByteStreamReader[U]): Props = Props(classOf[GatherActor[T, U]], producer, timeout, writer) class GatherActor[T : ClassTag, U : ClassTag](serviceIdentifier: ServiceIdentifier, producer: Publisher, timeout: Timeout, writer: ByteStreamWriter[T], reader: ByteStreamReader[U]) extends Actor with ActorLogging with AvroByteStreams{ implicit val system = context.system import system.dispatcher val cluster = Cluster(context.system) val correlationId = UUID.randomUUID().toString log.debug(s"adding http actor ${self.path.toStringWithoutAddress}") def clusterPath = s"${cluster.selfAddress}${self.path.toStringWithoutAddress}" var replyTo:ActorRef = null val cancel = context.system.scheduler.scheduleOnce(timeout.duration){ log.warning("GatherActor timeout ... shutting down!") context.stop(self) } def receive = { case msg: GatherActor.Request[T] => replyTo = context.sender() val meta = EventMeta( eventId = UUID.randomUUID().toString, eventType = EventType.parse(msg.data.getClass.getCanonicalName), directReply = Some(RpcClient(clusterPath, serviceIdentifier)), correlationId = Some(correlationId) ) try { log.info(s"[GatherActor] publish ${msg.data}") val outEvent = PublishedEvent( meta = meta, payload = writer.write(msg.data) ) producer.publish(outEvent) producer.traceEvent(OutEventTrace(producer.serviceIdentifier, outEvent), outEvent.meta) }catch{ case t:Exception => producer.produceErrorReport(t,meta) cancel.cancel() context.stop(self) } case x:PublishedEvent => log.info(s"GatherActor got reply.... ${x.meta.eventType}") try{ log.info(s"GatherActor try to deserialize reader: ${reader} for type: ${x.meta.eventType}") val responsePayload = Try(reader.read(x.payload)).toOption.getOrElse( ServiceExceptionReader.read(x.payload) ) replyTo ! responsePayload producer.traceEvent(InEventTrace(producer.serviceIdentifier, x), x.meta) }catch{ case t: Throwable => t.printStackTrace() val tType = scala.reflect.classTag[T].runtimeClass.getCanonicalName log.error(s"Gather actor failed to reply for response: ${tType}", t) replyTo ! producer.produceErrorReport(t, x.meta) }finally { cancel.cancel() context.stop(self) } case x => log.warning(s"GatherActor got Wrong message type ${x.getClass.getSimpleName}") cancel.cancel() context.stop(self) } override def postStop() { log.debug(s"GatherActor ACTOR STOP !!! ${self.path.toStringWithoutAddress}") } }
Example 56
Source File: GeneralFunctions.scala From spark-bench with Apache License 2.0 | 5 votes |
package com.ibm.sparktc.sparkbench.utils import java.io.StringWriter import scala.util.{Failure, Random, Success, Try} object GeneralFunctions { val any2Long: (Any) => Long = { case x: Number => x.longValue case x => x.toString.toLong } private def defaultFn[A](any: Any): A = any.asInstanceOf[A] private[utils] def tryWithDefault[A](a: Any, default: A, func: Any => A): A = Try(func(a)) match { case Success(b) => b case Failure(_) => default } def getOrDefaultOpt[A](opt: Option[Any], default: A, func: Any => A = defaultFn[A](_: Any)): A = { opt.map(tryWithDefault(_, default, func)).getOrElse(default) } def getOrDefault[A](map: Map[String, Any], name: String, default: A, func: Any => A = defaultFn[A](_: Any)): A = { getOrDefaultOpt(map.get(name), default, func) } def time[R](block: => R): (Long, R) = { val t0 = System.nanoTime() val result = block // call-by-name val t1 = System.nanoTime() (t1 - t0, result) } // https://gist.github.com/lauris/7dc94fb29804449b1836 def ccToMap(cc: AnyRef): Map[String, Any] = (Map[String, Any]() /: cc.getClass.getDeclaredFields) { (a, f) => f.setAccessible(true) a + (f.getName -> f.get(cc)) } def verifyOrThrow[A]( m: Map[String, Any], key: String, whatItShouldBe: A, errorMessage: String): A = m.get(key) match { case None => throw SparkBenchException(s"Required key not found: $key") case Some(`whatItShouldBe`) => whatItShouldBe.asInstanceOf[A] case _ => throw SparkBenchException(errorMessage) } def getOrThrow[A](opt: Option[A], msg: String = "Empty option"): A = opt match { case Some(x) => x case _ => throw SparkBenchException(s"Error: $msg") } def getOrThrow(m: Map[String, Any], key: String): Any = getOrThrow(m.get(key)) def getOrThrowT[T](m: Map[String, Any], key: String): T = getOrThrow(m.get(key)).asInstanceOf[T] def optionallyGet[A](m: Map[String, Any], key: String): Option[A] = m.get(key).map { any => any.asInstanceOf[A] } def stringifyStackTrace(e: Throwable): String = { import java.io.PrintWriter val sw = new StringWriter() val pw = new PrintWriter(sw) e.printStackTrace(pw) sw.toString } def randomLong(max: Long): Long = { val start = 0L (start + (Random.nextDouble() * (max - start) + start)).toLong } }
Example 57
Source File: TnCmd.scala From TopNotch with Apache License 2.0 | 5 votes |
package com.bfm.topnotch.tnengine import java.io.{PrintWriter, StringWriter} import org.json4s._ import org.json4s.native.Serialization import org.json4s.native.Serialization.writePretty /** * A command for TnEngine to run */ abstract class TnCmd { val outputKey: String /** Whether to cache the resulting dataframe in memory. This should be a boolean defaulting to false, * but json4s has a problem with default values other than None for option. Change it to a default value if json4s * solves the bug. */ val cache: Option[Boolean] val outputPath: Option[String] /** If writing the output in hdfs, the name of the table to mount, otherwise none. Note: this will be ignored if * outputPath is not specified. */ val tableName: Option[String] implicit val formats = Serialization.formats(NoTypeHints) /** * Overriding toString to making output of unit tests that have cmds in error logs easier to understand */ override def toString = writePretty(this) } /** * The input to a command * @param ref The reference to the data set, either the path on hdfs or the name in the lookup table * @param onDisk Whether the input data set is stored on disk * @param delimiter The delimiter for plain text, delimited files. Leave to empty string for parquet. */ case class Input(ref: String, onDisk: Boolean, delimiter: Option[String] = None) /** * The strings used for converting a config file into a TnCmd */ object TnCmdStrings { val ioNamespace = "io" val commandListStr = "commands" val writerStr = "writer" val commandStr = "command" val paramsStr = "params" val externalParamsStr = "externalParamsFile" val outputKeyStr = "outputKey" val writeToDiskStr = "writeToDisk" val outputPathStr = "outputPath" } /** * The class indicating that there was at least one error in the configuration for this command * @param cmdString The JSON string for the command. * @param errorStr The errors encountered in creating this command. * @param cmdIdx The index of the command in the plan that failed * @param outputKey This is meaningless in this class. This exists only so that TnErrorCmd can extend TnCmd. * @param writeToDisk This is meaningless in this class. This exists only so that TnErrorCmd can extend TnCmd. * @param outputPath This is meaningless in this class. This exists only so that TnErrorCmd can extend TnCmd. */ case class TnErrorCmd ( cmdString: String, errorStr: String, cmdIdx: Int, outputKey: String = "", cache: Option[Boolean] = None, writeToDisk: Boolean = false, outputPath: Option[String] = None, tableName: Option[String] = None ) extends TnCmd { override def toString: String = { s"There was an error with the command in position ${cmdIdx} in its plan. The command was: \n ${cmdString} \n " + s"The message was: \n ${errorStr} \n\n END OF ERROR MESSAGE FOR COMMAND IN POSITION ${cmdIdx} \n\n" } } object TnErrorCmd { /** * Helper method for easily getting the stack trace of an exception as a string * @param e The exception * @return The exception's stack trace */ def getExceptionStackTrace(e: Exception): String = { val sw = new StringWriter e.printStackTrace(new PrintWriter(sw)) sw.toString } }
Example 58
Source File: VelocityUtils.scala From InteractiveGraph-neo4j with BSD 2-Clause "Simplified" License | 5 votes |
package org.grapheco.server.util import java.io.{File, FileOutputStream, StringWriter} import java.util.Properties import cn.pidb.blob.Blob import cn.pidb.engine.blob.{BlobIO, InlineBlob, RemoteBlob} import org.apache.velocity.app.VelocityEngine import org.apache.velocity.tools.ToolManager import org.apache.velocity.tools.config.DefaultKey import org.neo4j.values.storable.{BlobValue, ValueWriter} import org.springframework.util.ClassUtils import scala.collection.JavaConversions import java.io.FileOutputStream import java.io.IOException object VelocityUtils { val pro = new Properties(); val toolManager = new ToolManager(); toolManager.configure("tools.xml"); pro.setProperty("input.encoding", "UTF-8"); pro.setProperty("output.encoding", "UTF-8"); val ve = new VelocityEngine(pro); val props = new Properties() props.put("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem") props.put("runtime.log.logsystem.log4j.category", "velocity") props.put("runtime.log.logsystem.log4j.logger", "velocity") ve.init(props) def parse(expr: String, context: Map[String, Any]): Any = { val vc = toolManager.createContext(); val writer = new StringWriter(); context.foreach(kv => vc.put(kv._1, //is a scala Map? if (kv._2.isInstanceOf[Map[_, _]]) { JavaConversions.mapAsJavaMap(kv._2.asInstanceOf[Map[_, _]]) } else { kv._2 })); try { if (expr.startsWith("=")) { val expr1 = expr.substring(1); ve.evaluate(vc, writer, "", s"#set($$__VAR=$expr1)"); var value = vc.get("__VAR"); //if is a blob if(value.isInstanceOf[Blob]){ //get blob var result:String = "" try { val data = value.asInstanceOf[Blob].toBytes() val path = ClassUtils.getDefaultClassLoader.getResource("").getPath.replace("/WEB-INF/classes","") + "static/" val tool = new FileSystemTool() result = tool.filesave(data,path, System.currentTimeMillis.toString+".jpg") } catch{ case e:Throwable => print(e.toString) } //TODO url return "http://localhost:9999/graphserver/static/"+result } return value } else { ve.evaluate(vc, writer, "", expr); writer.getBuffer.toString.trim } } catch { case e: Throwable => throw new WrongExpressionException(expr, e); } } } class WrongExpressionException(msg: String, e: Throwable) extends RuntimeException(msg, e) { } @DefaultKey("fileTool") class FileSystemTool { def exists(path: String) = new File(path).exists(); @throws[IOException] def filesave(file: Array[Byte], filePath: String, fileName: String): String = { //目标目录 val targetfile = new File(filePath) if (!targetfile.exists) targetfile.mkdirs //二进制流写入 val out = new FileOutputStream(filePath + fileName) out.write(file) out.flush() out.close() return fileName } }
Example 59
Source File: ScalaObjectHandlerTest.scala From fintrospect with Apache License 2.0 | 5 votes |
package io.fintrospect.templating import java.io.{StringReader, StringWriter} import java.util.concurrent.Callable import com.github.mustachejava.DefaultMustacheFactory import org.scalatest.{FunSpec, Matchers} class ScalaObjectHandlerTest extends FunSpec with Matchers { describe("ScalaObjectHandler") { it("maps") { render("{{#map}}{{test}}{{test2}}{{/map}}", Map("map" -> Map("test" -> "fred"))) shouldBe "fred" } it("handler") { val model = new { val list = Seq(new { lazy val optionalHello = Some("Hello") val futureWorld = new Callable[String] { def call(): String = "world" } val test = true val num = 0 }, new { val optionalHello = Some("Goodbye") val futureWorld = new Callable[String] { def call(): String = "thanks for all the fish" } lazy val test = false val map = Map("value" -> "test") val num = 1 }) } render("{{#list}}{{optionalHello}}, {{futureWorld}}!" + "{{#test}}?{{/test}}{{^test}}!{{/test}}{{#num}}?{{/num}}{{^num}}!{{/num}}" + "{{#map}}{{value}}{{/map}}\n{{/list}}", model) shouldBe "Hello, world!?!\nGoodbye, thanks for all the fish!!?test\n" } it("steams") { val model = new { val stream = Stream( new { val value = "hello" }, new { val value = "world" }) } render("{{#stream}}{{value}}{{/stream}}", model) shouldBe "helloworld" } it("unit") { val model = new { val test = if (false) "test" } render("{{test}}", model) shouldBe "" } it("options") { val model = new { val foo = Some("Hello") val bar = None } render("{{foo}}{{bar}}", model) shouldBe "Hello" } } private def render(template: String, model: Any): String = { val mf = new DefaultMustacheFactory() mf.setObjectHandler(new ScalaObjectHandler) val m = mf.compile(new StringReader(template), "name") val sw = new StringWriter m.execute(sw, model).close() sw.toString } }
Example 60
Source File: CsvRowsOps.scala From kantan.csv with Apache License 2.0 | 5 votes |
package kantan.csv package ops import engine.WriterEngine import java.io.StringWriter def asCsv(conf: CsvConfiguration)(implicit e: WriterEngine): String = { val out = new StringWriter() CsvWriter(out, conf).write(as).close() out.toString } } trait ToCsvRowsOps { implicit def toCsvRowsOps[A: HeaderEncoder](as: IterableOnce[A]): CsvRowsOps[A] = new CsvRowsOps(as) } object csvRows extends ToCsvRowsOps
Example 61
Source File: CsvRowsOps.scala From kantan.csv with Apache License 2.0 | 5 votes |
package kantan.csv package ops import engine.WriterEngine import java.io.StringWriter def asCsv(conf: CsvConfiguration)(implicit e: WriterEngine): String = { val out = new StringWriter() CsvWriter(out, conf).write(as).close() out.toString } } trait ToCsvRowsOps { implicit def toCsvRowsOps[A: HeaderEncoder](as: TraversableOnce[A]): CsvRowsOps[A] = new CsvRowsOps(as) } object csvRows extends ToCsvRowsOps
Example 62
Source File: RawJson.scala From tethys with Apache License 2.0 | 5 votes |
package tethys.commons import java.io.StringWriter import tethys.readers.FieldName import tethys.readers.tokens.TokenIterator import tethys.writers.tokens.{TokenWriter, TokenWriterProducer} import tethys.{JsonReader, JsonStreaming, JsonWriter} final case class RawJson(json: String) object RawJson { implicit val rawJsonWriter: JsonWriter[RawJson] = new JsonWriter[RawJson] { override def write(value: RawJson, tokenWriter: TokenWriter): Unit = tokenWriter.writeRawJson(value.json) } implicit def rawJsonReader(implicit tokenWriterProducer: TokenWriterProducer): JsonReader[RawJson] = new JsonReader[RawJson] { override def read(it: TokenIterator)(implicit fieldName: FieldName): RawJson = { val stringWriter = new StringWriter() val tokenWriter: TokenWriter = tokenWriterProducer.forWriter(stringWriter) JsonStreaming.streamValue(it, tokenWriter) tokenWriter.flush() RawJson(stringWriter.toString) } } }
Example 63
Source File: package.scala From tethys with Apache License 2.0 | 5 votes |
import java.io.{Reader, StringReader, StringWriter, Writer} import tethys.readers.{FieldName, ReaderError} import tethys.readers.tokens.{TokenIterator, TokenIteratorProducer} import tethys.writers.tokens.{TokenWriter, TokenWriterProducer} import scala.Specializable.Group package object tethys { final val specializations = new Group((Short, Int, Long, Float, Double, Boolean)) implicit class JsonWriterOps[A](val a: A) extends AnyVal { def asJson(implicit jsonWriter: JsonWriter[A], tokenWriterProducer: TokenWriterProducer): String = { val stringWriter = new StringWriter() writeJson(tokenWriterProducer.forWriter(stringWriter)) stringWriter.toString } def asJsonWith(jsonWriter: JsonWriter[A])(implicit tokenWriterProducer: TokenWriterProducer): String = { asJson(jsonWriter, tokenWriterProducer) } def writeJson(tokenWriter: TokenWriter)(implicit jsonWriter: JsonWriter[A]): Unit = { try jsonWriter.write(a, tokenWriter) finally { tokenWriter.flush() } } } implicit class WriterOps(val w: Writer) extends AnyVal { def toTokenWriter(implicit tokenWriterProducer: TokenWriterProducer): TokenWriter = tokenWriterProducer.forWriter(w) } implicit class StringReaderOps(val json: String) extends AnyVal { def jsonAs[A](implicit jsonReader: JsonReader[A], producer: TokenIteratorProducer): Either[ReaderError, A] = { new StringReader(json).readJson[A] } def toTokenIterator(implicit producer: TokenIteratorProducer): Either[ReaderError, TokenIterator] = { new StringReader(json).toTokenIterator } } implicit class ReaderReaderOps(val reader: Reader) extends AnyVal { def readJson[A](implicit jsonReader: JsonReader[A], producer: TokenIteratorProducer): Either[ReaderError, A] = { implicit val root: FieldName = FieldName() producer.fromReader(reader).right.flatMap(_.readJson[A]) } def readJsonWith[A](jsonReader: JsonReader[A])(implicit producer: TokenIteratorProducer): Either[ReaderError, A] = { readJson[A](jsonReader, producer) } def toTokenIterator(implicit producer: TokenIteratorProducer): Either[ReaderError, TokenIterator] = { producer.fromReader(reader) } } implicit class TokenIteratorOps(val tokenIterator: TokenIterator) extends AnyVal { def readJson[A](implicit jsonReader: JsonReader[A]): Either[ReaderError, A] = { implicit val fieldName: FieldName = FieldName() ReaderError.catchNonFatal(jsonReader.read(tokenIterator)) } } }
Example 64
Source File: JacksonTokenWriterTest.scala From tethys with Apache License 2.0 | 5 votes |
package tethys.jackson import java.io.StringWriter import org.scalatest.matchers.should.Matchers import org.scalatest.flatspec.AnyFlatSpec import tethys._ import tethys.writers.tokens.TokenWriter class JacksonTokenWriterTest extends AnyFlatSpec with Matchers { def iterate(fun: (TokenWriter) => Unit): String = { val sw = new StringWriter() val tokenWriter = sw.toTokenWriter fun(tokenWriter) tokenWriter.close() sw.toString } behavior of "JacksonTokenWriter" it should "write String value" in { iterate(_.writeString("string")) shouldBe """"string"""" } it should "write Short value" in { iterate(_.writeNumber(1: Short)) shouldBe """1""" } it should "write Int value" in { iterate(_.writeNumber(1: Int)) shouldBe """1""" } it should "write Long value" in { iterate(_.writeNumber(1: Long)) shouldBe """1""" } it should "write Float value" in { iterate(_.writeNumber(1: Float)) shouldBe """1.0""" } it should "write Double value" in { iterate(_.writeNumber(1: Double)) shouldBe """1.0""" } it should "write BitInt value" in { iterate(_.writeNumber(1: BigInt)) shouldBe """1""" } it should "write BigDecimal value" in { iterate(_.writeNumber(1: BigDecimal)) shouldBe """1""" } it should "write true value" in { iterate(_.writeBoolean(true)) shouldBe """true""" } it should "write false value" in { iterate(_.writeBoolean(false)) shouldBe """false""" } it should "write null value" in { iterate(_.writeNull()) shouldBe """null""" } it should "write object structure" in { iterate(_.writeObjectStart().writeObjectEnd()) shouldBe """{}""" } it should "write array structure" in { iterate(_.writeArrayStart().writeArrayEnd()) shouldBe """[]""" } it should "write raw json" in { iterate(_.writeRawJson("""{"some" : "raw json"}""")) shouldBe """{"some" : "raw json"}""" } it should "write complex object structure" in { iterate { _.writeObjectStart() .writeFieldName("a") .writeNumber(1) .writeFieldName("b") .writeArrayStart() .writeString("s") .writeRawJson("false") .writeBoolean(true) .writeObjectStart() .writeFieldName("a") .writeNull() .writeObjectEnd() .writeArrayEnd() .writeFieldName("c") .writeRawJson("""{"some" : "raw json"}""") .writeObjectEnd() } shouldBe """{"a":1,"b":["s",false,true,{"a":null}],"c":{"some" : "raw json"}}""" } }
Example 65
Source File: BsonInputOutputBenchmark.scala From scala-commons with MIT License | 5 votes |
package com.avsystem.commons package mongo import java.io.StringWriter import java.nio.ByteBuffer import com.avsystem.commons.rpc.akka.serialization.{Nested, Something} import org.bson.io.BasicOutputBuffer import org.bson.json.{JsonReader, JsonWriter} import org.bson.{BsonBinaryReader, BsonBinaryWriter, BsonDocument, BsonDocumentReader, BsonDocumentWriter, BsonReader, BsonValue, BsonWriter} import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Fork, Measurement, Mode, Scope, State, Warmup} @Warmup(iterations = 10) @Measurement(iterations = 20) @Fork(1) @BenchmarkMode(Array(Mode.Throughput)) @State(Scope.Thread) class BsonInputOutputBenchmark { private val something = Something(42, Nested(List(4, 8, 15, 16, 23, 42, 0), 131), "lol") private val bytes = binaryEncode(something) private val doc = documentEncode(something) private val json = jsonEncode(something) def write(something: Something, bsonWriter: BsonWriter): Unit = { val output = new BsonWriterOutput(bsonWriter) Something.codec.write(output, something) } def binaryEncode(something: Something): Array[Byte] = { val bsonOutput = new BasicOutputBuffer() write(something, new BsonBinaryWriter(bsonOutput)) bsonOutput.toByteArray } def documentEncode(something: Something): BsonDocument = { val doc = new BsonDocument() write(something, new BsonDocumentWriter(doc)) doc } def jsonEncode(something: Something): String = { val stringWriter = new StringWriter() write(something, new JsonWriter(stringWriter)) stringWriter.toString } @Benchmark def binaryEncoding(): Array[Byte] = { binaryEncode(something) } @Benchmark def documentEncoding(): BsonDocument = { documentEncode(something) } @Benchmark def jsonEncoding(): String = { jsonEncode(something) } @Benchmark def valueEncoding(): BsonValue = { BsonValueOutput.write(something) } def read(bsonReader: BsonReader): Something = { val input = new BsonReaderInput(bsonReader) Something.codec.read(input) } @Benchmark def binaryDecoding(): Something = { read(new BsonBinaryReader(ByteBuffer.wrap(bytes))) } @Benchmark def documentDecoding(): Something = { read(new BsonDocumentReader(doc)) } @Benchmark def jsonDecoding(): Something = { read(new JsonReader(json)) } }
Example 66
Source File: AnalyzerRule.scala From scala-commons with MIT License | 5 votes |
package com.avsystem.commons package analyzer import java.io.{PrintWriter, StringWriter} import scala.tools.nsc.Global import scala.util.control.NonFatal abstract class AnalyzerRule(val global: Global, val name: String, defaultLevel: Level = Level.Warn) { import global._ var level: Level = defaultLevel var argument: String = _ protected def classType(fullName: String): Type = try rootMirror.staticClass(fullName).asType.toType.erasure catch { case _: ScalaReflectionException => NoType } protected def analyzeTree(fun: PartialFunction[Tree, Unit])(tree: Tree): Unit = try fun.applyOrElse(tree, (_: Tree) => ()) catch { case NonFatal(t) => val sw = new StringWriter t.printStackTrace(new PrintWriter(sw)) reporter.error(tree.pos, s"Analyzer rule $this failed: " + sw.toString) } private def adjustMsg(msg: String): String = s"[AVS] $msg" protected def report(pos: Position, message: String): Unit = level match { case Level.Off => case Level.Info => reporter.echo(pos, adjustMsg(message)) case Level.Warn => reporter.warning(pos, adjustMsg(message)) case Level.Error => reporter.error(pos, adjustMsg(message)) } def analyze(unit: CompilationUnit): Unit override def toString: String = getClass.getSimpleName } sealed trait Level object Level { case object Off extends Level case object Info extends Level case object Warn extends Level case object Error extends Level }
Example 67
Source File: FormatActor.scala From scastie with Apache License 2.0 | 5 votes |
package com.olegych.scastie package sbt import api.{FormatRequest, FormatResponse, ScalaTarget} import akka.actor.Actor import org.scalafmt.{Scalafmt, Formatted} import org.scalafmt.config.{ScalafmtConfig, ScalafmtRunner} import org.slf4j.LoggerFactory import java.io.{PrintWriter, StringWriter} class FormatActor() extends Actor { private val log = LoggerFactory.getLogger(getClass) private def format(code: String, isWorksheetMode: Boolean, scalaTarget: ScalaTarget): Either[String, String] = { log.info(s"format (isWorksheetMode: $isWorksheetMode)") log.info(code) val config = if (isWorksheetMode && scalaTarget.hasWorksheetMode) ScalafmtConfig.default.copy(runner = ScalafmtRunner.sbt) else ScalafmtConfig.default Scalafmt.format(code, style = config) match { case Formatted.Success(formattedCode) => Right(formattedCode) case Formatted.Failure(failure) => Left(failure.toString) } } override def receive: Receive = { case FormatRequest(code, isWorksheetMode, scalaTarget) => sender ! FormatResponse(format(code, isWorksheetMode, scalaTarget)) } }