php.java.bridge.http
Class ContextFactory

java.lang.Object
  extended by php.java.bridge.JavaBridgeFactory
      extended by php.java.bridge.SessionFactory
          extended by php.java.bridge.http.ContextFactory
All Implemented Interfaces:
IContextFactory, IJavaBridgeFactory

public final class ContextFactory
extends SessionFactory
implements IContextFactory

Create session, jsr223 contexts.

The ContextFactory may keep a promise (a "proxy") which one may evaluate to a session reference (for PHP/JSP session sharing), and/or it may reference a "half-executed" bridge for local channel re-directs (for "high speed" communication links).

A unique context instance should be created for each request and destroyed when the request is done.

Clients of the PHP clients may attach additional data and run with a customized ContextFactory by using the visitor pattern, see accept(IContextFactoryVisitor).

The string ID of the instance should be passed to the client, which may pass it back together with the getSession request or the "local channel re-direct". If the former happens, we invoke the promise and return the session object to the client. Different promises can evaluate to the same session object. For local channel re-directs the ContextFactory is given to a ContextRunner which handles the local channel communication.

When a php client is not interested in a context for 5 seconds (checked every 10 minutes), the context is destroyed: a) switching from the HTTP tunnel to the local channel of the ContextRunner or b) switching from the fresh context created by the client of the PHP client to the recycled, persistent context, costs only one round-trip. The time for such a context switch is usually much less than 10ms unless either the php client or the client that waits for the php client is traced. If 5 seconds is not enough during debugging, change the ORPHANED_TIMEOUT.

In a shared environment with k web contexts there can be up to n*k active JavaBridge/ContextFactory instances (where n is the number of active php clients). All ContextFactories are kept in a shared, per-loader map. But the map can only be accessed via get(String), which checks if the ContextFactory belongs to the same ContextServer.

See Also:
ServletContextFactory, ContextServer, SessionFactory.TIMER_DURATION

Field Summary
static java.lang.String EMPTY_CONTEXT_NAME
          This context name can be used when a ContextFactory is used outside of a servlet environment
 
Fields inherited from class php.java.bridge.SessionFactory
TIMER_DURATION
 
Constructor Summary
ContextFactory(java.lang.String webContext, boolean isManaged)
          Create a new ContextFactory.
 
Method Summary
 void accept(IContextFactoryVisitor visitor)
          Use this method to attach a visitor to the ContextFactory.
static IContextFactory addNew()
          Create a new simple ContextFactory (a factory which creates an emulated JSR223 context) and add it to the list of context factories kept by this classloader.
 void destroy()
          Destroy the factory
static void destroyAll()
          Remove all context factories from the classloader.
static IContextFactory get(java.lang.String id)
          Only for internal use.
 IContext getContext()
          Returns the 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 standard session, shared with JSP
 ISession getSimpleSession(java.lang.String name, short clientIsNew, int timeout)
          Return a simple session which cannot be shared with JSP
 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()
          Called before destroy()
 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.
 java.lang.String toString()
          
 void waitFor(long timeout)
          Wait until this context is finished.
 
Methods inherited from class php.java.bridge.SessionFactory
destroyTimer, flushBuffer
 
Methods inherited from class php.java.bridge.JavaBridgeFactory
getBridge, isNew
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface php.java.bridge.IJavaBridgeFactory
flushBuffer, getBridge, isNew
 

Field Detail

EMPTY_CONTEXT_NAME

public static final java.lang.String EMPTY_CONTEXT_NAME
This context name can be used when a ContextFactory is used outside of a servlet environment

See Also:
Constant Field Values
Constructor Detail

ContextFactory

public ContextFactory(java.lang.String webContext,
                      boolean isManaged)
Create a new ContextFactory.

Parameters:
webContext - The current web context or "@"
isManaged - true if the factory should NOT be gc'ed after MAX_TIMEOUT
Method Detail

addNew

public static IContextFactory addNew()
Create a new simple ContextFactory (a factory which creates an emulated JSR223 context) and add it to the list of context factories kept by this classloader.

Returns:
The created ContextFactory.
See Also:
get(String)

get

public static IContextFactory get(java.lang.String id)
Only for internal use. Returns the context factory associated with the given id

Parameters:
id - The ID
Returns:
The ContextFactory or null.
Throws:
java.lang.SecurityException - if id belongs to a different ContextServer.
See Also:
addNew()

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:
recycle(), Request.setBridge(php.java.bridge.JavaBridge), Request.recycle()

recycle

public void recycle()
Called before destroy()

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

destroy

public void destroy()
Destroy the factory

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

destroyAll

public static void destroyAll()
Remove all context factories from the classloader. May only be called by the ContextServer.

See Also:
ContextServer

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

getId

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

Specified by:
getId in interface IContextFactory
Returns:
The ID

toString

public java.lang.String toString()

Overrides:
toString in class java.lang.Object

getContext

public IContext getContext()
Returns the context.

Specified by:
getContext in interface IContextFactory
Specified by:
getContext in interface IJavaBridgeFactory
Overrides:
getContext in class SessionFactory
Returns:
The context or null.
See Also:
getContext()

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
Parameters:
context - The context.
See Also:
addNew()

accept

public void accept(IContextFactoryVisitor visitor)
Use this method to attach a visitor to the ContextFactory.

Parameters:
visitor - The custom ContextFactory

getSimpleSession

public ISession getSimpleSession(java.lang.String name,
                                 short clientIsNew,
                                 int timeout)
Return a simple session which cannot be shared with JSP

Specified by:
getSimpleSession in interface IContextFactory
Parameters:
name - The session name
clientIsNew - true, if the client wants a new session
timeout - expires in n seconds
Returns:
The session
See Also:
ISession

getSession

public ISession getSession(java.lang.String name,
                           short clientIsNew,
                           int timeout)
Return a standard session, shared with JSP

Specified by:
getSession in interface IContextFactory
Specified by:
getSession in interface IJavaBridgeFactory
Overrides:
getSession in class SessionFactory
Parameters:
name - The session name
clientIsNew - true, if the client wants a new session
timeout - expires in n seconds
Returns:
The session
See Also:
ISession

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

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
Overrides:
invalidate in class SessionFactory
See Also:
IJavaBridgeFactory.destroy(), IJavaBridgeFactory.recycle()

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