Index: branches/prototype-v0/zoo-project/zoo-kernel/zoo_service_loader.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/zoo_service_loader.c	(revision 839)
@@ -102,4 +102,7 @@
 #endif
 
+#include "service_json.h"
+#include "service_callback.h"
+
 #include <dirent.h>
 #include <signal.h>
@@ -158,7 +161,7 @@
 
 #ifdef WIN32
-  #ifndef PROGRAMNAME
-    #define PROGRAMNAME "zoo_loader.cgi"
-  #endif
+#ifndef PROGRAMNAME
+#define PROGRAMNAME "zoo_loader.cgi"
+#endif
 #endif
 
@@ -213,4 +216,5 @@
   fclose (f2);
   fclose (f3);
+  free(tmps1);
   return 1;
 }
@@ -233,7 +237,7 @@
  */
 int
-recursReaddirF ( maps * m, registry *r, xmlNodePtr n, char *conf_dir, 
+recursReaddirF ( maps * m, registry *r, xmlDocPtr doc, xmlNodePtr n, char *conf_dir, 
 		 char *prefix, int saved_stdout, int level, 
-		 void (func) (registry *, maps *, xmlNodePtr, service *) )
+		 void (func) (registry *, maps *, xmlDocPtr, xmlNodePtr, service *) )
 {
   struct dirent *dp;
@@ -282,5 +286,5 @@
             setMapInMaps (m, "lenv", "level", levels1);
             res =
-              recursReaddirF (m, r, n, tmp, prefix, saved_stdout, level + 1,
+              recursReaddirF (m, r, doc, n, tmp, prefix, saved_stdout, level + 1,
                               func);
             sprintf (levels1, "%d", level);
@@ -320,5 +324,5 @@
                   return -1;
                 }
-  #ifndef DEBUG
+  #ifdef DEBUG
               fprintf (stderr, "#################\n%s\n#################\n",
                        tmps1);
@@ -346,5 +350,5 @@
   #endif
 	      inheritance(r,&s1);
-              func (r, m, n, s1);
+              func (r, m, doc, n, s1);
               freeService (&s1);
               free (s1);
@@ -949,6 +953,9 @@
 #endif
 
+  
   map *r_inputs = NULL;
   map *request_inputs = *inputs;
+  //fprintf(stderr,"%s \n",json_object_to_json_string_ext(mapToJson(request_inputs),JSON_C_TO_STRING_PLAIN));
+  
 #ifdef IGNORE_METAPATH
   addToMap(request_inputs, "metapath", "");
@@ -1305,5 +1312,5 @@
 	    }
 	    inheritance(zooRegistry, &svc);
-	    printGetCapabilitiesForProcess(zooRegistry, m, n, svc);
+	    printGetCapabilitiesForProcess(zooRegistry, m, doc, n, svc);
 	    freeService(&svc);
 	    free(svc);                             
@@ -1314,5 +1321,5 @@
 
       if (int res =		  
-          recursReaddirF (m, zooRegistry, n, conf_dir, NULL, saved_stdout, 0,
+          recursReaddirF (m, zooRegistry, doc, n, conf_dir, NULL, saved_stdout, 0,
                           printGetCapabilitiesForProcess) < 0)
         {
@@ -1331,5 +1338,7 @@
       dup2 (saved_stdout, fileno (stdout));
 #ifdef META_DB
-      fetchServicesFromDb(zooRegistry,m,n,printGetCapabilitiesForProcess);
+      fetchServicesFromDb(zooRegistry,m,doc,n,printGetCapabilitiesForProcess,1);
+      fprintf(stderr,"************************* %s %d\n\n",__FILE__,__LINE__);
+      close_sql(m,0);
 #endif      
       printDocument (m, doc, getpid ());
@@ -1353,5 +1362,5 @@
 	  runGetStatus(m,r_inputs->value,REQUEST);
 	  freeMaps (&m);
-	  free (m);
+	  free(m);
 	  if(zooRegistry!=NULL){
 	    freeRegistry(&zooRegistry);
@@ -1429,5 +1438,9 @@
 		      }
 		      inheritance(zooRegistry, &svc);
-		      printDescribeProcessForProcess(zooRegistry, m, n, svc);
+#ifdef USE_HPC
+		      addNestedOutputs(&svc);
+#endif
+
+		      printDescribeProcessForProcess(zooRegistry, m, doc, n, svc);
 		      freeService(&svc);
 		      free(svc);                             
@@ -1438,9 +1451,10 @@
   
 		if (int res =
-		    recursReaddirF (m, zooRegistry, n, conf_dir, NULL, saved_stdout, 0,
+		    recursReaddirF (m, zooRegistry, doc, n, conf_dir, NULL, saved_stdout, 0,
 				    printDescribeProcessForProcess) < 0)
 		  return res;
 #ifdef META_DB
-		fetchServicesFromDb(zooRegistry,m,n,printDescribeProcessForProcess);
+		fetchServicesFromDb(zooRegistry,m,doc,n,printDescribeProcessForProcess,0);
+		close_sql(m,0);
 #endif      
 
@@ -1461,7 +1475,5 @@
 		      {
 #ifdef META_DB			
-			service* s2=extractServiceFromDb(m,import->name);
-			fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-			fflush(stderr);
+			service* s2=extractServiceFromDb(m,import->name,0);
 			if(s2==NULL){
 #endif
@@ -1500,7 +1512,9 @@
 			  dumpService (s1);
 #endif
-
 			  inheritance(zooRegistry,&s1);
-			  printDescribeProcessForProcess (zooRegistry,m, n, s1);
+#ifdef USE_HPC
+			  addNestedOutputs(&s1);
+#endif
+			  printDescribeProcessForProcess (zooRegistry, m, doc, n, s1);
 			  freeService (&s1);
 			  free (s1);
@@ -1520,9 +1534,6 @@
 			  addToMap (request_inputs, "metapath", tmpMap->value);
 			map *tmpMapI = getMapFromMaps (m, "lenv", "Identifier");
-
 #ifdef META_DB
-			service* s2=extractServiceFromDb(m,tmpMapI->name);
-			fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-			fflush(stderr);
+			service* s2=extractServiceFromDb(m,tmpMapI->name,0);
 			if(s2==NULL){
 #endif
@@ -1567,5 +1578,8 @@
 #endif
 			  inheritance(zooRegistry,&s1);
-			  printDescribeProcessForProcess (zooRegistry,m, n, s1);
+#ifdef USE_HPC
+			  addNestedOutputs(&s1);
+#endif
+			  printDescribeProcessForProcess (zooRegistry, m, doc, n, s1);
 			  freeService (&s1);
 			  free (s1);
@@ -1580,14 +1594,17 @@
 		    else
 		      {
-			fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-			fflush(stderr);
 #ifdef META_DB
 			_init_sql(m,"metadb");
-			service* s2=extractServiceFromDb(m,corig);
+			//FAILED CONNECTING DB
+			if(getMapFromMaps(m,"lenv","dbIssue")!=NULL){
+			  fprintf(stderr,"ERROR CONNECTING METADB");
+			}
+			service* s2=extractServiceFromDb(m,corig,0);
 			if(s2!=NULL){
-			  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-			  fflush(stderr);
 			  inheritance(zooRegistry,&s2);
-			  printDescribeProcessForProcess (zooRegistry,m, n, s2);
+#ifdef USE_HPC
+			  addNestedOutputs(&s2);
+#endif
+			  printDescribeProcessForProcess (zooRegistry,m, doc, n, s2);
 			  freeService (&s2);
 			  free (s2);
@@ -1664,5 +1681,8 @@
 #endif
 				  inheritance(zooRegistry,&s1);
-				  printDescribeProcessForProcess (zooRegistry,m, n, s1);
+#ifdef USE_HPC
+				  addNestedOutputs(&s1);
+#endif
+				  printDescribeProcessForProcess (zooRegistry,m, doc, n, s1);
 				  freeService (&s1);
 				  free (s1);
@@ -1725,4 +1745,8 @@
 	    free (SERVICE_URL);
 	    fflush (stdout);
+#ifdef META_DB
+	    close_sql(m,0);
+      //end_sql();
+#endif
 	    return 0;
 	  }
@@ -1803,21 +1827,8 @@
   }
   s1 = NULL;
-  s1 = (service *) malloc (SERVICE_SIZE);
-  if (s1 == NULL)
-    {
-      freeMaps (&m);
-      free (m);
-      if(zooRegistry!=NULL){
-	freeRegistry(&zooRegistry);
-	free(zooRegistry);
-      }
-      free (REQUEST);
-      free (SERVICE_URL);
-      return errorException (m, _("Unable to allocate memory"),
-                             "InternalError", NULL);
-    }
-
+  if(postRequest!=NULL)
+    setMapInMaps (m, "lenv", "xrequest", postRequest->value);
+  
   r_inputs = getMap (request_inputs, "Identifier");
-
   map* import = getMapFromMaps (m, IMPORTSERVICE, r_inputs->value); 
   if (import != NULL && import->value != NULL) { 
@@ -1848,8 +1859,47 @@
 
   r_inputs = getMapFromMaps (m, "lenv", "Identifier");
+  
+#ifdef META_DB
+  int metadb_id=_init_sql(m,"metadb");
+  fprintf(stderr,"CONNECTING METADB!\n");
+  //FAILED CONNECTING DB
+  if(getMapFromMaps(m,"lenv","dbIssue")!=NULL){
+    fprintf(stderr,"ERROR CONNECTING METADB\n");
+  }
+  s1=extractServiceFromDb(m,r_inputs->value,0);
+  //close_sql(m,0);
+  if(s1!=NULL){
+    inheritance(zooRegistry,&s1);
+#ifdef USE_HPC
+    addNestedOutputs(&s1);
+#endif
+    if(zooRegistry!=NULL){
+      freeRegistry(&zooRegistry);
+      free(zooRegistry);
+    }
+  }else /* Not found in MetaDB */{
+#endif
+  s1 = (service *) malloc (SERVICE_SIZE);
+  if (s1 == NULL)
+    {
+      freeMaps (&m);
+      free (m);
+      if(zooRegistry!=NULL){
+	freeRegistry(&zooRegistry);
+	free(zooRegistry);
+      }
+      free (REQUEST);
+      free (SERVICE_URL);
+      return errorException (m, _("Unable to allocate memory"),
+                             "InternalError", NULL);
+    }
+
   int saved_stdout = dup (fileno (stdout));
   dup2 (fileno (stderr), fileno (stdout));
   t = readServiceFile (m, tmps1, &s1, r_inputs->value);
   inheritance(zooRegistry,&s1);
+#ifdef USE_HPC
+  addNestedOutputs(&s1);
+#endif
   if(zooRegistry!=NULL){
     freeRegistry(&zooRegistry);
@@ -1875,5 +1925,8 @@
     }
   close (saved_stdout);
-
+#ifdef META_DB
+  }
+#endif
+  
 #ifdef DEBUG
   dumpService (s1);
@@ -1912,5 +1965,6 @@
     return 0;
   }
-
+  //InternetCloseHandle (&hInternet);
+  
   // Define each env variable in runing environment
   maps *curs = getMaps (m, "env");
@@ -2052,4 +2106,10 @@
   int cpid = getpid ();
 
+  // Create a map containing a copy of the request map
+  maps *_tmpMaps = createMaps("request");
+  addMapToMap(&_tmpMaps->content,request_inputs);
+  addMapsToMaps (&m, _tmpMaps);
+  freeMaps (&_tmpMaps);
+  free (_tmpMaps);
   /**
    * Initialize the specific [lenv] section which contains runtime variables:
@@ -2072,5 +2132,5 @@
    *
    */
-  maps *_tmpMaps = createMaps("lenv");
+  _tmpMaps = createMaps("lenv");
   char tmpBuff[100];
   struct ztimeval tp;
@@ -2182,21 +2242,25 @@
 #endif
   int ei = 1;
-  char *s = 
+  char **orig = 
 #ifdef WIN32
-    GetEnvironmentStrings();
+    GetEnvironmentStrings()
 #else
-    *environ;
-#endif
+    environ
+#endif
+    ;
+  char *s=*orig;
   _tmpMaps = createMaps("renv");
   for (; s; ei++) {
     char* tmpName=zStrdup(s);
     char* tmpValue=strstr(s,"=")+1;
-    tmpName[strlen(tmpName)-strlen(tmpValue)-1]=0;
+    char* tmpName1=(char*)malloc((1+(strlen(tmpName)-strlen(tmpValue)))*sizeof(char));
+    snprintf(tmpName1,(strlen(tmpName)-strlen(tmpValue)),"%s",tmpName);
     if(_tmpMaps->content == NULL)
-      _tmpMaps->content = createMap (tmpName,tmpValue);
+      _tmpMaps->content = createMap (tmpName1,tmpValue);
     else
-      addToMap (_tmpMaps->content,tmpName,tmpValue);
+      addToMap (_tmpMaps->content,tmpName1,tmpValue);
     free(tmpName);
-    s = *(environ+ei);
+    free(tmpName1);
+    s = *(orig+ei);
   }
   addMapsToMaps (&m, _tmpMaps);
@@ -2228,4 +2292,7 @@
   }
 #endif
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  invokeCallback(m,NULL,NULL,0,0);
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
 
   char *fbkp, *fbkpid, *fbkpres, *fbkp1, *flog;
@@ -2236,4 +2303,11 @@
   if (status == NULLMAP)
     {
+      /*        hInternet = InternetOpen (
+#ifndef WIN32
+				  (LPCTSTR)
+#endif
+				  "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);
@@ -2251,4 +2325,7 @@
 	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);
@@ -2300,4 +2377,5 @@
           int cpid = atoi (tmpm->value);
           r_inputs = getMapFromMaps (m, "main", "tmpPath");
+          setMapInMaps (m, "lenv", "async","true");
 	  map* r_inputs1 = createMap("ServiceName", s1->name);
 
@@ -2396,4 +2474,20 @@
           f1 = freopen (fbkp1, "w+", stdout);
 
+	  map* serviceTypeMap=getMap(s1->content,"serviceType");
+	  if(serviceTypeMap!=NULL)
+	    setMapInMaps (m, "lenv", "serviceType", serviceTypeMap->value);
+
+          char *flenv =
+            (char *)
+            malloc ((strlen (r_inputs->value) + 
+                     strlen (usid->value) + 12) * sizeof (char));
+          sprintf (flenv, "%s/%s_lenv.cfg", r_inputs->value, usid->value);
+	  maps* lenvMaps=getMaps(m,"lenv");
+	  dumpMapsToFile(lenvMaps,flenv,0);
+	  free(flenv);
+
+	  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+	  invokeCallback(m,request_input_real_format,NULL,1,0);
+	  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
 	  if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
 	    freeService (&s1);
@@ -2419,4 +2513,5 @@
 	    return -1;
 	  }
+	  setMapInMaps(m,"lenv","ds_nb","1");
           loadServiceAndRun (&m, s1, request_inputs,
                              &request_input_real_format,
@@ -2435,8 +2530,9 @@
     }
 
-  //#ifdef DEBUG
+#ifdef DEBUG
+  fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__);
   dumpMaps (request_output_real_format);
-  //#endif
-  //sleep(120);
+  fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__);
+#endif
   if (eres != -1)
     outputResponse (s1, request_input_real_format,
@@ -2462,5 +2558,4 @@
     {
       fclose (stdout);
-      fclose (stderr);
 
       fclose (f0);
@@ -2486,4 +2581,7 @@
 #else
       recordResponse(m,fbkp1);
+      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+      invokeCallback(m,NULL,request_output_real_format,5,1);
+      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
 #endif
       freeMaps(&bmap);
@@ -2492,4 +2590,8 @@
       unlink (flog);
       unhandleStatus (m);
+#ifdef META_DB
+      close_sql(m,0);
+      //end_sql();
+#endif
       free(fbkpid);
       free(fbkpres); 
@@ -2499,6 +2601,16 @@
       if(cgiSid!=NULL)
 	free(cgiSid);
-    }
-
+      //InternetCloseHandle (&hInternet);  
+      fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__);
+      fflush(stderr);
+      fclose (stderr);
+    }
+  else{
+    //InternetCloseHandle (&hInternet);  
+#ifdef META_DB
+    //close_sql(m,0);
+#endif
+  }
+  
   freeService (&s1);
   free (s1);
