1 /*
2  * Copyright 1999-2006 University of Chicago
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "globus_i_wsrf_app.h"
18 #include "globus_wsrf_options.h"
19 #include "version.h"
20
21 /**
22  * @defgroup globus_wsrp_tools_programs WS-ResourceProperties Client Programs
23  */
24 /**
25  * @defgroup globus_wsrl_tools_programs WS-ResourceLifetime Client Programs
26  */
27
28 #ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
29 globus_result_t
30 globus_i_wsrf_run_app(
31     globus_i_wsrf_app_t *               app,
32     int                                 argc,
33     char *                              argv[])
34 0 {
35 0     globus_result_t                     result;
36 0     globus_module_descriptor_t *        failed_module;
37 0     int                                 rc;
38 0     xsd_any *                           fault = NULL;
39
40 0     rc = globus_module_activate_array(app->module_list, &failed_module);
41
42 0     if (rc != GLOBUS_SUCCESS)
43     {
44 0         fprintf(stderr, "Error %d initializing %s\n", rc, failed_module->module_name);
45
46 0         result = GLOBUS_FAILURE;
47
48 0         goto out;
49     }
50
51 0     if (app->initialize_arg)
52     {
53 0         result = app->initialize_arg(app);
54
55 0         if (result != GLOBUS_SUCCESS)
56         {
57 0             goto deactivate_out;
58         }
59     }
60
61 0     result = globus_i_wsrf_app_parse_options(
62             argc,
63             argv,
64             app->usage,
65             app->help_footer,
66             app->options_table,
67             app->options_arg,
68             app->unknown_handler,
69             &app->epr,
70             &app->attr,
71             &app->debug);
72 0     if (result != GLOBUS_SUCCESS)
73     {
74 0         if (globus_error_match(globus_error_peek(result), NULL, GLOBUS_OPTIONS_HELP))
75         {
76 0             result = GLOBUS_SUCCESS;
77         }
78 0         goto deactivate_out;
79     }
80
81
82 0     result = app->invoke_function(
83             app,
84             &fault);
85
86 0     if (fault != NULL)
87     {
88 0         if (app->debug)
89         {
90 0             result = globus_i_wsrf_app_print_fault(fault);
91         }
92         else
93         {
94 0             fprintf(stderr, "Error: %s\n", fault->element->local);
95 0             result = GLOBUS_SUCCESS;
96         }
97     }
98 0     if (result != GLOBUS_SUCCESS)
99     {
100 0         printf("Error: %s\n", globus_error_print_friendly(globus_error_get(result)));
101     }
102 deactivate_out:
103 0     globus_module_deactivate_all();
104
105 out:
106 0     return result;
107 }
108 /* globus_i_wsrf_app() */
109
110
111 globus_result_t
112 globus_i_wsrf_app_print_fault(
113     xsd_any *                           fault)
114 0 {
115 0     globus_result_t                     result = GLOBUS_SUCCESS;
116 0     globus_soap_message_handle_t        stderr_handle;
117
118 0     result = globus_soap_message_handle_init_to_file(
119             &stderr_handle,
120             "stderr://",
121             O_WRONLY);
122
123 0     if (result != GLOBUS_SUCCESS)
124     {
125 0         goto out;
126     }
127 0     result = xsd_any_serialize(
128             fault->element,
129             fault,
130             stderr_handle,
131             0);
132 0     globus_soap_message_handle_destroy(stderr_handle);
133 out:
134
135 0     return result;
136 }
137 /* globus_i_wsrf_app_print_fault() */
138
139 globus_result_t
140 globus_i_wsrf_app_parse_options(
141     int                                 argc,
142     char *                              argv[],
143     const char *                        usage,
144     const char *                        footer,
145     globus_options_entry_t *            app_options_table,
146     void *                              app_options_arg,
147     globus_options_unknown_callback_t   unknown_handler,
148     wsa_EndpointReferenceType *         epr,
149     globus_soap_message_attr_t *        attr,
150     globus_bool_t *                     debug)
151 0 {
152 0     globus_result_t                     result;
153 0     globus_wsrf_options_handle_t        wsrf_options;
154 0     globus_options_handle_t             options_handle;
155 0     char * progname;
156     
157 0     progname = strrchr(argv[0], '/');
158
159 0     progname = progname ? progname + 1 : argv[0];
160
161 0     result = globus_wsrf_options_handle_init(
162             &wsrf_options,
163             argv[0],
164             &local_version,
165             usage,
166             footer);
167
168 0     if (result != GLOBUS_SUCCESS)
169     {
170 0         goto out;
171     }
172 0     result = globus_options_init(
173             &options_handle, 
174             unknown_handler,
175             app_options_arg);
176 0     if (result != GLOBUS_SUCCESS)
177     {
178 0         goto destroy_wsrf_options_out;
179     }
180 0     result = globus_wsrf_options_handle_add_to_table(
181             wsrf_options,
182             options_handle);
183 0     if (result != GLOBUS_SUCCESS)
184     {
185 0         goto destroy_options_out;
186     }
187 0     if (app_options_table)
188     {
189 0         result = globus_options_add_table(
190                 options_handle,
191                 app_options_table,
192                 app_options_arg);
193 0         if (result != GLOBUS_SUCCESS)
194         {
195 0             goto destroy_options_out;
196         }
197     }
198 0     result = globus_options_command_line_process(
199             options_handle,
200             argc,
201             argv);
202 0     if (result != GLOBUS_SUCCESS)
203     {
204 0         if (!globus_error_match(globus_error_peek(result), NULL, GLOBUS_OPTIONS_HELP))
205         {
206 0             fprintf(stderr, "%s\n",
207                     globus_error_print_friendly(globus_error_peek(result)));
208 0             fprintf(stderr, "Usage: %s %s\n", progname, usage);
209         }
210 0         goto destroy_options_out;
211     }
212 0     result = globus_wsrf_options_handle_get_debug(
213             wsrf_options,
214             debug);
215 0     if (result != GLOBUS_SUCCESS)
216     {
217 0         fprintf(stderr, "Usage: %s %s\n", progname, usage);
218
219 0         goto destroy_options_out;
220     }
221 0     result = globus_wsrf_options_handle_get_attr(
222             wsrf_options,
223             attr);
224 0     if (result != GLOBUS_SUCCESS)
225     {
226 0         fprintf(stderr, "Usage: %s %s\n", progname, usage);
227
228 0         goto destroy_options_out;
229     }
230 0     result = globus_wsrf_options_handle_get_epr(
231             wsrf_options,
232             epr);
233 0     if (result != GLOBUS_SUCCESS)
234     {
235 0         fprintf(stderr, "Usage: %s %s\n", progname, usage);
236
237         goto destroy_attr_out;
238     }
239
240 destroy_attr_out:
241 0     if (result != GLOBUS_SUCCESS)
242     {
243 0         globus_soap_message_attr_destroy(*attr);
244 0         *attr = NULL;
245     }
246 destroy_options_out:
247 0     globus_options_destroy(options_handle);
248 destroy_wsrf_options_out:
249 0     globus_wsrf_options_handle_destroy(wsrf_options);
250 out:
251 0     return result;
252 }
253 /* globus_i_wsrf_app_parse_options() */