Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@marcospereira
Copy link
Member

Purpose

The current app was still being initialized even with the global state disabled. This removes that and adds some more warnings to instruct users to move away from Play.current and similars.

Background Context

See discussion in our Gitter channel: https://gitter.im/playframework/contributors?at=5b106c1d52e35117cdf94838

References

This can be considered a follow up of #8183.

@marcospereira marcospereira requested a review from ignasi35 June 7, 2018 15:54

// Set the current app if the global application is enabled
// Also set it if the current app is null, in order to display more useful errors if we try to use the app
if (globalApp || _currentApp == null) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the main purpose of the PR.

This will then have an impact on privateMaybeApplication that now returns a Try[Application] since it can fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have the check above globalApp && _currentApp != null && _currentApp.globalApplicationEnabled to decide whether to stop the current app. I think that can be globalApp && _currentApp != null, since the current app will only be set if the global app is enabled.


private[play] def privateMaybeApplication: Try[Application] = {
if (_currentApp != null) {
logger.warn(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is warn level too noise?

@richdougherty @gmethvin @schmitch what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can make sure this only prints once when the app starts up it's fine, but it'd be pretty annoying to have this on every access (which could be every request). I think the deprecation is good enough. We're already making users opt in to having the global app.

Copy link
Contributor

@schmitch schmitch Jun 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well we could create a dummy variable and initialize it here:

private lazy val nonEmptyCurrentApp = {
logger.warn(...)
true
}
private[play] def privateMaybeApplication: Try[Application] = {
 Option(_currentApp) match {
if (_currentApp != null) {
val _ = nonEmptyCurrentApp
}

guess this would memoize the logging and only print the warning once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the simplest solution is to move the log to where we set the current app (in Play.start).

/**
* A lazy HTTP error handler, that looks up the error handler from the current application
*/
@deprecated("Access the global state. Inject a HttpErrorHandler instead", "2.7.0")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not deprecated before, but it should.

import scala.util.{ Failure, Success }

// Keep Crypto around to manage global state for now...
@deprecated("Access global state. Inject a CookieSigner instead", "2.7.0")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a private API, but adding deprecated here to help us to find it later.

Copy link
Member

@ignasi35 ignasi35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (with small comment wrt logs/exceptionMessage)

|You are accessing the application using deprecated methods that exists only to access
|global state. If you need an instance of Application, use Dependency Injection.
""".stripMargin)
Success(_currentApp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be a multiline trace?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

But of course, users can silence this via Logback configuration. It shouldn't happen a lot though since it is quite unusual to access the current application for each request. Do you think it would be better to have a single line here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ignasi35 I'm moving this to Play.start to ensure the log will happen only once.


private[play] def privateMaybeApplication: Try[Application] = {
if (_currentApp != null) {
logger.warn(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can make sure this only prints once when the app starts up it's fine, but it'd be pretty annoying to have this on every access (which could be every request). I think the deprecation is good enough. We're already making users opt in to having the global app.

|The global application is disabled. Set $GlobalAppConfigKey to allow global state here.
|If $GlobalAppConfigKey is already configured to allow global state, then you need to start
|the application too.
""".stripMargin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message would be a good place to recommend using dependency injection and warn that Play.current will be removed in the future, something like:

The global application reference is disabled. Play's global state is deprecated and will be removed in a future release. You should use dependency injection instead. To enable the global application anyway, set $GlobalAppConfigKey = true


// Set the current app if the global application is enabled
// Also set it if the current app is null, in order to display more useful errors if we try to use the app
if (globalApp || _currentApp == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have the check above globalApp && _currentApp != null && _currentApp.globalApplicationEnabled to decide whether to stop the current app. I think that can be globalApp && _currentApp != null, since the current app will only be set if the global app is enabled.

@marcospereira
Copy link
Member Author

@gmethvin and @ignasi35 please review again. :-)

@marcospereira marcospereira merged commit a39b208 into playframework:master Jun 12, 2018
@marcospereira marcospereira deleted the remove-set-current-app-when-allow-global-false branch June 12, 2018 19:28
Kavit900 added a commit to Kavit900/playframework that referenced this pull request Jun 14, 2018
* remove apache commons libs (playframework#8455)

* remove apache commons libs

currently play added a lot of "helper" libraries in the past,
however with recent JVM upgrades and more and more stuff inside
the JVM itself these got more and more unnecessary.

Actually this imports some Classes from apache commons lang3,
however only a really small subset of the library is imported.
The subset itself barly changed over the recent years and Play
mostly used just the reflect stuff of commons lang3

* Remove set current app when allow global false (playframework#8462)

* Remove imports to current app

* Add missing deprecation notes

* Remove invalid scaladoc tags

There is no `@deprecated` tag in scaladocs since we have
the `@deprecated` annotation.

* Do not set current app when global state is disabled

* Configure mima filter

* Clear warn messages when using global state

* Just stop the current app if global state is enabled

* logback graceful shutdown (playframework#8407)

* add shutdown hook to logback-play-default.xml playframework#8277

* update SettingsLogger.md documentation playframework#8277

* delete an unnecessary line playframework#8277.

* add shutdown hook to logback-play-logSql.xml playframework#8277

* add line playframework#8277

* update SettingsLogger.md playframework#8277

* update SettingsLogger.md playframework#8277
marcospereira added a commit to playframework/scalatestplus-play that referenced this pull request Jun 22, 2018
* Update Play, interplay and other dependencies

* Remove access to global state and deprecated APIs

The main change is to remove any access to `Play.maybeApplication` which
Play does not set by default anymore. See playframework/playframework#8462.

This implies changing tests that also access the global state when using the
`Action` object. So now all the tests are instead using `appRoutes` instead of
`routes` since the later does not provides a way to access an ActionBuilder.

Finally, some minor deprecated calls were replaced with the new APIs.

* Travis configuration tweaks

Avoid generating too much log when running the tests. It was
also necessary to use `sudo: required` to workaround a Travis
problem when using Google Chrome:

See travis-ci/travis-ci#8836

Moreover, Scala versions were updated.

* Deprecate PhantomJS support

Selenium had deprecated PhantomJS support since it is
no longer actively developed. Support will be dropped in
Selenium so we should deprecated it here too.

* Enable users to better customize FirefoxDriver creation

It was possible to customize the FirefoxProfile, but not
FirefoxOptions. Now both are available across all the APIs.

* Enable users to better customize ChromeDriver creation

* Remove ehcache dependencies

* Remove code duplication
@TimMoore TimMoore added this to the Play 2.7.0 milestone Dec 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants