Index: /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/OAIServer.java
===================================================================
--- /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/OAIServer.java	(revision 28851)
+++ /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/OAIServer.java	(revision 28852)
@@ -27,4 +27,5 @@
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
+import javax.servlet.UnavailableException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -75,16 +76,8 @@
 
 	/**
-	 * a converter class to parse XML and create Docs This is only used for
-	 * generating internal requests passed to MessageRouter. The response
-	 * message is generated by parsing an existing xml skeleton file
-	 * (web/WEB-INF/oaixml/oaiversion2.xml, for example).
+	 * a converter class to parse XML and create Documents for 
+	 * XML generation
 	 */
 	protected XMLConverter converter = null;
-	/**
-	 * container Document to create XML Nodes (but only request to the oai
-	 * receptionist, not response (which is created in OAIXML.java) created by
-	 * converter class
-	 */
-	protected Document doc = null;
 
 	/** A HashSet which contains all the legal verbs. */
@@ -101,4 +94,5 @@
 	protected String site = "";
 
+  // can be overriddden in OAIConfig.xml
 	// do we output the stylesheet processing instruction?
 	protected boolean use_oai_stylesheet = true;
@@ -136,7 +130,7 @@
 			if (remote_site_name == null || remote_site_type == null || remote_site_address == null)
 			{
-				System.err.println("initialisation paramters not all set!");
-				System.err.println("if site_name is not set, then you must have remote_site_name, remote_site_type and remote_site_address set");
-				System.exit(1);
+				logger.error("initialisation paramters not all set!");
+				logger.error("if site_name is not set, then you must have remote_site_name, remote_site_type and remote_site_address set");
+				throw new UnavailableException("OAIServer: incorrect servlet parameters");
 			}
 		}
@@ -150,5 +144,6 @@
 		// the receptionist -the servlet will talk to this
 		this.recept = new OAIReceptionist();
-
+		// the converter - used to get new Documents to generate XML messages
+		this.converter = new XMLConverter();
 		// the receptionist uses a MessageRouter or Communicator to send its requests to. We either create a MessageRouter here for the designated site (if site_name set), or we create a Communicator for a remote site. The is given to teh Receptionist, and the servlet never talks to it again.directly.
 		if (site_name != null)
@@ -169,7 +164,6 @@
 				catch (Exception e)
 				{ // cant use this new one, so use normal one
-					System.err.println("OAIServlet configure exception when trying to use a new MessageRouter " + mr_name + ": " + e.getMessage());
-					e.printStackTrace();
-					message_router = new MessageRouter();
+				  logger.error("OAIServlet configure exception when trying to use a new MessageRouter " + mr_name, e);
+				  message_router = new MessageRouter();
 				}
 			}
@@ -177,5 +171,7 @@
 			message_router.setSiteName(site_name);
 			// lots of work is done in this step; see MessageRouter.java
-			message_router.configure();
+			if (!message_router.configure()) {
+			  throw new UnavailableException("OAIServer: Couldn't configure MessageRouter");
+			}
 			this.recept.setSiteName(site_name);
 			this.recept.setMessageRouter(message_router);
@@ -187,5 +183,6 @@
 			Communicator communicator = null;
 			// we need to create the XML to configure the communicator
-			Element site_elem = this.doc.createElement(GSXML.SITE_ELEM);
+			Document site_doc = this.converter.newDOM();
+			Element site_elem = site_doc.createElement(GSXML.SITE_ELEM);
 			site_elem.setAttribute(GSXML.TYPE_ATT, remote_site_type);
 			site_elem.setAttribute(GSXML.NAME_ATT, remote_site_name);
@@ -198,35 +195,35 @@
 			else
 			{
-				System.err.println("OAIServlet.init Error: invalid Communicator type: " + remote_site_type);
-				System.exit(1);
+				logger.error("OAIServlet.init Error: invalid Communicator type: " + remote_site_type);
+				throw new UnavailableException("OAIServer: invalid communicator type");
 			}
 
 			if (!communicator.configure(site_elem))
 			{
-				System.err.println("OAIServlet.init Error: Couldn't configure communicator");
-				System.exit(1);
+				logger.error("OAIServlet.init Error: Couldn't configure communicator");
+				throw new UnavailableException("OAIServer: Couldn't configure communicator");
 			}
 			this.recept.setSiteName(remote_site_name);
 			this.recept.setMessageRouter(communicator);
 		}
-		// used for composing internal xml requests, but not xml responses.
-		// the converter may be used to get pretty xml, though.    
-		this.converter = new XMLConverter();
-		this.doc = this.converter.newDOM();
 
 		// Read in OAIConfig.xml (residing web/WEB-INF/classes/) and 
-		//use it to configure the receptionist. The init() is also called in which
-		//the resumption token file is read in and all expired tokens cleared.
+		//use it to configure the receptionist. 
 		Element oai_config = OAIXML.getOAIConfigXML();
 		if (oai_config == null)
 		{
 			logger.error("Fail to parse oai config file OAIConfig.xml.");
-			return;
+			throw new UnavailableException("OAIServer: Couldn't parse OAIConfig.xml");
 		}
 		// pass it to the receptionist
-		this.recept.configure(oai_config);
-
+		if (!this.recept.configure(oai_config)) {
+		  logger.error("Couldn't configure receptionist");
+		  throw new UnavailableException("OAIServer: Couldn't configure receptionist"); 
+		}
 		// also, we have something we want to get from here - useOAIStylesheet
 		this.configure(oai_config);
+		// Initialise the resumption tokens
+		OAIResumptionToken.init();
+
 	}//end of init()
 
@@ -331,6 +328,4 @@
 	{
 		logUsageInfo(request);
-		//out.println("url="+request.getRequestURL());// /oaiserver    
-		//out.println("query="+request.getQueryString());// is /greenstone3
 
 		// oai always requires the content type be text/xml
@@ -355,5 +350,6 @@
 		String[] pairs = (query == null) ? null : query.split("&");//split into key/value pairs
 		String verb = getVerb(query);
-		Element xml_response = OAIXML.createBasicResponse(verb, pairs);
+		Document response_doc = this.converter.newDOM();
+		Element xml_response = OAIXML.createBasicResponse(response_doc, verb, pairs);
 		Element verb_elem = null;
 
@@ -363,5 +359,5 @@
 			{
 				logger.error(OAIXML.BAD_VERB + ": " + query);
-				verb_elem = OAIXML.createErrorElement(OAIXML.BAD_VERB, OAIXML.ILLEGAL_OAI_VERB);
+				verb_elem = OAIXML.createErrorElement(response_doc, OAIXML.BAD_VERB, OAIXML.ILLEGAL_OAI_VERB);
 			}
 			else
@@ -369,5 +365,5 @@
 				//must be something else other than bad verbs caused an error, so bad argument
 				logger.error(OAIXML.BAD_ARGUMENT + ": " + query);
-				verb_elem = OAIXML.createErrorElement(OAIXML.BAD_ARGUMENT, "");
+				verb_elem = OAIXML.createErrorElement(response_doc, OAIXML.BAD_ARGUMENT, "");
 			}
 			xml_response.appendChild(verb_elem);
@@ -384,6 +380,7 @@
 		// The query is valid, we can now
 		// compose the request message to the receptionist
-		Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
-		Element xml_request = this.doc.createElement(GSXML.REQUEST_ELEM);
+		Document request_doc = this.converter.newDOM();
+		Element xml_message = request_doc.createElement(GSXML.MESSAGE_ELEM);
+		Element xml_request = request_doc.createElement(GSXML.REQUEST_ELEM);
 		// The type attribute is set to be 'oaiService' from OAIServer to OAIReceptionist.
 		//xml_request.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SERVICE);
@@ -399,5 +396,5 @@
 		{
 			logger.info("xml_result is null");
-			verb_elem = OAIXML.createErrorElement("Internal error", "");
+			verb_elem = OAIXML.createErrorElement(response_doc, "Internal error", "");
 			xml_response.appendChild(verb_elem);
 		}
@@ -410,9 +407,9 @@
 			 * is optional! </resumptionToken> </verb> </response> </message>
 			 */
-			Node res = GSXML.getChildByTagName(xml_result, OAIXML.RESPONSE);
+			Node res = GSXML.getChildByTagName(xml_result, GSXML.RESPONSE_ELEM);
 			if (res == null)
 			{
 				logger.info("response element in xml_result is null");
-				verb_elem = OAIXML.createErrorElement("Internal error", "");
+				verb_elem = OAIXML.createErrorElement(response_doc, "Internal error", "");
 			}
 			else
@@ -421,7 +418,10 @@
 			}
 
-			if (OAIXML.oai_version.equals(OAIXML.OAI_VERSION2) || verb_elem.getTagName().equals(OAIXML.ERROR))
-			{
-				xml_response.appendChild(xml_response.getOwnerDocument().importNode(verb_elem, true));
+			if ( verb_elem.getTagName().equals(OAIXML.ERROR))
+			{
+			  xml_response.appendChild(response_doc.importNode(verb_elem, true));
+			}
+			else if (OAIXML.oai_version.equals(OAIXML.OAI_VERSION2)) {
+			  xml_response.appendChild(response_doc.importNode(verb_elem, true));
 			}
 			else
@@ -442,4 +442,5 @@
 	public void addParams(Element request, String[] pairs)
 	{
+	  Document doc = request.getOwnerDocument();
 		// no params apart from the verb
 		if (pairs == null || pairs.length < 2)
@@ -456,8 +457,6 @@
 			if (index != -1)
 			{ //just a double check
-				Element param = this.doc.createElement(OAIXML.PARAM);
-				param.setAttribute(OAIXML.NAME, pairs[i].substring(0, index));
-				param.setAttribute(OAIXML.VALUE, OAIXML.oaiDecode(pairs[i].substring(index + 1)));
-				request.appendChild(param);
+			  Element param = GSXML.createParameter(doc, pairs[i].substring(0, index), OAIXML.oaiDecode(pairs[i].substring(index + 1)));
+			  request.appendChild(param);
 			}
 		}
