Index: src/edu/ucsb/nceas/metacat/MetaCatServlet.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 2728)
+++ src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 2729)
@@ -48,6 +48,7 @@
 import java.util.Vector;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
+import java.util.Timer;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
@@ -168,6 +169,8 @@
     private static final String CONFIG_NAME = "metacat.properties";
      	 
     private static Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
+       
+    private Timer timer = null;
     
     /**
      * Initialize the servlet by creating appropriate database connections
@@ -213,6 +216,17 @@
             // Index the paths specified in the metacat.properties
             checkIndexPaths();
 
+            // initiate the indexing Queue
+            IndexingQueue.getInstance();
+            
+            // start the IndexingThread if indexingTimerTaskTime more than 0. 
+            // It will index all the documents not yet indexed in the database             
+            int indexingTimerTaskTime = Integer.parseInt(MetaCatUtil.getOption("indexingTimerTaskTime"));
+            if(indexingTimerTaskTime > 0){
+            	timer = new Timer();
+            	timer.schedule(new IndexingTimerTask(), 0, indexingTimerTaskTime);
+            }
+            
             MetaCatUtil.printMessage("Metacat (" + Version.getVersion()
                                + ") initialized.");
 
@@ -232,6 +246,7 @@
         // Close all db connection
         System.out.println("Destroying MetacatServlet");
         DBConnectionPool.release();
+        timer.cancel();
     }
 
     /** Handle "GET" method requests from HTTP clients */
@@ -303,7 +318,7 @@
                               + "n.nodedata, n.nodedatanumerical, n.parentnodeid"
                               + " FROM xml_nodes n, xml_index i WHERE"
                               + " i.path = ? and n.parentnodeid=i.nodeid and"
-                              + " n.nodetype LIKE 'TEXT'");
+                              + " n.nodetype LIKE 'TEXT' order by n.parentnodeid");
                         pstmt.setString(1, (String) MetaCatUtil.
                                         pathsForIndexing.elementAt(i));
                         pstmt.execute();
@@ -1799,6 +1814,7 @@
                 out.println(npe.getMessage());
                 out.println("</error>");
                 logMetacat.warn("Error in writing eml document to the database" + npe.getMessage());
+                npe.printStackTrace();
             }
         } catch (Exception e) {
             //response.setContentType("text/xml");
@@ -1807,6 +1823,7 @@
             out.println(e.getMessage());
             out.println("</error>");
             logMetacat.warn("Error in writing eml document to the database" + e.getMessage());
+            e.printStackTrace();
         }
     }
 
@@ -1892,8 +1909,10 @@
 
         	// find if the root element has prefix
         	String prefix = getPrefix(targetLine);
+        	logMetacat.info("prefix is:" + prefix);
         	int startIndex = 0;
         	
+        	
         	if(prefix != null)
         	{
         		// if prefix found then look for xmlns:prefix
@@ -1901,11 +1920,14 @@
         		String namespaceWithPrefix = NAMESPACEKEYWORD 
         					+ ":" + prefix;
         		startIndex = targetLine.indexOf(namespaceWithPrefix);
+            	logMetacat.info("namespaceWithPrefix is:" + namespaceWithPrefix+":");
+            	logMetacat.info("startIndex is:" + startIndex);
         		
         	} else {
         		// if prefix not found then look for xmlns
         		// attribute to find the ns 
         		startIndex = targetLine.indexOf(NAMESPACEKEYWORD);
+            	logMetacat.info("startIndex is:" + startIndex);
         	}
         		
             int start = 1;
@@ -1928,19 +1950,26 @@
             } 
             // else: xmlns not found. namespace = null will be returned
 
+         	logMetacat.info("targetLine is " + targetLine);
+         	logMetacat.debug("start is " + end);
+         	logMetacat.debug("end is " + end);
+           
             if(start < end){
             	namespaceString = targetLine.substring(start + 1, end);
+            	logMetacat.info("namespaceString is " + namespaceString);
             }
             logMetacat.info("namespace in xml is: "
                     + namespaceString);
-            if (namespaceString.indexOf(eml2_0_0NameSpace) != -1) {
-                namespace = eml2_0_0NameSpace;
-            } else if (namespaceString.indexOf(eml2_0_1NameSpace) != -1) {
-                namespace = eml2_0_1NameSpace;
-            } else if (namespaceString.indexOf(eml2_1_0NameSpace) != -1) {
-                namespace = eml2_1_0NameSpace;
-            } else {
-            	namespace = namespaceString;
+            if(namespaceString != null){
+            	if (namespaceString.indexOf(eml2_0_0NameSpace) != -1) {
+            		namespace = eml2_0_0NameSpace;
+            	} else if (namespaceString.indexOf(eml2_0_1NameSpace) != -1) {
+            		namespace = eml2_0_1NameSpace;
+            	} else if (namespaceString.indexOf(eml2_1_0NameSpace) != -1) {
+            		namespace = eml2_1_0NameSpace;
+            	} else {
+            		namespace = namespaceString;
+            	}
             }
         }
 
@@ -2006,14 +2035,13 @@
     {
     	String prefix = null;
     	String rootElement = schemaLine.substring(0, schemaLine.indexOf(" "));
-        logMetacat.warn("rootElement:" + rootElement);
+        logMetacat.info("rootElement:" + rootElement);
         
         if(rootElement.indexOf(":") > 0){
         	prefix = rootElement.substring(rootElement.indexOf(":") + 1,
         			rootElement.length());
         } 
-        logMetacat.warn("prefix:" + prefix);        
-        return prefix;
+        return prefix.trim();
     }
 
     /**
