nimbus-jose-jwt Plugin Type Not Found Error - scala

I am trying to compile code sample from this book (Play Framework Cookbook 2nd Edition) for JWT with nimbus-jose-jwt.
However it said Plugin trait type not found during compilation. I checked API documentation, Plugin is available at play.api package.
import play.api.{Logger, Play, Application, Plugin}
class JWTPlugin #Inject() (app: Application) extends Plugin {...}
I tried DI version with and without
routesGenerator := InjectedRoutesGenerator at build.sbt with same error.
nimbus-jose-jwt version: 3.8.2. Tried with latest 4.22, with same error.
scala: 2.11.8
play: 2.5.4
Appreciate any pointer. Thanks.

play.api.Plugin has been removed in Play 2.5.x. You can turn the plugin into a Module instead as described here: https://www.playframework.com/documentation/2.5.x/PluginsToModules
But the easiest solution is to turn the JWTPlugin class into a plain Scala object (e.g. JWTUtil), remove the onStart and onStop methods, remove the "private val jwt" declarations/assigments from all files in the example and call the sign() and verify() methods on the JWTUtil object instead.
Maybe you need some more minor modifications that I have forgotten, but anyway, the example works for me when the JWT implementation doesn't inherit from play.api.Plugin.

Related

Unable to use Apache Commons CLI Option.builder() in Scala

In a spark shell or application (written in Scala/maven build), I am unable to use the static builder method from the Apache Commons CLI package. I have confirmed that I am including the jar in the class path and have access to the Option class along with other classes in the package like Options, DefaultParser, etc. Why can I not use this public static method in Scala?
import org.apache.commons.cli.Option
val opt = Option.builder("foo").build()
error: value builder is not a member of object org.apache.commons.cli.Option
I can however see the static fields Option.UNINITIALIZED and Option.UNLIMITED_VALUES
using commons-cli 1.3.1
Scala version: 2.11.8
Spark version: 2.2.0
command to start the shell: spark-shell --jars .m2/repository/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.jar
Let me help you clarify your problem scenario.
You can open your .idea folder, find that it have some internal jar dependencies already, and of the list commons_cli exists, but 1.2 version.
This would lead to class collision.
The solution is straightforward, refer the doc, use the compatible constructor method.

class java.lang.RuntimeException/Scala class file does not contain Scala annotation

I'm trying to execute a scala code using scala 2.10.2, the code uses some jar libraries compiled with sbt.
I get the following error:
scala: error while loading Order, class file '..\prestashop-scala-client-0.2.4\target\prestasac-0.2.4.jar(co/orderly/prestasac/representations/Order.class)' is broken
(class java.lang.RuntimeException/Scala class file does not contain Scala annotation)
Sources of the prestasac-0.2.4.jar are on github: Order.class
Is there something to do to fix this issue ?
Thank you
Looks like the library is configured to compile against Scala 2.9.1. Major versions of Scala are not binary compatible.
I put the necessary SBT changes here: https://github.com/mpartel/prestashop-scala-client/commit/e9a1df40bfe35518aaebac899e438b9b6fa6d728

scalatra 2.2.0 upgrade encountered NoClassDefFoundError:GenTraversableLike

I am trying to upgrade Scala/Scalatra versions in our project to the following:
scala 2.10.1
scalatra (and scalatra-scalate, scalatra-scalatest) 2.2.0
using sbt 0.11.3
The code compiles OK but, when running the unit tests which uses scalatra TemplateEngine or when starting up our jetty server (also uses scalatra), I'm getting the error below.
Has anyone seen this? Any suggestions is very much appreciated.
Thanks!
java.lang.NoClassDefFoundError:
scala/collection/GenTraversableLike$class
at org.fusesource.scalate.TemplateEngine$$anon$1.(TemplateEngine.scala:162)
at org.fusesource.scalate.TemplateEngine.sourceDirectoriesForwarder(TemplateEngine.scala:162)
at org.fusesource.scalate.TemplateEngine.(TemplateEngine.scala:114)
at com.springer.core.template.TemplateTestHelper$class.beforeAll(TemplateTestHelper.scala:19)
at com.springer.core.template.SupportTemplateTest.beforeAll(SupportTemplateTest.scala:10)
at org.scalatest.BeforeAndAfterAll$class.beforeAll(BeforeAndAfterAll.scala:150)
at com.springer.core.template.SupportTemplateTest.beforeAll(SupportTemplateTest.scala:10)
Without seeing your build.sbt or project/build.scala file it's hard to be sure, but here are a few things you might want to try.
First, I'm not aware of anybody yet trying Scalatra 2.2.0 with Scala 2.10.1, so you're in uncharted territory here. In theory, it should work, but a quick downgrade to 2.10.0 would rule it out.
Secondly, there were changes to the default wiring up of Scalate templates between 2.1.x and 2.2.0.
Scalatra 2.1.0 didn't include Scalate template compilation in the default g8 project skeleton, and this was a source of confusion for a lot of people who later wanted to deploy compiled templates in production. As a result, the default Scalatra 2.2.0 project skeleton comes with a few new features.
It wires up compiled templates, like this:
/* wire up the precompiled templates */
override protected def defaultTemplatePath: List[String] = List("/templates/views")
override protected def createTemplateEngine(config: ConfigT) = {
val engine = super.createTemplateEngine(config)
engine.layoutStrategy = new DefaultLayoutStrategy(engine,
TemplateEngine.templateTypes.map("/templates/layouts/default." + _): _*)
engine.packagePrefix = "templates"
engine
}
It also adds a Scalate template compiler:
addSbtPlugin("com.mojolly.scalate" % "xsbt-scalate-generator" % "0.4.2")
If it's not a Scala 2.10.1-related problem, I'd recommend that the easiest way to determine whether it's the presentation compiler might be to look at (or generate and use) a new Scalatra 2.2.0 project skeleton using the latest g8.
How are your dependencies defined in the build? Are they using %% instead of %
From 2.2.x and up we require the %% where before it was %
This could be the cause of the error you're seeing

Relation between Akka and scala.actors in 2.10

The Scala 2.10 release notes says this: "Akka Actors now part of the distribution. The original Scala actors are now deprecated."
The latest Akka library ("Akka 2.1.0 for Scala 2.10") mentions the following dependency:
com.typesafe.akka:akka-actor_2.10:2.1.0
and the following examples:
import akka.actor.Actor
class MyActor extends Actor {..}
val system = ActorSystem("MySystem")
My project includes these libraries:
org.scala-lang:scala-library:2.10.0
org.scala-lang:scala-actors:2.10.0
In my classpath, I have no package called "akka". I do see instead scala.actors, but it doesn't seem deprecated.
So, in which way are Akka Actors "part of the distribution"?
If this is, indeed, the case, then am I still supposed to add the "akka-actor_2.10" library as a dependency? If so, do I use akka.Actor or the non-deprecated scala.actors.Actor ?
In scala 2.9.2 scala actors was part of scala-library.jar.
If you download a scala-2.10.0 distribution it there are no actors in scala-library and both akka-actors.jar and scala-actors.jar are supplied.
The latter is present for backwards compatibility and will be removed in a future major release.
akka-actors is the replacement and what you should use for any new development (and look to move anything using scala-actors across when possible).
If you have no current code in your project using actors you should probably reconfigure your project's dependencies to remove org.scala-lang:scala-actors:2.10.0 and instead depend on com.typesafe.akka:akka-actors_2.10:2.1.0 if you want to use actors.
I am not sure why there's no deprecation annotation on the classes in scala-actors in 2.10.0 but I believe it's to be added in 2.10.1.
You can find more info on the migration guide.

Compile and execute Scala code at runtime

Is is possible to compile and execute scala code as a string at runtime either in Scala or in Java?
My idea is to build a DSL using Scala then let Java programmers use the DSL inside Java.
I heard that the class scala.tools.nsc.Interpreter can do something like that, but when I imported it inside my scala file, I got "object tools is not a member of package scala."
So could anybody give me a hint?
In 2.10.0 we expose Scala reflection API, which among everything else includes a runtime compilation facility. More details can be found here: Generating a class from string and instantiating it in Scala 2.10.
I recommend you twitter-util's Eval
For scala3 this can be now achieved with dotty:
https://index.scala-lang.org/lampepfl/dotty
https://github.com/lampepfl/dotty
https://dotty.epfl.ch/docs/internals/overall-structure.html
The sbt dependency is e.g. "org.scala-lang" %% "scala3-compiler" % "3.1.3"