Skip to content

Commit

Permalink
Define deployment properties as constructor arguments when using @Web…
Browse files Browse the repository at this point in the history
…Servlet annotation
  • Loading branch information
Henri Kerola committed Dec 30, 2015
1 parent bf8bcee commit bdecf15
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
36 changes: 35 additions & 1 deletion addon/src/main/scala/vaadin/scala/server/ScaladinServlet.scala
@@ -1,11 +1,23 @@
package vaadin.scala.server

import java.util.Properties

import com.vaadin.server
import com.vaadin.server.Constants._
import com.vaadin.server.DefaultDeploymentConfiguration._
import com.vaadin.server.VaadinServlet
import javax.servlet.ServletConfig
import vaadin.scala.UI
import vaadin.scala.internal.{ DefaultScaladinUIProvider, WrapperUtil }
import vaadin.scala.server.mixins.ScaladinServletServiceMixin

class ScaladinServlet extends VaadinServlet {
class ScaladinServlet(
ui: Class[_ <: UI] = null,
productionMode: Boolean = false,
widgetset: String = "com.vaadin.DefaultWidgetSet",
resourceCacheTime: Int = DEFAULT_RESOURCE_CACHE_TIME,
heartbeatInterval: Int = DEFAULT_HEARTBEAT_INTERVAL,
closeIdleSessions: Boolean = DEFAULT_CLOSE_IDLE_SESSIONS) extends VaadinServlet {

override def init(servletConfig: ServletConfig) {
super.init(servletConfig)
Expand Down Expand Up @@ -33,4 +45,26 @@ class ScaladinServlet extends VaadinServlet {
val classLoader = Some(service.classLoader).getOrElse(getClass.getClassLoader)
Class.forName(className, true, classLoader).newInstance.asInstanceOf[vaadin.scala.server.ScaladinUIProvider]
}

override def createDeploymentConfiguration(initParameters: Properties): server.DeploymentConfiguration = {

applyPropertiesDefinedInConstructor(initParameters)

super.createDeploymentConfiguration(initParameters)
}

private def applyPropertiesDefinedInConstructor(initParameters: Properties): Unit = {

def setAbsentProperty(property: String, value: String): Unit =
if (initParameters.getProperty(property) == null) {
initParameters.setProperty(property, value)
}

setAbsentProperty("ScaladinUI", Option(ui).map(_.getName).orNull)
setAbsentProperty(SERVLET_PARAMETER_PRODUCTION_MODE, productionMode.toString)
setAbsentProperty(PARAMETER_WIDGETSET, widgetset)
setAbsentProperty(SERVLET_PARAMETER_RESOURCE_CACHE_TIME, resourceCacheTime.toString)
setAbsentProperty(SERVLET_PARAMETER_HEARTBEAT_INTERVAL, heartbeatInterval.toString)
setAbsentProperty(SERVLET_PARAMETER_CLOSE_IDLE_SESSIONS, closeIdleSessions.toString)
}
}
8 changes: 4 additions & 4 deletions demo/src/main/scala/vaadin/scala/demo/DemoUI.scala
Expand Up @@ -5,11 +5,11 @@ import javax.servlet.annotation.{ WebInitParam, WebServlet }
import vaadin.scala._
import vaadin.scala.server.{ ScaladinServlet, ScaladinRequest }

@WebServlet(
urlPatterns = Array("/*"),
initParams = Array(new WebInitParam(name = "ScaladinUI", value = "vaadin.scala.demo.DemoUI"))
@WebServlet(urlPatterns = Array("/*"))
class Servlet extends ScaladinServlet(
ui = classOf[DemoUI],
productionMode = false
)
class Servlet extends ScaladinServlet

class DemoUI extends UI(title = "Hello World") {

Expand Down

0 comments on commit bdecf15

Please sign in to comment.