php.java.servlet
Class RemoteHttpServletContextFactory

java.lang.Object
  extended by php.java.bridge.JavaBridgeFactory
      extended by php.java.servlet.RemoteHttpServletContextFactory
All Implemented Interfaces:
java.io.Serializable, IContextFactory, IJavaBridgeFactory

public class RemoteHttpServletContextFactory
extends JavaBridgeFactory
implements IContextFactory, java.io.Serializable

Create session contexts for servlets.

This ContextFactory can be used in environments where no custom class loaders and no threads are allowed.

Author:
jostb
See Also:
ContextFactory, ContextServer, Serialized Form

Field Summary
static java.lang.String CONTEXT_FACTORY_ATTRIBUTE
           
 
Constructor Summary
RemoteHttpServletContextFactory(javax.servlet.Servlet servlet, javax.servlet.ServletContext ctx, javax.servlet.http.HttpServletRequest proxy, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
           
 
Method Summary
static IContextFactory addNew(javax.servlet.Servlet servlet, javax.servlet.ServletContext kontext, javax.servlet.http.HttpServletRequest proxy, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, IContextFactoryVisitor impl)
          Create and add a new ContextFactory.
 void destroy()
          Destroy the factory
 void flushBuffer()
          Flush the response buffer
 IContext getContext()
          Return the associated JSR223 context
 java.lang.String getId()
          Return the serializable ID of the context factory
 ISession getSession(java.lang.String name, short clientIsNew, int timeout)
          Return a session for the JavaBridge
 ISession getSimpleSession(java.lang.String name, short clientIsNew, int timeout)
          
 void handleRequests(java.io.InputStream in, java.io.OutputStream out)
          Handle requests from the InputStream, write the responses to OutputStream
 void initialize()
          Called when the context runner starts
 void invalidate()
          Hook is called at the end of the life cycle.
 void parseHeader(Request req, java.io.InputStream in)
          Called for the request header
 void recycle(java.lang.String id)
           Update the context factory with the new JavaBridge obtained from the servlet
 void release()
          Releases the context factory.
 void releaseManaged()
          Wait until this context is finished and release/destroy it.
 void setContext(IContext context)
          Set the Context into this factory.
 void setResponse(javax.servlet.http.HttpServletResponse out)
          Set the current response
 void waitFor(long timeout)
          Wait until this context is finished.
 
Methods inherited from class php.java.bridge.JavaBridgeFactory
getBridge, isNew, recycle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface php.java.bridge.IJavaBridgeFactory
getBridge, isNew, recycle
 

Field Detail

CONTEXT_FACTORY_ATTRIBUTE

public static final java.lang.String CONTEXT_FACTORY_ATTRIBUTE
Constructor Detail

RemoteHttpServletContextFactory

public RemoteHttpServletContextFactory(javax.servlet.Servlet servlet,
                                       javax.servlet.ServletContext ctx,
                                       javax.servlet.http.HttpServletRequest proxy,
                                       javax.servlet.http.HttpServletRequest req,
                                       javax.servlet.http.HttpServletResponse res)
Method Detail

addNew

public static IContextFactory addNew(javax.servlet.Servlet servlet,
                                     javax.servlet.ServletContext kontext,
                                     javax.servlet.http.HttpServletRequest proxy,
                                     javax.servlet.http.HttpServletRequest req,
                                     javax.servlet.http.HttpServletResponse res,
                                     IContextFactoryVisitor impl)
Create and add a new ContextFactory.

Parameters:
servlet - The servlet
kontext - The servlet context
proxy - The request proxy
req - The HttpServletRequest
res - The HttpServletResponse
Returns:
The created ContextFactory

getId

public java.lang.String getId()
Return the serializable ID of the context factory

Specified by:
getId in interface IContextFactory
Returns:
The ID

getSimpleSession

public ISession getSimpleSession(java.lang.String name,
                                 short clientIsNew,
                                 int timeout)

Specified by:
getSimpleSession in interface IContextFactory
Parameters:
name - The session name. If name is null, the name PHPSESSION will be used.
clientIsNew - true if the client wants a new session
timeout - timeout in seconds. If 0 the session does not expire.
Returns:
The session
See Also:
ISession

getSession

public ISession getSession(java.lang.String name,
                           short clientIsNew,
                           int timeout)
Return a session for the JavaBridge

Specified by:
getSession in interface IContextFactory
Specified by:
getSession in interface IJavaBridgeFactory
Specified by:
getSession in class JavaBridgeFactory
Parameters:
name - The session name. If name is null, the name PHPSESSION will be used.
clientIsNew - one of ISession.SESSION_CREATE_NEW ISession.SESSION_GET_OR_CREATE or ISession.SESSION_GET
timeout - timeout in seconds. If 0 the session does not expire.
Returns:
The session
See Also:
ISession

initialize

public void initialize()
Called when the context runner starts

Specified by:
initialize in interface IContextFactory
See Also:
IContextFactory.releaseManaged(), IJavaBridgeFactory.destroy()

invalidate

public void invalidate()
Hook is called at the end of the life cycle. Either from destroy(), recycle() or from destroyOrphaned().

Specified by:
invalidate in interface IJavaBridgeFactory
See Also:
IJavaBridgeFactory.destroy(), IJavaBridgeFactory.recycle()

recycle

public void recycle(java.lang.String id)

Update the context factory with the new JavaBridge obtained from the servlet

Specified by:
recycle in interface IContextFactory
Parameters:
id - The fresh id
See Also:
ContextFactory.recycle(), Request.setBridge(php.java.bridge.JavaBridge), Request.recycle()

release

public void release()
Releases the context factory. This method should be called when the factory is not needed anymore. Implementations could then remove any unused context factory from the classloader's list of context factories.

Specified by:
release in interface IContextFactory

releaseManaged

public void releaseManaged()
                    throws java.lang.InterruptedException
Wait until this context is finished and release/destroy it. This method returns immediately if this context is not in use yet or it is no longer in use. Call this method only if Java has initiated the communication and Java have full control over the connection, for example via a ScriptEngine's URLReader or CGIRunner. For Apache/PHP initiated requests use a combination of #waitFor(long) and #release() instead.

Specified by:
releaseManaged in interface IContextFactory
Throws:
java.lang.InterruptedException
See Also:
ContextRunner

waitFor

public void waitFor(long timeout)
             throws java.lang.InterruptedException
Wait until this context is finished.

Specified by:
waitFor in interface IContextFactory
Parameters:
timeout - The timeout
Throws:
java.lang.InterruptedException
See Also:
ContextRunner

parseHeader

public void parseHeader(Request req,
                        java.io.InputStream in)
                 throws java.io.IOException
Called for the request header

Specified by:
parseHeader in interface IJavaBridgeFactory
Overrides:
parseHeader in class JavaBridgeFactory
Parameters:
req - the current request
in - the input stream
Throws:
java.io.IOException

setContext

public void setContext(IContext context)
Set the Context into this factory. Should be called by Context.addNew() only.

Specified by:
setContext in interface IContextFactory
See Also:
ContextFactory.addNew()

getContext

public IContext getContext()
Return the associated JSR223 context

Specified by:
getContext in interface IContextFactory
Specified by:
getContext in interface IJavaBridgeFactory
Specified by:
getContext in class JavaBridgeFactory
Returns:
Always null
See Also:
ContextFactory.getContext()

destroy

public void destroy()
Destroy the factory

Specified by:
destroy in interface IJavaBridgeFactory
Overrides:
destroy in class JavaBridgeFactory

flushBuffer

public void flushBuffer()
                 throws java.io.IOException
Flush the response buffer

Specified by:
flushBuffer in interface IJavaBridgeFactory
Throws:
java.io.IOException

setResponse

public void setResponse(javax.servlet.http.HttpServletResponse out)
Set the current response

Parameters:
out - the PhpJavaServlet response

handleRequests

public void handleRequests(java.io.InputStream in,
                           java.io.OutputStream out)
                    throws java.io.IOException
Handle requests from the InputStream, write the responses to OutputStream

Parameters:
in - the InputStream
out - the OutputStream
Throws:
java.io.IOException - Example:
protected void doPut (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  IContextFactory ctx = new RemoteHttpServletContextFactory(this, getServletContext(), req, req, res);
  res.setHeader("X_JAVABRIDGE_CONTEXT", ctx.getId());
  res.setHeader("Pragma", "no-cache");
  res.setHeader("Cache-Control", "no-cache");
  try { ctx.handleRequests(req.getInputStream(), res.getOutputStream()); } finally { ctx.destroy(); }
}