from Common import *


class FilterChain(Object):
	"""

	A FilterChain holds the specific, ordered list of Filters which are
	found to apply to a given request.	They participate in transactions.
	
	XXX NOT IMPLEMENTED YET XXX
	It is intended that FilterChains can be created once, then used and
	destroyed, or they may be reused several times over (it's up to the
	server). 

	Objects that participate in a transaction include:
	
	* Application
	* Request
	* Transaction
	* Session
	* Servlet
	* Response
	* FilterChain

	The awake(), respond() and sleep() methods form a message
	sandwich. Each is passed an instance of Transaction which gives further
	access to all the objects involved.
	"""

	## Init ##

	def __init__(self):
	""" Subclasses must invoke super. """
	Object.__init__(self)
	self._serverSidePath = None
	self._filters = []


	## Access ##
	
	def name(self):
	""" Returns the name which is simple the name of the class.
	Subclasses should *not* override this method. It is used for
	logging and debugging. """
	return self.__class__.__name__


	## Filter Creation ##
	def addFilter(self, filter):
	self._filters.append(filter)

	## Request-response cycles ##

	def awake(self, trans):
	""" This message is sent to all objects that participate in the
	request-response cycle in a top-down fashion, prior to respond()."""
	self._transaction = trans
	for f in self._filters:
		f.awake(trans)

	def respond(self, trans):
	filters = self._filters
	next = Next(filters)
	first = filters[0]
	first.filter(trans, next)

	def sleep(self, trans):
	for f in self._filters:
		f.sleep(trans)
	#del self._filters


	## Abilities ##

	def canBeThreaded(self):
	""" Returns 0 or 1 to indicate if the servlet can be multithreaded. This value should not change during the lifetime of the object. The default implementation returns 0. Note: This is not currently used. """
	return 0

	def canBeReused(self):
	""" Returns 0 or 1 to indicate if a single servlet instance can be reused. The default is 1, but subclasses con override to return 0. Keep in mind that performance may seriously be degraded if instances can't be reused. Also, there's no known good reasons not to reuse and instance. Remember the awake() and sleep() methods are invoked for every transaction. But just in case, your servlet can refuse to be reused. """
	return 1


	## Server side filesystem ##

	def serverSidePath(self, path=None):
	""" Returns the filesystem path of the page on the server. """
	if self._serverSidePath is None:
		if hasattr(self, "_request") and self._request is not None:
		self._serverSidePath = self._request.serverSidePath()
		else:
		self._serverSidePath = self._transaction.request().serverSidePath()
	if path:
		return os.path.normpath(os.path.join(os.path.dirname(self._serverSidePath), path))
	else:
		return self._serverSidePath


	## Cleanup ##
	
	def clearTransaction(self):
	del self._transaction



class Next(Object):

	def __init__(self, chain, curIdx = 0):
	Object.__init__(self)
	self._chain = chain
	self._curIdx = curIdx

	def __call__(self, trans):
	nextIdx = self._curIdx + 1
	chain = self._chain
	if nextIdx > len(chain):
		raise Exception("next called past end of FilterChain: %s" % chain)
	nextFilter = chain[nextIdx]
	nextNext = Next(chain, nextIdx)
	nextFilter.filter(trans, nextNext)
	
