source: trunk/zoo-kernel/service_internal_python.c @ 57

Last change on this file since 57 was 57, checked in by djay, 13 years ago

Adding contains and getIoTypeFromElement functions in service.h. Remove memory leaks from the Service Configuration Parser, add the support for multiple supported formats as before. Small modification of the ZOO-Kernel Java Support, adding the capability to access the modified main_conf HashMap? from the Kernel to let lenv message map pass when an error occurs and it is handled in the Service code. Adding same capability for the ZOO-Kernel Python Support. Use strcasecmp in service.h rather than strlen+strncasecmp. Ensure that only OWS compliant informations are available for Contact.Phone and Contact.Adress. Remove memory leak in createExceptionReportNode. Correction of the addDefaultValues function to add the default format using the informations from DataInputs? if present, this should correct the behavior of the ZOO-Kernel when choosing the extension value which should now point to the corresponding zcfg value if present. Don't set the NULL value for inputs not provided in the DataInputs?, still set NULL as default value for outputs. Avoid segfault in freeElements when some zcfg values was not set correctly. Link against the client libjvm.so file rather than the server one.

File size: 8.5 KB
Line 
1/**
2 * Author : Gérald FENOY
3 *
4 * Copyright (c) 2009-2010 GeoLabs SARL
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "service_internal_python.h"
26
27int zoo_python_support(maps** main_conf,map* request,service* s,maps **real_inputs,maps **real_outputs){
28  maps* m=*main_conf;
29  maps* inputs=*real_inputs;
30  maps* outputs=*real_outputs;
31  char ntmp[1024];
32  getcwd(ntmp,1024);
33  map* tmp=NULL;
34  tmp=getMapFromMaps(*main_conf,"env","PYTHONPATH");
35  char *python_path;
36  fprintf(stderr,"PYTHON SUPPORT \n");
37  fflush(stderr);
38  if(tmp!=NULL){
39    fprintf(stderr,"PYTHON SUPPORT (%i)\n",strlen(tmp->value));
40    python_path=(char*)malloc((strlen(tmp->value))*sizeof(char));
41    sprintf(python_path,"%s",tmp->value);
42  }
43  else{
44    python_path=strdup(".");
45  }
46  tmp=NULL;
47  tmp=getMap(request,"metapath");
48  char *pythonpath=(char*)malloc((1+strlen(python_path)+2048)*sizeof(char));
49  if(tmp!=NULL && strcmp(tmp->value,"")!=0)
50#ifdef WIN32
51    sprintf(pythonpath,"%s/%s/;%s",ntmp,tmp->value,python_path);
52#else
53  sprintf(pythonpath,"%s/%s/:%s",ntmp,tmp->value,python_path);
54#endif
55  else
56#ifdef WIN32
57    sprintf(pythonpath,"%s;%s",ntmp,python_path);
58#else
59  sprintf(pythonpath,"%s:%s",ntmp,python_path);
60#endif
61  //#ifdef DEBUG
62    fprintf(stderr,"PYTHONPATH=%s\n",pythonpath);
63  //#endif
64#ifndef WIN32
65  setenv("PYTHONPATH",pythonpath,1);
66#else
67  SetEnvironmentVariable("PYTHONPATH",pythonpath);
68#endif
69  free(python_path);
70  free(pythonpath);
71
72  Py_Initialize();
73  PyObject *pName, *pModule, *pFunc;
74  tmp=getMap(s->content,"serviceProvider");
75  if(tmp!=NULL)
76    pName = PyString_FromString(tmp->value);
77  else{
78    map* err=createMap("text","Unable to parse serviceProvider please check your zcfg file.");
79    addToMap(err,"code","NoApplicableCode");
80    printExceptionReportResponse(m,err);
81    exit(-1);
82  }
83  pModule = PyImport_Import(pName);
84  int i;
85  int res=SERVICE_FAILED;
86  int cpid=getpid();
87  if (pModule != NULL) {
88    pFunc=PyObject_GetAttrString(pModule,s->name);
89    if (pFunc && PyCallable_Check(pFunc)){
90      PyDictObject* arg1=PyDict_FromMaps(m);
91      PyDictObject* arg2=PyDict_FromMaps(inputs);
92      PyDictObject* arg3=PyDict_FromMaps(outputs);
93      PyObject *pArgs=PyTuple_New(3);
94      PyObject *pValue;
95      PyTuple_SetItem(pArgs, 0, (PyObject *)arg1);
96      PyTuple_SetItem(pArgs, 1, (PyObject *)arg2);
97      PyTuple_SetItem(pArgs, 2, (PyObject *)arg3);
98      tmp=getMap(request,"storeExecuteResponse");
99#ifdef DEBUG
100      fprintf(stderr,"RUN IN NORMAL MODE \n");
101      fflush(stderr);
102#endif
103      pValue = PyObject_CallObject(pFunc, pArgs);
104      if (pValue != NULL) {
105        res=PyInt_AsLong(pValue);
106        freeMaps(real_outputs);
107        free(*real_outputs);
108        //*real_inputs=mapsFromPyDict(arg2);
109        //createMapsFromPyDict(real_outputs,arg3);
110        *main_conf=mapsFromPyDict(arg1);
111        *real_outputs=mapsFromPyDict(arg3);
112#ifdef DEBUG
113        fprintf(stderr,"Result of call: %i\n", PyInt_AsLong(pValue));
114        dumpMaps(inputs);
115        dumpMaps(outputs);
116#endif
117        Py_DECREF(arg1);
118        Py_DECREF(arg2);
119        Py_DECREF(arg3);
120        Py_DECREF(pArgs);
121        Py_DECREF(pValue);
122        Py_XDECREF(pFunc);
123        Py_DECREF(pModule);
124      }else{     
125        PyObject *ptype,*pvalue, *ptraceback;
126        PyErr_Fetch(&ptype, &pvalue, &ptraceback);
127        PyObject *trace=PyObject_Str(pvalue);
128        char tb[1024];
129        char pbt[10240];
130        if(PyString_Check(trace))
131          sprintf(pbt,"TRACE : %s",PyString_AsString(trace));
132        else
133          fprintf(stderr,"EMPTY TRACE ?");
134        trace=NULL;
135        trace=PyObject_Str(ptype);
136        if(PyString_Check(trace))
137          sprintf(pbt,"%s\nTRACE : %s",strdup(pbt),PyString_AsString(trace));
138        else
139          fprintf(stderr,"EMPTY TRACE ?");
140        PyObject *t;
141        pName = PyString_FromString("traceback");
142        pModule = PyImport_Import(pName);
143        pArgs = PyTuple_New(1);
144        PyTuple_SetItem(pArgs, 0, ptraceback);
145        pFunc = PyObject_GetAttrString(pModule,"format_tb");
146        pValue = PyObject_CallObject(pFunc, pArgs);
147        trace=NULL;
148        trace=PyObject_Str(pValue);
149        if(PyString_Check(trace))
150          sprintf(pbt,"%s\nUnable to run your python process properly. Please check the following messages : %s",strdup(pbt),PyString_AsString(trace));
151        else
152          sprintf(pbt,"%s \n Unable to run your python process properly. Unable to provide any futher informations.",strdup(pbt));
153        map* err=createMap("text",pbt);
154        addToMap(err,"code","NoApplicableCode");
155        printExceptionReportResponse(m,err);
156        Py_XDECREF(pFunc);
157        Py_DECREF(pArgs);
158        Py_DECREF(pModule);
159        exit(-1);
160      }
161    }
162    else{
163      char tmpS[1024];
164      sprintf(tmpS, "Cannot find the %s function int the %s file.\n", s->name, tmp->value);
165      map* tmps=createMap("text",tmpS);
166      printExceptionReportResponse(m,tmps);
167      Py_XDECREF(pFunc);
168      Py_DECREF(pModule);
169      exit(-1);
170    }
171  } else{
172    char tmpS[1024];
173    sprintf(tmpS, "Python module %s cannot be loaded.\n", tmp->value);
174    map* tmps=createMap("text",tmpS);
175    printExceptionReportResponse(m,tmps);
176    if (PyErr_Occurred())
177      PyErr_Print();
178    exit(-1);
179  } 
180#ifndef DEBUG
181  // Failed when DEBUG is defined
182  Py_Finalize();
183#endif
184  return res;
185}
186
187PyDictObject* PyDict_FromMaps(maps* t){
188  PyObject* res=PyDict_New( );
189  maps* tmp=t;
190  while(tmp!=NULL){
191    if(PyDict_SetItem(res,PyString_FromString(tmp->name),(PyObject*)PyDict_FromMap(tmp->content))<0){
192      fprintf(stderr,"Unable to parse params...");
193      exit(1);
194    }
195    tmp=tmp->next;
196  } 
197  return (PyDictObject*) res;
198}
199
200PyDictObject* PyDict_FromMap(map* t){
201  PyObject* res=PyDict_New( );
202  map* tmp=t;
203  while(tmp!=NULL){
204    /*if(strcasecmp(tmp->name,"value")==0){
205      map* ttmp=getMap(t,"size");
206      if(ttmp!=NULL)
207        if(PyDict_SetItem(res,PyString_FromString(tmp->name),PyString_FromStringAndSize(tmp->value,(Py_ssize_t) atoi(ttmp->value)))<0){
208          fprintf(stderr,"Unable to parse params...");
209          exit(1);
210        }
211      else
212        if(PyDict_SetItem(res,PyString_FromString(tmp->name),PyString_FromString(tmp->value))<0){
213          fprintf(stderr,"Unable to parse params...");
214          exit(1);
215        }
216    }
217    else*/
218      if(PyDict_SetItem(res,PyString_FromString(tmp->name),PyString_FromString(tmp->value))<0){
219        fprintf(stderr,"Unable to parse params...");
220        exit(1);
221      }
222    tmp=tmp->next;
223  }
224  return (PyDictObject*) res;
225}
226
227maps* mapsFromPyDict(PyDictObject* t){
228  maps* res=NULL;
229  maps* cursor=res;
230  PyObject* list=PyDict_Keys((PyObject*)t);
231  int nb=PyList_Size(list);
232  int i;
233  for(i=0;i<nb;i++){
234#ifdef DEBUG
235    fprintf(stderr,">> parsing maps %d\n",i);
236#endif
237    PyObject* key=PyList_GetItem(list,i);
238    PyObject* value=PyDict_GetItem((PyObject*)t,key);
239#ifdef DEBUG
240    fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
241            PyString_AsString(key),PyString_AsString(value));
242#endif
243    while(cursor!=NULL){
244      cursor=cursor->next;
245    }
246    cursor=(maps*)malloc(MAPS_SIZE);
247    cursor->name=PyString_AsString(key);
248#ifdef DEBUG
249    dumpMap(mapFromPyDict((PyDictObject*)value));
250#endif
251    cursor->content=mapFromPyDict((PyDictObject*)value);
252    cursor->next=NULL;
253    if(res==NULL)
254      res=cursor;
255    else
256      addMapsToMaps(&res,cursor);
257#ifdef DEBUG
258    dumpMaps(res);
259    fprintf(stderr,">> parsed maps %d\n",i);
260#endif
261  }
262  return res;
263}
264
265map* mapFromPyDict(PyDictObject* t){
266  map* res=NULL;
267  PyObject* list=PyDict_Keys((PyObject*)t);
268  int nb=PyList_Size(list);
269  int i;
270  for(i=0;i<nb;i++){
271    PyObject* key=PyList_GetItem(list,i);
272    PyObject* value=PyDict_GetItem((PyObject*)t,key);
273#ifdef DEBUG
274    fprintf(stderr,">> DEBUG VALUES : %s => %s\n",
275            PyString_AsString(key),PyString_AsString(value));
276#endif
277    if(res!=NULL)
278      addToMap(res,PyString_AsString(key),PyString_AsString(value));
279    else
280      res=createMap(PyString_AsString(key),PyString_AsString(value));
281  }
282  return res;
283}
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png