Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

We need a singleton rather than a new instance per request

  • Loading branch information...
commit 0fe438c3aabc3d3300939c0e3ded6401ae629edf 1 parent c6faf9a
@dpp dpp authored
View
30 src/main/scala/com/something/lift/NothingServer.scala
@@ -7,6 +7,9 @@ import java.util.concurrent.Executors
import org.jboss.netty.bootstrap.ServerBootstrap
import java.net.InetSocketAddress
import org.jboss.netty.channel.{Channels, ChannelPipeline, ChannelPipelineFactory, ChannelFactory}
+import net.liftweb.util.Helpers
+import net.liftweb.http.{LiftRules, LiftServlet}
+import net.liftweb.common.{Empty, Box}
/**
* Created by IntelliJ IDEA.
@@ -15,6 +18,8 @@ import org.jboss.netty.channel.{Channels, ChannelPipeline, ChannelPipelineFactor
* Time: 10:04 PM
*/
+import MyTypes._
+
object NothingServer extends App {
val allChannels = new DefaultChannelGroup("nothing-server")
@@ -24,13 +29,36 @@ object NothingServer extends App {
Executors.newCachedThreadPool()
)
+ private def findObject(cls: String): Box[AnyRef] =
+ Helpers.tryo[Class[_]](Nil)(Class.forName(cls + "$")).flatMap {
+ c =>
+ Helpers.tryo {
+ val field = c.getField("MODULE$")
+ field.get(null)
+ }
+ }
+
+ val transientVarProvider: VarProvider =
+ findObject("net.liftweb.http.TransientRequestVarHandler").open_!.asInstanceOf[VarProvider]
+ val reqVarProvider: VarProvider =
+ findObject("net.liftweb.http.RequestVarHandler").open_!.asInstanceOf[VarProvider]
+
+ val nettyContext = new NettyContext
+
+
+ val liftLand = new LiftServlet(nettyContext)
+
+ LiftRules.setContext(nettyContext)
+
private[this] val bootstrap = new ServerBootstrap(factory)
+ val handler = new NothingServerHandler(nettyContext, transientVarProvider, reqVarProvider, liftLand)
+
bootstrap.setPipelineFactory(new ChannelPipelineFactory {
def getPipeline: ChannelPipeline = Channels.pipeline(
new HttpRequestDecoder,
new HttpResponseEncoder,
- new NothingServerHandler
+ handler
)
})
View
30 src/main/scala/com/something/lift/NothingServerHandler.scala
@@ -85,29 +85,20 @@ class NettyContext extends HTTPContext {
def removeAttribute(name: String): Unit = {}
}
-
-class NothingServerHandler extends SimpleChannelUpstreamHandler with HTTPProvider{
+object MyTypes {
type VarProvider = { def apply[T](session: Box[LiftSession], f: => T): T}
- lazy val transientVarProvider: VarProvider =
- findObject("net.liftweb.http.TransientRequestVarHandler").open_!.asInstanceOf[VarProvider]
- lazy val reqVarProvider: VarProvider =
- findObject("net.liftweb.http.RequestVarHandler").open_!.asInstanceOf[VarProvider]
-
- private def findObject(cls: String): Box[AnyRef] =
- Helpers.tryo[Class[_]](Nil)(Class.forName(cls + "$")).flatMap {
- c =>
- Helpers.tryo {
- val field = c.getField("MODULE$")
- field.get(null)
- }
- }
+}
- lazy val nettyContext = new NettyContext
+import MyTypes._
+class NothingServerHandler(val nettyContext: NettyContext,
+ val transientVarProvider: VarProvider,
+ val reqVarProvider: VarProvider,
+ val liftLand: LiftServlet) extends SimpleChannelUpstreamHandler with HTTPProvider{
def context = nettyContext
- lazy val liftLand = new LiftServlet(nettyContext)
+ bootLift(Empty)
/**
* Wrap the loans around the incoming request
@@ -407,7 +398,10 @@ class NettyHttpRequest extends HTTPRequest {
doNotHandled()
})}))
} catch {
- case e => e.printStackTrace
+ case excp => excp.printStackTrace
+ val response: HttpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)
+ val future = e.getChannel.write(response)
+ future.addListener(ChannelFutureListener.CLOSE)
}
})
Please sign in to comment.
Something went wrong with that request. Please try again.