Index: trunk/zoo-project/zoo-kernel/server_internal.c
===================================================================
--- trunk/zoo-project/zoo-kernel/server_internal.c	(revision 889)
+++ trunk/zoo-project/zoo-kernel/server_internal.c	(revision 917)
@@ -26,4 +26,5 @@
 #include "service_internal.h"
 #include "response_print.h"
+//#include "service_callback.h"
 #include "mimetypes.h"
 #ifndef WIN32
@@ -366,27 +367,33 @@
  */
 void readGeneratedFile(maps* m,map* content,char* filename){
+  char rsize[1024];
   FILE * file=fopen(filename,"rb");
   if(file==NULL){
-    fprintf(stderr,"Failed to open file %s for reading purpose.\n",filename);
     setMapInMaps(m,"lenv","message","Unable to read produced file. Please try again later");
     return ;
   }
   fseek(file, 0, SEEK_END);
-  long count = ftell(file);
+  zStatStruct f_status;
+  int s=zStat(filename, &f_status);
+  sprintf(rsize,"%lld",f_status.st_size);
   rewind(file);
-  struct stat file_status; 
-  stat(filename, &file_status);
-  map* tmpMap1=getMap(content,"value");
-  if(tmpMap1==NULL){
-    addToMap(content,"value","");
-    tmpMap1=getMap(content,"value");
-  }
-  free(tmpMap1->value);
-  tmpMap1->value=(char*) malloc((count+1)*sizeof(char));  
-  fread(tmpMap1->value,1,count,file);
-  tmpMap1->value[count]=0;
-  fclose(file);
-  char rsize[1000];
-  sprintf(rsize,"%ld",count);
+  if(getMap(content,"storage")==NULL){
+    map* memUse=getMapFromMaps(m,"main","memory");
+    if(memUse==NULL || strncmp(memUse->value,"load",4)==0){
+      map* tmpMap1=getMap(content,"value");
+      if(tmpMap1==NULL){
+	addToMap(content,"value","");
+	tmpMap1=getMap(content,"value");
+      }
+      free(tmpMap1->value);
+      tmpMap1->value=(char*) malloc((f_status.st_size+1)*sizeof(char));
+      if(tmpMap1->value==NULL){
+	setMapInMaps(m,"lenv","message","Unable to allocate the memory required to read the produced file.");
+      }
+      fread(&tmpMap1->value,1,f_status.st_size,file);
+      tmpMap1->value[f_status.st_size]=0;
+    }
+  }
+  fclose(file);  
   addToMap(content,"size",rsize);
 }
@@ -423,4 +430,5 @@
 void dumpMapsValuesToFiles(maps** main_conf,maps** in){
   map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
+  map* tmpUrl=getMapFromMaps(*main_conf,"main","tmpUrl");
   map* tmpSid=getMapFromMaps(*main_conf,"lenv","usid");
   maps* inputs=*in;
@@ -445,7 +453,13 @@
 	  if(cSize!=NULL){
 	    length=atoi(cSize->value);
-	  }
+	  }else
+	    length=strlen(cValue->value);
 	  writeFile(val,cValue->value,length);
 	  setMapArray(cMap,"cache_file",k,val);
+	  free(val);
+	  val=(char*)malloc((strlen(tmpUrl->value)+strlen(inputs->name)+strlen(tmpSid->value)+strlen(file_ext)+16)*sizeof(char));
+	  sprintf(val,"%s/Input_%s_%s_%d.%s",tmpUrl->value,inputs->name,tmpSid->value,k,file_ext);
+	  setMapArray(cMap,"cache_url",k,val);
+	  setMapArray(cMap,"byValue",k,"true");
 	  free(val);
 	}
@@ -461,7 +475,13 @@
 	if(cSize!=NULL){
 	  length=atoi(cSize->value);
-	}
+	}else
+	  length=strlen(cValue->value);
 	writeFile(val,cValue->value,length);
 	addToMap(cMap,"cache_file",val);
+	free(val);
+	val=(char*)malloc((strlen(tmpUrl->value)+strlen(inputs->name)+strlen(tmpSid->value)+strlen(file_ext)+16)*sizeof(char));
+	sprintf(val,"%s/Input_%s_%s_%d.%s",tmpUrl->value,inputs->name,tmpSid->value,0,file_ext);
+	addToMap(cMap,"cache_url",val);
+	addToMap(cMap,"byValue",val);
 	free(val);
       }
@@ -638,4 +658,11 @@
 	}
 	if(res==NULL){
+	  map* tmpMaxO0=getMap(tmpInputs->content,"useMapserver");
+	  if(tmpMaxO0!=NULL){
+	    if(tmpMaps2->content==NULL)
+	      tmpMaps2->content=createMap("useMapserver",tmpMaxO0->value);
+	    else
+	      addToMap(tmpMaps2->content,"useMapserver",tmpMaxO0->value);
+	  }
 	  map* tmpMaxO=getMap(tmpInputs->content,"maxOccurs");
 	  if(tmpMaxO!=NULL){
@@ -701,5 +728,5 @@
       }
     }
-    else { 
+    else /*toto*/{ 
       iotype* tmpIoType=NULL;
       if(tmpMaps->content!=NULL){
@@ -711,24 +738,16 @@
 	   * content map.
 	   */
-	  map* tmpMap1=getMap(tmpInputs->content,"minOccurs");
-	  if(tmpMap1!=NULL){
-	    if(tmpMaps->content==NULL)
-	      tmpMaps->content=createMap("minOccurs",tmpMap1->value);
-	    else
-	      addToMap(tmpMaps->content,"minOccurs",tmpMap1->value);
-	  }
-	  map* tmpMaxO=getMap(tmpInputs->content,"maxOccurs");
-	  if(tmpMaxO!=NULL){
-	    if(tmpMaps->content==NULL)
-	      tmpMaps->content=createMap("maxOccurs",tmpMaxO->value);
-	    else
-	      addToMap(tmpMaps->content,"maxOccurs",tmpMaxO->value);
-	  }
-	  map* tmpMaxMB=getMap(tmpInputs->content,"maximumMegabytes");
-	  if(tmpMaxMB!=NULL){
-	    if(tmpMaps->content==NULL)
-	      tmpMaps->content=createMap("maximumMegabytes",tmpMaxMB->value);
-	    else
-	      addToMap(tmpMaps->content,"maximumMegabytes",tmpMaxMB->value);
+	  char* keys[4]={
+	    "minOccurs",
+	    "maxOccurs",
+	    "maximumMegabytes",
+	    "useMapserver"
+	  };
+	  int i=0;
+	  for(i=0;i<4;i++){
+	    map* tmpMap1=getMap(tmpInputs->content,keys[i]);
+	    if(tmpMap1!=NULL){
+	      addToMap(tmpMaps->content,keys[i],tmpMap1->value);
+	    }
 	  }
 	  /**
@@ -743,5 +762,5 @@
 	      map* tmpV=getMap(tmpI->content,"value");
 	      if(tmpV!=NULL){
-		char *tmpVS=strdup(tmpV->value);
+		char *tmpVS=zStrdup(tmpV->value);
 		map* tmp=parseBoundingBox(tmpVS);
 		free(tmpVS);
@@ -780,5 +799,5 @@
 	      map* length=getMap(tmpMaps->content,"length");
 	      int i;
-	      char *tcn=strdup(tmpContent->name);
+	      char *tcn=zStrdup(tmpContent->name);
 	      for(i=1;i<atoi(length->value);i++){
 #ifdef DEBUG
@@ -806,7 +825,8 @@
 	 */
 	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
+	    addToMap(tmpMaps->content,"useMapserver","true");
 	    tmpIoType=getIoTypeFromElement(tmpInputs,tmpInputs->name,NULL);
 	    tmpCheck=getMap(tmpMaps->content,"mimeType");
@@ -834,9 +854,17 @@
 	addToMap(tmpMaps->content,"inRequest","true");
       elements* tmpElements=getElements(in,tmpMaps->name);
-      if(tmpElements!=NULL && tmpElements->child!=NULL){
+      if(/*tmpMaps->child!=NULL && */tmpElements!=NULL && tmpElements->child!=NULL){
 	char *res=addDefaultValues(&tmpMaps->child,tmpElements->child,m,type,err);
 	if(strlen(res)>0){
 	  return res;
 	}
+      }
+    }
+    if(tmpInputs->child!=NULL){
+      tmpInputss=tmpInputs->next;
+      tmpInputs=tmpInputs->child;
+      if(tmpMaps!=NULL){
+	out1=tmpMaps->child;
+	out1s=tmpMaps;
       }
     }
@@ -972,5 +1000,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. The service is currently running."),
 			"ResultNotReady", pid);
 	return;
@@ -981,6 +1009,6 @@
 	  char* tmpStr=_getStatus(conf,pid);
 	  if(tmpStr!=NULL && strncmp(tmpStr,"-1",2)!=0){
-	    char *tmpStr1=strdup(tmpStr);
-	    char *tmpStr0=strdup(strstr(tmpStr,"|")+1);
+	    char *tmpStr1=zStrdup(tmpStr);
+	    char *tmpStr0=zStrdup(strstr(tmpStr,"|")+1);
 	    free(tmpStr);
 	    tmpStr1[strlen(tmpStr1)-strlen(tmpStr0)-1]='\0';
@@ -1000,9 +1028,11 @@
 	  printf("%s",result);
 	  fflush(stdout);
+	  free(sid);
 	  freeMap(&statusInfo);
 	  free(statusInfo);
+	  free(result);
 	  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. The service ends but it still needs to produce the outputs."),
 			  "ResultNotReady", pid);
 	  freeMap(&statusInfo);
@@ -1015,6 +1045,6 @@
 	  char* tmpStr=_getStatus(conf,pid);
 	  if(tmpStr!=NULL && strncmp(tmpStr,"-1",2)!=0){
-	    char *tmpStr1=strdup(tmpStr);
-	    char *tmpStr0=strdup(strstr(tmpStr,"|")+1);
+	    char *tmpStr1=zStrdup(tmpStr);
+	    char *tmpStr0=zStrdup(strstr(tmpStr,"|")+1);
 	    free(tmpStr);
 	    tmpStr1[strlen(tmpStr1)-strlen(tmpStr0)-1]='\0';
@@ -1026,4 +1056,5 @@
 	}
     }
+    free(sid);
     printStatusInfo(conf,statusInfo,req);
     freeMap(&statusInfo);
@@ -1083,5 +1114,5 @@
 	if(strstr(dp->d_name,pid)!=0){
 	  sprintf(fileName,"%s/%s",r_inputs->value,dp->d_name);
-	  if(unlink(fileName)!=0){
+	  if(zUnlink(fileName)!=0){
 	    errorException (conf, 
 			    _("The job cannot be removed, a file cannot be removed"),
@@ -1095,4 +1126,9 @@
     removeService(conf,pid);
 #endif
+    /* No need to call 7_1 when an execution is dismissed.
+      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+      invokeCallback(conf,NULL,NULL,7,1);
+      fprintf(stderr,"************************* %s %d \n\n",__FILE__,__LINE__);
+    */
     map* statusInfo=createMap("JobID",pid);
     addToMap(statusInfo,"Status","Dismissed");
