1 | /** |
---|
2 | * Author : David Saggiorato |
---|
3 | * |
---|
4 | * Copyright 2008-2009 GeoLabs SARL. All rights reserved. |
---|
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 | |
---|
26 | |
---|
27 | #include <string.h> |
---|
28 | #include <stdio.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <glib.h> |
---|
31 | #include <sys/stat.h> |
---|
32 | #include "service.h" |
---|
33 | #include "service_internal.h" |
---|
34 | #include "response_print.h" |
---|
35 | |
---|
36 | extern "C" { |
---|
37 | #include <libxml/tree.h> |
---|
38 | #include <libxml/xmlmemory.h> |
---|
39 | #include <libxml/parser.h> |
---|
40 | #include <libxml/xpath.h> |
---|
41 | #include <libxml/xpathInternals.h> |
---|
42 | #include "service.h" |
---|
43 | #include "service_internal.h" |
---|
44 | } |
---|
45 | |
---|
46 | int |
---|
47 | readServiceFile (maps * conf, char *file, service ** service, char *name); |
---|
48 | int |
---|
49 | createRegistry (maps* m,registry ** r, char *reg_dir); |
---|
50 | static int SCAN_DEPTH = 7; |
---|
51 | |
---|
52 | |
---|
53 | static GList *serviceCfgList; |
---|
54 | |
---|
55 | //static maps * main_conf = NULL; |
---|
56 | |
---|
57 | |
---|
58 | /*int |
---|
59 | load_main_conf(char * path){ |
---|
60 | if (main_conf != NULL) |
---|
61 | freeMaps(&main_conf); |
---|
62 | main_conf = (maps *) malloc (MAP_SIZE); |
---|
63 | return conf_read (path, main_conf); |
---|
64 | } |
---|
65 | |
---|
66 | maps * get_main_conf(){ |
---|
67 | return dupMaps(&main_conf); |
---|
68 | } |
---|
69 | |
---|
70 | */ |
---|
71 | |
---|
72 | static int |
---|
73 | scanServiceCfg (char *rootDir, GList ** serviceList, int level) |
---|
74 | { |
---|
75 | if (level >= SCAN_DEPTH) |
---|
76 | return 1; |
---|
77 | struct dirent *d; |
---|
78 | DIR *dir = opendir (rootDir); |
---|
79 | if (dir != NULL) |
---|
80 | { |
---|
81 | while ((d = readdir (dir))) |
---|
82 | { |
---|
83 | if ((d->d_type == DT_DIR || d->d_type == DT_LNK) |
---|
84 | && d->d_name[0] != '.' && strstr (d->d_name, ".") == NULL) |
---|
85 | { |
---|
86 | char *name = |
---|
87 | (char *) malloc (strlen (rootDir) + strlen (d->d_name) + 2); |
---|
88 | name[0] = '\0'; |
---|
89 | strncat (name, rootDir, strlen (rootDir)); |
---|
90 | strncat (name, "/", strlen ("/")); |
---|
91 | strncat (name, d->d_name, strlen (d->d_name)); |
---|
92 | scanServiceCfg (name, serviceList, level + 1); |
---|
93 | } |
---|
94 | else |
---|
95 | { |
---|
96 | if (d->d_name[0] != '.') |
---|
97 | if (strstr (d->d_name, ".zcfg") != NULL) |
---|
98 | { |
---|
99 | char *name = |
---|
100 | (char *) malloc (strlen (rootDir) + strlen (d->d_name) + |
---|
101 | 2); |
---|
102 | name[0] = '\0'; |
---|
103 | strncat (name, rootDir, strlen (rootDir)); |
---|
104 | strncat (name, "/", strlen ("/")); |
---|
105 | strncat (name, d->d_name, strlen (d->d_name)); |
---|
106 | *serviceList = g_list_append (*serviceList, name); |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | if (level > 0) |
---|
111 | free(rootDir); |
---|
112 | } |
---|
113 | closedir (dir); |
---|
114 | return 1; |
---|
115 | } |
---|
116 | |
---|
117 | static int |
---|
118 | get_identifier (char *root_dir, char *zcfg_path, char **identifier) |
---|
119 | { |
---|
120 | // On extrait le repertoire racine ainsi que l'extention .zcfg pour contruire l'identifiant |
---|
121 | // root_dir = /var/www/zoo/cgi-bin/ zcfg_path=/var/www/zoo/cgi-bin/gdal/ndvi/ExtractNDVI.zcfg ===> gdal.ndvi.ExtractNDVI |
---|
122 | char *identifier_tmp = |
---|
123 | (char *) malloc ((strlen (zcfg_path) + 1) * sizeof (char)); |
---|
124 | identifier_tmp[0] = '\0'; |
---|
125 | int ext_len = strlen (".zcfg"); |
---|
126 | int s_tmp_len = strlen (zcfg_path) - ext_len - strlen (root_dir); |
---|
127 | if (s_tmp_len > 1) |
---|
128 | { |
---|
129 | char *s_tmp = (char *) malloc ((s_tmp_len + 1) * sizeof (char)); |
---|
130 | int j; |
---|
131 | for (j = 0; j < s_tmp_len; j++) |
---|
132 | s_tmp[j] = zcfg_path[strlen (root_dir) + j]; |
---|
133 | s_tmp[s_tmp_len] = '\0'; |
---|
134 | char *save_ptr_strtok; |
---|
135 | char *token = strtok_r (s_tmp, "/", &save_ptr_strtok); |
---|
136 | strncat (identifier_tmp, token, strlen (token)); |
---|
137 | while (token != NULL) |
---|
138 | { |
---|
139 | token = strtok_r (NULL, "/", &save_ptr_strtok); |
---|
140 | if (token != NULL) |
---|
141 | { |
---|
142 | strncat (identifier_tmp, ".", strlen (".")); |
---|
143 | strncat (identifier_tmp, token, strlen (token)); |
---|
144 | } |
---|
145 | |
---|
146 | } |
---|
147 | *identifier = |
---|
148 | (char *) malloc ((strlen (identifier_tmp) + 1) * sizeof (char)); |
---|
149 | strncpy (*identifier, identifier_tmp, strlen (identifier_tmp) + 1); |
---|
150 | free (s_tmp); |
---|
151 | } |
---|
152 | free (identifier_tmp); |
---|
153 | return 1; |
---|
154 | } |
---|
155 | |
---|
156 | void |
---|
157 | init_services_conf (char *rootDir,char *regDir) |
---|
158 | { |
---|
159 | registry* zooRegistry=NULL; |
---|
160 | maps *m = (maps *) malloc (MAPS_SIZE); |
---|
161 | if (regDir != NULL) |
---|
162 | createRegistry (m,&zooRegistry,regDir); |
---|
163 | |
---|
164 | GList *L = NULL; |
---|
165 | scanServiceCfg (rootDir, &L, 0); |
---|
166 | GList *l = NULL; |
---|
167 | for (l = L; l; l = l->next) |
---|
168 | { |
---|
169 | service *s1 = (service *) malloc (SERVICE_SIZE); |
---|
170 | get_identifier (rootDir, (char *) (l->data), &(s1->identifier)); |
---|
171 | s1->zcfg = (char *) (l->data); |
---|
172 | readServiceFile (m, (char *) l->data, &s1, s1->identifier); |
---|
173 | inheritance(zooRegistry,&s1); |
---|
174 | serviceCfgList = g_list_append (serviceCfgList, s1); |
---|
175 | } |
---|
176 | if(zooRegistry!=NULL){ |
---|
177 | freeRegistry(&zooRegistry); |
---|
178 | free(zooRegistry); |
---|
179 | } |
---|
180 | //freeMaps(&m); |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | service * |
---|
185 | search_service (char *identifier) |
---|
186 | { |
---|
187 | GList *l; |
---|
188 | int i = 0; |
---|
189 | for (l = serviceCfgList; l; l = l->next) |
---|
190 | { |
---|
191 | #ifdef DEBUG |
---|
192 | fprintf (stderr, "%d ### %s ###\n", i, |
---|
193 | ((service *) (l->data))->identifier); |
---|
194 | i++; |
---|
195 | #endif |
---|
196 | if (strcasecmp (identifier, ((service *) (l->data))->identifier) == 0) |
---|
197 | return (service *) l->data; |
---|
198 | } |
---|
199 | return NULL; |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * Create the profile registry. |
---|
204 | * |
---|
205 | * The profile registry is optional (created only if the registry key is |
---|
206 | * available in the [main] section of the main.cfg file) and can be used to |
---|
207 | * store the profiles hierarchy. The registry is a directory which should |
---|
208 | * contain the following sub-directories: |
---|
209 | * * concept: direcotry containing .html files describing concept |
---|
210 | * * generic: directory containing .zcfg files for wps:GenericProcess |
---|
211 | * * implementation: directory containing .zcfg files for wps:Process |
---|
212 | * |
---|
213 | * @param m the conf maps containing the main.cfg settings |
---|
214 | * @param r the registry to update |
---|
215 | * @param reg_dir the resgitry |
---|
216 | * @param saved_stdout the saved stdout identifier |
---|
217 | * @return 0 if the resgitry is null or was correctly updated, -1 on failure |
---|
218 | */ |
---|
219 | |
---|
220 | int |
---|
221 | createRegistry (maps* m,registry ** r, char *reg_dir) |
---|
222 | { |
---|
223 | char registryKeys[3][15]={ |
---|
224 | "concept", |
---|
225 | "generic", |
---|
226 | "implementation" |
---|
227 | }; |
---|
228 | int scount = 0,i=0; |
---|
229 | if (reg_dir == NULL) |
---|
230 | return 0; |
---|
231 | for(i=0;i<3;i++){ |
---|
232 | char * tmpName = |
---|
233 | (char *) malloc ((strlen (reg_dir) + strlen (registryKeys[i]) + 2) * |
---|
234 | sizeof (char)); |
---|
235 | sprintf (tmpName, "%s/%s", reg_dir, registryKeys[i]); |
---|
236 | |
---|
237 | DIR *dirp1 = opendir (tmpName); |
---|
238 | struct dirent *dp1; |
---|
239 | while ((dp1 = readdir (dirp1)) != NULL){ |
---|
240 | char* extn = strstr(dp1->d_name, ".zcfg"); |
---|
241 | if(dp1->d_name[0] != '.' && extn != NULL && strlen(extn) == 5) |
---|
242 | { |
---|
243 | int t; |
---|
244 | char *tmps1= |
---|
245 | (char *) malloc ((strlen (tmpName) + strlen (dp1->d_name) + 2) * |
---|
246 | sizeof (char)); |
---|
247 | sprintf (tmps1, "%s/%s", tmpName, dp1->d_name); |
---|
248 | char *tmpsn = zStrdup (dp1->d_name); |
---|
249 | tmpsn[strlen (tmpsn) - 5] = 0; |
---|
250 | service* s1 = (service *) malloc (SERVICE_SIZE); |
---|
251 | if (s1 == NULL) |
---|
252 | { |
---|
253 | return -1; |
---|
254 | } |
---|
255 | t = readServiceFile (m, tmps1, &s1, tmpsn); |
---|
256 | free (tmpsn); |
---|
257 | if (t < 0) |
---|
258 | { |
---|
259 | map *tmp00 = getMapFromMaps (m, "lenv", "message"); |
---|
260 | char tmp01[1024]; |
---|
261 | if (tmp00 != NULL) |
---|
262 | sprintf (tmp01, _("Unable to parse the ZCFG file: %s (%s)"), |
---|
263 | dp1->d_name, tmp00->value); |
---|
264 | else |
---|
265 | sprintf (tmp01, _("Unable to parse the ZCFG file: %s."), |
---|
266 | dp1->d_name); |
---|
267 | return -1; |
---|
268 | } |
---|
269 | #ifdef DEBUG |
---|
270 | dumpService (s1); |
---|
271 | fflush (stdout); |
---|
272 | fflush (stderr); |
---|
273 | #endif |
---|
274 | if(strncasecmp(registryKeys[i],"implementation",14)==0){ |
---|
275 | inheritance(*r,&s1); |
---|
276 | } |
---|
277 | addServiceToRegistry(r,registryKeys[i],s1); |
---|
278 | freeService (&s1); |
---|
279 | free (s1); |
---|
280 | scount++; |
---|
281 | } |
---|
282 | } |
---|
283 | (void) closedir (dirp1); |
---|
284 | } |
---|
285 | return 0; |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | void |
---|
290 | CapabilitiesAllProcess (maps * m, xmlNodePtr nc) |
---|
291 | { |
---|
292 | GList *l; |
---|
293 | for (l = serviceCfgList; l; l = l->next) |
---|
294 | { |
---|
295 | service *serv = (service *) l->data; |
---|
296 | printGetCapabilitiesForProcess(m,nc,serv); |
---|
297 | |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | |
---|
302 | void DescribeProcess(maps * m, xmlNodePtr nc,char * identifiers) |
---|
303 | { |
---|
304 | if (strcasecmp ("all", identifiers) == 0){ |
---|
305 | GList *l; |
---|
306 | for (l = serviceCfgList; l; l = l->next) |
---|
307 | { |
---|
308 | service *serv = (service *) l->data; |
---|
309 | printDescribeProcessForProcess(m,nc,serv); |
---|
310 | } |
---|
311 | } |
---|
312 | else { |
---|
313 | char *save_ptr_strtok; |
---|
314 | char *token = strtok_r (identifiers, ",", &save_ptr_strtok); |
---|
315 | while (token != NULL) |
---|
316 | { |
---|
317 | service *serv = search_service (token); |
---|
318 | if (serv != NULL) |
---|
319 | printDescribeProcessForProcess (m, nc, serv); |
---|
320 | token = strtok_r (NULL, ",", &save_ptr_strtok); |
---|
321 | } |
---|
322 | } |
---|
323 | } |
---|