Wednesday, May 25, 2016

Migrating to Play 2.5 - can GlobalSettings be removed?

The post Migration from Play 2.4 to Play 2.5 explains the mandatory steps of migration to Play 2.5.
According to Migration guide removing of GlobalSettings implementation class is strongly recommended, but is optional. The exact recommendation is to move from it as much code as possible and in ideal to remove it at all.

In previous Play releases hooking of the application start-up, shut-down and request interception was done with a class Global, which extends the default GlobalSettings.
Such Global exists also in my project to be migrated to Play 2.5.

The original Global (before migration) has the following methods:
     public void onStart(Application arg0) { 
     public Handler onRouteRequest(RequestHeader routeRequest) { 
     public Action onRequest(Request request, Method method) {
     public CompletionStage onHandlerNotFound(RequestHeader requestHeader) {

The GlobalSettings.onRouteRequest cannot be migrated. The rest of methods are recommended for migration.

Removing Global.onStart

The migration guide instructions do not elaborate how to do it. I wish they will show some example.

The following solution was figured out and implemented instead of Global.onStart:
1. The singleton class StartUpHandler executes in a constructor all the stuff from onStart. The class is created by injection:
 package modules;  
 import javax.inject.Inject;  
 import javax.inject.Singleton;  
 import com.att.tlv.cspl.ssp.commons.mapper.SspObjectsMapper;  
 import com.fasterxml.jackson.annotation.PropertyAccessor;  
 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;  
 import com.fasterxml.jackson.databind.ObjectMapper;  
 import play.Configuration;  
 import play.libs.Json;  
 public class StartUpHandler {  
      private static final String CONFIG_DIR_PROPERTY = "config.dir";  
      private static final String LOG4J2_CONFIG_PROPERTY = "log4j.configurationFile";  
      public StartUpHandler(final Configuration configuration) {  
           System.setProperty(CONFIG_DIR_PROPERTY, configuration.getString(CONFIG_DIR_PROPERTY));  
           System.setProperty(LOG4J2_CONFIG_PROPERTY, configuration.getString(LOG4J2_CONFIG_PROPERTY));  
      private void configureJson() {  
           ObjectMapper mapper = new ObjectMapper();  
           mapper.setVisibility(PropertyAccessor.ALL, Visibility.ANY);  
           mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);  
2. The module class OnStartupModule registers StartUpHandler to be injected:
 package modules;  
 public class OnStartupModule extends AbstractModule {  
      protected void configure() {  
3. Module was added into application.conf:
play.modules.enabled += "modules.OnStartupModule"

Removing Global.onRequest

The manual states:
Create a class that inherits from DefaultHttpRequestHandler, and move an implementation of your GlobalSettings.onRequest method inside the DefaultHttpRequestHandler.createAction method.
I created a class as it was directed:
import play.http.DefaultHttpRequestHandler;
public class SspHttpRequestHandler extends DefaultHttpRequestHandler {
Unfortunately, I did not managed to resolve the compilation error:
The constructor DefaultHttpRequestHandler(JavaCompatibleHttpRequestHandler) refers to the missing type.
And so it was no point to proceed and override the method handlerForRequest:
public HandlerForRequest handlerForRequest(Http.RequestHeader request)

I did not remove the Global.onRequest.

Removing Global.onHandlerNotFound

According to the manual:
Create a class that inherits from HttpErrorHandler, and provide an implementation for HttpErrorHandler.onClientError...

I created a class as it was directed:
import play.http.DefaultHttpErrorHandler;
public class ErrorHandler extends DefaultHttpErrorHandler {
Again, I did not managed to resolve the compilation error:
The constructor DefaultHttpErrorHandler(Configuration, Environment, OptionalSourceMapper, Provider) refers to the missing type OptionalSourceMapper.

So I did not remove the Global.onHandlerNotFound.

No comments :

About the author

My Photo
I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.
Back to Top