How to access play configuration from a different module - scala

I have a multi module sbt application, where one of the modules is a play application.
So my module layout is like:
/module1/
/module2/
/module-web <-- this is play 2.x
/module3/
Now in my other modules I am using the typesafe configuration library (com.typesafe.config).
Now I have a module, module3, that will be used inside of my module-web (play) and it will also be used in another project. I will synchronize the application.conf so it is the same in both the play project and in the other project.
My question is, how can I access the play application.conf from inside of module3?
module3 does not have the play framework as a dependency, just the typesaf config library.
I know play lets you do:
Play.current.configuration.getString("db.driver")
Is there something similiar but not using the Play.current method?

You can use the ConfigFactory class (most probably the load method will fit your needs and you can also look at the parseString/parseFile methods), then call toConfig():
import com.typesafe.config.ConfigFactory
import play.api.Configuration
val config = new Configuration(ConfigFactory.load())
config.getString("db.driver") ..etc

Related

access to a rest webservice in grails 4.0.1

I have a lot of successful rest accesses in grails 2.x.x
I simply coded
import grails.plugins.rest.client.*
import grails.util.Holders
import org.codehaus.groovy.grails.web.json.JSONObject
import org.codehaus.groovy.grails.web.json.JSONArray
and Classes JSONObject, JSONArray, RestBuilder, RestResponse where available for further use.
What are the corresponding imports in 4.0.1 and what jars resp. what lines in build.gradle are necessary?
What are the corresponding imports in 4.0.1 and what jars resp. what
lines in build.gradle are necessary?
Grails 4 offers better options than to interact with the classes you asked about but to answer the question as asked...
org.grails.web.json.JSONObject is in grails-web-common-4.0.1.jar. Use import org.grails.web.json.JSONObject.
org.grails.web.json.JSONArray is in grails-web-common-4.0.1.jar. Use import org.grails.web.json.JSONArray.
grails.plugins.rest.client.RestBuilder is in grails-datastore-rest-client-6.1.12.RELEASE.jar. Use import grails.plugins.rest.client.RestBuilder.
grails.plugins.rest.client.RestResponse is in grails-datastore-rest-client-6.1.12.RELEASE.jar. Use import grails.plugins.rest.client.RestResponse.
Depending on what other dependencies you may have in your project those may or may not be pulled in transitively so you may not need to add them to your build.gradle directly. The most likely scenario is you won't need to add anything to pull in grails-web-common-4.0.1.jar but you probably will need to pull in grails-datastore-rest-client-6.1.12.RELEASE.jar which can be done by adding the following to your build.gradle:
compile "org.grails:grails-datastore-rest-client:6.1.12.RELEASE"
If you want to pull in grails-web-common explicitly you could use the following:
compile "org.grails:grails-web-common:4.0.1"
If you are using the BOM correctly, you could simplify that with the following:
compile "org.grails:grails-web-common"
I hope that helps.

Guice Scala module - No valid constructors

I have playframework application written in scala. Problem is when I want to add new module for Silhouette. My module class is very similar to one from Silhouette example. I can run application trough sbt with simple run command but when I build jar using sbt-assembly and try run it I get:
No valid constructors
at play.api.inject.Modules$.$anonfun$constructModule$6(Module.scala:155)
at scala.Option.getOrElse(Option.scala:138)
at play.api.inject.Modules$.constructModule(Module.scala:155)
at play.api.inject.Modules$.$anonfun$locate$4(Module.scala:127)
at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:237)
at scala.collection.immutable.HashSet$HashSet1.foreach(HashSet.scala:321)
at scala.collection.immutable.HashSet$HashTrieSet.foreach(HashSet.scala:977)
at scala.collection.TraversableLike.map(TraversableLike.scala:237)
at scala.collection.TraversableLike.map$(TraversableLike.scala:230)
at scala.collection.AbstractSet.scala$collection$SetLike$$super$map(Set.scala:51)
at scala.collection.SetLike.map(SetLike.scala:104)
at scala.collection.SetLike.map$(SetLike.scala:104)
at scala.collection.AbstractSet.map(Set.scala:51)
at play.api.inject.Modules$.locate(Module.scala:125)
at play.api.inject.guice.GuiceableModule$.loadModules(GuiceInjectorBuilder.scala:276)
at play.api.inject.guice.GuiceApplicationBuilder$.$anonfun$$lessinit$greater$default$9$1(GuiceApplicationBuilder.scala:30)
at play.api.inject.guice.GuiceApplicationBuilder.applicationModule(GuiceApplicationBuilder.scala:102)
at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:185)
at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:137)
at play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
at play.core.server.ProdServerStart$.start(ProdServerStart.scala:51)
at play.core.server.ProdServerStart$.main(ProdServerStart.scala:25)
at play.core.server.ProdServerStart.main(ProdServerStart.scala)
I had similar issue and solved it by adding configuration as a parameter to constructor, for some reason it searches for constructor with configuration, not sure if this is the same issue as yours.
import com.typesafe.config.Config;
#Inject
public TradeClearingWorkboardGuiceModule(Environment environment, Config configuration) {
}

build.sbt imports for docker commands

I have a build.sbt file but I am not able to figure out the role of this import import docker.{addDockerPackage}
Is this an open source import? I am not able to find info about it. Further down in the script it calls a method addDockerPackage()
I wonder if the method is in that package? Or all this is some proprietary stuff? If it is a standard import, where do I read about it?
You can use sbt-native it has a Docker plugin:
http://www.scala-sbt.org/sbt-native-packager/formats/docker.html
Here are 4 different good example that you can run to see how it works:
https://github.com/marcuslonnberg/sbt-docker
As far as:
import docker.{addDockerPackage}
I don't think that is a package. It looks more like a helper to define something like this:
packageName in Docker := packageName.value

Calling into Play framework app from the Scala console

I have a Play Framework 2.3 app. I can drop into a Scala console with activator console. However, when I try to call into code from my app, specifically some helper function which uses WS, which uses the implicit import play.api.Play.current to retrieve the currently running app, I get the error message java.lang.RuntimeException: There is no started application.
What steps do I have to take to be able to load my app into the current console session?
There is a similar existing question, but the accepted answer appears to be using a mock app from the framework's test helpers. Preferably, I would like to run in the context of my actual app. If I must use a fake app, would it be possible to make it match my development environment (what I get when running activator run) rather than my test environment (what I get when running the unit tests)?
Thanks in advance!
In this specific case you can just create an Application instance and use it instead of the implicit one:
// Tested in 2.3.7
import play.api.{Play, Mode, DefaultApplication}
import java.io.File
import play.api.libs.ws.WS
val application = new DefaultApplication(
new File("."),
Thread.currentThread().getContextClassLoader(),
None,
Mode.Dev
)
import scala.concurrent.ExecutionContext.Implicits.global
WS.client(application).url("http://www.google.com").get().map((x) => println(x.body))
For future readers, for Play framework 2.5.x:
import play.api._
val env = Environment(new java.io.File("."), this.getClass.getClassLoader, Mode.Dev)
val context = ApplicationLoader.createContext(env)
val loader = ApplicationLoader(context)
val app = loader.load(context)
Play.start(app)
Source: https://www.playframework.com/documentation/2.5.x/PlayConsole#Launch-the-interactive-console

scala package conflict

I have a library which has root package "scala", and now I have a project using this library, and I have a sub package named "com.zjffdu.scala". And the class file in this package needs to import classes from the library. So I have the following import statement.
import scala._
But because this class is also in package "scala", the scala compiler will look for files in current directory rather than the library.
So how can I explicit to tell scala to import classes from the library.
Thanks
Use this:
import _root_.scala._
As you can see it's not very pretty — the best option is probably to avoid naming one of your packages scala.
And by the way — the root scala package is always preimported (though subpackages, of course, are not).