Skip to content

Commit

Permalink
LiftPipelineFactory
Browse files Browse the repository at this point in the history
move boot code and pipeline factory to its own class. cleaned up example.
  • Loading branch information
jrwest committed Feb 24, 2012
1 parent 3db01b7 commit cd63189
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/com/something/lift/LiftChannelHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class LiftChannelHandler(val nettyContext: HTTPNettyContext,
reqVarProvider(Empty,{


val httpRequest: HTTPRequest = new NettyHttpRequest(request, ctx, nettyContext, this) // FIXME new HTTPRequestServlet(httpReq, this)
val httpRequest: HTTPRequest = new NettyHttpRequest(request, ctx, nettyContext, this)
val httpResponse = new NettyHttpResponse(ctx)

handleLoanWrappers(service(httpRequest, httpResponse) {
Expand Down
37 changes: 2 additions & 35 deletions src/main/scala/com/something/lift/LiftNettyExampleServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory
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}
import org.jboss.netty.channel.ChannelFactory

/**
* Created by IntelliJ IDEA.
Expand All @@ -18,8 +15,6 @@ import net.liftweb.common.{Empty, Box}
* Time: 10:04 PM
*/

import MyTypes._

object LiftNettyExampleServer extends App {

val allChannels = new DefaultChannelGroup("lift-netty-example-server")
Expand All @@ -29,37 +24,9 @@ object LiftNettyExampleServer 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 HTTPNettyContext

val liftLand = new LiftServlet(nettyContext)

LiftRules.setContext(nettyContext)

private[this] val bootstrap = new ServerBootstrap(factory)

val handler = new LiftChannelHandler(nettyContext, transientVarProvider, reqVarProvider, liftLand)

bootstrap.setPipelineFactory(new ChannelPipelineFactory {
def getPipeline: ChannelPipeline = Channels.pipeline(
new HttpRequestDecoder,
new HttpResponseEncoder,
handler
)
})
bootstrap.setPipelineFactory(new LiftPipelineFactory)

bootstrap.setOption("child.tcpNoDelay", true)
bootstrap.setOption("child.keepAlive", true)
Expand Down
49 changes: 49 additions & 0 deletions src/main/scala/com/something/lift/LiftPipelineFactory.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.something.lift

import org.jboss.netty.handler.codec.http.{HttpResponseEncoder, HttpRequestDecoder}
import net.liftweb.common.Box
import net.liftweb.util.Helpers
import net.liftweb.http.{LiftRules, LiftServlet}
import org.jboss.netty.channel.{Channels, ChannelPipeline, ChannelPipelineFactory}


/**
* Created by IntelliJ IDEA.
* User: jordanrw
* Date: 2/23/12
* Time: 9:55 PM
*/

import MyTypes._
class LiftPipelineFactory extends ChannelPipelineFactory {

// TODO make better, extensible, etc
def getPipeline: ChannelPipeline = Channels.pipeline(
new HttpRequestDecoder,
new HttpResponseEncoder,
handler
)

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 HTTPNettyContext

val liftLand = new LiftServlet(nettyContext)

LiftRules.setContext(nettyContext)

val handler = new LiftChannelHandler(nettyContext, transientVarProvider, reqVarProvider, liftLand)

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)
}
}

}

0 comments on commit cd63189

Please sign in to comment.