简体   繁体   中英

Play Framework 2.1: Overriding configuration file programmatically in Global settings

I'm working to develop a multi-tenant Play Framework 2.1 application. I intend to override the onRequest method of the GlobalSettings class to load and set a custom configuration based on the subdomain of the request. Problem is, I don't see how this would be possible in Play 2.x.

I can override system properties at the command line when starting the server, but how can I do this programmatically in Java code for each request?

The code would look something like this (I assume):

@Override
public play.mvc.Action onRequest(Request request, Method actionMethod) {
    //Look up configuration settings in Cache based on request subdomain
    //(i.e. Cache.get("subdomain.conf"))
    //if not in cache: 
           //load appropriate configuration file for this subdomain (java.io.File)
           //set new configuration from file for this request
           //cache the configuration for future use in a new thread
   //else
           //set configuration from cache for this request
   return super.onRequest(request, actionMethod);
  }

}

Looking up the URL and getting/setting the cache is easy, but I cannot figure out how to SET a new configuration programmatically for Play Framework 2.1 and the documentation is a little light on things like this.

Any thoughts? Anyone know a better, more efficient way to do this?

So, in a sort of roundabout way, I created the basis for a multi-tenant Play application using a Scala Global. There may be a more efficient way to implement this using a filter, but I'm finding this seems to work so far. This does not appear to be as easily implemented in Java.

Instead of using the configuration file, I'm using the database. I assume it would be far more efficient to use a key-value cache, but this seems to work for now.

In Global.scala:

object Global extends GlobalSettings {

  override def onRouteRequest(request: RequestHeader): Option[Handler] = {
    if (request.session.get("site").isEmpty){
      val id = models.Site.getSiteIDFromURL(request.host)
      request.session.+("site" -> id)
    }
    super.onRouteRequest(request)
  }

}

And then, obviously, you have to create a database model to query the site based on the request domain and/or the session value set in the request. If anyone knows a better way I'd love to hear it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM