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_wsn_app.h"
18 #include "globus_wsrf_options.h"
19 #include "version.h"
20
21 /**
22  * @defgroup globus_wsn_programs WS-BaseNotification Client Programs
23  */
24
25 #ifndef GLOBUS_DONT_DOCUMENT_INTERNAL
26 globus_result_t
27 globus_i_wsrf_run_app(
28     globus_i_wsrf_app_t *               app,
29     int                                 argc,
30     char *                              argv[])
31 0 {
32 0     globus_result_t                     result;
33 0     globus_module_descriptor_t *        failed_module;
34 0     int                                 rc;
35 0     xsd_any *                           fault = NULL;
36
37 0     rc = globus_module_activate_array(app->module_list, &failed_module);
38
39 0     if (rc != GLOBUS_SUCCESS)
40     {
41 0         fprintf(stderr, "Error %d initializing %s\n", rc, failed_module->module_name);
42
43 0         result = GLOBUS_FAILURE;
44
45 0         goto out;
46     }
47
48 0     if (app->initialize_arg)
49     {
50 0         result = app->initialize_arg(app);
51
52 0         if (result != GLOBUS_SUCCESS)
53         {
54 0             goto deactivate_out;
55         }
56     }
57
58 0     result = globus_i_wsrf_app_parse_options(
59             argc,
60             argv,
61             app->usage,
62             app->help_footer,
63             app->options_table,
64             app->options_arg,
65             app->unknown_handler,
66             &app->epr,
67             &app->attr,
68             &app->debug);
69 0     if (result != GLOBUS_SUCCESS)
70     {
71 0         if (globus_error_match(globus_error_peek(result), NULL, GLOBUS_OPTIONS_HELP))
72         {
73 0             result = GLOBUS_SUCCESS;
74         }
75 0         goto deactivate_out;
76     }
77
78 0     result = app->invoke_function(
79             app,
80             &fault);
81
82 0     if (fault != NULL)
83     {
84 0         if (app->debug)
85         {
86 0             result = globus_i_wsrf_app_print_fault(fault);
87         }
88         else
89         {
90 0             fprintf(stderr, "Error: %s\n", fault->element->local);
91 0             result = GLOBUS_SUCCESS;
92         }
93     }
94 0     if (result != GLOBUS_SUCCESS)
95     {
96 0         printf("Error: %s\n", globus_error_print_friendly(globus_error_get(result)));
97     }
98 deactivate_out:
99 0     globus_module_deactivate_all();
100
101 out:
102 0     return result;
103 }
104 /* globus_i_wsrf_app() */
105
106
107 globus_result_t
108 globus_i_wsrf_app_print_fault(
109     xsd_any *                           fault)
110 0 {
111 0     globus_result_t                     result = GLOBUS_SUCCESS;
112 0     globus_soap_message_handle_t        stderr_handle;
113
114 0     result = globus_soap_message_handle_init_to_file(
115             &stderr_handle,
116             "stderr://",
117             O_WRONLY);
118
119 0     if (result != GLOBUS_SUCCESS)
120     {
121 0         goto out;
122     }
123 0     result = xsd_any_serialize(
124             fault->element,
125             fault,
126             stderr_handle,
127             0);
128 0     globus_soap_message_handle_destroy(stderr_handle);
129 out:
130
131 0     return result;
132 }
133 /* globus_i_wsrf_app_print_fault() */
134
135 globus_result_t
136 globus_i_wsrf_app_parse_options(
137     int                                 argc,
138     char *                              argv[],
139     const char *                        usage,
140     const char *                        footer,
141     globus_options_entry_t *            app_options_table,
142     void *                              app_options_arg,
143     globus_options_unknown_callback_t   unknown_handler,
144     wsa_EndpointReferenceType *         epr,
145     globus_soap_message_attr_t *        attr,
146     globus_bool_t *                     debug)
147 0 {
148 0     globus_result_t                     result;
149 0     globus_wsrf_options_handle_t        wsrf_options;
150 0     globus_options_handle_t             options_handle;
151 0     char * progname;
152     
153 0     progname = strrchr(argv[0], '/');
154
155 0     progname = progname ? progname + 1 : argv[0];
156
157 0     result = globus_wsrf_options_handle_init(
158             &wsrf_options,
159             argv[0],
160             &local_version,
161             usage,
162             footer);
163
164 0     if (result != GLOBUS_SUCCESS)
165     {
166 0         goto out;
167     }
168 0     result = globus_options_init(
169             &options_handle, 
170             unknown_handler,
171             app_options_arg);
172 0     if (result != GLOBUS_SUCCESS)
173     {
174 0         goto destroy_wsrf_options_out;
175     }
176 0     result = globus_wsrf_options_handle_add_to_table(
177             wsrf_options,
178             options_handle);
179 0     if (result != GLOBUS_SUCCESS)
180     {
181 0         goto destroy_options_out;
182     }
183 0     if (app_options_table)
184     {
185 0         result = globus_options_add_table(
186                 options_handle,
187                 app_options_table,
188                 app_options_arg);
189 0         if (result != GLOBUS_SUCCESS)
190         {
191 0             goto destroy_options_out;
192         }
193     }
194 0     result = globus_options_command_line_process(
195             options_handle,
196             argc,
197             argv);
198 0     if (result != GLOBUS_SUCCESS)
199     {
200 0         if (!globus_error_match(globus_error_peek(result), NULL, GLOBUS_OPTIONS_HELP))
201         {
202 0             fprintf(stderr, "%s\n",
203                     globus_error_print_friendly(globus_error_peek(result)));
204 0             fprintf(stderr, "Usage: %s %s\n", progname, usage);
205         }
206 0         goto destroy_options_out;
207     }
208 0     result = globus_wsrf_options_handle_get_debug(
209             wsrf_options,
210             debug);
211 0     if (result != GLOBUS_SUCCESS)
212     {
213 0         fprintf(stderr, "Usage: %s %s\n", progname, usage);
214
215 0         goto destroy_options_out;
216     }
217 0     result = globus_wsrf_options_handle_get_attr(
218             wsrf_options,
219             attr);
220 0     if (result != GLOBUS_SUCCESS)
221     {
222 0         fprintf(stderr, "Usage: %s %s\n", progname, usage);
223
224 0         goto destroy_options_out;
225     }
226 0     result = globus_wsrf_options_handle_get_epr(
227             wsrf_options,
228             epr);
229 0     if (result != GLOBUS_SUCCESS)
230     {
231 0         fprintf(stderr, "Usage: %s %s\n", progname, usage);
232
233         goto destroy_attr_out;
234     }
235
236 destroy_attr_out:
237 0     if (result != GLOBUS_SUCCESS)
238     {
239 0         globus_soap_message_attr_destroy(*attr);
240 0         *attr = NULL;
241     }
242 destroy_options_out:
243 0     globus_options_destroy(options_handle);
244 destroy_wsrf_options_out:
245 0     globus_wsrf_options_handle_destroy(wsrf_options);
246 out:
247 0     return result;
248 }
249 /* globus_i_wsrf_app_parse_options() */