Index: branches/prototype-v0/thirds/otb2zcfg/otb2zcfg.cxx
===================================================================
--- branches/prototype-v0/thirds/otb2zcfg/otb2zcfg.cxx	(revision 822)
+++ branches/prototype-v0/thirds/otb2zcfg/otb2zcfg.cxx	(revision 839)
@@ -312,4 +312,5 @@
 	  std::string s=m_Application->GetParameterDescription(paramKey);
 	  if(s.length()>0){
+	    s=ReplaceAll(ReplaceAll(s,std::string("\n"),std::string("")),std::string("\t"),std::string(""));
 	    std::cout << "   Title = " << s << std::endl;
 	    std::cout << "   Abstract = " << s << std::endl;
Index: branches/prototype-v0/thirds/zcfg2sql/Makefile
===================================================================
--- branches/prototype-v0/thirds/zcfg2sql/Makefile	(revision 839)
+++ branches/prototype-v0/thirds/zcfg2sql/Makefile	(revision 839)
@@ -0,0 +1,11 @@
+ZRPATH=../../zoo-project
+include ${ZRPATH}/zoo-kernel/ZOOMakefile.opts
+LCFLAGS=${CFLAGS} ${ZOO_CFLAGS} ${XML2CFLAGS} ${GDAL_CFLAGS} ${PYTHONCFLAGS} -DLINUX_FREE_ISSUE #-DDEBUG
+CC=gcc
+
+zcfg2sql: zcfg2sql.c
+	g++ -g -O2 ${JSCFLAGS} ${RUBYCFLAGS} ${XML2CFLAGS} ${LCFLAGS} -c zcfg2sql.c  -fno-common -DPIC -o zcfg2sql.o
+	g++  ${XML2CFLAGS} ${LCFLAGS} zcfg2sql.o ${ZRPATH}/zoo-kernel/server_internal.o ${ZRPATH}/zoo-kernel/service_internal.o ${ZRPATH}/zoo-kernel/${MS_FILE} ${ZRPATH}/zoo-kernel/response_print.o ${ZRPATH}/zoo-kernel/lex.cr.o ${ZRPATH}/zoo-kernel/lex.sr.o ${ZRPATH}/zoo-kernel/service_conf.tab.o ${ZRPATH}/zoo-kernel/main_conf_read.tab.o  ${ZRPATH}/zoo-kernel/${YAML_FILE} -o zcfg2sql -L${ZRPATH}/zoo-kernel/ ${LDFLAGS}
+
+clean:
+	rm -f zcfg2sql
Index: branches/prototype-v0/thirds/zcfg2sql/zcfg2sql.c
===================================================================
--- branches/prototype-v0/thirds/zcfg2sql/zcfg2sql.c	(revision 839)
+++ branches/prototype-v0/thirds/zcfg2sql/zcfg2sql.c	(revision 839)
@@ -0,0 +1,478 @@
+/*
+ * Author : Gérald FENOY
+ *
+ *  Copyright (c) 2017 GeoLabs SARL. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "service.h"
+
+extern int getServiceFromFile (maps *, const char *, service **);
+
+/**
+ * Dump an elements on stderr using the SQL syntaxe
+ *
+ * @param e the elements to dump
+ */
+void dumpElementsAsSQL(const char* type,elements* e,int index,int level,FILE* f){
+  elements* tmp=e;
+  int i;
+  while(tmp!=NULL){
+    fprintf(f,"--\n-- %s %s \n--\n",type,tmp->name);
+    map* mcurs=tmp->content;
+    iotype* tmpio=tmp->defaults;
+    int ioc=0;
+    if(tmp->format!=NULL){
+      //fprintf(stderr,"%s:\n",tmp->format);
+      if(strncmp(tmp->format,"Complex",7)==0){
+	while(tmpio!=NULL){
+	  map* values[3]={
+	    getMap(tmpio->content,"mimeType"),
+	    getMap(tmpio->content,"encoding"),
+	    getMap(tmpio->content,"schema")
+	  };
+	  char cond[3][256]={
+	    "mime_type is NULL",
+	    "encoding is NULL",
+	    "schema is NULL"
+	  };
+	  char *condNames[3]={
+	    "mime_type",
+	    "encoding",
+	    "schema"
+	  };
+	  for(i=0;i<3;i++)
+	    if(values[i]!=NULL){
+	      sprintf(cond[i],"%s='%s'",condNames[i],values[i]->value);
+	    }
+	  fprintf(f,"INSERT INTO CollectionDB.ows_Format"
+		  " (def,primitive_format_id) VALUES \n(true,");
+	  fprintf(f,"(SELECT id from CollectionDB.PrimitiveFormats"
+		  " WHERE %s AND %s AND %s));\n",cond[0],cond[1],cond[2]);
+	  fprintf(f,"INSERT INTO CollectionDB.ows_DataDescription (format_id)"
+		  " VALUES ((SELECT last_value FROM CollectionDB.ows_Format_id_seq));\n");
+	  tmpio=tmpio->next;
+	  ioc++;
+	}
+      }else{
+	if(strncmp(tmp->format,"Literal",7)==0){
+	  while(tmpio!=NULL){
+	    map* values[4]={
+	      getMap(tmpio->content,"dataType"),
+	      getMap(tmpio->content,"value"),
+	      getMap(tmpio->content,"uom"),
+	      getMap(tmpio->content,"AllowedValues")
+	    };
+	    char *fields[20]={
+	      "default_value",
+	      "def"
+	    };
+	    fprintf(f,"INSERT INTO CollectionDB.LiteralDataDomain"
+		    " (def,data_type_id) VALUES \n(true,");
+	    fprintf(f,"(SELECT id from CollectionDB.PrimitiveDatatypes"
+		    " where name = '%s'));\n",values[0]->value);
+	    if(values[1]!=NULL)
+	      fprintf(f,"UPDATE CollectionDB.LiteralDataDomain \n"
+		      "set %s = $q$%s$q$ \n WHERE id = \n"
+		      "  ((SELECT last_value FROM CollectionDB.ows_DataDescription_id_seq));\n ",
+		      fields[0],values[1]->value);
+	    if(values[2]!=NULL)
+	      fprintf(f,"UPDATE CollectionDB.LiteralDataDomain \n"
+		      "set uom = (SELECT id from CollectionDB.PrimitiveDatatypes WHERE name=$q$%s$q$ \n WHERE id = \n"
+		      "  (SELECT last_value FROM CollectionDB.ows_DataDescription_id_seq);\n ",
+		      values[2]->value);
+	    if(values[3]!=NULL){
+	      char *tmp=strtok(values[3]->value,",");
+	      while(tmp!=NULL){
+		fprintf(f,"INSERT INTO CollectionDB.AllowedValues (allowed_value) VALUES ('%s');\n",tmp);
+		fprintf(f,"INSERT INTO CollectionDB.AllowedValuesAssignment"
+			" (allowed_value_id,literal_data_domain_id) VALUES "
+			"((SELECT last_value FROM CollectionDB.AllowedValues_id_seq),"
+			"(SELECT last_value FROM CollectionDB.ows_DataDescription_id_seq));\n",tmp);
+		tmp=strtok(NULL,",");
+	      }
+	    }
+	    tmpio=tmpio->next;
+	    ioc++;
+	  }	  
+	}
+      }
+
+    }
+    
+    map* values[4]={
+      getMap(tmp->content,"Title"),
+      getMap(tmp->content,"Abstract"),
+      getMap(tmp->content,"minOccurs"),
+      getMap(tmp->content,"maxOccurs")
+    };
+    char *toto=",min_occurs,max_occurs";
+    if(strcmp(type,"Input")==0){
+      fprintf(f,"INSERT INTO CollectionDB.ows_Input "
+	      "(identifier,title,abstract,min_occurs,max_occurs)"
+	      " VALUES "
+	      "($q$%s$q$,\n$q$%s$q$,\n$q$%s$q$,\n%s,\n%s);\n",
+	      tmp->name,
+	      values[0]->value,
+	      values[1]->value,
+	      values[2]->value,
+	      values[3]->value
+	      );
+      if(tmp->format!=NULL)
+	fprintf(f,"INSERT INTO CollectionDB.InputDataDescriptionAssignment"
+		" (input_id,data_description_id) VALUES "
+		"((select last_value as id "
+	        "from CollectionDB.Descriptions_id_seq),"
+		"(select last_value "
+		"from CollectionDB.ows_DataDescription_id_seq));\n");
+      if(level==0)
+	fprintf(f,"INSERT INTO CollectionDB.ProcessInputAssignment"
+		"(process_id,input_id)"
+		" VALUES"
+		"((select id from pid),"
+		"(select last_value as id from CollectionDB.Descriptions_id_seq));\n");
+      else
+	fprintf(f,"INSERT INTO CollectionDB.InputInputAssignment"
+		"(parent_input,child_input)"
+		" VALUES"
+		"((select id from cid_%d),"
+		"(select last_value as id from CollectionDB.Descriptions_id_seq));\n",level-1);
+    }
+    else{
+      fprintf(f,"INSERT INTO CollectionDB.ows_Output\n"
+	      "\t(identifier,title,abstract)\n"
+	      "\t VALUES \n"
+	      "($q$%s$q$,$q$%s$q$,$q$%s$q$);\n",
+	      tmp->name,
+	      values[0]->value,
+	      values[1]->value
+	      );
+      if(tmp->format!=NULL)
+	fprintf(f,"INSERT INTO CollectionDB.OutputDataDescriptionAssignment"
+		" (output_id,data_description_id) VALUES "
+		"((select last_value as id "
+	        "from CollectionDB.Descriptions_id_seq),"
+		"(select last_value "
+		"from CollectionDB.ows_DataDescription_id_seq));\n");
+      if(level==0)
+	fprintf(f,"INSERT INTO CollectionDB.ProcessOutputAssignment"
+		"(process_id,output_id)"
+		" VALUES"
+		"((select id from pid),"
+		"(select last_value as id from CollectionDB.Descriptions_id_seq));\n");
+      else
+	fprintf(f,"INSERT INTO CollectionDB.OutputOutputAssignment"
+		"(parent_output,child_output)"
+		" VALUES"
+		"((select id from cid_%d),"
+		"(select last_value as id from CollectionDB.Descriptions_id_seq));\n",level-1);
+    }
+
+    map* mcurs1=tmp->metadata;
+    if(mcurs1!=NULL){
+      while(mcurs1!=NULL){
+	if(strncasecmp(mcurs1->name,"title",5)==0){
+	  fprintf(stdout,"INSERT INTO CollectionDB.ows_Metadata (title) "
+		  "VALUES ($q$%s$q$);\n",mcurs->value);
+	tryagain0:
+	  if(mcurs1->next!=NULL){
+	    if(strncasecmp(mcurs->next->name,"role",4)==0){
+	      fprintf(stdout,"UPDATE CollectionDB.ows_Metadata set role = \n"
+		      "$q$%s$q$ where id=(SELECT last_value FROM CollectionDB.ows_Metadata_id_seq);\n",mcurs1->next->value);
+	      mcurs1=mcurs1->next;
+	      goto tryagain0;
+	    }
+	    else{
+	      if(strncasecmp(mcurs1->next->name,"href",4)==0){
+		fprintf(stdout,"UPDATE CollectionDB.ows_Metadata set href = \n"
+			"$q$%s$q$ where id=(SELECT last_value FROM CollectionDB.ows_Metadata_id_seq);\n",mcurs1->next->value);
+		mcurs1=mcurs1->next;
+		goto tryagain0;
+	      }
+	    }
+	  }
+	  fprintf(stdout,"INSERT INTO CollectionDB.DescriptionsMetadataAssignment (descriptions_id,metadata_id) "
+		  "VALUES ((select last_value as id from CollectionDB.Descriptions_id_seq),"
+		  "(SELECT last_value FROM CollectionDB.ows_Metadata_id_seq));\n");	  
+	}
+	mcurs1=mcurs1->next;
+      }
+    }
+    mcurs=tmp->additional_parameters;
+    if(mcurs!=NULL){
+      while(mcurs!=NULL){
+	if(strncasecmp(mcurs->name,"title",5)==0){
+	  fprintf(stdout,"INSERT INTO CollectionDB.ows_AdditionalParameters (title) "
+		  "VALUES ($q$%s$q$);\n",mcurs->value);
+	tryagain:
+	  if(mcurs->next!=NULL){
+	    if(strncasecmp(mcurs->next->name,"role",4)==0){
+	      fprintf(stdout,"UPDATE CollectionDB.ows_AdditionalParameters set role = \n"
+		      "$q$%s$q$ where id=(SELECT last_value FROM CollectionDB.ows_AdditionalParameters_id_seq);\n",mcurs->next->value);
+	      mcurs=mcurs->next;
+	      goto tryagain;
+	    }
+	    else{
+	      if(strncasecmp(mcurs->next->name,"href",4)==0){
+		fprintf(stdout,"UPDATE CollectionDB.ows_AdditionalParameters set href = \n"
+			"$q$%s$q$ where id=(SELECT last_value FROM CollectionDB.ows_AdditionalParameters_id_seq);\n",mcurs->next->value);
+		mcurs=mcurs->next;
+		goto tryagain;
+	      }
+	    }
+	  }
+	  fprintf(stdout,"INSERT INTO CollectionDB.DescriptionsAdditionalParametersAssignment (descriptions_id,additional_parameters_id) "
+		  "VALUES ((select last_value as id from CollectionDB.Descriptions_id_seq),"
+		  "(SELECT last_value FROM CollectionDB.ows_AdditionalParameters_id_seq));\n");	  
+	}else{
+	  fprintf(stdout,"INSERT INTO CollectionDB.ows_AdditionalParameter (key,value,additional_parameters_id) "
+		  "VALUES ($q$%s$q$,$q$%s$q$,"
+		  "(SELECT last_value FROM CollectionDB.ows_AdditionalParameters_id_seq));\n",mcurs->name,mcurs->value);	  
+	}
+	mcurs=mcurs->next;
+      }
+    }
+
+    if(tmp->format!=NULL){
+      tmpio=tmp->supported;
+      if(strncmp(tmp->format,"Complex",7)==0){
+	while(tmpio!=NULL){
+	  map* values[3]={
+	    getMap(tmpio->content,"mimeType"),
+	    getMap(tmpio->content,"encoding"),
+	    getMap(tmpio->content,"schema")
+	  };
+	  char cond[3][256]={
+	    "mime_type is NULL",	    
+	    "encoding is NULL",
+	    "schema is NULL",
+	  };
+	  char *condNames[3]={
+	    "mime_type",
+	    "encoding",
+	    "schema"
+	  };
+	  for(i=0;i<3;i++)
+	    if(values[i]!=NULL){
+	      sprintf(cond[i],"%s='%s'",condNames[i],values[i]->value);
+	    }
+	  fprintf(f,"INSERT INTO CollectionDB.ows_Format"
+		  " (def,primitive_format_id) VALUES \n(false,");
+	  fprintf(f,"(SELECT id from CollectionDB.PrimitiveFormats"
+		  " WHERE %s AND %s AND %s));\n",cond[0],cond[1],cond[2]);
+	  fprintf(f,"INSERT INTO CollectionDB.ows_DataDescription (format_id)"
+		  " VALUES ((SELECT last_value FROM CollectionDB.ows_Format_id_seq));\n");
+	  fprintf(f,"INSERT INTO CollectionDB.%sDataDescriptionAssignment"
+		  " (%s_id,data_description_id) VALUES "
+		  "((select last_value as id "
+		  "from CollectionDB.Descriptions_id_seq),"
+		  "(select last_value "
+		  "from CollectionDB.ows_DataDescription_id_seq));\n",
+		  type,type);	  
+	  tmpio=tmpio->next;
+	  ioc++;
+	}
+      }else{
+	if(strncmp(tmp->format,"Literal",7)==0){
+	  while(tmpio!=NULL){
+	    map* values[4]={
+	      getMap(tmpio->content,"dataType"),
+	      getMap(tmpio->content,"value"),
+	      getMap(tmpio->content,"uom"),
+	      getMap(tmpio->content,"AllowedValues")
+	    };
+	    char *fields[20]={
+	      "default_value",
+	      "def"
+	    };
+	    fprintf(f,"INSERT INTO CollectionDB.LiteralDataDomain"
+		    " (def,data_type_id) VALUES \n(false,");
+	    fprintf(f,"(SELECT id from CollectionDB.PrimitiveDatatypes"
+		    " where name = '%s'));\n",values[0]->value);
+	    if(values[1]!=NULL)
+	      fprintf(f,"UPDATE CollectionDB.LiteralDataDomain \n"
+		      "set %s = $q$%s$q$ \n WHERE id = \n"
+		      "  ((SELECT last_value FROM CollectionDB.ows_DataDescription_id_seq));\n ",
+		      fields[0],values[1]->value);
+	    fprintf(f,"INSERT INTO CollectionDB.%sDataDescriptionAssignment"
+		    " (%s_id,data_description_id) VALUES "
+		    "((select last_value "
+		    "from CollectionDB.Descriptions_id_seq),"
+		    "(select last_value "
+		    "from CollectionDB.ows_DataDescription_id_seq));\n",
+		    type,type);	  
+	    tmpio=tmpio->next;
+	    ioc++;
+	  }	  
+	}
+      }
+    }
+
+    if(tmp->format==NULL){
+      fprintf(f,"--\n-- Child %d \n--\n",level);
+      fprintf(f,"CREATE TEMPORARY TABLE cid_%d AS (select last_value as id from CollectionDB.Descriptions_id_seq) ;\n",level);
+      if(tmp->child!=NULL)
+	dumpElementsAsSQL(type,tmp->child,0,level+1,f);
+      fprintf(f,"DROP TABLE cid_%d;\n",level);
+      fprintf(f,"--\n-- End Child %d \n--\n",level);
+    }
+    tmp=tmp->next;
+  }
+}
+
+
+int main(int argc, char *argv[]) {
+  service* s=NULL;
+  maps *m=NULL;
+  char conf_file[1024];
+  snprintf(conf_file,1024,"%s",argv[1]);
+  s=(service*)malloc(SERVICE_SIZE);
+  if(s == NULL){
+    fprintf(stderr,"Unable to allocate memory");
+    return -1;
+  }
+  int t=getServiceFromFile(m,conf_file,&s);
+
+  fprintf(stdout,"--\n-- Load from %s %d \n--",__FILE__,__LINE__);
+  int i;
+  fprintf(stdout,"--\n-- Service %s\n--\n",s->name);
+  if(s->content!=NULL){
+    map* values[4]={
+      getMap(s->content,"Title"),
+      getMap(s->content,"Abstract"),
+      getMap(s->content,"serviceProvider"),
+      getMap(s->content,"serviceType")
+    };
+    fprintf(stdout,"INSERT INTO CollectionDB.zoo_DeploymentMetadata\n"
+	    "(executable_name,service_type_id)\n"
+	    "\nVALUES\n"
+	    "($q$%s$q$,\n(SELECT id from CollectionDB.zoo_ServiceTypes WHERE service_type=$q$%s$q$));\n\n",
+	    values[2]->value,
+	    values[3]->value);
+    fprintf(stdout,"INSERT INTO CollectionDB.zoo_PrivateMetadata"
+	    "(id) VALUES (default);\n"); 
+    fprintf(stdout,"INSERT INTO CollectionDB.PrivateMetadataDeploymentMetadataAssignment"
+	    "(private_metadata_id,deployment_metadata_id) VALUES \n"
+	    "((SELECT last_value FROM CollectionDB.zoo_PrivateMetadata_id_seq),\n"
+	    "(SELECT last_value FROM CollectionDB.zoo_DeploymentMetadata_id_seq));\n");
+    fprintf(stdout,"INSERT INTO CollectionDB.ows_Process\n"
+	    "(identifier,title,abstract,private_metadata_id,"
+	    "availability)\n"
+	    "\nVALUES\n"
+	    "($q$%s$q$,\n$q$%s$q$,\n$q$%s$q$,(SELECT last_value FROM CollectionDB.PrivateMetadataDeploymentMetadataAssignment_id_seq),\ntrue);\n\n",
+	    s->name,
+	    values[0]->value,
+	    values[1]->value);
+    fprintf(stdout,"CREATE TEMPORARY TABLE pid AS "
+	    "(select last_value as id from CollectionDB.Descriptions_id_seq);\n");
+    map* mcurs=s->content;
+    //dumpMap(mcurs);
+    mcurs=s->metadata;
+    if(mcurs!=NULL){
+      while(mcurs!=NULL){
+	if(strncasecmp(mcurs->name,"title",5)==0){
+	  fprintf(stdout,"INSERT INTO CollectionDB.ows_Metadata (title) "
+		  "VALUES ($q$%s$q$);\n",mcurs->value);
+	tryagain0:
+	  if(mcurs->next!=NULL){
+	    if(strncasecmp(mcurs->next->name,"role",4)==0){
+	      fprintf(stdout,"UPDATE CollectionDB.ows_Metadata set role = \n"
+		      "$q$%s$q$ where id=(SELECT last_value FROM CollectionDB.ows_Metadata_id_seq);\n",mcurs->next->value);
+	      mcurs=mcurs->next;
+	      goto tryagain0;
+	    }
+	    else{
+	      if(strncasecmp(mcurs->next->name,"href",4)==0){
+		fprintf(stdout,"UPDATE CollectionDB.ows_Metadata set href = \n"
+			"$q$%s$q$ where id=(SELECT last_value FROM CollectionDB.ows_Metadata_id_seq);\n",mcurs->next->value);
+		mcurs=mcurs->next;
+		goto tryagain0;
+	      }
+	    }
+	  }
+	  fprintf(stdout,"INSERT INTO CollectionDB.DescriptionsMetadataAssignment (descriptions_id,metadata_id) "
+		  "VALUES ((select last_value as id from CollectionDB.Descriptions_id_seq),"
+		  "(SELECT last_value FROM CollectionDB.ows_Metadata_id_seq));\n");	  
+	}
+	mcurs=mcurs->next;
+      }
+    }
+    mcurs=s->additional_parameters;
+    if(mcurs!=NULL){
+      while(mcurs!=NULL){
+	if(strncasecmp(mcurs->name,"title",5)==0){
+	  fprintf(stdout,"INSERT INTO CollectionDB.ows_AdditionalParameters (title) "
+		  "VALUES ($q$%s$q$);\n",mcurs->value);
+	tryagain:
+	  if(mcurs->next!=NULL){
+	    if(strncasecmp(mcurs->next->name,"role",4)==0){
+	      fprintf(stdout,"UPDATE CollectionDB.ows_AdditionalParameters set role = \n"
+		      "$q$%s$q$ where id=(SELECT last_value FROM CollectionDB.ows_AdditionalParameters_id_seq);\n",mcurs->next->value);
+	      mcurs=mcurs->next;
+	      goto tryagain;
+	    }
+	    else{
+	      if(strncasecmp(mcurs->next->name,"href",4)==0){
+		fprintf(stdout,"UPDATE CollectionDB.ows_AdditionalParameters set href = \n"
+			"$q$%s$q$ where id=(SELECT last_value FROM CollectionDB.ows_AdditionalParameters_id_seq);\n",mcurs->next->value);
+		mcurs=mcurs->next;
+		goto tryagain;
+	      }
+	    }
+	  }
+	  fprintf(stdout,"INSERT INTO CollectionDB.DescriptionsAdditionalParametersAssignment (descriptions_id,additional_parameters_id) "
+		  "VALUES ((select last_value as id from CollectionDB.Descriptions_id_seq),"
+		  "(SELECT last_value FROM CollectionDB.ows_AdditionalParameters_id_seq));\n");	  
+	}else{
+	  fprintf(stdout,"INSERT INTO CollectionDB.ows_AdditionalParameter (key,value,additional_parameters_id) "
+		  "VALUES ($q$%s$q$,$q$%s$q$,"
+		  "(SELECT last_value FROM CollectionDB.ows_AdditionalParameters_id_seq));\n",mcurs->name,mcurs->value);	  
+	}
+	mcurs=mcurs->next;
+      }
+    }
+    
+  }
+  if(s->inputs!=NULL){
+    //fprintf(stderr,"\ninputs:\n");
+    dumpElementsAsSQL("Input",s->inputs,0,0,stdout);
+  }
+  if(s->outputs!=NULL){
+    //fprintf(stderr,"\noutputs:\n");
+    dumpElementsAsSQL("Output",s->outputs,0,0,stdout);
+  }
+  fprintf(stdout,"--\n-- Load from %s %d \n--",__FILE__,__LINE__);
+
+  
+  
+  /*fprintf(stderr,"--\n-- Load from %s %d \n--",__FILE__,__LINE__);
+  fflush(stderr);
+  printf("--\n-- Load from %s %d \n--",__FILE__,__LINE__);
+  if(t>=0){
+    fprintf(stderr,"--\n-- Service %s\n--",s->name);
+    fflush(stderr);
+    dumpServiceAsSQL(s); 
+    fprintf(stderr,"--\n-- Service %s\n--",s->name);
+    fflush(stderr);
+  }
+  */
+  fflush(stderr);
+  return 0;
+}
Index: branches/prototype-v0/zoo-project/zoo-kernel/Makefile.in
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/Makefile.in	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/Makefile.in	(revision 839)
@@ -41,4 +41,10 @@
 	g++ -fPIC ${CFLAGS} -c sshapi.c
 
+service_json.o: service_json.c
+	g++ -fPIC ${JSON_CFLAGS} ${CFLAGS} -c service_json.c
+
+service_callback.o: service_callback.c
+	g++ -fPIC ${JSON_CFLAGS} ${CFLAGS} -c service_callback.c
+
 request_parser.o: request_parser.c request_parser.h
 	g++ -fPIC ${XML2CFLAGS} ${CFLAGS} ${JSCFLAGS} ${JS_ENABLED} -c request_parser.c
@@ -51,5 +57,5 @@
 
 response_print.o: response_print.c response_print.h
-	g++ -fPIC ${GDAL_CFLAGS} ${XML2CFLAGS} ${CFLAGS} ${JSCFLAGS} ${JS_ENABLED} -c response_print.c
+	g++ -fPIC ${GDAL_CFLAGS} ${XML2CFLAGS} ${CFLAGS} ${JSCFLAGS} ${JS_ENABLED} ${HPC_ENABLED} -c response_print.c
 
 server_internal.o: server_internal.c server_internal.h service.h mimetypes.h
@@ -76,5 +82,5 @@
 
 service_internal_hpc.o: service_internal_hpc.c service_internal_hpc.h service.h
-	g++  ${XML2CFLAGS} ${HPCCFLAGS} ${CFLAGS} -c service_internal_hpc.c
+	g++  ${JSON_CFLAGS} ${XML2CFLAGS} ${HPCCFLAGS} ${CFLAGS} -c service_internal_hpc.c
 
 service_internal_saga.o: service_internal_saga.c service_internal_saga.h service.h
@@ -109,12 +115,12 @@
 
 zoo_service_loader.o: zoo_service_loader.c service.h version.h ${MS_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE} ${RUBY_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} ${MONO_FILE} ${HPC_FILES} ${METADB_FILE}
-	g++ -g -O2 ${GDAL_CFLAGS} ${XML2CFLAGS} ${CFLAGS} ${MONO_CFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${PYTHONCFLAGS} ${JAVACFLAGS} ${JSCFLAGS} ${PERLCFLAGS} ${PHPCFLAGS} ${SAGA_ENABLED} ${OTB_ENABLED} ${HPC_ENABLED} ${PYTHON_ENABLED} ${JS_ENABLED} ${PHP_ENABLED} ${PERL_ENABLED} ${JAVA_ENABLED} ${MONO_ENABLED} ${METADB_ENABLED} -c zoo_service_loader.c  -fno-common -DPIC -o zoo_service_loader.o
+	g++ -g -O2 ${JSON_CFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${CFLAGS} ${MONO_CFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${PYTHONCFLAGS} ${JAVACFLAGS} ${JSCFLAGS} ${PERLCFLAGS} ${PHPCFLAGS} ${SAGA_ENABLED} ${OTB_ENABLED} ${HPC_ENABLED} ${PYTHON_ENABLED} ${JS_ENABLED} ${PHP_ENABLED} ${PERL_ENABLED} ${JAVA_ENABLED} ${MONO_ENABLED} ${METADB_ENABLED} -c zoo_service_loader.c  -fno-common -DPIC -o zoo_service_loader.o
 
 libzoo_service.${EXT}: version.h service_internal.o service.o sqlapi.o
-	gcc -shared  ${GDAL_CFLAGS} ${DEFAULT_OPTS} -fpic -o libzoo_service.${EXT} ${CFLAGS}  service_internal.o service.o sqlapi.o ${FCGI_LDFLAGS} ${GDAL_LIBS}
+	gcc -shared  ${GDAL_CFLAGS} ${DEFAULT_OPTS} -fpic -o libzoo_service.${EXT} ${CFLAGS}  service_internal.o service.o sqlapi.o ${FCGI_LDFLAGS} ${GDAL_LIBS} ${ZOO_LDFLAGS}
 
-zoo_loader.cgi: version.h libzoo_service.${EXT} zoo_loader.c zoo_service_loader.o  ulinet.o service.h lex.sr.o service_conf.tab.o service_conf.y ulinet.o main_conf_read.tab.o lex.cr.o request_parser.o response_print.o server_internal.o caching.o ${MS_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE} ${RUBY_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} ${HPC_FILES} ${METADB_FILE}
-	g++ -g -O2 ${JSCFLAGS} ${PHPCFLAGS}  ${PERLCFLAGS} ${RUBYCFLAGS}  ${JAVACFLAGS} ${XML2CFLAGS} ${PYTHONCFLAGS} ${CFLAGS} -c zoo_loader.c  -fno-common -DPIC -o zoo_loader.o
-	g++  ${JSCFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${PHPCFLAGS} ${PERLCFLAGS} ${JAVACFLAGS} ${PYTHONCFLAGS} ${CFLAGS} zoo_loader.o zoo_service_loader.o ${MS_FILE} ${PYTHON_FILE}  ${PERL_FILE} ${PHP_FILE}  ${JS_FILE} ${JAVA_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} ${MONO_FILE} ${HPC_FILES} ${METADB_FILE} response_print.o server_internal.o caching.o request_parser.o ulinet.o lex.cr.o lex.sr.o service_conf.tab.o main_conf_read.tab.o -o zoo_loader.cgi -L. ${LDFLAGS}
+zoo_loader.cgi: version.h libzoo_service.${EXT} zoo_loader.c zoo_service_loader.o  ulinet.o service.h lex.sr.o service_conf.tab.o service_conf.y ulinet.o main_conf_read.tab.o lex.cr.o request_parser.o response_print.o server_internal.o caching.o ${MS_FILE} ${PYTHON_FILE} ${PHP_FILE} ${JAVA_FILE} ${JS_FILE} ${PERL_FILE} ${RUBY_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} ${HPC_FILES} ${METADB_FILE} ${JSON_FILE} service_callback.o
+	g++ -g -O2 ${JSON_CFLAGS} ${JSCFLAGS} ${PHPCFLAGS}  ${PERLCFLAGS} ${RUBYCFLAGS}  ${JAVACFLAGS} ${XML2CFLAGS} ${PYTHONCFLAGS} ${CFLAGS} -c zoo_loader.c  -fno-common -DPIC -o zoo_loader.o
+	g++  ${JSON_CFLAGS} ${JSCFLAGS} ${SAGA_CFLAGS} ${OTBCFLAGS} ${GDAL_CFLAGS} ${XML2CFLAGS} ${PHPCFLAGS} ${PERLCFLAGS} ${JAVACFLAGS} ${PYTHONCFLAGS} ${CFLAGS} zoo_loader.o zoo_service_loader.o ${MS_FILE} ${PYTHON_FILE}  ${PERL_FILE} ${PHP_FILE}  ${JS_FILE} ${JAVA_FILE} ${YAML_FILE} ${OTB_FILE} ${SAGA_FILE} ${MONO_FILE} ${HPC_FILES} ${METADB_FILE} ${JSON_FILE} service_callback.o response_print.o server_internal.o caching.o request_parser.o ulinet.o lex.cr.o lex.sr.o service_conf.tab.o main_conf_read.tab.o -o zoo_loader.cgi -L. ${LDFLAGS}
 
 zcfg2yaml: zcfg2yaml.c service.h lex.sr.o service_conf.tab.o service_conf.y main_conf_read.tab.o lex.cr.o response_print.o server_internal.o service_internal.o ${MS_FILE} ${YAML_FILE}
Index: branches/prototype-v0/zoo-project/zoo-kernel/ZOOMakefile.opts.in
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/ZOOMakefile.opts.in	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/ZOOMakefile.opts.in	(revision 839)
@@ -29,4 +29,8 @@
 YAML_FILE=@YAML_FILE@
 YAML_FILE1=@YAML_FILE1@
+
+JSON_CFLAGS=@JSON_CPPFLAGS@
+JSON_LDFLAGS=@JSON_LDFLAGS@
+JSON_FILE=@JSON_FILE@
 
 METADB_ENABLED=@METADB@
@@ -111,5 +115,5 @@
 
 CFLAGS=@SSH2_CPPFLAGS@ @GETTEXT_CFLAGS@ @PROJ_CPPFLAGS@ @RELY_ON_DB@ @DEB_DEF@ -fpic @OPENSSL_CFLAGS@ @UUID_CFLAGS@ ${FCGI_CFLAGS} ${YAML_CFLAGS} ${MACOS_CFLAGS} ${MS_CFLAGS} -I../../thirds/cgic206 -I. -DLINUX_FREE_ISSUE #-DDEBUG #-DDEBUG_SERVICE_CONF
-LDFLAGS=-lzoo_service @SSH2_LDFLAGS@ @PROJ_LDFLAGS@ @DEFAULT_LIBS@ -L../../thirds/cgic206 -lcgic ${GDAL_LIBS} ${XML2LDFLAGS} ${PYTHONLDFLAGS} ${PERLLDFLAGS}  ${PHPLDFLAGS} ${JAVALDFLAGS} ${JSLDFLAGS}  ${FCGI_LDFLAGS} @OPENSSL_LDFLAGS@ @UUID_LDFLAGS@ ${MS_LDFLAGS} ${MACOS_LD_FLAGS} ${MACOS_LD_NET_FLAGS} ${YAML_LDFLAGS} ${OTBLDFLAGS} ${SAGA_LDFLAGS} ${MONO_LDFLAGS} @GETTEXT_LDFLAGS@
+LDFLAGS=-lzoo_service @SSH2_LDFLAGS@ @PROJ_LDFLAGS@ @DEFAULT_LIBS@ -L../../thirds/cgic206 -lcgic ${GDAL_LIBS} ${XML2LDFLAGS} ${PYTHONLDFLAGS} ${PERLLDFLAGS}  ${PHPLDFLAGS} ${JAVALDFLAGS} ${JSLDFLAGS}  ${FCGI_LDFLAGS} @OPENSSL_LDFLAGS@ @UUID_LDFLAGS@ ${MS_LDFLAGS} ${MACOS_LD_FLAGS} ${MACOS_LD_NET_FLAGS} ${YAML_LDFLAGS} ${OTBLDFLAGS} ${SAGA_LDFLAGS} ${MONO_LDFLAGS} @GETTEXT_LDFLAGS@ @JSON_LDFLAGS@
 
 DATAROOTDIR=@datarootdir@/zoo-project
Index: branches/prototype-v0/zoo-project/zoo-kernel/caching.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/caching.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/caching.c	(revision 839)
@@ -158,4 +158,5 @@
  */
 int readCurrentInput(maps** m,maps** in,int* index,HINTERNET* hInternet,map** error){
+  
   map* tmp1;
   char sindex[5];
@@ -212,6 +213,6 @@
     sprintf(sindex,"%d",*index+1);
     if((tmp1=getMap(content->content,xname))!=NULL && tmap!=NULL && strcasecmp(tmap->value,sindex)==0){
-      
-      if(getMap(content->content,icname)==NULL){
+
+      if(getMap(content->content,icname)==NULL){	
 	fcontent=(char*)malloc((hInternet->ihandle[*index].nDataLen+1)*sizeof(char));
 	if(fcontent == NULL){
@@ -266,9 +267,9 @@
 	  char* md5str=NULL;
 	  if((tmp2=getMap(content->content,bname))!=NULL){
-	    dumpMap(tmp2);
 	    char *tmpStr=(char*)malloc((strlen(tmp1->value)+strlen(tmp2->value)+1)*sizeof(char));
 	    sprintf(tmpStr,"%s%s",tmp1->value,tmp2->value);
 	    if((tmp2=getMap(content->content,"headers"))!=NULL){
 	      char *tmpStr2=zStrdup(tmpStr);
+	      free(tmpStr);
 	      tmpStr=(char*)malloc((strlen(tmpStr2)+strlen(tmp2->value)+1)*sizeof(char));
 	      sprintf(tmpStr,"%s%s",tmpStr2,tmp2->value);
@@ -293,6 +294,5 @@
 	free(mimeType);
 	free(request);
-	*index++;
-	
+	(*index)++;
       }
     }
@@ -361,5 +361,5 @@
     free(oreq);
   }else{
-    setMapArray(oreq->content,"value",hInternet->nb-1,url);
+    setMapArray(oreq->content,"value",hInternet->nb,url);
   }
 }
@@ -444,4 +444,5 @@
       addToMap(*content,"cache_file",fname);
       free(fname);
+      free(md5str);
     }
   }
Index: branches/prototype-v0/zoo-project/zoo-kernel/configure.ac
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/configure.ac	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/configure.ac	(revision 839)
@@ -60,4 +60,30 @@
 AC_SUBST([UUID_CFLAGS])
 AC_SUBST([UUID_LDFLAGS])
+
+#============================================================================
+# Detect if json-c is available
+#============================================================================
+
+AC_ARG_WITH([json],
+    [AS_HELP_STRING([--with-json=PATH], [Specifies an alternative location for the json-c library])],
+    [JSON_DIR="$withval"], [JSON_DIR="/usr/"])
+JSON_CPPFLAGS="-I$JSON_DIR/include/json-c/"
+JSON_LDFLAGS="-L$JSON_DIR/lib -ljson-c"
+CPPFLAGS_SAVE="$CPPFLAGS"
+CPPFLAGS="$JSON_CPPFLAGS"
+LIBS_SAVE="$LIBS"
+LIBS="$JSON_LDFLAGS"
+echo $CPPFLAGS
+AC_CHECK_HEADERS([json_object.h],
+			[], [AC_MSG_ERROR([could not find header file json_object.h])])
+AC_CHECK_LIB([json-c], [json_object_new_object],
+			[], [AC_MSG_ERROR([could not find function in json-c library])])
+CPPFLAGS="$CPPFLAGS_SAVE"
+JSON_FILE="service_json.o"
+JSON_ENABLED="-DJSON"
+AC_SUBST([JSON_CPPFLAGS])
+AC_SUBST([JSON_LDFLAGS])
+AC_SUBST([JSON_FILE])
+AC_SUBST([JSON_ENABLED])
 
 #============================================================================
Index: branches/prototype-v0/zoo-project/zoo-kernel/main_conf_read.y
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/main_conf_read.y	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/main_conf_read.y	(revision 839)
@@ -99,4 +99,5 @@
   }
   free(curr_key);
+  curr_key=NULL;
   }
 | SPAIR  {curr_key=zStrdup($1);if(debug) printf("SPAIR FOUND !!\n"); }
@@ -162,4 +163,7 @@
     free(current_content);
   }
+  if(curr_key!=NULL){
+    free(curr_key);
+  }
 
   fclose(crin);
Index: branches/prototype-v0/zoo-project/zoo-kernel/mimetypes.h
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/mimetypes.h	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/mimetypes.h	(revision 839)
@@ -180,5 +180,5 @@
 	{ "application/vnd.dece.unspecified", "uvx" },
 	{ "application/vnd.dece.zip", "uvz" },
-	{ "application/vnd.denovo.fcselayout-link", "fe_launch" },
+	{ "application/vnd.denovo.fcselayout-link", "fe_launch" },	
 	{ "application/vnd.dna", "dna" },
 	{ "application/vnd.dolby.mlp", "mlp" },
@@ -836,2 +836,29 @@
 	return ext;
 }
+
+static int isGeographic(const char* mimeType){
+  char* imageMimeType[4]={
+    "image/tiff",
+    "image/png",
+    "image/jpeg",
+    "application/vnd.google-earth.kmz"
+  };
+  char* vectorMimeType[5]={
+    "text/xml",
+    "application/json",
+    "application/gml+xml",
+    "application/zip",
+    "application/vnd.google-earth.kml+xml"
+  };
+  int i=0;
+  for(;i<4;i++){
+    if(strncmp(imageMimeType[i],mimeType,strlen(imageMimeType[i]))==0)
+      return 1;
+  }
+  i=0;
+  for(;i<5;i++){
+    if(strncmp(vectorMimeType[i],mimeType,strlen(vectorMimeType[i]))==0)
+      return 2;
+  }
+  return -1;
+}
Index: branches/prototype-v0/zoo-project/zoo-kernel/request_parser.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/request_parser.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/request_parser.c	(revision 839)
@@ -533,4 +533,5 @@
 	    xmlChar *val = xmlGetProp (cur, BAD_CAST "id");
 	    tmpmaps = createMaps((char *) val);
+	    xmlFree(val);
 	  }
 
@@ -624,5 +625,4 @@
 			    tmpmaps->content =
 			      createMap (refs[l], (char *) val);
-
 			  map *ltmp = getMap (tmpmaps->content, "method");
 			  if (l == 4 )
@@ -750,4 +750,5 @@
 				      map *btmp =
 					getMap (tmpmaps->content, "Reference");
+				      addToMap (tmpmaps->content, "Body", tmp);
 				      if (btmp != NULL)
 					{
@@ -815,5 +816,6 @@
 						  &bRead);
 				tmp[bInternet.ihandle[0].nDataLen] = 0;
-				InternetCloseHandle (&bInternet);
+				InternetCloseHandle(&bInternet);
+				addToMap (tmpmaps->content, "Body", tmp);
 				map *btmp =
 				  getMap (tmpmaps->content, "href");
@@ -832,4 +834,5 @@
 				  }
 				free (tmp);
+				xmlFree (val);
 			      }
 			}
@@ -1120,4 +1123,5 @@
       const char ress[4][13] =
 	{ "mimeType", "encoding", "schema", "transmission" };
+      xmlFree (val);
       for (l = 0; l < 4; l++){
 	val = xmlGetProp (cur, BAD_CAST ress[l]);
@@ -1139,9 +1143,11 @@
 	while (ccur != NULL){
 	  if(ccur->type == XML_ELEMENT_NODE){
-	    char *xpathExpr=(char*)malloc(65+strlen(tmpmaps->name));
-	    sprintf(xpathExpr,"/*/*[local-name()='Output' and @id='%s']/*[local-name()='Output']",tmpmaps->name);
+	    char *xpathExpr=(char*)malloc(66+strlen(tmpmaps->name));
+	    sprintf(xpathExpr,"/*/*[local-name()='Output' and @id='%s']/*[local-name()='Output']",tmpmaps->name);	    
 	    xmlXPathObjectPtr tmpsptr = extractFromDoc (doc, xpathExpr);
 	    xmlNodeSet* cnodes = tmpsptr->nodesetval;
 	    xmlParseOutputs2(main_conf,request_inputs,&tmpmaps->child,doc,cnodes);
+	    xmlXPathFreeObject (tmpsptr);
+	    free(xpathExpr);
 	    break;
 	  }
@@ -1424,4 +1430,5 @@
 	  else
 	    addToMap(*request_inputs,"mode","auto");
+	  xmlFree(val);
 	  val = xmlGetProp (cur, BAD_CAST "response");
 	  if(val!=NULL){
@@ -1436,4 +1443,5 @@
 	    addToMap(*request_inputs,"ResponseDocument","");
 	  }
+	  xmlFree(val);
 	}
       }
Index: branches/prototype-v0/zoo-project/zoo-kernel/response_print.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/response_print.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/response_print.c	(revision 839)
@@ -23,4 +23,5 @@
  */
 
+#include "service.h"
 #include "response_print.h"
 #include "request_parser.h"
@@ -644,5 +645,5 @@
  * @return the generated wps:ProcessOfferings xmlNodePtr 
  */
-void printGetCapabilitiesForProcess(registry *reg, maps* m,xmlNodePtr nc,service* serv){
+void printGetCapabilitiesForProcess(registry *reg, maps* m,xmlDocPtr doc,xmlNodePtr nc,service* serv){
   xmlNsPtr ns,ns_ows,ns_xml,ns_xlink;
   xmlNodePtr n=NULL,nc1,nc2,nc3;
@@ -692,26 +693,12 @@
     tmp1=serv->metadata;
 
-    addMetadata(tmp1,nc1,ns_ows,ns_xlink);
-
-    /*while(tmp1!=NULL){
-      nc2 = xmlNewNode(ns_ows, BAD_CAST "Metadata");
-      if(strcmp(tmp1->name,"title")==0 ||
-	 strcmp(tmp1->name,"href")==0 ||
-	 strcmp(tmp1->name,"role")==0 ||
-	 strcmp(tmp1->name,"link_type")==0){
-	xmlNewNsProp(nc2,ns_xlink,BAD_CAST tmp1->name,BAD_CAST tmp1->value);
-	xmlAddChild(nc1,nc2);
-      }else{
-	if(nc3==NULL)
-	  nc3 = xmlNewNode(ns_ows, BAD_CAST "AdditionalParameter");
-	xmlNodePtr nc4 = xmlNewNode(ns_ows, BAD_CAST "Name");
-	xmlAddChild(nc4,xmlNewText(BAD_CAST tmp1->name));
-	xmlNodePtr nc5 = xmlNewNode(ns_ows, BAD_CAST "Value");
-	xmlAddChild(nc5,xmlNewText(BAD_CAST tmp1->value));
-	xmlAddChild(nc3,nc4);
-	xmlAddChild(nc3,nc5);
-      }
-      tmp1=tmp1->next;
-      }*/
+    addMetadata(tmp1,doc,nc1,ns_ows,ns_xlink);
+    tmp1=serv->additional_parameters;
+    int fromDb=-1;
+    map* test=getMap(serv->content,"fromDb");
+    if(test!=NULL && strncasecmp(test->value,"true",4)==0)
+      fromDb=1;
+    addAdditionalParameters(tmp1,doc,nc1,ns_ows,ns_xlink,fromDb);
+
     if(nc3!=NULL)
       xmlAddChild(nc1,nc3);
@@ -756,5 +743,5 @@
 }
 
-int addMetadata(map* meta,xmlNodePtr nc,xmlNsPtr ns_ows,xmlNsPtr ns_xlink){
+int addMetadata(map* meta,xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_ows,xmlNsPtr ns_xlink){
     int hasTitle=-1;
     int hasValue=-1;
@@ -830,4 +817,86 @@
 }
 
+
+int addAdditionalParameters(map* meta,xmlDocPtr doc,xmlNodePtr nc,xmlNsPtr ns_ows,xmlNsPtr ns_xlink,int fromDb){
+    int hasTitle=-1;
+    int hasValue=-1;
+    xmlNodePtr nc1;
+    map* oMeta=meta;
+    int isAdditionalParameters=-1;
+    //if(count(oMeta)>=2){
+    int level=0;
+    map* test=getMap(meta,"title");
+    if(test!=NULL)
+      level+=1;
+    test=getMap(meta,"href");
+    if(test!=NULL)
+      level+=1;
+    test=getMap(meta,"role");
+    if(test!=NULL)
+      level+=1;
+    if(count(oMeta)>level+1)
+      isAdditionalParameters=1;
+    //}
+    char *ctitle=NULL;
+    while(meta!=NULL){
+      if(hasTitle<0)
+	if(hasValue<0)
+	  nc1 = xmlNewNode(ns_ows, BAD_CAST "AdditionalParameters");
+      if(strncasecmp(meta->name,"title",5)==0 ||
+	 strcasecmp(meta->name,"href")==0 ||
+	 strcasecmp(meta->name,"role")==0 ){
+	int index=5;
+	if(strncasecmp(meta->name,"title",5)==0){
+	  index=6;
+	  hasTitle=1;
+	  if(ctitle!=NULL && strcasecmp(meta->value,ctitle)!=0){
+	      xmlAddChild(nc,nc1);
+	      nc1 = xmlNewNode(ns_ows, BAD_CAST "AdditionalParameters");
+	      free(ctitle);
+	      ctitle=NULL;
+	  }
+	  if(ctitle==NULL){
+	    char *tmp=(char*)malloc((strlen(meta->name)+1)*sizeof(char));
+	    snprintf(tmp,index,"%s",meta->name);
+	    xmlNewNsProp(nc1,ns_xlink,BAD_CAST tmp,BAD_CAST meta->value);
+	    free(tmp);
+	  }	  
+	  if(ctitle!=NULL)
+	    free(ctitle);
+	  ctitle=zStrdup(meta->value);
+	}
+      }else{
+	xmlNodePtr nc2 = xmlNewNode(ns_ows, BAD_CAST "AdditionalParameter");
+	xmlNodePtr nc3 = xmlNewNode(ns_ows, BAD_CAST "Name");
+	xmlAddChild(nc3,xmlNewText(BAD_CAST meta->name));
+	xmlAddChild(nc2,nc3);
+	if(fromDb<0){
+	  char *mptr;
+	  char* meta_values=strtok_r(meta->value,",",&mptr);
+	  while(meta_values!=NULL){
+	    xmlNodePtr nc4 = xmlNewNode(ns_ows, BAD_CAST "Value");
+	    xmlAddChild(nc4,xmlNewText(BAD_CAST meta_values));
+	    xmlAddChild(nc2,nc4);
+	    meta_values=strtok_r(NULL,",",&mptr);
+	  }
+	}else{
+	  xmlNodePtr nc4 = xmlNewNode(ns_ows, BAD_CAST "Value");
+	  xmlAddChild(nc4,xmlNewCDataBlock(doc,BAD_CAST meta->value,strlen(meta->value)));
+	  xmlAddChild(nc2,nc4);
+	}
+	xmlAddChild(nc1,nc2);
+	hasTitle=-1;
+      }
+      meta=meta->next;
+      if(hasTitle<0){
+	xmlAddChild(nc,nc1);
+	hasValue=1;
+      }
+    }
+    if(oMeta!=NULL && hasValue<0 && nc1!=NULL){
+      xmlAddChild(nc,nc1);
+    }
+}
+
 /**
  * Add the ows:Metadata nodes relative to the profile registry
@@ -886,5 +955,5 @@
  * @return the generated wps:ProcessOfferings xmlNodePtr 
  */
-void printDescribeProcessForProcess(registry *reg, maps* m,xmlNodePtr nc,service* serv){
+void printDescribeProcessForProcess(registry *reg, maps* m,xmlDocPtr doc,xmlNodePtr nc,service* serv){
   xmlNsPtr ns,ns_ows,ns_xlink;
   xmlNodePtr n,nc1;
@@ -892,4 +961,9 @@
   map* version=getMapFromMaps(m,"main","rversion");
   int vid=getVersionId(version->value);
+  int fromDb=-1;
+  map* serviceType=getMap(serv->content,"serviceType");
+  map* test=getMap(serv->content,"fromDb");
+  if(test!=NULL && strncasecmp(test->value,"true",4)==0)
+    fromDb=1;
 
   n=nc;
@@ -937,5 +1011,5 @@
     tmp1=serv->metadata;
     while(tmp1!=NULL){
-      addMetadata(tmp1,nc,ns_ows,ns_xlink);
+      addMetadata(tmp1,doc,nc,ns_ows,ns_xlink);
       /*nc1 = xmlNewNode(ns_ows, BAD_CAST "Metadata");
       xmlNewNsProp(nc1,ns_xlink,BAD_CAST tmp1->name,BAD_CAST tmp1->value);
@@ -951,6 +1025,8 @@
   }else{
     tmp1=serv->metadata;
-    addMetadata(tmp1,nc,ns_ows,ns_xlink);
+    addMetadata(tmp1,doc,nc,ns_ows,ns_xlink);
     addInheritedMetadata(nc,ns_ows,ns_xlink,reg,m,serv);
+    tmp1=serv->additional_parameters;
+    addAdditionalParameters(tmp1,doc,nc,ns_ows,ns_xlink,fromDb);
   }
 
@@ -959,9 +1035,9 @@
     if(vid==0){
       nc1 = xmlNewNode(NULL, BAD_CAST "DataInputs");
-      printFullDescription(1,e,"Input",ns,ns_ows,nc1,vid);
+      printFullDescription(doc,1,e,"Input",ns,ns_ows,nc1,vid,fromDb,NULL);
       xmlAddChild(nc,nc1);
     }
     else{
-      printFullDescription(1,e,"wps:Input",ns,ns_ows,nc,vid);
+      printFullDescription(doc,1,e,"wps:Input",ns,ns_ows,nc,vid,fromDb,NULL);
     }
   }
@@ -970,9 +1046,9 @@
   if(vid==0){
     nc1 = xmlNewNode(NULL, BAD_CAST "ProcessOutputs");
-    printFullDescription(0,e,"Output",ns,ns_ows,nc1,vid);
+    printFullDescription(doc,0,e,"Output",ns,ns_ows,nc1,vid,fromDb,NULL);
     xmlAddChild(nc,nc1);
   }
   else{
-    printFullDescription(0,e,"wps:Output",ns,ns_ows,nc,vid);
+    printFullDescription(doc,0,e,"wps:Output",ns,ns_ows,nc,vid,fromDb,serviceType);
   }
   if(vid==0)
@@ -996,6 +1072,8 @@
  * @param nc1 the XML node to use to add the created tree
  * @param vid the WPS version id (0 for 1.0.0, 1 for 2.0.0)
- */
-void printFullDescription(int in,elements *elem,const char* type,xmlNsPtr ns,xmlNsPtr ns_ows,xmlNodePtr nc1,int vid){
+ * @param fromDb 1 in case the metadata comes from the DB, -1 in other cases
+ * @param serviceType the serviceType found in the ZCFG file or the DB
+ */
+void printFullDescription(xmlDocPtr doc,int in,elements *elem,const char* type,xmlNsPtr ns,xmlNsPtr ns_ows,xmlNodePtr nc1,int vid,int fromDb,const map* serviceType){
   xmlNsPtr ns1=NULL;
   if(vid==1)
@@ -1032,4 +1110,7 @@
 
     if(e->format!=NULL){
+#ifdef USE_HPC	    
+    DEFAULT_OUT:
+#endif
       const char orderedFields[13][14]={
 	"mimeType",
@@ -1138,5 +1219,6 @@
 	      char *tmps=strdup(token);
 	      tmps[strlen(tmps)]=0;
-	      xmlAddChild(nc7,xmlNewText(BAD_CAST tmps));
+	      nc8 = xmlNewText(BAD_CAST tmps);
+	      xmlAddChild(nc7,nc8);
 	      free(tmps);
 	      xmlAddChild(nc6,nc7);
@@ -1437,6 +1519,7 @@
 	xmlNsPtr ns_xlink=usedNs[xlinkId];
 
-	dumpMap(metadata);
-	addMetadata(metadata,nc2,ns_ows,ns_xlink);
+	//dumpMap(metadata);
+	addMetadata(metadata,doc,nc2,ns_ows,ns_xlink);
+	addAdditionalParameters(e->additional_parameters,doc,nc2,ns_ows,ns_xlink,fromDb);
 
       }
@@ -1583,9 +1666,10 @@
       xmlAddChild(nc2,nc3);
     }else{
-      if(e->child!=NULL && vid!=0){
-	printFullDescription(in,e->child,type,ns,ns_ows,nc2,vid);
-      }
-    }
-    
+      if(e->child!=NULL){
+	if(e->child!=NULL && vid!=0){
+	  printFullDescription(doc,in,e->child,type,ns,ns_ows,nc2,vid,fromDb,NULL);
+	}
+      }
+    }
     if(e->child!=NULL && vid==0){
       elements* children=dupElements(e->child);
@@ -1598,5 +1682,5 @@
 	cursor=cursor->next;
       }
-      printFullDescription(in,children,type,ns,ns_ows,nc2,vid);
+      printFullDescription(doc,in,children,type,ns,ns_ows,nc2,vid,fromDb,serviceType);
       xmlAddChild(nc1,nc2);
       freeElements(&children);
@@ -1604,5 +1688,4 @@
     }else
       xmlAddChild(nc1,nc2);
-    
     e=e->next;
   }
@@ -2588,5 +2671,5 @@
       toto=getMap(tmpI->content,"asReference");
 #ifdef USE_MS
-      if(toto!=NULL && strcasecmp(toto->value,"true")==0 && testMap==NULL)
+      if(toto!=NULL && strcasecmp(toto->value,"true")==0 && (testMap==NULL || strncasecmp(testMap->value,"true",4)!=0) )
 #else
       if(toto!=NULL && strcasecmp(toto->value,"true")==0)
Index: branches/prototype-v0/zoo-project/zoo-kernel/response_print.h
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/response_print.h	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/response_print.h	(revision 839)
@@ -209,7 +209,7 @@
   xmlNodePtr printWPSHeader(xmlDocPtr,maps*,const char*,const char*,const char*,int);
   xmlNodePtr printGetCapabilitiesHeader(xmlDocPtr,maps*,const char*);
-  void printGetCapabilitiesForProcess(registry*,maps*,xmlNodePtr,service*);
-  void printDescribeProcessForProcess(registry*,maps*,xmlNodePtr,service*);
-  void printFullDescription(int,elements*,const char*,xmlNsPtr,xmlNsPtr,xmlNodePtr,int);
+  void printGetCapabilitiesForProcess(registry*,maps*,xmlDocPtr,xmlNodePtr,service*);
+  void printDescribeProcessForProcess(registry*,maps*,xmlDocPtr,xmlNodePtr,service*);
+  void printFullDescription(xmlDocPtr,int,elements*,const char*,xmlNsPtr,xmlNsPtr,xmlNodePtr,int,int,const map*);
   void printDocument(maps*,xmlDocPtr,int);
   void printDescription(xmlNodePtr,xmlNsPtr,const char*,map*,int);
@@ -220,5 +220,6 @@
   void printOutputDefinitions(xmlDocPtr,xmlNodePtr,xmlNsPtr,xmlNsPtr,elements*,maps*,const char*);
   void printStatusInfo(maps*,map*,char*);
-  int addMetadata(map* meta,xmlNodePtr nc,xmlNsPtr ns_ows,xmlNsPtr ns_xlink);
+  int addAdditionalParameters(map*,xmlDocPtr,xmlNodePtr,xmlNsPtr,xmlNsPtr,int);
+  int addMetadata(map*,xmlDocPtr,xmlNodePtr,xmlNsPtr,xmlNsPtr);
 
   void outputResponse(service*,maps*,maps*,map*,int,maps*,int);
Index: branches/prototype-v0/zoo-project/zoo-kernel/server_internal.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/server_internal.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/server_internal.c	(revision 839)
@@ -597,5 +597,4 @@
 	  setMapArray(res,"value",nb,out1->name);
 	}
-
 	nb++;
 	result=out1->name;
@@ -806,6 +805,6 @@
 	 */
 	if(tmpIoType!=NULL){
-	  map* tmpCheck=getMap(tmpIoType->content,"useMapServer");
-	  if(tmpCheck!=NULL){
+	  map* tmpCheck=getMap(tmpIoType->content,"useMapserver");
+	  if(tmpCheck!=NULL && strncasecmp(tmpCheck->value,"true",4)==0){
 	    // Get the default value
 	    tmpIoType=getIoTypeFromElement(tmpInputs,tmpInputs->name,NULL);
@@ -841,5 +840,5 @@
       }
     }
-    /*if(tmpInputs->child!=NULL){
+    if(tmpInputs->child!=NULL){
       tmpInputss=tmpInputs->next;
       tmpInputs=tmpInputs->child;
@@ -848,6 +847,6 @@
 	out1s=tmpMaps;
       }
-      }else*/
-      tmpInputs=tmpInputs->next;
+    }
+    tmpInputs=tmpInputs->next;
   }
   if(tmpInputss!=NULL){
@@ -980,5 +979,5 @@
     if(isRunning(conf,pid)>0){
       if(strncasecmp(req,"GetResult",strlen(req))==0){
-	errorException (conf, _("The result for the requested JobID has not yet been generated. "),
+	errorException (conf, _("The result for the requested JobID has not yet been generated. (1)"),
 			"ResultNotReady", pid);
 	return;
@@ -1012,5 +1011,5 @@
 	  return;
 	}else{
-	  errorException (conf, _("The result for the requested JobID has not yet been generated. "),
+	  errorException (conf, _("The result for the requested JobID has not yet been generated. (2)"),
 			  "ResultNotReady", pid);
 	  freeMap(&statusInfo);
@@ -1034,4 +1033,5 @@
 	}
     }
+    free(sid);
     printStatusInfo(conf,statusInfo,req);
     freeMap(&statusInfo);
Index: branches/prototype-v0/zoo-project/zoo-kernel/service.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service.c	(revision 839)
@@ -146,4 +146,16 @@
 
 /**
+ * Create a new iotype*
+ *
+ * @return a pointer to the allocated iotype 
+ */
+iotype* createIoType(){
+  iotype* io=(iotype*)malloc(IOTYPE_SIZE);
+  io->content=NULL;
+  io->next=NULL;
+  return io;
+}
+
+/**
  * Create a new map
  *
@@ -400,4 +412,7 @@
     if(tmp->metadata!=NULL)
       free(tmp->metadata);
+    freeMap(&tmp->additional_parameters);
+    if(tmp->additional_parameters!=NULL)
+      free(tmp->additional_parameters);
     if(tmp->format!=NULL)
       free(tmp->format);
@@ -831,6 +846,8 @@
     }
   }
-  else
+  else{
     sprintf(tmp,"%s",key);
+    addToMap(m,"length","1");
+  }
   map* tmpSize=getMapArray(m,"size",index);
   if(tmpSize!=NULL && strncasecmp(key,"value",5)==0){
@@ -964,4 +981,5 @@
   res->content=NULL;
   res->metadata=NULL;
+  res->additional_parameters=NULL;  
   res->format=NULL;
   res->defaults=NULL;
@@ -983,4 +1001,5 @@
   res->content=NULL;
   res->metadata=NULL;
+  res->additional_parameters=NULL;
   res->format=NULL;
   res->defaults=NULL;
@@ -1022,4 +1041,6 @@
     fprintf(stderr," > METADATA [%s]\n",tmp->name);
     dumpMap(tmp->metadata);
+    fprintf(stderr," > ADDITIONAL PARAMETERS [%s]\n",tmp->name);
+    dumpMap(tmp->additional_parameters);
     fprintf(stderr," > FORMAT [%s]\n",tmp->format);
     iotype* tmpio=tmp->defaults;
@@ -1139,5 +1160,5 @@
   elements* cursor=e;
   elements* tmp=NULL;
-  if(cursor!=NULL){
+  if(cursor!=NULL && e->name!=NULL){
 #ifdef DEBUG
     fprintf(stderr,">> %s %i\n",__FILE__,__LINE__);
@@ -1151,4 +1172,6 @@
     tmp->metadata=NULL;
     addMapToMap(&tmp->metadata,e->metadata);
+    tmp->additional_parameters=NULL;
+    addMapToMap(&tmp->additional_parameters,e->additional_parameters);
     if(e->format!=NULL)
       tmp->format=zStrdup(e->format);
@@ -1187,5 +1210,8 @@
     else
       tmp->child=NULL;
-    tmp->next=dupElements(cursor->next);
+    if(cursor->next!=NULL)
+      tmp->next=dupElements(cursor->next);
+    else
+      tmp->next=NULL;
   }
   return tmp;
@@ -1234,6 +1260,10 @@
     fprintf(stderr,"CONTENT MAP\n");
     dumpMap(s->content);
-    fprintf(stderr,"CONTENT METADATA\n");
+    if(s->metadata!=NULL)
+      fprintf(stderr,"CONTENT METADATA\n");
     dumpMap(s->metadata);
+    if(s->additional_parameters!=NULL)
+      fprintf(stderr,"CONTENT AdditionalParameters\n");
+    dumpMap(s->additional_parameters);
   }
   if(s->inputs!=NULL){
@@ -1293,4 +1323,6 @@
   res->metadata=NULL;
   addMapToMap(&res->metadata,s->metadata);
+  res->additional_parameters=NULL;
+  addMapToMap(&res->additional_parameters,s->additional_parameters);
   res->inputs=dupElements(s->inputs);
   res->outputs=dupElements(s->outputs);
Index: branches/prototype-v0/zoo-project/zoo-kernel/service.h
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service.h	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service.h	(revision 839)
@@ -139,5 +139,5 @@
  * The memory size to create an elements
  */
-#define ELEMENTS_SIZE (sizeof(char*)+(((2*sizeof(char*))+sizeof(maps*))*2)+sizeof(char*)+(((2*sizeof(char*))+sizeof(iotype*))*2)+(2*sizeof(elements*)))
+#define ELEMENTS_SIZE (sizeof(char*)+(((2*sizeof(char*))+sizeof(maps*))*3)+sizeof(char*)+(((2*sizeof(char*))+sizeof(iotype*))*2)+(2*sizeof(elements*)))
 /**
  * The memory size to create a map
@@ -159,5 +159,5 @@
  */
 //#define SERVICE_SIZE (ELEMENTS_SIZE*2)+(MAP_SIZE*2)+sizeof(char*)
-#define SERVICE_SIZE sizeof(char*) + 2*sizeof(map*) + 2*sizeof(elements*)
+#define SERVICE_SIZE sizeof(char*) + 3*sizeof(map*) + 2*sizeof(elements*)
 /**
  * The memory size to create a services
@@ -228,4 +228,5 @@
     struct map* content; //!< the content map
     struct map* metadata; //!< the metadata map
+    struct map* additional_parameters; //!< the additional parameters map
     char* format; //!< the format: LiteralData or ComplexData or BoundingBoxData
     struct iotype* defaults; //!< the default iotype 
@@ -242,4 +243,5 @@
     struct map* content; //!< the content map
     struct map* metadata; //!< the metadata map
+    struct map* additional_parameters; //!< the additional parameters map
     struct elements* inputs; //!< the inputs elements
     struct elements* outputs; //!< the outputs elements
@@ -278,6 +280,5 @@
   ZOO_DLL_EXPORT void freeMap(map**);
   ZOO_DLL_EXPORT void freeMaps(maps** mo);
-  
-
+  ZOO_DLL_EXPORT iotype* createIoType();
   ZOO_DLL_EXPORT elements* createEmptyElements();
   ZOO_DLL_EXPORT elements* createElements(const char*);
Index: branches/prototype-v0/zoo-project/zoo-kernel/service_conf.y
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_conf.y	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_conf.y	(revision 839)
@@ -33,5 +33,7 @@
 static bool wait_maincontent=true;
 static bool wait_mainmetadata=false;
+static bool wait_mainap=false;
 static bool wait_metadata=false;
+static bool wait_ap=false;
 static bool wait_nested=false;
 static bool wait_inputs=false;
@@ -156,15 +158,16 @@
     current_content=NULL;
     my_service->metadata=NULL;
+    my_service->additional_parameters=NULL;
     wait_maincontent=false;
   }
   if(strncasecmp($2,"EndNested",9)==0){
 #ifdef DEBUG_SERVICE_CONF
-      fprintf(stderr,"(ENDNESTED - %d) \n",__LINE__);
-      fflush(stderr);
-#endif
-      nested_level-=1;
-      if(nested_level==0){
-	wait_nested=false;
-      }
+    fprintf(stderr,"(ENDNESTED - %d) \n",__LINE__);
+    fflush(stderr);
+#endif
+    nested_level-=1;
+    if(nested_level==0){
+      wait_nested=false;
+    }
   }
 
@@ -229,25 +232,56 @@
     }
     else
-      if(strncasecmp($2,"MetaData",8)==0){
-	previous_data=current_data;
-	current_data=3;
-	if(current_element!=NULL){
-#ifdef DEBUG_SERVICE_CONF
-	  fprintf(stderr,"add current_content to current_element->content\n");
-	  fprintf(stderr,"LINE %d",__LINE__);
-#endif
-	  addMapToMap(&current_element->content,current_content);
-	  freeMap(&current_content);
-	  free(current_content);
-	  if(previous_data==1 || previous_data==2)
-	    wait_metadata=true;
-	  else
-	    wait_mainmetadata=true;
-	}
-	else{
-	  if(previous_data==1 || previous_data==2)
-	    wait_metadata=true;
-	  else
-	    wait_mainmetadata=true;
+      if(strncasecmp($2,"MetaData",8)==0 ||
+	 strncasecmp($2,"AdditionalParameters",8)==0){
+	if(strncasecmp($2,"AdditionalParameters",8)==0){
+	  previous_data=current_data;
+	  current_data=4;
+	  if(current_element!=NULL){
+#ifdef DEBUG_SERVICE_CONF
+	    fprintf(stderr,"add current_content to current_element->content\n");
+	    fprintf(stderr,"LINE %d",__LINE__);
+#endif
+	    if(wait_mainmetadata)
+	      addMapToMap(&my_service->metadata,current_content);
+	    else
+	      if(wait_metadata)
+		addMapToMap(&current_element->metadata,current_content);
+	      else
+		addMapToMap(&current_element->content,current_content);		
+	    freeMap(&current_content);
+	    free(current_content);
+	    if(previous_data==1 || previous_data==2)
+	      wait_ap=true;
+	    else
+	      wait_mainap=true;
+	  }
+	  else{
+	    if(previous_data==1 || previous_data==2)
+	      wait_ap=true;
+	    else
+	      wait_mainap=true;
+	  }
+	}else{
+	  previous_data=current_data;
+	  current_data=3;
+	  if(current_element!=NULL){
+#ifdef DEBUG_SERVICE_CONF
+	    fprintf(stderr,"add current_content to current_element->content\n");
+	    fprintf(stderr,"LINE %d",__LINE__);
+#endif
+	    addMapToMap(&current_element->content,current_content);
+	    freeMap(&current_content);
+	    free(current_content);
+	    if(previous_data==1 || previous_data==2)
+	      wait_metadata=true;
+	    else
+	      wait_mainmetadata=true;
+	  }
+	  else{
+	    if(previous_data==1 || previous_data==2)
+	      wait_metadata=true;
+	    else
+	      wait_mainmetadata=true;
+	  }
 	}
 	current_content=NULL;
@@ -425,4 +459,31 @@
   if(strcmp($3,"DataOutputs")==0){
     current_data=2;
+  }  
+  if(strcmp($3,"AdditionalParameters")==0){
+    if(current_content!=NULL){
+#ifdef DEBUG_SERVICE_CONF
+      fprintf(stderr,"add current_content to current_element->content\n");
+      fprintf(stderr,"LINE %d",__LINE__);
+#endif
+      if(wait_ap){
+	current_element->additional_parameters=NULL;
+	addMapToMap(&current_element->additional_parameters,current_content);
+	current_element->next=NULL;
+	current_element->defaults=NULL;
+	current_element->supported=NULL;
+	current_element->child=NULL;
+      }else{
+	if(wait_mainap){
+	  addMapToMap(&my_service->additional_parameters,current_content);
+	}
+      }
+
+      freeMap(&current_content);
+      free(current_content);
+      current_content=NULL;
+    }
+    current_data=previous_data;
+    wait_mainap=false;
+    wait_ap=false;
   }
   if(strcmp($3,"MetaData")==0){
@@ -866,4 +927,6 @@
   wait_mainmetadata=false;
   wait_metadata=false;
+  wait_mainap=false;
+  wait_ap=false;
   wait_inputs=false;
   wait_defaults=false;
Index: branches/prototype-v0/zoo-project/zoo-kernel/service_internal.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_internal.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_internal.c	(revision 839)
@@ -148,6 +148,6 @@
 
     //FILE* f0 = fopen (fileName, "r");
-	// knut: open file in binary mode to avoid conversion of line endings (yielding extra bytes) on Windows platforms
-	FILE* f0 = fopen(fileName, "rb"); 
+    // knut: open file in binary mode to avoid conversion of line endings (yielding extra bytes) on Windows platforms
+    FILE* f0 = fopen(fileName, "rb"); 
     if(f0!=NULL){
       fseek (f0, 0, SEEK_END);
@@ -162,5 +162,4 @@
 	free(stat);
       }
-
       return tmps1;
     }
Index: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.c	(revision 839)
@@ -2,5 +2,9 @@
  * Author : Gérald FENOY
  *
- * Copyright (c) 2015 GeoLabs SARL
+ * Copyright (c) 2017 GeoLabs SARL
+ *
+ * This work was supported by public funds received in the framework of GEOSUD,
+ * a project (ANR-10-EQPX-20) of the program "Investissements d'Avenir" managed 
+ * by the French National Research Agency
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -22,7 +26,4 @@
  * THE SOFTWARE.
  *
- * See Ref: http://hg.orfeo-toolbox.org/OTB/ Copyright
- * Some parts of this code are derived from ITK. See ITKCopyright.txt for 
- * details.
  */
 
@@ -30,25 +31,130 @@
 #include "response_print.h"
 #include "server_internal.h"
+#include "service_callback.h"
+#include "mimetypes.h"
 #include <sys/un.h>
 
-void appendOutputParameters(maps* input,char** parameters,int* cnt,service* s,map* uuid,map* targetPathMap){
-  while(input!=NULL){
-    if(input->child==NULL){
-      *cnt+=1;
-      if(*cnt==1)
-	parameters=(char**)malloc((*cnt)*sizeof(char*));
-      else
-	parameters=(char**)realloc(parameters,(*cnt)*sizeof(char*));
-      // Name every files that should be produced by the service execution
-      char* targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+10)*sizeof(char));
-      sprintf(targetName,"output_%s_%s_%s",s->name,input->name,uuid->value);
-      // TODO: We should verify if any optional tag for output is required (i.e. -out output.tiff int8)
-      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
-      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
-      parameters[*cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
-      sprintf(parameters[*cnt-1],"-%s %s",input->name,targetPath);
-    }else
-      appendOutputParameters(input->child,parameters,cnt,s,uuid,targetPathMap);
-    input=input->next;
+typedef struct {
+  maps* conf;
+  char* local_file;
+  char* target_file;
+} local_params;
+
+#ifdef PTHREADS
+#endif
+
+void addNestedOutputs(service** s){
+  if((*s)==NULL){
+    return;
+  }    
+  if(*s==NULL || (*s)->outputs==NULL){
+    return;
+  }
+  elements *out=(*s)->outputs;
+  elements* cur=out;
+  map* serviceType=getMap((*s)->content,"ServiceType");
+  if(strncmp(serviceType->value,"HPC",3)!=0)
+    return;
+  while(cur!=NULL && cur->defaults!=NULL){
+    map* mimeType=getMap(cur->defaults->content,"mimeType");
+    if(mimeType!=NULL){
+      int geo=isGeographic(mimeType->value);
+      if(geo>0){
+	elements *tmp[3]={
+	  dupElements(cur),
+	  dupElements(cur),
+	  dupElements(cur)
+	};
+	char *geoLink="wcs_link";
+	if(geo==2){
+	  geoLink="wfs_link";
+	}
+	int i=0;
+	for(;i<3;i++){
+	  if(tmp[i]->next!=NULL){
+	    freeElements(&tmp[i]->next);
+	    free(tmp[i]->next);
+	    tmp[i]->next=NULL;
+	  }
+	  free(tmp[i]->name);
+	  tmp[i]->format=zStrdup("ComplexData");
+	  freeMap(&tmp[i]->content);
+	  free(tmp[i]->content);
+	  tmp[i]->content=NULL;
+	  switch(i){
+	  case 0:
+	    tmp[i]->name=zStrdup("download_link");
+	    tmp[i]->content=createMap("Title",_("Download link"));
+	    addToMap(tmp[i]->content,"Abstract",_("The download link"));
+	    addToMap(tmp[i]->defaults->content,"useMapserver","false");
+	    if(tmp[i]->supported!=NULL){
+	      freeIOType(&tmp[i]->supported);
+	      free(tmp[i]->supported);
+	      tmp[i]->supported=NULL;
+	    }
+	    break;
+	  case 1:
+	    tmp[i]->name=zStrdup("wms_link");
+	    tmp[i]->content=createMap("Title",_("WMS link"));
+	    addToMap(tmp[i]->content,"Abstract",_("The WMS link"));
+	    if(tmp[i]->supported!=NULL && tmp[i]->supported->next!=NULL){
+	      freeIOType(&tmp[i]->supported->next);
+	      free(tmp[i]->supported->next);
+	      tmp[i]->supported->next=NULL;
+	    }else{
+	      if(tmp[i]->supported!=NULL)
+		addToMap(tmp[i]->supported->content,"useMapserver","true");
+	      addToMap(tmp[i]->defaults->content,"useMapserver","true");
+	    }
+	    break;
+	  case 2:
+	    if(geo==2){
+	      tmp[i]->name=zStrdup("wfs_link");
+	      tmp[i]->content=createMap("Title",_("WFS link"));
+	      addToMap(tmp[i]->content,"Abstract",_("The WFS link"));
+	    }else{
+	      tmp[i]->name=zStrdup("wcs_link");
+	      tmp[i]->content=createMap("Title",_("WCS link"));
+	      addToMap(tmp[i]->content,"Abstract",_("The WCS link"));
+	    }
+	    if(tmp[i]->supported!=NULL && tmp[i]->supported->next!=NULL &&
+	       tmp[i]->supported->next->content!=NULL){
+	      freeIOType(&tmp[i]->supported);
+	      free(tmp[i]->supported);
+	      tmp[i]->supported=NULL;
+	      tmp[i]->supported=createIoType();
+	      iotype* cnext=cur->supported->next;
+	      tmp[i]->supported->content=createMap(cnext->content->name,cnext->content->value);
+	      addMapToMap(&tmp[i]->supported->content,cnext->content->next);
+	      addToMap(tmp[i]->supported->content,"useMapserver","true");
+	    }else
+	      addToMap(tmp[i]->defaults->content,"useMapserver","true");
+	    break;
+	  }
+	}
+	
+	addToElements(&cur->child,tmp[0]);
+	addToElements(&cur->child,tmp[1]);
+	addToElements(&cur->child,tmp[2]);
+	free(cur->format);
+	cur->format=NULL;
+	if(cur->defaults!=NULL){
+	  freeIOType(&cur->defaults);
+	  cur->defaults=NULL;
+	}
+	if(cur->supported!=NULL){
+	  freeIOType(&cur->supported);
+	  cur->supported=NULL;
+	}
+	freeElements(&tmp[2]);
+	free(tmp[2]);
+	freeElements(&tmp[1]);
+	free(tmp[1]);
+	freeElements(&tmp[0]);
+	free(tmp[0]);
+	//addToMap(cur->content,"internal","true");
+      }
+    }
+    cur=cur->next;
   }
 }
@@ -71,59 +177,94 @@
   map* tmp=NULL;
   int res=-1;
-  map* targetPathMap=getMapFromMaps(*main_conf,"HPC","storagePath");
+  char *serviceType;
+  map* mServiceType=getMap(s->content,"serviceType");
+  if(mServiceType!=NULL)
+    serviceType=mServiceType->value;
+  else
+    serviceType="HPC";
+  map* targetPathMap=getMapFromMaps(*main_conf,serviceType,"storagePath");
   map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
   map* uuid=getMapFromMaps(*main_conf,"lenv","usid");
-
-  sleep(5);
+  pthread_t threads_pool[50];
+  // Force the HPC services to be called asynchronously
+  map* isAsync=getMapFromMaps(*main_conf,"lenv","async");
+  if(isAsync==NULL){
+    errorException(*main_conf,_("The synchronous mode is not supported by this type of service"),"NoSuchMode",s->name);
+    return -1;
+  }
 
   maps* input=*real_inputs;
   char **parameters=NULL;
   int parameters_cnt=0;
-  while(input!=NULL){
-    parameters_cnt+=1;
-    if(parameters_cnt==1)
-      parameters=(char**)malloc(parameters_cnt*sizeof(char*));
-    else
-      parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
-    if(getMap(input->content,"mimeType")!=NULL){
-      // Input is ComplexData
-      SSHCON *test=ssh_connect(*main_conf);
-      dumpMaps(getMaps(*main_conf,"lenv"));
-      if(test==NULL){	
-	sleep(3600);
-	return -1;
-      }
-      if(getMap(input->content,"cache_file")==NULL){
-	// Input data has been passed by value
-	// TODO: store data remotely
-	// TODO: publish input through MapServer / use output publication
-	dumpMapsValuesToFiles(main_conf,&input);
-      }
-      if(getMap(input->content,"cache_file")!=NULL){
-	// Input data passed by reference or by value
-	map* tmp=getMap(input->content,"cache_file");
-	char* targetName=strrchr(tmp->value,'/');
-	char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
-	sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
-	ssh_copy(*main_conf,tmp->value,targetPath);
-	parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
-	sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
-	free(targetPath);
+  while(input!=NULL && input->content!=NULL){
+    if(getMaps(*real_outputs,input->name)==NULL){
+      parameters_cnt+=1;
+      if(parameters_cnt==1)
+	parameters=(char**)malloc(parameters_cnt*sizeof(char*));
+      else
+	parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
+      if(getMap(input->content,"mimeType")!=NULL){
+	// Input is ComplexData
+	if(getMap(input->content,"cache_file")==NULL){
+	  // Input data has been passed by value
+	  // TODO: publish input through MapServer / use output publication
+	  dumpMapsValuesToFiles(main_conf,&input);
+	  addToMap(input->content,"toPublish","true");
+	}
+	if(getMap(input->content,"cache_file")!=NULL){
+	  map* length=getMap(input->content,"length");
+	  if(length==NULL){
+	    addToMap(input->content,"length","1");
+	    length=getMap(input->content,"length");
+	  }
+	  int len=atoi(length->value);
+	  int i=0;
+	  for(i=0;i<len;i++){
+	    map* tmp=getMapArray(input->content,"cache_file",i);
+	    char* targetName=strrchr(tmp->value,'/');
+	    char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
+	    sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
+	    setMapArray(input->content,"targetPath",i,targetPath);
+	    setMapArray(input->content,"localPath",i,tmp->value);
+	    addToUploadQueue(main_conf,input);
+	    if(i==0){
+	      parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
+	      sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
+	    }else{
+	      char *tmpStr=zStrdup(parameters[parameters_cnt-1]);
+	      parameters[parameters_cnt-1]=(char*)realloc(parameters[parameters_cnt-1],(strlen(tmpStr)+strlen(targetPath)+2)*sizeof(char));
+	      sprintf(parameters[parameters_cnt-1],"%s %s",tmpStr,targetPath);
+	      free(tmpStr);
+	    }
+	    free(targetPath);
+	  }
+	}else{
+	  // ???
+	  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+	  fflush(stderr);
+	}
       }else{
-	// ???
-	fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
-	fflush(stderr);
-      }
-    }else{
-      // LitteralData and BboxData 
-      if(getMap(input->content,"dataType")!=NULL){
-	// For LitteralData, simply pass the value
-	map* val=getMap(input->content,"value");
-	sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,val->value);
-      }
-      
+	// LitteralData and BboxData 
+	if(getMap(input->content,"dataType")!=NULL){
+	  // For LitteralData, simply pass the value
+	  map* val=getMap(input->content,"value");
+	  parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(val->value)+3)*sizeof(char));
+	  sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,val->value);
+	}
+      }
     }
     input=input->next;
   }
+
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  invokeCallback(m,inputs,NULL,2,0);
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+
+  // Upload data on HPC
+  runUpload(main_conf);
+  
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  invokeCallback(m,inputs,NULL,2,1);
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
 
   // Add the filename to generate for every output to the parameters
@@ -140,24 +281,89 @@
 	parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
       // Name every files that should be produced by the service execution
-      char* targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+10)*sizeof(char));
-      sprintf(targetName,"output_%s_%s_%s",s->name,input->name,uuid->value);
-      // TODO: We should verify if any optional tag for output is required (i.e. -out output.tiff *int8*)
-      // TODO: Add support for Array values
+      map* mime=getMap(input->content,"mimeType");
+      char* targetName;
+      if(mime!=NULL){
+	bool hasExt=false;
+	map* fileExt=getFileExtensionMap(mime->value,&hasExt);
+	targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
+	sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
+	freeMap(&fileExt);
+	free(fileExt);
+      }else{
+	targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
+	sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
+      }
       char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
       sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
       setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
-      parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
-      sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
+      // We should verify if any optional tag for output is required
+      // (i.e. -out output.tiff *int8*), meaning that we should search
+      // for a corresponding inputs name.
+      map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
+      if(inValue!=NULL){
+	parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
+	sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
+      }else{
+	parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
+	sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
+      }
+      free(targetPath);
+    }// In other case it means we need to return the cache_file as generated_file
+    else{
+      parameters_cnt+=1;
+      if(parameters_cnt==1)
+	parameters=(char**)malloc(parameters_cnt*sizeof(char*));
+      else
+	parameters=(char**)realloc(parameters,parameters_cnt*sizeof(char*));
+      // Name every files that should be produced by the service execution
+      map* mime=getMap(input->child->content,"mimeType");
+      char* targetName;
+      if(mime!=NULL){
+	bool hasExt=false;
+	map* fileExt=getFileExtensionMap(mime->value,&hasExt);
+	targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+strlen(fileExt->value)+11)*sizeof(char));
+	sprintf(targetName,"output_%s_%s_%s.%s",s->name,input->name,uuid->value,fileExt->value);
+	freeMap(&fileExt);
+	free(fileExt);
+      }else{
+	targetName=(char*)malloc((strlen(s->name)+strlen(input->name)+strlen(uuid->value)+14)*sizeof(char));
+	sprintf(targetName,"output_%s_%s_%s.tif",s->name,input->name,uuid->value);
+      }
+      char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
+      sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
+      addToMap(input->content,"generated_file",targetPath);
+      // We should verify if any optional tag for output is required
+      // (i.e. -out output.tiff *int8*), meaning that we should search
+      // for a corresponding inputs name.
+      map* inValue=getMapFromMaps(*real_inputs,input->name,"value");
+      if(inValue!=NULL){
+	parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+strlen(inValue->value)+4)*sizeof(char));
+	sprintf(parameters[parameters_cnt-1],"-%s %s %s",input->name,targetPath,inValue->value);
+      }else{
+	parameters[parameters_cnt-1]=(char*)malloc((strlen(input->name)+strlen(targetPath)+3)*sizeof(char));
+	sprintf(parameters[parameters_cnt-1],"-%s %s",input->name,targetPath);
+      }
+      free(targetPath);
     }
     input=input->next;
   }
   
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  fflush(stderr);
+  invokeCallback(m,inputs,NULL,3,0);
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  fflush(stderr);
+
   // Produce the SBATCH File locally
-  char *scriptPath=(char*)malloc((strlen(s->name)+strlen(tmpPath->value)+strlen(uuid->value)+9)*sizeof(char));
+  char *scriptPath=(char*)malloc((strlen(s->name)+strlen(tmpPath->value)+strlen(uuid->value)+10)*sizeof(char));
   sprintf(scriptPath,"%s/zoo_%s_%s.sh",tmpPath->value,s->name,uuid->value);
+  setMapInMaps(*main_conf,"lenv","local_script",scriptPath);
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  fflush(stderr);
+  invokeCallback(m,inputs,NULL,3,0);
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  fflush(stderr);
   FILE* scriptFile=fopen(scriptPath,"w+");
-  maps* hpc_opts=getMaps(*main_conf,"sbatch_options");
-  map* hpc_opts_content=hpc_opts->content;
-  map* headerMap=getMapFromMaps(*main_conf,"HPC","header");
+  map* headerMap=getMapFromMaps(*main_conf,serviceType,"header");
   if(headerMap!=NULL){
     // Use the header file if defined in the HPC section of the main.cfg file
@@ -171,5 +377,5 @@
       fcontent[fsize]=0;
       fclose(f);
-      fprintf(scriptFile,"%s\n### --- ZOO-Service HEADER end --- ###",fcontent);
+      fprintf(scriptFile,"%s\n### --- ZOO-Service HEADER end --- ###\n\n",fcontent);
       free(fcontent);
     }else
@@ -177,8 +383,11 @@
   }else
     fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service HEADER *** ###\n\n");
-  
-  while(hpc_opts_content!=NULL){
-    fprintf(scriptFile,"#SBATCH --%s=%s\n",hpc_opts_content->name,hpc_opts_content->value);
-    hpc_opts_content=hpc_opts_content->next;
+  maps* hpc_opts=getMaps(*main_conf,"sbatch_options");
+  if(hpc_opts!=NULL){
+    map* hpc_opts_content=hpc_opts->content;
+    while(hpc_opts_content!=NULL){
+      fprintf(scriptFile,"#SBATCH --%s=%s\n",hpc_opts_content->name,hpc_opts_content->value);
+      hpc_opts_content=hpc_opts_content->next;
+    }
   }
   fprintf(scriptFile,"#SBATCH --job-name=ZOO-Project_%s_%s\n\n",uuid->value,s->name);
@@ -186,7 +395,27 @@
   if(mods!=NULL)
     fprintf(scriptFile,"#SBATCH --export=MODULES=%s\n",mods->value);
-  else
-    fprintf(scriptFile,"#SBATCH --export=MODULES=\n");
+
+  map* bodyMap=getMapFromMaps(*main_conf,serviceType,"body");
+  if(bodyMap!=NULL){
+    // Use the header file if defined in the HPC section of the main.cfg file
+    struct stat f_status;
+    int s=stat(bodyMap->value, &f_status);
+    if(s==0){
+      char* fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
+      FILE* f=fopen(bodyMap->value,"rb");
+      fread(fcontent,f_status.st_size,1,f);
+      int fsize=f_status.st_size;
+      fcontent[fsize]=0;
+      fclose(f);
+      fprintf(scriptFile,"%s\n### --- ZOO-Service BODY end --- ###\n\n",fcontent);
+      free(fcontent);
+    }else
+      fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service BODY (no body found) *** ###\n\n");
+  }else
+    fprintf(scriptFile,"#!/bin/bash\n\n### *** Default ZOO-Service BODY *** ###\n\n");
+
+  
   map* sp=getMap(s->content,"serviceProvider");
+  
   // Require to produce the command line to be executed
   fprintf(scriptFile,"\n\necho \"Job started at: $(date)\"\n");
@@ -195,15 +424,23 @@
   for(int i=0;i<parameters_cnt;i++){
     fprintf(scriptFile," %s",parameters[i]);
-    //free(parameters[i]);
+  }
+  for(int i=parameters_cnt-1;i>=0;i--){
+    free(parameters[i]);
   }
   free(parameters);
   fprintf(scriptFile,"\n");
   fprintf(scriptFile,"echo \"Job finished at: $(date)\"\n");
-  
   fflush(scriptFile);
   fclose(scriptFile);
+  
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  invokeCallback(m,inputs,NULL,3,1);
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
 
   // Upload the SBATCH File to the remote host
-  targetPathMap=getMapFromMaps(*main_conf,"HPC","executePath");
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  invokeCallback(m,inputs,NULL,4,0);
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  targetPathMap=getMapFromMaps(*main_conf,serviceType,"executePath");
   if(targetPathMap==NULL){
     setMapInMaps(*main_conf,"lenv","message",_("There is no executePath defined in you HPC section!"));
@@ -213,19 +450,18 @@
   char *targetPath=(char*)malloc((strlen(targetPathMap->value)+strlen(targetName)+2)*sizeof(char));
   sprintf(targetPath,"%s/%s",targetPathMap->value,targetName);
+  setMapInMaps(*main_conf,"lenv","remote_script",targetPath);
   SSHCON *test=ssh_connect(*main_conf);
-  ssh_copy(*main_conf,scriptPath,targetPath);
+  ssh_copy(*main_conf,scriptPath,targetPath,ssh_get_cnt(*main_conf));
   
   // Execute the SBATCH script remotely
   map* subStr=getMapFromMaps(*main_conf,"HPC","subStr");
-  //char *command=(char*)malloc((strlen(targetPath)+strlen(targetPathMap->value)+strlen(subStr->value)+strlen(uuid->value)+37)*sizeof(char));
   char *command=(char*)malloc((strlen(targetPath)+strlen(targetPathMap->value)+strlen(subStr->value)+strlen(uuid->value)+137)*sizeof(char));
-  //sprintf(command,"ls # %s 2> %s/error_%s.log | sed \"s:%s::g\"",targetPath,targetPathMap->value,uuid->value,subStr->value);
   sprintf(command,"sbatch %s 2> %s/error_%s.log | sed \"s:%s::g\"",targetPath,targetPathMap->value,uuid->value,subStr->value);
-  if(ssh_exec(*main_conf,command)==0){
+  if(ssh_exec(*main_conf,command,ssh_get_cnt(m))==0){
     // The sbatch command has failed!
     // Download the error log file from the HPC server
     char tmpS[1024];
     free(command);
-    command=(char*)malloc((strlen(targetPathMap->value)+strlen(uuid->value)+22)*sizeof(char));
+    command=(char*)malloc((strlen(targetPathMap->value)+strlen(uuid->value)+11)*sizeof(char));
     sprintf(command,"%s/error_%s.log",targetPathMap->value,uuid->value);
     targetName=strrchr(command,'/');
@@ -233,5 +469,5 @@
     targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(targetName)+2)*sizeof(char));
     sprintf(targetPath,"%s/%s",tmpPath->value,targetName);
-    if(ssh_fetch(*main_conf,targetPath,command)==0){
+    if(ssh_fetch(*main_conf,targetPath,command,ssh_get_cnt(m))==0){
       struct stat f_status;
       int ts=stat(targetPath, &f_status);
@@ -251,4 +487,5 @@
       setMapInMaps(*main_conf,"lenv","message",_("Unable to fetch the remote error log file"));
     tmpPath=getMapFromMaps(*main_conf,"lenv","message");
+    invokeCallback(m,NULL,NULL,7,1);
     sprintf(tmpS, "Cannot execute the HPC ZOO-Service %s: %s", s->name, tmpPath->value);
     errorException(m,tmpS,"NoApplicableCode",NULL);
@@ -259,4 +496,10 @@
     return -1;
   }
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  fflush(stderr);
+  invokeCallback(m,NULL,NULL,4,1);
+  fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+  fflush(stderr);
+  free(command);
 
   struct sockaddr_un addr;
@@ -264,8 +507,8 @@
   addr.sun_family = AF_UNIX;
   int rc, cl, fd = socket(AF_UNIX, SOCK_STREAM, 0);
-  char buf[100];
-  char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+19));
+  char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(uuid->value)+20));
   sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,uuid->value);
   strncpy(addr.sun_path, sname, sizeof(addr.sun_path)-1);
+  
   if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
     perror("bind error");
@@ -274,94 +517,93 @@
   }
   if (listen(fd, 5) == -1) {
-    perror("listen error");
-    sleep(120);
+    setMapInMaps(*main_conf,"lenv","message",_("Listen error"));
     return -1;
   }
-  /*fd_set master, read_fds;
-  int fdmax;
-  FD_ZERO(&master);
-  FD_ZERO(&read_fds);
-  FD_SET(fd, &master);
-  if (select(fd+1, &master, NULL, NULL, NULL) == -1) {
-    perror("select");
-    sleep(120);
+  if ( (cl = accept(fd, NULL, NULL)) == -1) {
+    setMapInMaps(*main_conf,"lenv","message",_("Accept error"));
     return -1;
-  }
-  if (FD_ISSET(fd, &master)) {*/
-    if ( (cl = accept(fd, NULL, NULL)) == -1) {
-      perror("accept error");
+  }else{
+    int hasPassed=-1;
+    char buf[11];
+    memset(&buf,0,11);
+    while ( (rc=read(cl,buf,10)) ) {	  
+      if(rc==0){
+	sleep(1);
+	setMapInMaps(*main_conf,"lenv","message",_("Read closed"));
+	return -1;
+      }else{
+	if(rc<0){
+	  setMapInMaps(*main_conf,"lenv","message",_("Read error"));
+	  return -1;
+	}
+      }
+      hasPassed=1;
+      res=atoi(buf);
+      unlink(sname);
+      //free(sname);  
+
+      if(res==3){
+	fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+	fflush(stderr);
+	invokeCallback(m,NULL,outputs,5,0);
+	fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+	fflush(stderr);
+	input=*real_outputs;
+	while(input!=NULL){
+	  if(input->child==NULL){
+	    map* generatedFile=getMap(input->content,"generated_file");
+	    if(generatedFile!=NULL){
+	      char* filename=strrchr(generatedFile->value,'/');
+	      char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
+	      sprintf(targetPath,"%s/%s",tmpPath->value,filename);
+	      test=ssh_connect(*main_conf);
+	      if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
+		setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
+		free(targetPath);
+	      }else{
+		char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
+		sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
+		setMapInMaps(*main_conf,"lenv","message",tmpStr);
+		free(tmpStr);
+		return SERVICE_FAILED;
+	      }
+	    }	    
+	  }else{
+	    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+	    fflush(stderr);
+	    map* generatedFile=getMap(input->content,"generated_file");
+	    if(generatedFile!=NULL){
+	      char* filename=strrchr(generatedFile->value,'/');
+	      char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
+	      sprintf(targetPath,"%s/%s",tmpPath->value,filename);
+	      test=ssh_connect(*main_conf);
+	      if(ssh_fetch(*main_conf,targetPath,generatedFile->value,ssh_get_cnt(m))==0){
+		maps* tmp=getMaps(*real_outputs,input->name);
+		freeMap(&tmp->content);
+		free(tmp->content);
+		tmp->content=NULL;
+		maps* output=getMaps(*real_outputs,input->name);
+		setMapInMaps(output->child,"download_link","generated_file",targetPath);
+		setMapInMaps(output->child,"download_link","useMapserver","false");
+		setMapInMaps(output->child,"WMS_LINK","generated_file",targetPath);
+		setMapInMaps(output->child,"WMS_LINK","useMapserver","true");
+		setMapInMaps(output->child,"WCS_LINK","generated_file",targetPath);
+		setMapInMaps(output->child,"WCS_LINK","useMapserver","true");
+	      }
+	    }
+	  }
+	  input=input->next;
+	}
+      }
+      //free(buf);
+    }
+    if(hasPassed<0){
+      perror("Failed to read");
+      setMapInMaps(*main_conf,"lenv","message",_("Unable to parse the value returned by remote execution"));
       sleep(120);
-      return -1;
-    }else{
-      int hasPassed=-1;
-      /*FD_SET(cl,&read_fds);
-      if (select(cl+1, &read_fds, NULL, NULL, NULL) == -1) {
-	perror("select");
-	sleep(120);
-	return -1;
-	}*/
-      while ( (rc=read(cl,buf,10)) ) {	  
-	if(rc==0){
-	  sleep(120);
-	  return -1;
-	}else{
-	  if(rc<0){
-	    perror("read");
-	    sleep(120);
-	    return -1;
-	  }
-	}
-	hasPassed=1;
-	res=atoi(buf);
-	unlink(sname);
-	if(res==3){
-	  input=*real_outputs;
-	  while(input!=NULL){
-	    // TODO: parse all outputs including inner outputs if required.
-	    if(input->child==NULL){
-	      //map* dataPath=getMapFromMaps(*main_conf,"main","dataPath");
-	      map* generatedFile=getMap(input->content,"generated_file");
-	      if(generatedFile!=NULL){
-		char* filename=strrchr(generatedFile->value,'/');
-		char* targetPath=(char*)malloc((strlen(tmpPath->value)+strlen(filename)+2)*sizeof(char));
-		sprintf(targetPath,"%s/%s",tmpPath->value,filename);
-		test=ssh_connect(*main_conf);
-		if(ssh_fetch(*main_conf,targetPath,generatedFile->value)==0){
-		  setMapInMaps(*real_outputs,input->name,"generated_file",targetPath);
-		}else{
-		  char *tmpStr=(char*)malloc((strlen(filename)+strlen(_("Unable to fetch the remote file for %s"))+1)*sizeof(char));
-		  sprintf(tmpStr,_("Unable to fetch the remote file for %s"),filename);
-		  setMapInMaps(*main_conf,"lenv","message",tmpStr);
-		  free(tmpStr);
-		  return SERVICE_FAILED;
-		}
-	      }
-	    }/*else{
-	     // Generate the nested outputs based on each input value
-	     if(getMaps(*real_inputs,input->name)!=NULL && getMapFromMaps(*real_inputs,input->name,"mimeType")!=NULL){
-	     // Input was ComplexeData
-	     maps* output=getMaps(*real_outputs,input->name);
-	     map* cache=getMapFromMaps(*real_inputs,input->name,"cache_file");
-	     setMapInMaps(output->child,"download","generated_file",cache->value);
-	     setMapInMaps(output->child,"WCS_LINK","generated_file",cache->value);
-	     setMapInMaps(output->child,"WCS_LINK","useMs","true");
-	     setMapInMaps(output->child,"WMS_LINK","generated_file",cache->value);
-	     setMapInMaps(output->child,"WMS_LINK","useMs","true");
-	     }
-	     }*/
-	    input=input->next;
-	  }
-	}
-      }
-      if(hasPassed<0){
-	perror("Failed to read");
-	setMapInMaps(*main_conf,"lenv","message",_("Unable to parse the value returned by remote execution"));
-	sleep(120);
-	return SERVICE_FAILED;
-      }
+      return SERVICE_FAILED;
     }
-    //}
+  }
   ssh_close(*main_conf);
-  //sleep(120);
   return res;
 }
Index: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.h
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.h	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_internal_hpc.h	(revision 839)
@@ -2,5 +2,9 @@
  * Author : Gérald FENOY
  *
- * Copyright (c) 2015 GeoLabs SARL
+ * Copyright (c) 2017 GeoLabs SARL
+ *
+ * This work was supported by public funds received in the framework of GEOSUD,
+ * a project (ANR-10-EQPX-20) of the program "Investissements d'Avenir" managed 
+ * by the French National Research Agency
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -37,4 +41,5 @@
 #include "sshapi.h"
 
+void addNestedOutputs(service**);
 int zoo_hpc_support(maps**,map*,service*,maps**,maps**);
 
Index: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_js.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_internal_js.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_internal_js.c	(revision 839)
@@ -934,9 +934,19 @@
   }else{
     if(argc==3){
-      char *body=JSValToChar(cx,&argv[2]);
-      InternetOpenUrl(&hInternet,hInternet.waitingRequests[hInternet.nb],body,strlen(body),
-		      INTERNET_FLAG_NO_CACHE_WRITE,0);
-      processDownloads(&hInternet);
-      free(body);
+      if(strncasecmp(method,"GET",3)==0){
+	header=JSVAL_TO_OBJECT(argv[2]);
+	if(JS_IsArrayObject(cx,header)){
+	  setHeader(&hInternet,cx,header);
+	}
+	InternetOpenUrl(&hInternet,hInternet.waitingRequests[hInternet.nb],NULL,0,
+			INTERNET_FLAG_NO_CACHE_WRITE,0);
+	processDownloads(&hInternet);
+      }else{
+	char *body=JSValToChar(cx,&argv[2]);
+	InternetOpenUrl(&hInternet,hInternet.waitingRequests[hInternet.nb],body,strlen(body),
+			INTERNET_FLAG_NO_CACHE_WRITE,0);
+	processDownloads(&hInternet);
+	free(body);
+      }
     }else{
       InternetOpenUrl(&hInternet,hInternet.waitingRequests[hInternet.nb],NULL,0,
Index: branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/service_internal_ms.c	(revision 839)
@@ -131,15 +131,5 @@
  */
 void setReferenceUrl(maps* m,maps* tmpI){
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
-  dumpMaps(tmpI);
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   outputMapfile(m,tmpI);
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
-  dumpMaps(tmpI);
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   map *msUrl=getMapFromMaps(m,"main","mapserverAddress");
   if(msUrl==NULL){
@@ -169,5 +159,6 @@
   if(strncasecmp(rformat->value,"text/xml",8)==0)
     proto=1;
-  if(strncasecmp(rformat->value,"image/tiff",10)==0)
+  if(strncasecmp(rformat->value,"image/tiff",10)==0 ||
+     strncasecmp(rformat->value,"image/geotiff",10)==0)
     proto=2;
   if(protoMap!=NULL){
@@ -478,5 +469,5 @@
     fprintf(stderr,"Unable to access the DataSource as ZIP File\n");
     setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
-    OGR_DS_Destroy(poDS1);
+    //OGR_DS_Destroy(poDS1);
   }else{
 #ifdef DEBUGMS
@@ -549,6 +540,6 @@
 #endif
     setMapInMaps(conf,"lenv","message","Unable to open datasource in read only mode");
-    OGR_DS_Destroy(poDS);
-    OGRCleanupAll();
+    //OGR_DS_Destroy(poDS);
+    //OGRCleanupAll();
 #ifdef DEBUGMS
     fprintf(stderr,"Unable to access the DataSource, exit! \n"); 
@@ -652,4 +643,8 @@
     if (OGR_L_GetExtent(poLayer,&oExt, TRUE) == OGRERR_NONE){
       setMsExtent(output,m,myLayer,oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY);
+      char extent[1024];
+      memset(&extent,0,1024);
+      sprintf(extent,"%d,%d,%d,%d",oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY);
+      addToMap(output->content,"boundingbox",extent);
     }
   
@@ -724,6 +719,6 @@
   }
 
-  OGR_DS_Destroy(poDS);
-  OGRCleanupAll();
+  //OGR_DS_Destroy(poDS);
+  //OGRCleanupAll();
 
   return 1;
@@ -819,5 +814,4 @@
   }
 
-
   /**
    * Set extent
@@ -836,6 +830,9 @@
 	+ adfGeoTransform[4] * GDALGetRasterXSize(hDataset);
 
-       setMsExtent(output,m,myLayer,minX,minY,maxX,maxY);
-
+      setMsExtent(output,m,myLayer,minX,minY,maxX,maxY);
+      char extent[1024];
+      memset(&extent,0,1024);
+      sprintf(extent,"%d,%d,%d,%d",minX,minY,maxX,maxY);
+      addToMap(output->content,"boundingbox",extent);
     }
   }
@@ -846,7 +843,10 @@
    */
   char nBands[3];
+  memset(&nBands,0,3);
   int nBandsI=GDALGetRasterCount( hDataset );
-  sprintf(nBands,"%d",GDALGetRasterCount( hDataset ));
-  msInsertHashTable(&(myLayer->metadata), "ows_bandcount", nBands);
+  if(nBandsI<100){
+    sprintf(nBands,"%d",GDALGetRasterCount( hDataset ));
+    msInsertHashTable(&(myLayer->metadata), "ows_bandcount", nBands);
+  }
   if(nBandsI>=3)
     msLayerAddProcessing(myLayer,"BANDS=1,2,3");
@@ -860,7 +860,8 @@
    */
   char lBands[7];
+  memset(&nBands,0,7);
   char *nameBands=NULL;
   for( iBand = 0; iBand < nBandsI; iBand++ ){
-    sprintf(lBands,"Band%d",iBand+1);
+    sprintf(lBands,"Band%d",iBand+1);    
     if(nameBands==NULL){
       nameBands=(char*)malloc((strlen(lBands)+1)*sizeof(char));
@@ -869,5 +870,5 @@
       if(iBand<4){
 	char *tmpS=zStrdup(nameBands);
-	nameBands=(char*)realloc(nameBands,(strlen(nameBands)+strlen(lBands)+1)*sizeof(char));
+	nameBands=(char*)realloc(nameBands,(strlen(tmpS)+strlen(lBands)+2)*sizeof(char));
 	sprintf(nameBands,"%s %s",tmpS,lBands);
 	free(tmpS);
@@ -875,6 +876,9 @@
     }
   }
-  msInsertHashTable(&(myLayer->metadata), "ows_bandnames", nameBands);
-  
+  if(nameBands!=NULL){
+    msInsertHashTable(&(myLayer->metadata), "ows_bandnames", nameBands);
+    free(nameBands);
+  }
+
   /**
    * Loops over metadata information to setup specific information
@@ -894,5 +898,5 @@
       char tmpMm[100];
       sprintf(tmpMm,"%.3f %.3f",adfCMinMax[0],adfCMinMax[1]);
-      char tmpI[21];
+      char tmpI[31];      
       sprintf(tmpI,"%s_interval",tmpN);
       msInsertHashTable(&(myLayer->metadata), tmpI, tmpMm);
@@ -977,5 +981,5 @@
     }
     if( strlen(GDALGetRasterUnitType(hBand)) > 0 ){
-      char tmpU[21];
+      char tmpU[31];
       sprintf(tmpU,"%s_band_uom",tmpN);
       msInsertHashTable(&(myLayer->metadata), tmpU, GDALGetRasterUnitType(hBand));
@@ -988,4 +992,5 @@
   GDALClose( hDataset );
   GDALDestroyDriverManager();
+
   CPLCleanupTLS();
   return 1;
@@ -999,9 +1004,4 @@
  */
 void outputMapfile(maps* conf,maps* outputs){
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
-  dumpMaps(outputs);
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   /**
    * First store the value on disk
@@ -1013,10 +1013,8 @@
       ext="json";
 
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   map* tmpMap=getMapFromMaps(conf,"main","dataPath");
   map* sidMap=getMapFromMaps(conf,"lenv","usid");
   char *pszDataSource=(char*)malloc((strlen(tmpMap->value)+strlen(sidMap->value)+strlen(outputs->name)+17)*sizeof(char));
-  sprintf(pszDataSource,"%s/ZOO_DATA_%s_%s.%s",tmpMap->value,outputs->name,sidMap->value,ext); 
+  sprintf(pszDataSource,"%s/ZOO_DATA_%s_%s.%s",tmpMap->value,outputs->name,sidMap->value,ext);
   int f=zOpen(pszDataSource,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
   map *gfile=getMap(outputs->content,"generated_file");
@@ -1026,6 +1024,4 @@
   map* sizeMap=getMap(outputs->content,"size");
   map* vData=getMap(outputs->content,"value");
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   if(sizeMap!=NULL){
     zWrite(f,vData->value,atoi(sizeMap->value)*sizeof(char));
@@ -1034,13 +1030,7 @@
     zWrite(f,vData->value,(strlen(vData->value)+1)*sizeof(char));
   }
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   close(f);
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   addToMap(outputs->content,"storage",pszDataSource);
   free(pszDataSource);
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
 
   /*
@@ -1049,15 +1039,7 @@
   mapObj *myMap=msNewMapObj();
   free(myMap->name);
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   myMap->name=zStrdup("ZOO-Project_WXS_Server");
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   msMapSetSize(myMap,2048,2048);
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   msMapSetExtent(myMap,-1,-1,1,1);
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   
   /*
@@ -1068,6 +1050,4 @@
   tmp1=getMapFromMaps(conf,"main","tmpUrl");
   myMap->web.imageurl=zStrdup(tmp1->value);
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
   
   /*
@@ -1080,6 +1060,4 @@
   msAppendOutputFormat(myMap,msCloneOutputFormat(o1));
   msFreeOutputFormat(o1);
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
 
 #ifdef USE_KML
@@ -1094,6 +1072,4 @@
   }
 #endif
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
 
   outputFormatObj *o3=msCreateDefaultOutputFormat(NULL,"GDAL/GTiff","tiff");
@@ -1106,6 +1082,4 @@
     msFreeOutputFormat(o3);
   }
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
 
   outputFormatObj *o4=msCreateDefaultOutputFormat(NULL,"GDAL/AAIGRID","grd");
@@ -1118,6 +1092,4 @@
     msFreeOutputFormat(o4);
   }
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
 
 #ifdef USE_CAIRO
@@ -1133,6 +1105,4 @@
   }
 #endif
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
 
   /*
@@ -1141,6 +1111,4 @@
   msLoadProjectionStringEPSG(&myMap->projection,"EPSG:4326");
   myMap->transparent=1;
-  fprintf(stderr,"%s %D \n",__FILE__,__LINE__);
-  fflush(stderr);
 
   /**
Index: branches/prototype-v0/zoo-project/zoo-kernel/sql/schema.sql
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/sql/schema.sql	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/sql/schema.sql	(revision 839)
@@ -45,6 +45,10 @@
        creation_time timestamp with time zone default now(),
        end_time timestamp with time zone default NULL,
+       -- queue_duration ,
+       -- run_duration ,
        progress int,
-       message TEXT
+       message TEXT,
+       -- data_lifetime timestamp with time zone default now() + interval '48 hours',
+       rps_step int4 default 0
 );
 --------------------------------------------------------------------------------
@@ -52,5 +56,5 @@
 -- Used to store the response provided by a services running asynchronously
 create table responses (
-       uuid text references services(uuid) ON DELETE CASCADE,
+       uuid text references process(uuid) ON DELETE CASCADE,
        content text,
        creation_time timestamp with time zone default now()
Index: branches/prototype-v0/zoo-project/zoo-kernel/sql/zoo_collectiondb.sql
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/sql/zoo_collectiondb.sql	(revision 839)
+++ branches/prototype-v0/zoo-project/zoo-kernel/sql/zoo_collectiondb.sql	(revision 839)
@@ -0,0 +1,321 @@
+create schema CollectionDB;
+
+set search_path = CollectionDB, pg_catalog;
+
+CREATE OR REPLACE FUNCTION update_Description() RETURNS trigger AS 
+$$
+DECLARE
+	i integer;
+BEGIN
+	INSERT INTO CollectionDB.Descriptions (id) VALUES (NEW.id);
+	RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE FUNCTION is_in_ows_DataDescription(i integer) RETURNS boolean
+    LANGUAGE plpgsql
+    AS $$
+DECLARE
+        res int;
+        mymax int;
+BEGIN
+	SELECT id from CollectionDB.ows_DataDescription where id=i INTO res ;
+	if res is NULL then
+	   return false;
+	else
+	   return true;
+	end if;
+END;
+$$;
+
+create table CollectionDB.Descriptions (
+       id serial primary key
+);
+
+create table CollectionDB.ows_Metadata (
+       id serial primary key,
+       title text,
+       role text,
+       href text
+);
+
+create table CollectionDB.DescriptionsMetadataAssignment(
+       descriptions_id int references CollectionDB.Descriptions(id),
+       metadata_id int references CollectionDB.ows_Metadata(id)
+);
+
+create table CollectionDB.ows_Keywords (
+    id serial primary key,
+    keyword varchar
+);
+
+create table CollectionDB.DescriptionsKeywordsAssignment(
+       descriptions_id int references CollectionDB.Descriptions(id),
+       keywords_id int references CollectionDB.ows_Keywords(id)
+);
+
+create table CollectionDB.ows_AdditionalParameters (
+    id serial primary key,
+    title varchar,
+    role varchar,
+    href varchar
+);
+
+create table CollectionDB.DescriptionsAdditionalParametersAssignment (
+       descriptions_id int references CollectionDB.Descriptions(id),
+       additional_parameters_id int references CollectionDB.ows_AdditionalParameters(id)
+);
+
+--
+-- See reference for primitive datatypes
+-- https://www.w3.org/TR/xmlschema-2/#built-in-primitive-datatypes
+-- 
+create table CollectionDB.PrimitiveDataTypes (
+       id serial primary key,
+       name varchar(255)
+);
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('string');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('boolean');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('integer');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('float');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('double');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('duration');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('dateTime');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('time');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('date');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('gYearMonth');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('gYear');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('gMonthDay');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('gDay');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('gMonth');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('hexBinary');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('base64Binary');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('anyURI');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('QName');
+INSERT INTO CollectionDB.PrimitiveDataTypes (name) VALUES ('NOTATION');
+
+--
+-- List all primitive formats
+--
+create table CollectionDB.PrimitiveFormats (
+       id serial primary key,
+       mime_type varchar(255),
+       encoding varchar(15),
+       schema varchar(255)
+);
+
+-- https://tools.ietf.org/html/rfc4180
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type,encoding) VALUES ('text/csv','utf-8');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type,encoding) VALUES ('text/css','utf-8');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type,encoding) VALUES ('text/html','utf-8');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type,encoding) VALUES ('text/javascript','utf-8');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type,encoding) VALUES ('text/plain','utf-8');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type,encoding,schema)
+       VALUES ('text/xml','utf-8','http://schema.opengis.net/gml/3.2.1/gml.xsd');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type,encoding,schema)
+       VALUES ('text/xml','utf-8','http://schema.opengis.net/gml/3.1.0/gml.xsd');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type,encoding) VALUES ('application/gml+xml','utf-8');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type,encoding) VALUES ('application/json','utf-8');
+-- https://tools.ietf.org/html/rfc3302
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type) VALUES ('image/tiff');
+-- https://www.ietf.org/rfc/rfc4047.txt
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type) VALUES ('image/fits');
+-- https://tools.ietf.org/html/rfc3745
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type) VALUES ('image/jp2');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type) VALUES ('image/png');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type) VALUES ('image/jpeg');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type) VALUES ('image/gif');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type) VALUES ('application/octet-stream');
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type) VALUES ('application/vnd.google-earth.kml+xml');
+-- https://www.iana.org/assignments/media-types/application/zip
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type) VALUES ('application/zip');
+-- https://www.iana.org/assignments/media-types/application/xml
+INSERT INTO CollectionDB.PrimitiveFormats (mime_type,encoding) VALUES ('application/xml','utf-8');
+
+create table CollectionDB.ows_Format (
+    id serial primary key,
+    primitive_format_id int references CollectionDB.PrimitiveFormats(id),
+    maximum_megabytes int,
+    def boolean,
+	use_mapserver bool,
+	ms_styles text
+);
+
+create table CollectionDB.ows_DataDescription (
+    id serial primary key,
+    format_id int references CollectionDB.ows_Format(id)
+);
+
+create table CollectionDB.PrimitiveUom (
+	id serial primary key,
+	uom varchar
+);
+-- source : Open Geospatial Consortium - URNs of definitions in ogc namespace
+insert into CollectionDB.PrimitiveUom (uom) values ('degree');
+insert into CollectionDB.PrimitiveUom (uom) values ('radian');
+insert into CollectionDB.PrimitiveUom (uom) values ('metre');
+insert into CollectionDB.PrimitiveUom (uom) values ('unity');
+
+create table CollectionDB.LiteralDataDomain (
+    possible_literal_values varchar,
+    default_value varchar,
+    data_type_id int references CollectionDB.PrimitiveDataTypes(id),
+    uom int references CollectionDB.PrimitiveUom(id),
+    def boolean
+) inherits (CollectionDB.ows_DataDescription);
+alter table CollectionDB.LiteralDataDomain add constraint literal_data_domain_id unique (id);
+
+create table CollectionDB.BoundingBoxData (
+    epsg int
+) inherits (CollectionDB.ows_DataDescription);
+alter table CollectionDB.BoundingBoxData add constraint bounding_box_data_id unique (id);
+
+create table CollectionDB.ComplexData (
+) inherits (CollectionDB.ows_DataDescription);
+alter table CollectionDB.ComplexData add constraint complex_data_id unique (id);
+
+create table CollectionDB.AllowedValues (
+    id serial primary key,
+    allowed_value varchar(255)
+);
+
+create table CollectionDB.AllowedValuesAssignment (
+    id serial primary key,
+    literal_data_domain_id int references CollectionDB.LiteralDataDomain (id),
+    allowed_value_id int references CollectionDB.AllowedValues (id)
+);
+
+create table CollectionDB.ows_AdditionalParameter (
+    id serial primary key,
+    key varchar,
+    value varchar,
+    additional_parameters_id int references CollectionDB.ows_AdditionalParameters(id)
+);
+
+create table CollectionDB.ows_Input (
+    id int primary key default nextval('collectiondb.descriptions_id_seq'::regclass),
+    title text,
+    abstract text,
+    identifier varchar(255),
+    min_occurs int,
+    max_occurs int
+); -- inherits (CollectionDB.Descriptions);
+alter table CollectionDB.ows_Input add constraint codb_input_id unique (id);
+CREATE TRIGGER ows_Input_proc AFTER INSERT ON CollectionDB.ows_Input FOR EACH ROW EXECUTE PROCEDURE update_Description();
+
+create table CollectionDB.ows_Output (
+    id int primary key default nextval('collectiondb.descriptions_id_seq'::regclass),
+    title text,
+    abstract text,
+    identifier varchar(255)
+); --inherits (CollectionDB.Descriptions);
+alter table CollectionDB.ows_Output add constraint codb_output_id unique (id);
+CREATE TRIGGER ows_Output_proc AFTER INSERT ON CollectionDB.ows_Output FOR EACH ROW EXECUTE PROCEDURE update_Description();
+
+create table CollectionDB.zoo_PrivateMetadata (
+    id serial primary key,
+    identifier varchar,
+    metadata_date timestamp
+);
+
+create table CollectionDB.ows_Process (
+    id int primary key default nextval('collectiondb.descriptions_id_seq'::regclass),
+    title text,
+    abstract text,
+    identifier varchar(255),
+    availability boolean,
+    process_description_xml text,
+    private_metadata_id int references CollectionDB.zoo_PrivateMetadata(id)
+); -- inherits (CollectionDB.Descriptions);
+alter table CollectionDB.ows_Process add constraint codb_process_id unique (id);
+alter table CollectionDB.ows_Process add constraint codb_process_identifier unique (identifier);
+CREATE TRIGGER ows_Process_proc AFTER INSERT ON CollectionDB.ows_Process FOR EACH ROW EXECUTE PROCEDURE update_Description();
+
+create table CollectionDB.InputInputAssignment (
+    id serial primary key,
+    parent_input int references CollectionDB.ows_Input(id),
+    child_input int references CollectionDB.ows_Input(id)
+);
+
+create table CollectionDB.InputDataDescriptionAssignment (
+    id serial primary key,
+    input_id int references CollectionDB.ows_Input(id),
+    data_description_id int check (CollectionDB.is_in_ows_DataDescription(data_description_id))
+);
+
+create table CollectionDB.OutputOutputAssignment (
+    id serial primary key,
+    parent_output int references CollectionDB.ows_Output(id),
+    child_output int references CollectionDB.ows_Output(id)
+);
+
+create table CollectionDB.OutputDataDescriptionAssignment (
+    id serial primary key,
+    output_id int references CollectionDB.ows_Output(id),
+    data_description_id int check (CollectionDB.is_in_ows_DataDescription(data_description_id))
+);
+
+create table CollectionDB.zoo_ServiceTypes (
+	id serial primary key,
+	service_type varchar
+);
+insert into CollectionDB.zoo_ServiceTypes (service_type) VALUES ('HPC');
+insert into CollectionDB.zoo_ServiceTypes (service_type) VALUES ('C');
+insert into CollectionDB.zoo_ServiceTypes (service_type) VALUES ('Java');
+insert into CollectionDB.zoo_ServiceTypes (service_type) VALUES ('Mono');
+insert into CollectionDB.zoo_ServiceTypes (service_type) VALUES ('JS');
+insert into CollectionDB.zoo_ServiceTypes (service_type) VALUES ('PHP');
+insert into CollectionDB.zoo_ServiceTypes (service_type) VALUES ('Python');
+
+insert into CollectionDB.zoo_ServiceTypes (service_type) VALUES ('OTB');
+
+create table CollectionDB.zoo_DeploymentMetadata (
+    id serial primary key,
+    executable_name varchar,
+    configuration_identifier varchar,
+	service_type_id int references CollectionDB.zoo_ServiceTypes(id)
+);
+
+create table CollectionDB.zoo_PrivateProcessInfo (
+    id serial primary key
+);
+
+create table CollectionDB.PrivateMetadataDeploymentMetadataAssignment (
+    id serial primary key,
+    private_metadata_id int references CollectionDB.zoo_PrivateMetadata(id),
+    deployment_metadata_id int references CollectionDB.zoo_DeploymentMetadata(id)
+);
+
+create table CollectionDB.PrivateMetadataPrivateProcessInfoAssignment (
+    id serial primary key,
+    private_metadata_id int references CollectionDB.zoo_PrivateMetadata(id),
+    private_process_info_id int references CollectionDB.zoo_PrivateProcessInfo(id)
+);
+
+create table CollectionDB.ProcessInputAssignment (
+    id serial primary key,
+    process_id int references CollectionDB.ows_Process(id),
+    input_id int references CollectionDB.ows_Input(id),
+    index int
+);
+
+create table CollectionDB.ProcessOutputAssignment (
+    id serial primary key,
+    process_id int references CollectionDB.ows_Process(id),
+    output_id int references CollectionDB.ows_Output(id),
+    index int
+);
+
+CREATE OR REPLACE VIEW public.ows_process AS
+       (SELECT
+	id,
+	identifier,
+	title,
+	abstract,
+	(SELECT service_type FROM CollectionDB.zoo_ServiceTypes WHERE id = (SELECT service_type_id FROM CollectionDB.zoo_DeploymentMetadata WHERE id = (SELECT deployment_metadata_id FROM CollectionDB.PrivateMetadataDeploymentmetadataAssignment WHERE private_metadata_id=(SELECT id FROM CollectionDB.zoo_PrivateMetadata WHERE id = CollectionDB.ows_Process.private_metadata_id)))) as service_type,
+	(SELECT executable_name  as service_provider FROM CollectionDB.zoo_DeploymentMetadata WHERE id = (SELECT deployment_metadata_id FROM CollectionDB.PrivateMetadataDeploymentmetadataAssignment WHERE private_metadata_id=(SELECT id FROM CollectionDB.zoo_PrivateMetadata WHERE id = CollectionDB.ows_Process.private_metadata_id)))  as service_provider,
+	availability
+	FROM CollectionDB.ows_Process
+	WHERE
+	 availability
+	);
Index: branches/prototype-v0/zoo-project/zoo-kernel/sqlapi.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/sqlapi.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/sqlapi.c	(revision 839)
@@ -43,5 +43,5 @@
 OGRDataSource 
 #endif
- *zoo_DS = NULL;
+ **zoo_DS = NULL;
 
 /**
@@ -70,12 +70,10 @@
   int i=0;
   maps* cconf=getMaps(conf,key);
+  if(cconf==NULL)
+    return "-1";
   int len=0;
   for(i=0;i<6;i++){
     map* tmp=getMap(cconf->content,keywords[i]);
-    fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-    fflush(stderr);
     if(tmp!=NULL){
-      fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-      fflush(stderr);
       if(res==NULL){
 	res=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+4)*sizeof(char));
@@ -89,14 +87,8 @@
 	len+=strlen(res1);
 	res[len]=0;
-	fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-	fflush(stderr);
 	free(res1);
       }
     }
-    fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-    fflush(stderr);
-  }
-  fprintf(stderr,"%s %d \n",__FILE__,__LINE__);
-  fflush(stderr);
+  }
   map* tmp=getMap(cconf->content,"type");
   if(tmp!=NULL){
@@ -125,17 +117,46 @@
  * @see createInitString
  */
-void _init_sql(maps* conf,const char* key){
+int _init_sql(maps* conf,const char* key){
   char* sqlInitString=_createInitString(conf,key);
+  if(strncmp(sqlInitString,"-1",2)==0)
+    return -1;
   OGRSFDriver *poDriver = NULL;
   OGRRegisterAll();
-
+  int zoo_ds_nb=0;
+  map* dsNb=getMapFromMaps(conf,"lenv","ds_nb");
+  if(dsNb==NULL){
+    setMapInMaps(conf,"lenv","ds_nb","1");
+  }else{
+    zoo_ds_nb=atoi(dsNb->value);
+    char* tmp=(char*)malloc(11*sizeof(char));
+    sprintf(tmp,"%d",zoo_ds_nb+1);
+    setMapInMaps(conf,"lenv","ds_nb",(const char*)tmp);
+    free(tmp);
+  }
+  if(zoo_DS==NULL)
+    zoo_DS=
 #if GDAL_VERSION_MAJOR >= 2
-  zoo_DS = (GDALDataset*) GDALOpenEx( sqlInitString,
+      (GDALDataset**) malloc(sizeof(GDALDataset*))
+#else
+      (OGRDataSource**) malloc(sizeof(OGRDataSource*))
+#endif
+      ;
+  else
+    zoo_DS=      
+#if GDAL_VERSION_MAJOR >= 2
+      (GDALDataset**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(GDALDataset*))
+#else
+      (OGRDataSource**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(OGRDataSource*))
+#endif
+      ;
+  
+#if GDAL_VERSION_MAJOR >= 2
+  zoo_DS[zoo_ds_nb] = (GDALDataset*) GDALOpenEx( sqlInitString,
 				      GDAL_OF_UPDATE | GDAL_OF_VECTOR,
 				      NULL, NULL, NULL );
 #else
-  zoo_DS = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);
-#endif
-  if( zoo_DS == NULL ){
+  zoo_DS[zoo_ds_nb] = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);
+#endif
+  if( zoo_DS[zoo_ds_nb] == NULL ){
 #ifdef DEBUG
     fprintf(stderr,"sqlInitString: %s FAILED !\n",sqlInitString);
@@ -143,6 +164,7 @@
 #endif
     free(sqlInitString);
-    setMapInMaps(conf,"lenv","message","Failed to connect to the database backend");
-    return;
+    setMapInMaps(conf,"lenv","dbIssue","1");
+    setMapInMaps(conf,"lenv","message",_("Failed to connect to the database backend"));
+    return -2;
   }
 #ifdef DEBUG
@@ -150,5 +172,9 @@
   fflush(stderr);
 #endif
+  fprintf(stderr,"sqlInitString: %s SUCEED %d !\n",sqlInitString,zoo_ds_nb);
+  fflush(stderr);  
   free(sqlInitString);
+  zoo_ds_nb++;
+  return zoo_ds_nb;
 }
 
@@ -159,5 +185,5 @@
  * @see createInitString
  */
-void init_sql(maps* conf){
+int init_sql(maps* conf){
   return _init_sql(conf,"database");
 }
@@ -168,14 +194,14 @@
  * @param conf the maps containing the setting of the main.cfg file
  */
-void close_sql(maps* conf){
+void close_sql(maps* conf,int cid){
   if( zoo_ResultSet != NULL )
-    zoo_DS->ReleaseResultSet( zoo_ResultSet );
+    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
   if(zoo_DS!=NULL){
 #if GDAL_VERSION_MAJOR >= 2
-    GDALClose(zoo_DS);
-#else
-    OGRDataSource::DestroyDataSource( zoo_DS );
-#endif
-    zoo_DS=NULL;
+    GDALClose(zoo_DS[cid]);
+#else
+    OGRDataSource::DestroyDataSource( zoo_DS[cid] );
+#endif
+    zoo_DS[cid]=NULL;
   }
 }
@@ -196,13 +222,19 @@
  * @return NULL in case of failure or an OGRLayer pointer if the query succeed.
  */
-OGRLayer *fetchSql(maps* conf,const char* sqlQuery){
+OGRLayer *fetchSql(maps* conf,int cid,const char* sqlQuery){
+  if(zoo_DS[cid]==NULL)
+    return NULL;
   OGRLayer *res=NULL;
-  res = zoo_DS->ExecuteSQL( sqlQuery, NULL, NULL);
+#ifdef DEBUG
+  fprintf(stderr,"************************* %s %s %d\n\n",sqlQuery,__FILE__,__LINE__);
+  fflush(stderr);
+#endif
+  res = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL);
   return res;
 }
 
-void cleanFetchSql(maps* conf,OGRLayer *objects){
+void cleanFetchSql(maps* conf,int cid,OGRLayer *objects){
   if( objects != NULL ){
-    zoo_DS->ReleaseResultSet( objects );
+    zoo_DS[cid]->ReleaseResultSet( objects );
     objects=NULL;
   }
@@ -216,11 +248,10 @@
  * @return -1 in case of failure and 1 if the query succeed.
  */
-int execSql(maps* conf,const char* sqlQuery){
+int execSql(maps* conf,int cid,const char* sqlQuery){
   int res=-1;
-  zoo_ResultSet = zoo_DS->ExecuteSQL( sqlQuery, NULL, NULL);
+  zoo_ResultSet = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL);
   if( zoo_ResultSet != NULL ){
     res=1;
   }
-  zoo_DS->ReleaseResultSet( zoo_ResultSet );
   return res;
 }
@@ -233,9 +264,18 @@
  * @return -1 in case of failure and 1 if the query succeed.
  */
-void cleanUpResultSet(const maps* conf){
+void cleanUpResultSet(const maps* conf,int cid){
   if( zoo_ResultSet != NULL ){
-    zoo_DS->ReleaseResultSet( zoo_ResultSet );
+    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
     zoo_ResultSet=NULL;
   }
+}
+
+#ifdef RELY_ON_DB
+int getCurrentId(maps* conf){
+  int res=0;
+  map* dsNb=getMapFromMaps(conf,"lenv","ds_nb");
+  if(dsNb!=NULL)
+    res=atoi(dsNb->value);
+  return res;
 }
 
@@ -249,4 +289,5 @@
  */
 void recordStoredFile(maps* conf,const char* filename,const char* type,const char* name){
+  int zoo_ds_nb=getCurrentId(conf);
   map *uusid=getMapFromMaps(conf,"lenv","usid");
   map *schema=getMapFromMaps(conf,"database","schema");
@@ -256,7 +297,7 @@
   else
     sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s',NULL);",schema->value,uusid->value,filename,type);
-  execSql(conf,sqlQuery);
-  free(sqlQuery);
-  cleanUpResultSet(conf);
+  execSql(conf,zoo_ds_nb-1,sqlQuery);
+  free(sqlQuery);
+  cleanUpResultSet(conf,zoo_ds_nb-1);
 }
 
@@ -267,4 +308,5 @@
  */
 void recordServiceStatus(maps* conf){
+  int zoo_ds_nb=getCurrentId(conf);
   map *sid=getMapFromMaps(conf,"lenv","sid");
   map *osid=getMapFromMaps(conf,"lenv","osid");
@@ -284,7 +326,7 @@
 	  osid->value,
 	  wpsStatus[2]);
-  execSql(conf,sqlQuery);
-  free(sqlQuery);
-  cleanUpResultSet(conf);
+  execSql(conf,zoo_ds_nb-1,sqlQuery);
+  free(sqlQuery);
+  cleanUpResultSet(conf,zoo_ds_nb-1);
 }
 
@@ -296,4 +338,5 @@
  */
 void recordResponse(maps* conf,char* filename){
+  int zoo_ds_nb=getCurrentId(conf);
   FILE *file = fopen (filename, "rb");
   fseek (file, 0, SEEK_END);
@@ -308,11 +351,10 @@
   char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+57+1)*sizeof(char));
   sprintf(sqlQuery,"INSERT INTO %s.responses (content,uuid) VALUES ($$%s$$,$$%s$$);",schema->value,tmps,sid->value);
-  execSql(conf,sqlQuery);
+  execSql(conf,zoo_ds_nb-1,sqlQuery);
   free(sqlQuery);
   free(tmps);
-  cleanUpResultSet(conf);
-}
-
-#ifdef RELY_ON_DB
+  cleanUpResultSet(conf,zoo_ds_nb-1);
+}
+
 /**
  * Update the current status of the running service.
@@ -322,4 +364,5 @@
  */
 int _updateStatus(maps* conf){
+  int zoo_ds_nb=getCurrentId(conf);
   map *sid=getMapFromMaps(conf,"lenv","usid");
   map *p=getMapFromMaps(conf,"lenv","status");
@@ -328,9 +371,16 @@
   char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(msg->value)+strlen(p->value)+strlen(sid->value)+64+1)*sizeof(char));
   sprintf(sqlQuery,"UPDATE %s.services set status=$$%s$$,message=$$%s$$ where uuid=$$%s$$;",schema->value,p->value,msg->value,sid->value);
-  fflush(stderr);
-  if( zoo_DS == NULL )
+  if( zoo_ds_nb==
+#ifdef META_DB
+      1
+#else
+      0
+#endif
+      ){
     init_sql(conf);
-  execSql(conf,sqlQuery);
-  cleanUpResultSet(conf);
+    zoo_ds_nb++;
+  }
+  execSql(conf,zoo_ds_nb-1,sqlQuery);
+  cleanUpResultSet(conf,zoo_ds_nb-1);
   free(sqlQuery);
   return 0;
@@ -345,10 +395,19 @@
  */
 char* _getStatus(maps* conf,char* pid){
+  int zoo_ds_nb=getCurrentId(conf);
   map *schema=getMapFromMaps(conf,"database","schema");
   char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
   sprintf(sqlQuery,"select status||'|'||message from %s.services where uuid=$$%s$$;",schema->value,pid);
-  if( zoo_DS == NULL )
+  if( zoo_ds_nb==
+#ifdef META_DB
+      1
+#else
+      0
+#endif
+      ){
     init_sql(conf);
-  execSql(conf,sqlQuery);
+    zoo_ds_nb++;
+  }
+  execSql(conf,zoo_ds_nb-1,sqlQuery);
   OGRFeature  *poFeature = NULL;
   const char *tmp1;
@@ -363,5 +422,5 @@
     OGRFeature::DestroyFeature( poFeature );
   }
-  cleanUpResultSet(conf);
+  cleanUpResultSet(conf,zoo_ds_nb-1);
   free(sqlQuery);
   return (char*)tmp1;
@@ -376,4 +435,5 @@
  */
 char* _getStatusFile(maps* conf,char* pid){
+  int zoo_ds_nb=getCurrentId(conf);
   map *schema=getMapFromMaps(conf,"database","schema");
   char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+82+1)*sizeof(char));
@@ -381,24 +441,35 @@
 	  "select content from %s.responses where uuid=$$%s$$"
 	  " order by creation_time desc limit 1",schema->value,pid);
-  if( zoo_DS == NULL )
+  if( zoo_ds_nb==
+#ifdef META_DB
+      1
+#else
+      0
+#endif
+      ){
     init_sql(conf);
-  execSql(conf,sqlQuery);
+    zoo_ds_nb++;
+  }
+  execSql(conf,zoo_ds_nb-1,sqlQuery);
   OGRFeature  *poFeature = NULL;
   const char *tmp1;
   int hasRes=-1;
-  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
-    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
-      if( poFeature->IsFieldSet( iField ) ){
-	tmp1=strdup(poFeature->GetFieldAsString( iField ));
-	hasRes=1;
+  if(zoo_ResultSet!=NULL){
+      while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
+	for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
+	  if( poFeature->IsFieldSet( iField ) ){
+	    tmp1=strdup(poFeature->GetFieldAsString( iField ));
+	    hasRes=1;
+	  }
+	  else
+	    tmp1=NULL;
+	}
+	OGRFeature::DestroyFeature( poFeature );
       }
-      else
-	tmp1=NULL;
-    }
-    OGRFeature::DestroyFeature( poFeature );
   }
   if(hasRes<0)
     tmp1=NULL;
-  cleanUpResultSet(conf);
+  else
+    cleanUpResultSet(conf,zoo_ds_nb-1);
   free(sqlQuery);
   return (char*)tmp1;
@@ -412,16 +483,25 @@
  */
 void removeService(maps* conf,char* pid){
+  int zoo_ds_nb=getCurrentId(conf);
   map *schema=getMapFromMaps(conf,"database","schema");
   char *sqlQuery=(char*)
     malloc((strlen(pid)+strlen(schema->value)+38+1)
 	   *sizeof(char));
-  if( zoo_DS == NULL )
+  if( zoo_ds_nb==
+#ifdef META_DB
+      1
+#else
+      0
+#endif
+      ){
     init_sql(conf);
+    zoo_ds_nb++;
+  }
   sprintf(sqlQuery,
 	  "DELETE FROM %s.services where uuid=$$%s$$;",
 	  schema->value,pid);
-  execSql(conf,sqlQuery);
-  cleanUpResultSet(conf);
-  close_sql(conf);
+  execSql(conf,zoo_ds_nb-1,sqlQuery);
+  cleanUpResultSet(conf,zoo_ds_nb-1);
+  close_sql(conf,zoo_ds_nb-1);
   free(sqlQuery);
   end_sql();
@@ -434,4 +514,5 @@
  */
 void unhandleStatus(maps* conf){
+  int zoo_ds_nb=getCurrentId(conf);
   map *schema=getMapFromMaps(conf,"database","schema");
   map *sid=getMapFromMaps(conf,"lenv","usid");
@@ -447,7 +528,7 @@
 	  " where uuid=$$%s$$;",
 	  schema->value,(fstate!=NULL?fstate->value:"Failed"),sid->value);
-  execSql(conf,sqlQuery);
-  cleanUpResultSet(conf);
-  close_sql(conf);
+  execSql(conf,zoo_ds_nb-1,sqlQuery);
+  cleanUpResultSet(conf,zoo_ds_nb-1);
+  close_sql(conf,zoo_ds_nb-1);
   free(sqlQuery);
   end_sql();
@@ -462,4 +543,5 @@
  */
 char* getStatusId(maps* conf,char* pid){
+  int zoo_ds_nb=getCurrentId(conf);
   map *schema=getMapFromMaps(conf,"database","schema");
   char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
@@ -467,7 +549,9 @@
 	  "select osid from %s.services where uuid=$$%s$$",
 	  schema->value,pid);
-  if( zoo_DS == NULL )
+  if( zoo_ds_nb==0 ){
     init_sql(conf);
-  execSql(conf,sqlQuery);
+    zoo_ds_nb++;
+  }
+  execSql(conf,zoo_ds_nb-1,sqlQuery);
   OGRFeature  *poFeature = NULL;
   const char *tmp1;
@@ -486,4 +570,5 @@
     tmp1=NULL;
   free(sqlQuery);
+  cleanUpResultSet(conf,zoo_ds_nb-1);
   return (char*)tmp1;
 }
@@ -496,4 +581,5 @@
  */
 void readFinalRes(maps* conf,char* pid,map* statusInfo){
+  int zoo_ds_nb=getCurrentId(conf);
   map *schema=getMapFromMaps(conf,"database","schema");
   char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
@@ -503,5 +589,5 @@
   if( zoo_DS == NULL )
     init_sql(conf);
-  execSql(conf,sqlQuery);
+  execSql(conf,zoo_ds_nb-1,sqlQuery);
   OGRFeature  *poFeature = NULL;
   int hasRes=-1;
@@ -531,10 +617,13 @@
 int isRunning(maps* conf,char* pid){
   int res=0;
+  int zoo_ds_nb=getCurrentId(conf);
   map *schema=getMapFromMaps(conf,"database","schema");
   char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+73+1)*sizeof(char));
   sprintf(sqlQuery,"select count(*) as t from %s.services where uuid=$$%s$$ and end_time is null;",schema->value,pid);
-  if( zoo_DS == NULL )
+  if( zoo_ds_nb == 0 ){
     init_sql(conf);
-  execSql(conf,sqlQuery);
+    zoo_ds_nb++;
+  }
+  execSql(conf,zoo_ds_nb-1,sqlQuery);
   OGRFeature  *poFeature = NULL;
   const char *tmp1;
@@ -549,5 +638,5 @@
     OGRFeature::DestroyFeature( poFeature );
   }
-  cleanUpResultSet(conf);
+  cleanUpResultSet(conf,zoo_ds_nb-1);
   free(sqlQuery);
   return res;
Index: branches/prototype-v0/zoo-project/zoo-kernel/sqlapi.h
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/sqlapi.h	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/sqlapi.h	(revision 839)
@@ -42,11 +42,12 @@
 #ifdef META_DB
   ZOO_DLL_EXPORT char* _createInitString(maps*,const char*);
-  ZOO_DLL_EXPORT void _init_sql(maps*,const char*);
-  ZOO_DLL_EXPORT OGRLayer *fetchSql(maps*,const char*);
+  ZOO_DLL_EXPORT int _init_sql(maps*,const char*);
+  ZOO_DLL_EXPORT OGRLayer *fetchSql(maps*,int,const char*);
+  ZOO_DLL_EXPORT void cleanFetchSql(maps*,int,OGRLayer*);
 #endif
 #ifdef RELY_ON_DB
-  ZOO_DLL_EXPORT void init_sql(maps*);
-  ZOO_DLL_EXPORT void close_sql(maps*);
-  ZOO_DLL_EXPORT int execSql(maps*,const char*);
+  ZOO_DLL_EXPORT int init_sql(maps*);
+  ZOO_DLL_EXPORT void close_sql(maps*,int);
+  ZOO_DLL_EXPORT int execSql(maps*,int,const char*);
   ZOO_DLL_EXPORT void recordStoredFile(maps*,const char*,const char*,const char*);
   ZOO_DLL_EXPORT void recordServiceStatus(maps*);
@@ -56,4 +57,5 @@
   ZOO_DLL_EXPORT char* getStatusId(maps*,char*);
   ZOO_DLL_EXPORT void removeService(maps*,char*);
+  ZOO_DLL_EXPORT void end_sql();
 #endif
 
Index: branches/prototype-v0/zoo-project/zoo-kernel/sshapi.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/sshapi.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/sshapi.c	(revision 839)
@@ -9,5 +9,5 @@
  * by the French National Research Agency
  *
- * permission is hereby granted, free of charge, to any person obtaining a copy
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
@@ -249,7 +249,6 @@
  * @return true in case of success, false if failure occured
  */
-size_t ssh_file_exists(maps* conf,const char* targetPath){
+size_t ssh_file_exists(maps* conf,const char* targetPath,int cnt){
   size_t result=-1;
-  int cnt=ssh_get_cnt(conf);
   if(cnt>0)
     cnt-=1;
@@ -303,5 +302,5 @@
  * @return true in case of success, false if failure occured
  */
-bool ssh_copy(maps* conf,const char* localPath,const char* targetPath){
+bool ssh_copy(maps* conf,const char* localPath,const char* targetPath,int cnt){
   char mem[1024 * 1000];
   size_t nread;
@@ -310,5 +309,4 @@
   long total = 0;
   int duration;
-  int cnt=ssh_get_cnt(conf);
   int rc;
   LIBSSH2_SFTP_HANDLE *sftp_handle;
@@ -323,5 +321,5 @@
   if (!local) {
     fprintf(stderr, "Can't open local file %s\n", localPath);
-    return -1;
+    return false;
   }
 
@@ -382,5 +380,5 @@
   duration = (int)(time(NULL)-start);
   fclose(local);
-  libssh2_sftp_close(sftp_handle);
+  libssh2_sftp_close_handle(sftp_handle);
 
   libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
@@ -396,5 +394,5 @@
  * @return true in case of success, false if failure occured
  */
-int ssh_fetch(maps* conf,const char* localPath,const char* targetPath){
+int ssh_fetch(maps* conf,const char* localPath,const char* targetPath,int cnt){
   char mem[1024];
   size_t nread;
@@ -403,5 +401,4 @@
   long total = 0;
   int duration;
-  int cnt=ssh_get_cnt(conf);
   int rc;
   LIBSSH2_SFTP_HANDLE *sftp_handle;
@@ -428,5 +425,4 @@
         if (!sftp_handle) {
             if (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN) {
-
                 fprintf(stderr, "Unable to open file with SFTP\n");
                 return -1;
@@ -443,9 +439,8 @@
         do {
             rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
-            fprintf(stderr, "libssh2_sftp_read returned %d\n",
-                    rc);
- 
+            /*fprintf(stderr, "libssh2_sftp_read returned %d\n",
+	      rc);*/
             if(rc > 0) {
-                write(2, mem, rc);
+	      //write(2, mem, rc);
                 fwrite(mem, rc, 1, local);
             }
@@ -479,5 +474,5 @@
   duration = (int)(time(NULL)-start);
   fclose(local);
-  libssh2_sftp_close(sftp_handle);
+  libssh2_sftp_close_handle(sftp_handle);
 
   libssh2_sftp_shutdown(sessions[cnt]->sftp_session);
@@ -491,6 +486,5 @@
  * @return bytecount resulting from the execution of the command
  */
-int ssh_exec(maps* conf,const char* command){
-  int cnt=ssh_get_cnt(conf);
+int ssh_exec(maps* conf,const char* command,int cnt){
   LIBSSH2_CHANNEL *channel;
   int rc;
@@ -571,5 +565,4 @@
   while (libssh2_session_disconnect(con->session, "Normal Shutdown, Thank you for using the ZOO-Project sshapi")
 	 == LIBSSH2_ERROR_EAGAIN);
-  libssh2_session_free(con->session);
 #ifdef WIN32
   closesocket(con->sock_id);
@@ -577,4 +570,5 @@
   close(con->sock_id);
 #endif
+  libssh2_session_free(con->session);
   return true;
 }
@@ -596,2 +590,78 @@
   return true;
 }
+
+bool addToUploadQueue(maps** conf,maps* input){
+  map* queueMap=getMapFromMaps(*conf,"uploadQueue","length");
+  if(queueMap==NULL){
+    maps* queueMaps=createMaps("uploadQueue");
+    queueMaps->content=createMap("length","0");
+    addMapsToMaps(conf,queueMaps);
+    freeMaps(&queueMaps);
+    free(queueMaps);
+    queueMap=getMapFromMaps(*conf,"uploadQueue","length");
+  }
+  maps* queueMaps=getMaps(*conf,"uploadQueue");
+  int queueIndex=atoi(queueMap->value);
+  if(input!=NULL){
+    if(getMap(input->content,"cache_file")!=NULL){
+      map* length=getMap(input->content,"length");
+      if(length==NULL){
+	addToMap(input->content,"length","1");
+	length=getMap(input->content,"length");
+      }
+      int len=atoi(length->value);
+      int i=0;
+      for(i=0;i<len;i++){
+	
+	map* tmp[2]={getMapArray(input->content,"localPath",i),
+		     getMapArray(input->content,"targetPath",i)};
+
+	setMapArray(queueMaps->content,"input",queueIndex,input->name);
+	setMapArray(queueMaps->content,"localPath",queueIndex,tmp[0]->value);
+	setMapArray(queueMaps->content,"targetPath",queueIndex,tmp[1]->value);
+	queueIndex+=1;
+      }
+    }
+  }
+#ifdef DEBUG  
+  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+  fflush(stderr);
+  dumpMaps(queueMaps);
+  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+  fflush(stderr);
+  dumpMaps(*conf);
+  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+  fflush(stderr);
+#endif
+  return true;
+}
+
+bool runUpload(maps** conf){
+  SSHCON *test=ssh_connect(*conf);
+  if(test==NULL){
+    return false;
+  }
+#ifdef DEBUG
+  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+  fflush(stderr);
+  dumpMaps(getMaps(*conf,"uploadQueue"));
+  fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
+  fflush(stderr);
+#endif
+  map* queueLengthMap=getMapFromMaps(*conf,"uploadQueue","length");
+  maps* queueMaps=getMaps(*conf,"uploadQueue");
+  if(queueLengthMap!=NULL){
+    int cnt=atoi(queueLengthMap->value);
+    int i=0;
+    for(i=0;i<cnt;i++){
+      map* argv[3]={
+	getMapArray(queueMaps->content,"input",i),
+	getMapArray(queueMaps->content,"localPath",i),
+	getMapArray(queueMaps->content,"targetPath",i)
+      };
+      fprintf(stderr,"%s %d %s %s\n",__FILE__,__LINE__,argv[1]->value,argv[2]->value);
+      ssh_copy(*conf,argv[1]->value,argv[2]->value,ssh_get_cnt(*conf));
+    }    
+  }
+  return true; 
+}
Index: branches/prototype-v0/zoo-project/zoo-kernel/sshapi.h
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/sshapi.h	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/sshapi.h	(revision 839)
@@ -85,9 +85,12 @@
   
   ZOO_DLL_EXPORT SSHCON *ssh_connect(maps*);
-  ZOO_DLL_EXPORT bool ssh_copy(maps*,const char*,const char*);
-  ZOO_DLL_EXPORT int ssh_fetch(maps*,const char*,const char*);
-  ZOO_DLL_EXPORT int ssh_exec(maps*,const char*);
+  ZOO_DLL_EXPORT bool ssh_copy(maps*,const char*,const char*,int);
+  ZOO_DLL_EXPORT int ssh_get_cnt(maps*);
+  ZOO_DLL_EXPORT int ssh_fetch(maps*,const char*,const char*,int);
+  ZOO_DLL_EXPORT int ssh_exec(maps*,const char*,int);
   ZOO_DLL_EXPORT bool ssh_close_session(maps*,SSHCON*);
   ZOO_DLL_EXPORT bool ssh_close(maps*);
+  ZOO_DLL_EXPORT bool addToUploadQueue(maps**,maps*);
+  ZOO_DLL_EXPORT bool runUpload(maps**);
   
 #ifdef __cplusplus
Index: branches/prototype-v0/zoo-project/zoo-kernel/ulinet.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/ulinet.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/ulinet.c	(revision 839)
@@ -70,5 +70,5 @@
 /**
  * In case of presence of "Set-Cookie" in the headers red, store the cookie
- * identifier in CCookie
+ * identifier in cookie
  *
  * @param buffer the buffer to read
@@ -77,12 +77,9 @@
  * @param data the _HINTERNET structure to write in
  * @return the size red, -1 if buffer is NULL
- * @see CCookie
+ * @see cookie
  */
 size_t header_write_data(void *buffer, size_t size, size_t nmemb, void *data){
   if(strncmp("Set-Cookie: ",(char*)buffer,12)==0){
     int i;
-    char env[256];
-    char path[256];
-    char domain[256];
     char* tmp;
     for(i=0;i<12;i++)
@@ -92,12 +89,11 @@
 	;
 #endif
-    sscanf((char*)buffer,"%s; path=%s; domain=%s",env,path,domain);
+    tmp=strtok(buffer,";");
+    int cnt=0;
     _HINTERNET *psInternet=(_HINTERNET *)data;
-    tmp=strcat(env,CCookie[psInternet->id][0]);
-#ifdef MSG_LAF_OUT
-    printf("\n**Cookie env : [%s] , path : [%s], domain : [%s]**\n",env,path,domain);
-    printf("buffer : %d (%s) (%s) (%s)\n",(buffer==NULL),buffer,tmp,CCookie);
-#endif
-    strcpy(CCookie[psInternet->id][0],tmp);
+    if(tmp!=NULL && psInternet!=NULL){
+      psInternet->cookie=(char*)malloc(sizeof(char)*(strlen(tmp)+1));
+      sprintf(psInternet->cookie,"%s",tmp);
+    }
   }
   return size * nmemb;//write_data_into(buffer,size,nmemb,data,HEADER);
@@ -340,6 +336,8 @@
     if(handle.url!=NULL)
       free(handle.url);
-    free(handle.mimeType);
-    handle.mimeType = NULL;
+    if(handle.mimeType!=NULL)
+      free(handle.mimeType);
+    if(handle.cookie!=NULL)
+      free(handle.cookie);
   }
   if(handle0->handle)
@@ -371,4 +369,5 @@
   hInternet->ihandle[hInternet->nb].url = NULL;
   hInternet->ihandle[hInternet->nb].mimeType = NULL;
+  hInternet->ihandle[hInternet->nb].cookie = NULL;
   hInternet->ihandle[hInternet->nb].nDataLen = 0;
   hInternet->ihandle[hInternet->nb].id = hInternet->nb;
@@ -376,5 +375,5 @@
   hInternet->ihandle[hInternet->nb].pabyData = NULL;
   hInternet->ihandle[hInternet->nb].post = NULL;
-
+  
   curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_COOKIEFILE, "ALL");
 #ifndef TIGER
@@ -475,4 +474,6 @@
     curl_multi_remove_handle(hInternet->handle, hInternet->ihandle[i].handle);
     curl_easy_cleanup(hInternet->ihandle[i].handle);
+    //fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,hInternet->ihandle[i].mimeType);
+    //fprintf(stderr,"%s %d %s \n",__FILE__,__LINE__,hInternet->ihandle[i].pabyData);
   }
   return 0;
@@ -480,12 +481,12 @@
 
 /**
- * Initialize the CCookie for a specific index (hInternet.nb)
- *
- * @param hInternet the HINTERNET structure to know the CCookie index to reset
+ * Initialize the cookie for a specific index (hInternet.nb)
+ *
+ * @param hInternet the HINTERNET structure to know the cookie index to reset
  * @return 1
  * @see HINTERNET
  */
 int freeCookieList(HINTERNET hInternet){
-  memset(&CCookie[hInternet.nb][0],0,1024);
+  hInternet.ihandle[hInternet.nb].cookie=strdup("");
 #ifndef TIGER
   curl_easy_setopt(hInternet.ihandle[hInternet.nb].handle, CURLOPT_COOKIELIST, "ALL");
@@ -536,6 +537,4 @@
   }
 
-  CCookie[hInternet.id][0]=0;
-
   if( *lpdwNumberOfBytesRead < dwDataSize )
       return 0;
Index: branches/prototype-v0/zoo-project/zoo-kernel/ulinet.h
===================================================================
--- branches/prototype-v0/zoo-project/zoo-kernel/ulinet.h	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-kernel/ulinet.h	(revision 839)
@@ -54,13 +54,4 @@
 #define MAX_REQ 50
 
-#ifdef _ULINET
-static char CCookie[MAX_REQ][1024];
-#else
-/**
- * The cookies found
- */
-extern char CCookie[MAX_REQ][1024];
-#endif
-
 #ifdef __cplusplus
 extern "C" {
@@ -88,4 +79,5 @@
     char *mimeType; //!< the mimeType returned by the server
     char *post; //!< the potential POST XML content
+    char* cookie; //!< The potential Cookie returned by the server
     int hasCacheFile; //!< 1 if we used a cache file
     int nDataLen; //!< the length of the downloaded content
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);
Index: branches/prototype-v0/zoo-project/zoo-services/utils/hpc/Makefile
===================================================================
--- branches/prototype-v0/zoo-project/zoo-services/utils/hpc/Makefile	(revision 839)
+++ branches/prototype-v0/zoo-project/zoo-services/utils/hpc/Makefile	(revision 839)
@@ -0,0 +1,14 @@
+ZRPATH=../../..
+include ${ZRPATH}/zoo-kernel/ZOOMakefile.opts
+CFLAGS=-I${INST_INCLUDE} ${ZOO_CFLAGS} ${XML2CFLAGS} ${GDAL_CFLAGS} ${PYTHONCFLAGS} -DLINUX_FREE_ISSUE #-DDEBUG
+
+cgi-env/wps_hpc.zo: service.c
+	g++ ${CFLAGS} -shared -fpic -o cgi-env/wps_hpc.zo ./service.c ${GDAL_LIBS} ${XML2LDFLAGS} ${MACOS_LD_FLAGS} ${ZOO_LDFLAGS} ${MACOS_LD_NET_FLAGS} ${XSLT_LDFLAGS} ${FCGI_LDFLAGS} -L${ZRPATH}/zoo-kernel/ -lzoo_service ${ZRPATH}/zoo-kernel/sshapi.o ${HPCLDFLAGS}
+
+install:
+	install -d ${CGI_DIR}
+	install cgi-env/*.zcfg ${CGI_DIR}/
+	install cgi-env/*.zo ${CGI_DIR}/
+
+clean:
+	rm -f cgi-env/wps_hpc.zo
Index: branches/prototype-v0/zoo-project/zoo-services/utils/hpc/cgi-env/FinalizeHPC.zcfg
===================================================================
--- branches/prototype-v0/zoo-project/zoo-services/utils/hpc/cgi-env/FinalizeHPC.zcfg	(revision 839)
+++ branches/prototype-v0/zoo-project/zoo-services/utils/hpc/cgi-env/FinalizeHPC.zcfg	(revision 839)
@@ -0,0 +1,30 @@
+[FinalizeHPC]
+ Title = Start phase 3 of HPC execution
+ Abstract = Unblock the ZOO-Kernel awaiting for a remote job execution 
+ processVersion = 1
+ storeSupported = true
+ statusSupported = true
+ serviceProvider = wps_hpc.zo
+ serviceType = C
+ <DataInputs>
+  [JobId]
+   Title = the image
+   Abstract = The image to process
+   minOccurs = 1
+   maxOccurs = 1
+   <LiteralData>
+    dataType = string
+    <Default>
+    </Default>
+   </LiteralData>
+ </DataInputs>
+ <DataOutputs>
+  [Result]
+   Title = The hello string
+   Abstract = The Hello message string.
+   <LiteralOutput>
+    DataType = string
+    <Default>
+    </Default>
+   </LiteralOutput>
+ </DataOutputs>  
Index: branches/prototype-v0/zoo-project/zoo-services/utils/hpc/service.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-services/utils/hpc/service.c	(revision 839)
+++ branches/prototype-v0/zoo-project/zoo-services/utils/hpc/service.c	(revision 839)
@@ -0,0 +1,193 @@
+/**
+ * Author : Gérald FENOY
+ *
+ * Copyright 2008-2009 GeoLabs SARL. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+
+#include "service.h"
+#include "service_internal.h"
+#include "sshapi.h"
+
+#include <sys/socket.h>
+#include <sys/un.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>
+
+#include <dirent.h>
+extern "C" {
+
+  /**
+   * FinalizeHPC ZOO Service :
+   * This service is used to inform a ZOO-Kernel waiting for the end of the
+   * execution of a HPC service
+   */
+  ZOO_DLL_EXPORT int FinalizeHPC(maps*& conf,maps*& inputs,maps*& outputs){
+    // Retrieve the jobid corresponding to the identifier generated by SLURM
+    // by reading the file generated when running the SBATCH file
+    map* jobid=getMapFromMaps(inputs,"jobid","value");
+    struct sockaddr_un addr;
+    char buf[100]="3";
+    int fd,rc=NULL;
+    int i=0;
+    SSHCON *test=ssh_connect(conf);
+    if(test==NULL){
+      return SERVICE_FAILED;
+    }
+    map* usid=getMapFromMaps(conf,"lenv","usid");
+    map* tmpPath=getMapFromMaps(conf,"main","tmpPath");
+    char *logPath=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+12)*sizeof(char));
+    sprintf(logPath,"%s/exec_out_%s",tmpPath->value,jobid->value);
+    struct stat f_status;
+    int ts=stat(logPath, &f_status);
+    char* fcontent = NULL;
+    if(ts==0) {
+      fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
+      FILE* f=fopen(logPath,"rb");
+      fread(fcontent,f_status.st_size,1,f);
+      int fsize=f_status.st_size;
+      fcontent[fsize]=0;
+      fclose(f);
+    }else{
+      setMapInMaps(conf,"lenv","message",_("No service with this jobid can be found"));
+      return SERVICE_FAILED;
+    }
+    free(logPath);
+    // Run scontrol to see if the service execution ends
+    // Store all the informations returned by scontrol command as a cfg file to
+    // be parsed back by the ZOO-Kernel waiting for the execution of the remote
+    // service
+    maps* tmpMaps=createMaps("henv");
+    char* command=(char*)malloc((126)*sizeof(char));
+    //memset(&command,0,34);
+    sprintf(command,"scontrol show jobid | grep -A24 %s",fcontent);    
+    if(ssh_exec(conf,command,ssh_get_cnt(conf))==0){
+      free(command);
+      setMapInMaps(conf,"lenv","message",_("Failed to run scontrol remotely"));
+      return SERVICE_FAILED;
+    }else{
+      free(command);
+      logPath=(char*)malloc((strlen(tmpPath->value)+strlen(usid->value)+11)*sizeof(char));
+      sprintf(logPath,"%s/exec_out_%s",tmpPath->value,usid->value);
+      int ts=stat(logPath, &f_status);
+      if(ts==0) {
+        fcontent=(char*)malloc(sizeof(char)*(f_status.st_size+1));
+        FILE* f=fopen(logPath,"rb");
+        fread(fcontent,f_status.st_size,1,f);
+        int fsize=f_status.st_size;
+        fcontent[fsize]=0;
+        fclose(f);
+        free(logPath);
+        fprintf(stderr,"%s \n",fcontent);
+        char *token, *saveptr;
+        token = strtok_r (fcontent, " ", &saveptr);
+        while (token != NULL)
+          {
+            //fprintf(stderr,"%s %d\n",token,__LINE__);
+            char *token1, *saveptr1;
+            char *tmpToken=strdup(token);
+            token1 = strtok_r (tmpToken, "=", &saveptr1);
+            int isNext=-1;
+            int hasTwoElements=0;
+            char *name=NULL;
+            while (token1 != NULL)
+              {
+                if(hasTwoElements==0)
+                  name=strdup(token1);
+                if(hasTwoElements<1)
+                  hasTwoElements+=1;
+                else{
+                  char *value=strdup(token1);
+                  if(value[strlen(value)-1]=='\n')
+                    value[strlen(value)-1]=0;
+                  if(strlen(name)>0 && strlen(value)>0){
+                    if(tmpMaps->content==NULL)
+                      tmpMaps->content=createMap(name,value);
+                    else
+                      addToMap(tmpMaps->content,name,value);
+                    free(value);
+                  }
+                  free(name);
+                  hasTwoElements=0;
+                }
+                token1 = strtok_r (NULL, "=", &saveptr1);
+              }
+            free(tmpToken);
+            token = strtok_r (NULL, " ", &saveptr);
+          }
+      }else{
+        setMapInMaps(conf,"lenv","message",_("Unable to access the downloaded execution log file"));
+        return SERVICE_FAILED;
+      }
+    }
+    logPath=(char*)malloc((strlen(tmpPath->value)+strlen(usid->value)+15)*sizeof(char));
+    sprintf(logPath,"%s/exec_status_%s",tmpPath->value,usid->value);
+    dumpMapsToFile(tmpMaps,logPath,0);
+
+    char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+21));
+    sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,jobid->value);
+    if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+      perror("socket error");
+      setMapInMaps(conf,"lenv","message",_("Socket error"));
+      return SERVICE_FAILED;
+      }
+    }
+    logPath=(char*)malloc((strlen(tmpPath->value)+strlen(usid->value)+15)*sizeof(char));
+    sprintf(logPath,"%s/exec_status_%s",tmpPath->value,usid->value);
+    dumpMapsToFile(tmpMaps,logPath,0);
+    char *sname=(char*)malloc((strlen(tmpPath->value)+strlen(jobid->value)+21));
+    sprintf(sname,"%s/.wait_socket_%s.sock",tmpPath->value,jobid->value);
+    if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+      perror("socket error");
+      setMapInMaps(conf,"lenv","message",_("Socket error"));
+      return SERVICE_FAILED;
+    }
+    memset(&addr, 0, sizeof(addr));
+    addr.sun_family = AF_UNIX;
+    strncpy(addr.sun_path, sname, sizeof(addr.sun_path)-1);
+    if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
+      perror("connect error");
+      setMapInMaps(conf,"lenv","message",_("Unable to connect"));
+      return SERVICE_FAILED;
+    }
+    if (write(fd, "3", 1) != rc) {
+      if (rc < 0) {
+        perror("write error");
+        setMapInMaps(conf,"lenv","message",_("Unable to announce the successful execution of the HPC service"));
+        close(fd);
+        return SERVICE_FAILED;
+      }
+    }
+    close(fd);
+    setOutputValue(outputs,"Result",(char*)"\"FinalizeHPC run successfully\"",32);
+
+    return SERVICE_SUCCEEDED;
+  }
+
+}
Index: branches/prototype-v0/zoo-project/zoo-services/utils/status/service.c
===================================================================
--- branches/prototype-v0/zoo-project/zoo-services/utils/status/service.c	(revision 822)
+++ branches/prototype-v0/zoo-project/zoo-services/utils/status/service.c	(revision 839)
@@ -85,7 +85,7 @@
        */
       char *tmpStr=_getStatus(conf,tmpMap->value);
-#ifdef DEBUG
+      //#ifdef DEBUG
       fprintf(stderr,"DEBUG: %s \n",tmpStr);
-#endif
+      //#endif
       if(tmpStr!=NULL && strncmp(tmpStr,"-1",2)!=0){
 	char *tmpStr1=strdup(tmpStr);
@@ -140,4 +140,6 @@
       char message[10];
       sprintf(message,"Step %d",i);
+      fprintf (stderr, "RUN IN BACKGROUND MODE %s %d \n",__FILE__,__LINE__);
+      fflush(stderr);
       updateStatus(conf,i,message);
 #ifndef WIN32
