http://axis.apache.org/axis2/c/core/docs/axis2c_manual.html
摘要:
Axis2是实现Web Service的一种技术框架(架构)。对于用户而言,只需要按照axis2的框架(skeleton)实现server端的逻辑(Server端的逻辑以dll的形式呈现),再实现client端的main方法进行访问即可。
当然在main函数中需要指定访问server端的逻辑,例如:
axis2_options_set_action(options, env, "http://ws.apache.org/axis2/c/samples/hello")
其中c/samples/hello就是具体访问的逻辑了。
在server端,则是根据client端用户的请求,返回相应的结果,所以在server端需要配置文件来指定特定的服务,这通过service.xml来配置:
<service name="hello"> <parameter name="ServiceClass" locked="xsd:false">hello</parameter> <description> Quick start guide hello service sample. </description> <operation name="greet"/></service>
值"ServiceClass" 和hello将作为一个映射map到发布引擎的服务,就是上面提到的dll。所以最终的结构如下:
实例:
http://axis.apache.org/axis2/c/core/docs/axis2c_manual.html 中有一个简单的实例,按照步骤实现如下:
1. 下载axis2软件包,并解压,在环境变量中添加变量AXIS2_HOME,其值为解压后的路径,例如c:\axis2c;
2. 在系统环境变量中找到Path项,编辑该项,在它的最开始加入C:\VS8\Common7\IDE,和添加%AXIS2_HOME%\lib,从而保证代码编译时可以找到相关的依赖项的路径;
3. 创建server端的逻辑,hello_svc.c,代码如下:
1 #include <axis2_svc_skeleton.h>
2
3 #include <axutil_log_default.h>
4
5 #include <axutil_error_default.h>
6
7 #include <axutil_array_list.h>
8
9 #include <axiom_text.h>
10
11 #include <axiom_node.h>
12
13 #include <axiom_element.h>
14
15 #include <stdio.h>
16
17
18
19 axiom_node_t *axis2_hello_greet(const axutil_env_t *env,
20
21 axiom_node_t *node);
22
23
24
25 int AXIS2_CALL
26
27 hello_free(axis2_svc_skeleton_t *svc_skeleton,
28
29 const axutil_env_t *env);
30
31
32
33 axiom_node_t* AXIS2_CALL
34
35 hello_invoke(axis2_svc_skeleton_t *svc_skeleton,
36
37 const axutil_env_t *env,
38
39 axiom_node_t *node,
40
41 axis2_msg_ctx_t *msg_ctx);
42
43
44
45
46
47 int AXIS2_CALL
48
49 hello_init(axis2_svc_skeleton_t *svc_skeleton,
50
51 const axutil_env_t *env);
52
53
54
55 axiom_node_t* AXIS2_CALL
56
57 hello_on_fault(axis2_svc_skeleton_t *svc_skeli,
58
59 const axutil_env_t *env, axiom_node_t *node);
60
61
62
63
64
65 axiom_node_t *
66
67 build_greeting_response(const axutil_env_t *env,
68
69 axis2_char_t *greeting);
70
71
72
73 axiom_node_t *
74
75 axis2_hello_greet(const axutil_env_t *env, axiom_node_t *node)
76
77 {
78
79
80 axiom_node_t *client_greeting_node = NULL;
81
82 axiom_node_t *return_node = NULL;
83
84
85
86 AXIS2_ENV_CHECK(env, NULL);
87
88
89 printf("axis2_hello_greet \n");
90
91 if (node)
92
93 {
94
95 client_greeting_node = axiom_node_get_first_child(node, env);
96
97 if (client_greeting_node &&
98
99 axiom_node_get_node_type(client_greeting_node, env) == AXIOM_TEXT)
100
101 {
102
103 axiom_text_t *greeting = (axiom_text_t *)axiom_node_get_data_element(client_greeting_node, env);
104
105 if (greeting && axiom_text_get_value(greeting , env))
106
107 {
108
109 const axis2_char_t *greeting_str = axiom_text_get_value(greeting, env);
110
111 printf("Client greeted saying \"%s\" \n", greeting_str);
112
113 return_node = build_greeting_response(env, "Hello Client!");
114
115 }
116
117 }
118
119 }
120
121 else
122
123 {
124
125 AXIS2_ERROR_SET(env->error, AXIS2_ERROR_SVC_SKEL_INVALID_XML_FORMAT_IN_REQUEST, AXIS2_FAILURE);
126
127 printf("ERROR: invalid XML in request\n");
128
129 return_node = build_greeting_response(env, "Client! Who are you?");
130
131 }
132
133
134
135 return return_node;
136
137 }
138
139
140
141 axiom_node_t *
142
143 build_greeting_response(const axutil_env_t *env, axis2_char_t *greeting)
144
145 {
146
147 axiom_node_t* greeting_om_node = NULL;
148
149 axiom_element_t * greeting_om_ele = NULL;
150
151
152
153 greeting_om_ele = axiom_element_create(env, NULL, "greetResponse", NULL, &greeting_om_node);
154
155
156
157 axiom_element_set_text(greeting_om_ele, env, greeting, greeting_om_node);
158
159
160 printf("build_greeting_response \n");
161
162 return greeting_om_node;
163
164 }
165
166
167
168 static const axis2_svc_skeleton_ops_t hello_svc_skeleton_ops_var = {
169
170
171 hello_init,
172
173 hello_invoke,
174
175 hello_on_fault,
176
177 hello_free
178
179 };
180
181
182
183 axis2_svc_skeleton_t *
184
185 axis2_hello_create(const axutil_env_t *env)
186
187 {
188
189 axis2_svc_skeleton_t *svc_skeleton = NULL;
190
191 svc_skeleton = AXIS2_MALLOC((env->allocator), sizeof(axis2_svc_skeleton_t));
192
193
194
195 svc_skeleton->ops = &hello_svc_skeleton_ops_var;
196
197
198
199 svc_skeleton->func_array = NULL;
200
201 printf("axis2_hello_create \n");
202
203 return svc_skeleton;
204
205 }
206
207
208
209 int AXIS2_CALL
210
211 hello_init(axis2_svc_skeleton_t *svc_skeleton,
212
213 const axutil_env_t *env)
214
215 {
216
217 printf("hello_init \n");
218 svc_skeleton->func_array = axutil_array_list_create(env, 0);
219
220 axutil_array_list_add(svc_skeleton->func_array, env, "helloString");
221
222 return AXIS2_SUCCESS;
223
224 }
225
226
227
228 axiom_node_t* AXIS2_CALL
229
230 hello_invoke(axis2_svc_skeleton_t *svc_skeleton,
231
232 const axutil_env_t *env,
233
234 axiom_node_t *node,
235
236 axis2_msg_ctx_t *msg_ctx)
237
238 {
239
240 printf("hello_invoke \n");
241
242 return axis2_hello_greet(env, node);
243
244 }
245
246
247
248 axiom_node_t* AXIS2_CALL
249
250 hello_on_fault(axis2_svc_skeleton_t *svc_skeli,
251
252 const axutil_env_t *env, axiom_node_t *node)
253
254 {
255
256 axiom_node_t *error_node = NULL;
257
258 axiom_node_t* text_node = NULL;
259
260 axiom_element_t *error_ele = NULL;
261
262 error_ele = axiom_element_create(env, node, "EchoServiceError", NULL,
263
264 &error_node);
265
266 axiom_element_set_text(error_ele, env, "Echo service failed ",
267
268 text_node);
269
270 printf("hello_on_fault \n");
271
272 return error_node;
273
274 }
275
276
277
278 int AXIS2_CALL
279
280 hello_free(axis2_svc_skeleton_t *svc_skeleton,
281
282 const axutil_env_t *env)
283
284 {
285
286 if (svc_skeleton->func_array)
287
288 {
289
290 axutil_array_list_free(svc_skeleton->func_array, env);
291
292 svc_skeleton->func_array = NULL;
293
294 }
295
296
297
298 if (svc_skeleton)
299
300 {
301
302 AXIS2_FREE(env->allocator, svc_skeleton);
303
304 svc_skeleton = NULL;
305
306 }
307
308
309 printf("hello_free \n");
310
311 return AXIS2_SUCCESS;
312
313 }
314
315
316
317
318
319 AXIS2_EXPORT int
320
321 axis2_get_instance(axis2_svc_skeleton_t **inst,
322
323 const axutil_env_t *env)
324
325 {
326
327 *inst = axis2_hello_create(env);
328
329 if (!(*inst))
330
331 {
332
333 return AXIS2_FAILURE;
334
335 }
336
337
338
339 printf("axis2_get_instance \n");
340 return AXIS2_SUCCESS;
341
342 }
343
344
345
346 AXIS2_EXPORT int
347
348 axis2_remove_instance(axis2_svc_skeleton_t *inst,
349
350 const axutil_env_t *env)
351
352 {
353
354 axis2_status_t status = AXIS2_FAILURE;
355
356 if (inst)
357
358 {
359
360 status = AXIS2_SVC_SKELETON_FREE(inst, env);
361
362 }
363
364 printf("axis2_remove_instance \n");
365
366 return status;
367
368 }
2
3 #include <axutil_log_default.h>
4
5 #include <axutil_error_default.h>
6
7 #include <axutil_array_list.h>
8
9 #include <axiom_text.h>
10
11 #include <axiom_node.h>
12
13 #include <axiom_element.h>
14
15 #include <stdio.h>
16
17
18
19 axiom_node_t *axis2_hello_greet(const axutil_env_t *env,
20
21 axiom_node_t *node);
22
23
24
25 int AXIS2_CALL
26
27 hello_free(axis2_svc_skeleton_t *svc_skeleton,
28
29 const axutil_env_t *env);
30
31
32
33 axiom_node_t* AXIS2_CALL
34
35 hello_invoke(axis2_svc_skeleton_t *svc_skeleton,
36
37 const axutil_env_t *env,
38
39 axiom_node_t *node,
40
41 axis2_msg_ctx_t *msg_ctx);
42
43
44
45
46
47 int AXIS2_CALL
48
49 hello_init(axis2_svc_skeleton_t *svc_skeleton,
50
51 const axutil_env_t *env);
52
53
54
55 axiom_node_t* AXIS2_CALL
56
57 hello_on_fault(axis2_svc_skeleton_t *svc_skeli,
58
59 const axutil_env_t *env, axiom_node_t *node);
60
61
62
63
64
65 axiom_node_t *
66
67 build_greeting_response(const axutil_env_t *env,
68
69 axis2_char_t *greeting);
70
71
72
73 axiom_node_t *
74
75 axis2_hello_greet(const axutil_env_t *env, axiom_node_t *node)
76
77 {
78
79
80 axiom_node_t *client_greeting_node = NULL;
81
82 axiom_node_t *return_node = NULL;
83
84
85
86 AXIS2_ENV_CHECK(env, NULL);
87
88
89 printf("axis2_hello_greet \n");
90
91 if (node)
92
93 {
94
95 client_greeting_node = axiom_node_get_first_child(node, env);
96
97 if (client_greeting_node &&
98
99 axiom_node_get_node_type(client_greeting_node, env) == AXIOM_TEXT)
100
101 {
102
103 axiom_text_t *greeting = (axiom_text_t *)axiom_node_get_data_element(client_greeting_node, env);
104
105 if (greeting && axiom_text_get_value(greeting , env))
106
107 {
108
109 const axis2_char_t *greeting_str = axiom_text_get_value(greeting, env);
110
111 printf("Client greeted saying \"%s\" \n", greeting_str);
112
113 return_node = build_greeting_response(env, "Hello Client!");
114
115 }
116
117 }
118
119 }
120
121 else
122
123 {
124
125 AXIS2_ERROR_SET(env->error, AXIS2_ERROR_SVC_SKEL_INVALID_XML_FORMAT_IN_REQUEST, AXIS2_FAILURE);
126
127 printf("ERROR: invalid XML in request\n");
128
129 return_node = build_greeting_response(env, "Client! Who are you?");
130
131 }
132
133
134
135 return return_node;
136
137 }
138
139
140
141 axiom_node_t *
142
143 build_greeting_response(const axutil_env_t *env, axis2_char_t *greeting)
144
145 {
146
147 axiom_node_t* greeting_om_node = NULL;
148
149 axiom_element_t * greeting_om_ele = NULL;
150
151
152
153 greeting_om_ele = axiom_element_create(env, NULL, "greetResponse", NULL, &greeting_om_node);
154
155
156
157 axiom_element_set_text(greeting_om_ele, env, greeting, greeting_om_node);
158
159
160 printf("build_greeting_response \n");
161
162 return greeting_om_node;
163
164 }
165
166
167
168 static const axis2_svc_skeleton_ops_t hello_svc_skeleton_ops_var = {
169
170
171 hello_init,
172
173 hello_invoke,
174
175 hello_on_fault,
176
177 hello_free
178
179 };
180
181
182
183 axis2_svc_skeleton_t *
184
185 axis2_hello_create(const axutil_env_t *env)
186
187 {
188
189 axis2_svc_skeleton_t *svc_skeleton = NULL;
190
191 svc_skeleton = AXIS2_MALLOC((env->allocator), sizeof(axis2_svc_skeleton_t));
192
193
194
195 svc_skeleton->ops = &hello_svc_skeleton_ops_var;
196
197
198
199 svc_skeleton->func_array = NULL;
200
201 printf("axis2_hello_create \n");
202
203 return svc_skeleton;
204
205 }
206
207
208
209 int AXIS2_CALL
210
211 hello_init(axis2_svc_skeleton_t *svc_skeleton,
212
213 const axutil_env_t *env)
214
215 {
216
217 printf("hello_init \n");
218 svc_skeleton->func_array = axutil_array_list_create(env, 0);
219
220 axutil_array_list_add(svc_skeleton->func_array, env, "helloString");
221
222 return AXIS2_SUCCESS;
223
224 }
225
226
227
228 axiom_node_t* AXIS2_CALL
229
230 hello_invoke(axis2_svc_skeleton_t *svc_skeleton,
231
232 const axutil_env_t *env,
233
234 axiom_node_t *node,
235
236 axis2_msg_ctx_t *msg_ctx)
237
238 {
239
240 printf("hello_invoke \n");
241
242 return axis2_hello_greet(env, node);
243
244 }
245
246
247
248 axiom_node_t* AXIS2_CALL
249
250 hello_on_fault(axis2_svc_skeleton_t *svc_skeli,
251
252 const axutil_env_t *env, axiom_node_t *node)
253
254 {
255
256 axiom_node_t *error_node = NULL;
257
258 axiom_node_t* text_node = NULL;
259
260 axiom_element_t *error_ele = NULL;
261
262 error_ele = axiom_element_create(env, node, "EchoServiceError", NULL,
263
264 &error_node);
265
266 axiom_element_set_text(error_ele, env, "Echo service failed ",
267
268 text_node);
269
270 printf("hello_on_fault \n");
271
272 return error_node;
273
274 }
275
276
277
278 int AXIS2_CALL
279
280 hello_free(axis2_svc_skeleton_t *svc_skeleton,
281
282 const axutil_env_t *env)
283
284 {
285
286 if (svc_skeleton->func_array)
287
288 {
289
290 axutil_array_list_free(svc_skeleton->func_array, env);
291
292 svc_skeleton->func_array = NULL;
293
294 }
295
296
297
298 if (svc_skeleton)
299
300 {
301
302 AXIS2_FREE(env->allocator, svc_skeleton);
303
304 svc_skeleton = NULL;
305
306 }
307
308
309 printf("hello_free \n");
310
311 return AXIS2_SUCCESS;
312
313 }
314
315
316
317
318
319 AXIS2_EXPORT int
320
321 axis2_get_instance(axis2_svc_skeleton_t **inst,
322
323 const axutil_env_t *env)
324
325 {
326
327 *inst = axis2_hello_create(env);
328
329 if (!(*inst))
330
331 {
332
333 return AXIS2_FAILURE;
334
335 }
336
337
338
339 printf("axis2_get_instance \n");
340 return AXIS2_SUCCESS;
341
342 }
343
344
345
346 AXIS2_EXPORT int
347
348 axis2_remove_instance(axis2_svc_skeleton_t *inst,
349
350 const axutil_env_t *env)
351
352 {
353
354 axis2_status_t status = AXIS2_FAILURE;
355
356 if (inst)
357
358 {
359
360 status = AXIS2_SVC_SKELETON_FREE(inst, env);
361
362 }
363
364 printf("axis2_remove_instance \n");
365
366 return status;
367
368 }