package com.jxml.quick.recycle;

import com.jxml.quick.*;
import org.xml.sax.SAXException;

/**
 * Manages a pool or reusable QContext objects.
 */
public final class QContextPool
{
	private QDoc schema;
	private QPool pool=new QPool();

	/**
	 * Create QContext objects for the given schema.
	 */
	public QContextPool(QDoc schema)
	{
		this.schema=schema;
	}

	/**
	 * Allocate a previously used context. If none exists, create a new one.
	 */
	public synchronized QContext allocate()
		throws SAXException
	{
		QContext context=(QContext)pool.allocate();
		if (context!=null)
			return context;
		return com.jxml.quick.engine.QCMLBindings.createContext(schema);
	}

	/**
	 * The context is now available for reuse.
	 */
	public synchronized void release(QContext context)
	{
		context.doc=null;				// fix empty stack bug
		context.cl=null;
		pool.recycle(context);
	}
}
