Index: branches/prototype-v0/zoo-project/zoo-kernel/zoo_service_loader.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 851)
+++ branches/prototype-v0/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 854)
@@ -107,4 +107,5 @@
 #include <dirent.h>
 #include <signal.h>
+#include <execinfo.h>
 #include <unistd.h>
 #ifndef WIN32
@@ -122,4 +123,14 @@
 #include <time.h>
 #include <stdarg.h>
+
+#include <libxml/tree.h>
+#include <libxml/parser.h>
+#include <libxml/xpath.h>
+#include <libxml/xpathInternals.h>
+
+#include <libxslt/xslt.h>
+#include <libxslt/xsltInternals.h>
+#include <libxslt/transform.h>
+#include <libxslt/xsltutils.h>
 
 #ifndef WIN32
@@ -385,4 +396,5 @@
 sig_handler (int sig)
 {
+  
   char tmp[100];
   const char *ssig;
@@ -413,5 +425,5 @@
   sprintf (tmp,
            _
-           ("ZOO Kernel failed to process your request, receiving signal %d = %s"),
+           ("ZOO Kernel failed to process your request, receiving signal %d = %s "),
            sig, ssig);
   errorException (NULL, tmp, "InternalError", NULL);
@@ -990,7 +1002,7 @@
 #ifdef ETC_DIR
 #ifndef WIN32
-    getcwd (ntmp, 1024);
+  getcwd (ntmp, 1024);
 #else
-    _getcwd (ntmp, 1024);
+  _getcwd (ntmp, 1024);
 #endif
 #endif
@@ -1184,5 +1196,5 @@
   fprintf(stderr," ** DEBUG %s %d \n",__FILE__,__LINE__);
   fflush(stderr);
-  dumpMap(r_inputs);
+  //dumpMap(r_inputs);
   fprintf(stderr," ** DEBUG %s %d \n",__FILE__,__LINE__);
   fflush(stderr);
@@ -1294,4 +1306,6 @@
   }
 
+  dumpMap(request_inputs);
+
   if (strncasecmp (REQUEST, "GetCapabilities", 15) == 0)
     {
@@ -1348,5 +1362,4 @@
 #ifdef META_DB
       fetchServicesFromDb(zooRegistry,m,doc,n,printGetCapabilitiesForProcess,1);
-      fprintf(stderr,"************************* %s %d\n\n",__FILE__,__LINE__);
       close_sql(m,0);
 #endif      
@@ -1430,4 +1443,6 @@
 	    r_inputs = NULL;
 	    r_inputs = getMap (request_inputs, "version");
+	    fprintf(stderr," ** DEBUG %s %d \n",__FILE__,__LINE__);
+	    fflush(stderr);
 	    xmlNodePtr n = printWPSHeader(doc,m,"DescribeProcess",
 					  root_nodes[vid][1],(version!=NULL?version->value:"1.0.0"),1);
@@ -1519,5 +1534,5 @@
 			      free (REQUEST);
 			      closedir (dirp);
-			      xmlFreeDoc (doc);
+			      //xmlFreeDoc (doc);
 			      xmlCleanupParser ();
 			      zooXmlCleanupNs ();
@@ -1550,8 +1565,11 @@
 			  addToMap (request_inputs, "metapath", tmpMap->value);
 			map *tmpMapI = getMapFromMaps (m, "lenv", "Identifier");
+			/**
+			 * No support for dot in service name stored in metadb!?
 #ifdef META_DB
-			service* s2=extractServiceFromDb(m,tmpMapI->name,0);
+			service* s2=extractServiceFromDb(m,tmpMapI->value,0);
 			if(s2==NULL){
 #endif
+			*/
 			  s1 = (service *) malloc (SERVICE_SIZE);
 			  t = readServiceFile (m, buff1, &s1, tmpMapI->value);
@@ -1585,5 +1603,5 @@
 			      free (s1);
 			      closedir (dirp);
-			      xmlFreeDoc (doc);
+			      //xmlFreeDoc (doc);
 			      xmlCleanupParser ();
 			      zooXmlCleanupNs ();
@@ -1604,7 +1622,9 @@
 			  hasVal = 1;
 			  setMapInMaps (m, "lenv", "level", "0");
+			  /*
 #ifdef META_DB
 			}
 #endif
+			  */
 		      }
 		    else
@@ -1688,5 +1708,5 @@
 				      free (REQUEST);
 				      closedir (dirp);
-				      xmlFreeDoc (doc);
+				      //xmlFreeDoc (doc);
 				      xmlCleanupParser ();
 				      zooXmlCleanupNs ();
@@ -1736,5 +1756,5 @@
 			free (REQUEST);
 			closedir (dirp);
-			xmlFreeDoc (doc);
+			//xmlFreeDoc (doc);
 			xmlCleanupParser ();
 			zooXmlCleanupNs ();
@@ -1843,7 +1863,5 @@
   }
   s1 = NULL;
-  if(postRequest!=NULL)
-    setMapInMaps (m, "lenv", "xrequest", postRequest->value);
-  
+ 
   r_inputs = getMap (request_inputs, "Identifier");
   map* import = getMapFromMaps (m, IMPORTSERVICE, r_inputs->value); 
@@ -1880,8 +1898,9 @@
   fprintf(stderr,"CONNECTING METADB!\n");
   //FAILED CONNECTING DB
-  if(getMapFromMaps(m,"lenv","dbIssue")!=NULL){
+  if(getMapFromMaps(m,"lenv","dbIssue")!=NULL || metadb_id<0){
     fprintf(stderr,"ERROR CONNECTING METADB\n");
   }
-  s1=extractServiceFromDb(m,r_inputs->value,0);
+  if(metadb_id>=0)
+    s1=extractServiceFromDb(m,r_inputs->value,0);
   //close_sql(m,0);
   if(s1!=NULL){
@@ -2181,7 +2200,9 @@
     {
       int hasValidCookie = -1;
-      char *tcook = zStrdup (cgiCookie);
+      char *tcook = originalCookie = zStrdup (cgiCookie);
+      //fprintf(stderr,">>>>> %s %d %s\n",__FILE__,__LINE__,tcook );
       char *tmp = NULL;
       map *testing = getMapFromMaps (m, "main", "cookiePrefix");
+      parseCookie(&m,originalCookie);
       if (testing == NULL)
         {
@@ -2197,5 +2218,5 @@
         {
           char *token, *saveptr;
-          token = strtok_r (cgiCookie, ";", &saveptr);
+          token = strtok_r (tcook, ";", &saveptr);
           while (token != NULL)
             {
@@ -2244,7 +2265,12 @@
           if (istat == 0 && file_status.st_size > 0)
             {
+	      int saved_stdout = dup (fileno (stdout));
+	      dup2 (fileno (stderr), fileno (stdout));
               conf_read (session_file_path, tmpSess);
               addMapsToMaps (&m, tmpSess);
               freeMaps (&tmpSess);
+	      fflush(stdout);
+	      dup2 (saved_stdout, fileno (stdout));
+	      close(saved_stdout);
             }
 	  free (tmpSess);
@@ -2273,6 +2299,6 @@
       char* tmpName=zStrdup(s);
       char* tmpValue=strstr(s,"=")+1;
-      char* tmpName1=(char*)malloc((1+(len-strlen(tmpValue)))*sizeof(char));
-      snprintf(tmpName1,(len-strlen(tmpValue))+1,"%s",tmpName);
+      char* tmpName1=(char*)malloc((1+(len-(strlen(tmpValue)+1)))*sizeof(char));
+      snprintf(tmpName1,(len-strlen(tmpValue)),"%s",tmpName);
       if(_tmpMaps->content == NULL)
 	_tmpMaps->content = createMap (tmpName1,tmpValue);
@@ -2285,15 +2311,12 @@
   }
   if(_tmpMaps->content!=NULL && getMap(_tmpMaps->content,"HTTP_COOKIE")!=NULL){
-    /*map* tmpMap1=getMap(_tmpMaps->content,"HTTP_COOKIE");
-    free(tmpMap1->value);
-    tmpMap1->value=zStrdup(cgiCookie);*/
-    fprintf(stderr,"[%s]\n",cgiCookie);
     addToMap(_tmpMaps->content,"HTTP_COOKIE1",&cgiCookie[0]);
-    dumpMap(_tmpMaps->content);
   }
   addMapsToMaps (&m, _tmpMaps);
   freeMaps (&_tmpMaps);
   free (_tmpMaps);
-
+  if(postRequest!=NULL)
+    setMapInMaps (m, "renv", "xrequest", postRequest->value);
+  //dumpMaps(m);
 #ifdef WIN32
   char *cgiSidL = NULL;
@@ -2334,5 +2357,4 @@
 				  "ZooWPSClient\0",
 				  INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);*/
-
       if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
 	freeService (&s1);
@@ -2350,16 +2372,15 @@
 	return -1;
       }
-      fprintf(stderr,"*************************\n\n");
-      //fprintf(stderr,"%s \n",json_object_to_json_string_ext(mapsToJson(request_input_real_format),JSON_C_TO_STRING_PLAIN));
-      fprintf(stderr,"*************************\n\n");
       loadServiceAndRun (&m, s1, request_inputs, &request_input_real_format,
                          &request_output_real_format, &eres);
-#ifdef RELY_ON_DB
+
 #ifdef META_DB
-      close_sql(m,1);
+      close_sql(m,0);      
+#endif      
+
+      /*#ifdef RELY_ON_DB
+      //close_sql(m,1);
       //end_sql();
-#endif
-      close_sql(m,0);
-#endif      
+      #endif*/
     }
   else
@@ -2387,4 +2408,5 @@
         }
 #endif
+      //InternetCloseHandle (&hInternet);
       if (pid > 0)
         {
@@ -2411,4 +2433,15 @@
           setMapInMaps (m, "lenv", "async","true");
 	  map* r_inputs1 = createMap("ServiceName", s1->name);
+
+	  /*hInternet = InternetOpen (
+#ifndef WIN32
+				    (LPCTSTR)
+#endif
+				    "ZooWPSClient\0",
+				    INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
+#ifndef WIN32
+	  if (!CHECK_INET_HANDLE (hInternet))
+	    fprintf (stderr, "WARNING : hInternet handle failed to initialize");
+	    #endif*/
 
 	  // Create the filename for the result file (.res)
@@ -2525,13 +2558,34 @@
 	  invokeCallback(m,request_input_real_format,NULL,1,0);
 	  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+	  dumpMaps(request_output_real_format);
 	  if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
 	    freeService (&s1);
 	    free (s1);
+	    fflush (stdout);
+	    fflush (stderr);
 	    fclose (f0);
 	    fclose (f1);
 	    if(dumpBackFinalFile(m,fbkp,fbkp1)<0)
 	      return -1;
+	    //dumpBackFinalFile(m,fbkp,fbkp1);
+#ifndef RELY_ON_DB
+	    dumpMapsToFile(bmap,fbkpres,1);
+	    removeShmLock (m, 1);
+#else
+	    recordResponse(m,fbkp1);
+	    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+	    invokeCallback(m,NULL,NULL,7,0);
+	    fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+#endif
 	    unlink (fbkpid);
 	    unhandleStatus (m);
+#ifdef RELY_ON_DB
+#ifdef META_DB
+	    cleanupCallbackThreads();
+	    close_sql(m,1);
+	    //end_sql();
+#endif
+	    close_sql(m,0);
+#endif
 	    freeMaps (&m);
 	    free (m);
@@ -2544,15 +2598,22 @@
 	    freeMaps (&tmpmaps);
 	    free (tmpmaps);
-	    fflush (stdout);
-	    fflush (stderr);
 	    return -1;
 	  }
+	  dumpMaps(request_output_real_format);
 	  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
 	  invokeCallback(m,request_input_real_format,NULL,1,1);
 	  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
-	  setMapInMaps(m,"lenv","ds_nb","1");
-          loadServiceAndRun (&m, s1, request_inputs,
-                             &request_input_real_format,
-                             &request_output_real_format, &eres);
+          if(getMapFromMaps(m,"lenv","mapError")!=NULL){
+		setMapInMaps(m,"lenv","message",_("Issue with geographic data"));
+	  	invokeCallback(m,NULL,NULL,7,0);
+		eres=-1;//SERVICE_FAILED;
+	  }else{
+	    setMapInMaps(m,"lenv","ds_nb","0");
+	    close_sql(m,0);
+	    loadServiceAndRun (&m, s1, request_inputs,
+			       &request_input_real_format,
+			       &request_output_real_format, &eres);
+	    setMapInMaps(m,"lenv","ds_nb","2");
+	  }
         }
       else
@@ -2573,4 +2634,6 @@
   fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__);
 #endif
+fflush(stdout);
+rewind(stdout);
   if (eres != -1)
     outputResponse (s1, request_input_real_format,
@@ -2595,9 +2658,9 @@
   if (((int) getpid ()) != cpid || cgiSid != NULL)
     {
-      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+      if (eres == SERVICE_SUCCEEDED)
+	invokeCallback(m,NULL,request_output_real_format,5,1);
+
       fflush(stderr);
-      invokeCallback(m,NULL,request_output_real_format,5,1);
-      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
-      fflush(stderr);
+      fflush(stdout);
 
       fclose (stdout);
@@ -2619,5 +2682,5 @@
 	setMapInMaps(m,"lenv","fstate",wpsStatus[0]);
 	break;
-      }      
+      }
 #ifndef RELY_ON_DB
       dumpMapsToFile(bmap,fbkpres,1);
@@ -2625,7 +2688,6 @@
 #else
       recordResponse(m,fbkp1);
-      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
-      invokeCallback(m,NULL,request_output_real_format,6,0);
-      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+      if (eres == SERVICE_SUCCEEDED)
+	invokeCallback(m,NULL,request_output_real_format,6,0);
 #endif
       freeMaps(&bmap);
@@ -2637,12 +2699,10 @@
       cleanupCallbackThreads();
       close_sql(m,1);
-      //end_sql();
 #endif
       close_sql(m,0);
-#endif
-      unlink (flog);
+      end_sql();
+#endif
       free(fbkpid);
       free(fbkpres); 
-      free (flog);           
       free (fbkp1);
       // free (tmps1); // tmps1 is stack memory and should not be freed
@@ -2653,4 +2713,6 @@
       fflush(stderr);
       fclose (stderr);
+      unlink (flog);
+      free (flog);
     }
   else{
