用Axis2c 实现一个Web service 的小demo程序过程详解

1.     编译服务端代码:( windows 平台)

    代码hello_svc.c:

  1. /*  
  2.  
  3.  * Copyright 2004,2005 The Apache Software Foundation.  
  4.  
  5.  *  
  6.  
  7.  * Licensed under the Apache License, Version 2.0 (the "License");  
  8.  
  9.  * you may not use this file except in compliance with the License.  
  10.  
  11.  * You may obtain a copy of the License at  
  12.  
  13.  *  
  14.  
  15.  *      http://www.apache.org/licenses/LICENSE-2.0  
  16.  
  17.  *  
  18.  
  19.  * Unless required by applicable law or agreed to in writing, software  
  20.  
  21.  * distributed under the License is distributed on an "AS IS" BASIS,  
  22.  
  23.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  24.  
  25.  * See the License for the specific language governing permissions and  
  26.  
  27.  * limitations under the License.  
  28.  
  29.  */   
  30.   
  31. #include <axis2_svc_skeleton.h>   
  32.   
  33. #include <axutil_log_default.h>   
  34.   
  35. #include <axutil_error_default.h>   
  36.   
  37. #include <axutil_array_list.h>   
  38.   
  39. #include <axiom_text.h>   
  40.   
  41. #include <axiom_node.h>   
  42.   
  43. #include <axiom_element.h>   
  44.   
  45. #include <stdio.h>   
  46.   
  47.   
  48.   
  49. axiom_node_t *axis2_hello_greet(const  axutil_env_t *env,  
  50.   
  51.         axiom_node_t *node);  
  52.   
  53.   
  54.   
  55. int  AXIS2_CALL  
  56.   
  57. hello_free(axis2_svc_skeleton_t *svc_skeleton,  
  58.   
  59.         const  axutil_env_t *env);  
  60.   
  61.   
  62.   
  63. axiom_node_t* AXIS2_CALL  
  64.   
  65. hello_invoke(axis2_svc_skeleton_t *svc_skeleton,  
  66.   
  67.         const  axutil_env_t *env,  
  68.   
  69.         axiom_node_t *node,  
  70.   
  71.         axis2_msg_ctx_t *msg_ctx);  
  72.   
  73.   
  74.   
  75.   
  76.   
  77. int  AXIS2_CALL  
  78.   
  79. hello_init(axis2_svc_skeleton_t *svc_skeleton,  
  80.   
  81.         const  axutil_env_t *env);  
  82.   
  83.   
  84.   
  85. axiom_node_t* AXIS2_CALL  
  86.   
  87. hello_on_fault(axis2_svc_skeleton_t *svc_skeli,  
  88.   
  89.         const  axutil_env_t *env, axiom_node_t *node);  
  90.   
  91.   
  92.   
  93.   
  94.   
  95. axiom_node_t *  
  96.   
  97. build_greeting_response(const  axutil_env_t *env,   
  98.   
  99.         axis2_char_t *greeting);  
  100.   
  101.   
  102.   
  103. axiom_node_t *  
  104.   
  105. axis2_hello_greet(const  axutil_env_t *env, axiom_node_t *node)  
  106.   
  107. {  
  108.   
  109.     axiom_node_t *client_greeting_node = NULL;  
  110.   
  111.     axiom_node_t *return_node = NULL;  
  112.   
  113.   
  114.   
  115.     AXIS2_ENV_CHECK(env, NULL);  
  116.   
  117.   
  118.   
  119.     if  (node)  
  120.   
  121.     {  
  122.   
  123.         client_greeting_node = axiom_node_get_first_child(node, env);  
  124.   
  125.         if  (client_greeting_node &&  
  126.   
  127.                 axiom_node_get_node_type(client_greeting_node, env) == AXIOM_TEXT)  
  128.   
  129.         {  
  130.   
  131.             axiom_text_t *greeting = (axiom_text_t *)axiom_node_get_data_element(client_greeting_node, env);  
  132.   
  133.             if  (greeting && axiom_text_get_value(greeting , env))  
  134.   
  135.             {  
  136.   
  137.                 const  axis2_char_t *greeting_str = axiom_text_get_value(greeting, env);  
  138.   
  139.                 printf("Client greeted saying /"%s/" /n" , greeting_str);  
  140.   
  141.                 return_node = build_greeting_response(env, "Hello Client!" );  
  142.   
  143.             }  
  144.   
  145.         }  
  146.   
  147.     }  
  148.   
  149.     else   
  150.   
  151.     {  
  152.   
  153.         AXIS2_ERROR_SET(env->error, AXIS2_ERROR_SVC_SKEL_INVALID_XML_FORMAT_IN_REQUEST, AXIS2_FAILURE);  
  154.   
  155.         printf("ERROR: invalid XML in request/n" );  
  156.   
  157.         return_node = build_greeting_response(env, "Client! Who are you?" );  
  158.   
  159.     }  
  160.   
  161.   
  162.   
  163.     return  return_node;  
  164.   
  165. }  
  166.   
  167.   
  168.   
  169. axiom_node_t *  
  170.   
  171. build_greeting_response(const  axutil_env_t *env, axis2_char_t *greeting)  
  172.   
  173. {  
  174.   
  175.     axiom_node_t* greeting_om_node = NULL;  
  176.   
  177.     axiom_element_t * greeting_om_ele = NULL;  
  178.   
  179.   
  180.   
  181.     greeting_om_ele = axiom_element_create(env, NULL, "greetResponse" , NULL, &greeting_om_node);  
  182.   
  183.   
  184.   
  185.     axiom_element_set_text(greeting_om_ele, env, greeting, greeting_om_node);  
  186.   
  187.   
  188.   
  189.     return  greeting_om_node;  
  190.   
  191. }  
  192.   
  193.   
  194.   
  195. static   const  axis2_svc_skeleton_ops_t hello_svc_skeleton_ops_var = {  
  196.   
  197.     hello_init,  
  198.   
  199.     hello_invoke,  
  200.   
  201.     hello_on_fault,  
  202.   
  203.     hello_free  
  204.   
  205. };  
  206.   
  207.   
  208.   
  209. axis2_svc_skeleton_t *  
  210.   
  211. axis2_hello_create(const  axutil_env_t *env)  
  212.   
  213. {  
  214.   
  215.     axis2_svc_skeleton_t *svc_skeleton = NULL;  
  216.   
  217.     svc_skeleton = AXIS2_MALLOC(env->allocator,  
  218.   
  219.             sizeof (axis2_svc_skeleton_t));  
  220.   
  221.   
  222.   
  223.     svc_skeleton->ops = &hello_svc_skeleton_ops_var;  
  224.   
  225.   
  226.   
  227.     svc_skeleton->func_array = NULL;  
  228.   
  229.    
  230.   
  231.     return  svc_skeleton;  
  232.   
  233. }  
  234.   
  235.   
  236.   
  237. int  AXIS2_CALL  
  238.   
  239. hello_init(axis2_svc_skeleton_t *svc_skeleton,  
  240.   
  241.         const  axutil_env_t *env)  
  242.   
  243. {  
  244.   
  245.     svc_skeleton->func_array = axutil_array_list_create(env, 0);  
  246.   
  247.     axutil_array_list_add(svc_skeleton->func_array, env, "helloString" );  
  248.   
  249.     return  AXIS2_SUCCESS;  
  250.   
  251. }  
  252.   
  253.   
  254.   
  255. axiom_node_t* AXIS2_CALL  
  256.   
  257. hello_invoke(axis2_svc_skeleton_t *svc_skeleton,  
  258.   
  259.         const  axutil_env_t *env,  
  260.   
  261.         axiom_node_t *node,  
  262.   
  263.         axis2_msg_ctx_t *msg_ctx)  
  264.   
  265. {  
  266.   
  267.     return  axis2_hello_greet(env, node);  
  268.   
  269. }  
  270.   
  271.   
  272.   
  273. axiom_node_t* AXIS2_CALL  
  274.   
  275. hello_on_fault(axis2_svc_skeleton_t *svc_skeli,  
  276.   
  277.         const  axutil_env_t *env, axiom_node_t *node)  
  278.   
  279. {  
  280.   
  281.     axiom_node_t *error_node = NULL;  
  282.   
  283.     axiom_node_t* text_node = NULL;  
  284.   
  285.     axiom_element_t *error_ele = NULL;  
  286.   
  287.     error_ele = axiom_element_create(env, node, "EchoServiceError" , NULL,  
  288.   
  289.             &error_node);  
  290.   
  291.     axiom_element_set_text(error_ele, env, "Echo service failed " ,  
  292.   
  293.             text_node);  
  294.   
  295.     return  error_node;  
  296.   
  297. }  
  298.   
  299.   
  300.   
  301. int  AXIS2_CALL  
  302.   
  303. hello_free(axis2_svc_skeleton_t *svc_skeleton,  
  304.   
  305.         const  axutil_env_t *env)  
  306.   
  307. {  
  308.   
  309.     if  (svc_skeleton->func_array)  
  310.   
  311.     {  
  312.   
  313.         axutil_array_list_free(svc_skeleton->func_array, env);  
  314.   
  315.         svc_skeleton->func_array = NULL;  
  316.   
  317.     }  
  318.   
  319.   
  320.   
  321.     if  (svc_skeleton)  
  322.   
  323.     {  
  324.   
  325.         AXIS2_FREE(env->allocator, svc_skeleton);  
  326.   
  327.         svc_skeleton = NULL;  
  328.   
  329.     }  
  330.   
  331.   
  332.   
  333.     return  AXIS2_SUCCESS;  
  334.   
  335. }  
  336.   
  337.   
  338.   
  339.   
  340.   
  341. AXIS2_EXPORT int   
  342.   
  343. axis2_get_instance(axis2_svc_skeleton_t **inst,  
  344.   
  345.         const  axutil_env_t *env)  
  346.   
  347. {  
  348.   
  349.     *inst = axis2_hello_create(env);  
  350.   
  351.     if  (!(*inst))  
  352.   
  353.     {  
  354.   
  355.         return  AXIS2_FAILURE;  
  356.   
  357.     }  
  358.   
  359.   
  360.   
  361.     return  AXIS2_SUCCESS;  
  362.   
  363. }  
  364.   
  365.   
  366.   
  367. AXIS2_EXPORT int   
  368.   
  369. axis2_remove_instance(axis2_svc_skeleton_t *inst,  
  370.   
  371.         const  axutil_env_t *env)  
  372.   
  373. {  
  374.   
  375.     axis2_status_t status = AXIS2_FAILURE;  
  376.   
  377.     if  (inst)  
  378.   
  379.     {  
  380.   
  381.         status = AXIS2_SVC_SKELETON_FREE(inst, env);  
  382.   
  383.     }  
  384.   
  385.     return  status;  
  386.   
  387. }  

 

to compile,

cl.exe /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "AXIS2_DECLARE_EXPORT" /D "AXIS2_SVR_MULTI_THREADED" /w /nologo /I %AXIS2C_HOME%/include /c hello_svc.c


to link,

link.exe /nologo /LIBPATH:%AXIS2C_HOME%/lib axutil.lib axiom.lib axis2_parser.lib axis2_engine.lib /DLL /OUT:hello.dll *.obj

 

 

 

 

 

 

 

 

 

2.     部署服务程序

 

首先在 AXIS2C_HOME/services 文件夹下创建文件夹“ hello ”,在将 services.xml 文件和 hello.dll 文件拷贝到“ hello ”目录下。

      为了验证你的服务是不正确部署,运行 axis2_http_server.exe

在浏览器中输入 http://localhost:9090/axis2/services

在该页面中确定是否显示你所配置的 服务。

 

3.  编写客户端代码(见 hello.c )并编译

 

  1. /*  
  2.  
  3.  * Copyright 2004,2005 The Apache Software Foundation.  
  4.  
  5.  *  
  6.  
  7.  * Licensed under the Apache License, Version 2.0 (the "License");  
  8.  
  9.  * you may not use this file except in compliance with the License.  
  10.  
  11.  * You may obtain a copy of the License at  
  12.  
  13.  *  
  14.  
  15.  *      http://www.apache.org/licenses/LICENSE-2.0  
  16.  
  17.  *  
  18.  
  19.  * Unless required by applicable law or agreed to in writing, software  
  20.  
  21.  * distributed under the License is distributed on an "AS IS" BASIS,  
  22.  
  23.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  24.  
  25.  * See the License for the specific language governing permissions and  
  26.  
  27.  * limitations under the License.  
  28.  
  29.  */   
  30.  
  31.  
  32.  
  33. #include <stdio.h>   
  34.   
  35. #include <axiom.h>  
  36.  
  37. #include <axis2_util.h>   
  38.   
  39. #include <axiom_soap.h>  
  40.  
  41. #include <axis2_client.h>   
  42.   
  43.   
  44.   
  45. axiom_node_t *  
  46.   
  47. build_om_request(const  axutil_env_t *env);  
  48.   
  49.   
  50.   
  51. const  axis2_char_t *  
  52.   
  53. process_om_response(const  axutil_env_t *env,  
  54.   
  55.         axiom_node_t *node);  
  56.   
  57.   
  58.   
  59. int  main( int  argc,  char ** argv)  
  60.   
  61. {  
  62.   
  63.     const  axutil_env_t *env = NULL;  
  64.   
  65.     const  axis2_char_t *address = NULL;  
  66.   
  67.     axis2_endpoint_ref_t* endpoint_ref = NULL;  
  68.   
  69.     axis2_options_t *options = NULL;  
  70.   
  71.     const  axis2_char_t *client_home = NULL;  
  72.   
  73.     axis2_svc_client_t* svc_client = NULL;  
  74.   
  75.     axiom_node_t *payload = NULL;  
  76.   
  77.     axiom_node_t *ret_node = NULL;  
  78.   
  79.   
  80.         //创建 environment . 它是对内存分配、 错误处理、日志和线程机制的封装。   
  81.     env = axutil_env_create_all("hello_client.log" , AXIS2_LOG_LEVEL_TRACE);  
  82.   
  83.   
  84.         //创建和设置 客户端 Options。如,服务的端点 地址( the endpoint address of the service)   
  85.     options = axis2_options_create(env);  
  86.   
  87.   
  88.   
  89.     address = "http://localhost:9090/axis2/services/hello" ;  
  90.   
  91.     if  (argc > 1)  
  92.   
  93.         address = argv[1];  
  94.   
  95.     if  (axutil_strcmp(address,  "-h" ) == 0)  
  96.   
  97.     {  
  98.   
  99.         printf("Usage : %s [endpoint_url]/n" , argv[0]);  
  100.   
  101.         printf("use -h for help/n" );  
  102.   
  103.         return  0;  
  104.   
  105.     }  
  106.   
  107.     printf("Using endpoint : %s/n" , address);  
  108.   
  109.     endpoint_ref = axis2_endpoint_ref_create(env, address);  
  110.   
  111.     axis2_options_set_to(options, env, endpoint_ref);  
  112.   
  113.   
  114.   
  115.     client_home = AXIS2_GETENV("AXIS2C_HOME" );  
  116.   
  117.     if  (!client_home && !strcmp(client_home,  "" ))  
  118.   
  119.         client_home = "../.." ;  
  120.   
  121.   
  122.         //创建 服务客户端实例 ,使操作API更加简单。函数 要输入一个 Respository Folder 参数,   
  123.         //该参数相当于 axis2c 工作的目录   
  124.     svc_client = axis2_svc_client_create(env, client_home);  
  125.   
  126.     if  (!svc_client)  
  127.   
  128.     {  
  129.   
  130.         printf("Error creating service client/n" );  
  131.   
  132.         AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Stub invoke FAILED: Error code:"   
  133.   
  134.                 " %d :: %s" , env->error->error_number,  
  135.   
  136.                 AXIS2_ERROR_GET_MESSAGE(env->error));  
  137.   
  138.         return  -1;  
  139.   
  140.     }  
  141.   
  142.   
  143.   
  144.     axis2_svc_client_set_options(svc_client, env, options);  
  145.   
  146.   
  147.   
  148.     payload = build_om_request(env);  
  149.   
  150.   
  151.   
  152.     ret_node = axis2_svc_client_send_receive(svc_client, env, payload);  
  153.   
  154.   
  155.   
  156.     if  (ret_node)  
  157.   
  158.     {  
  159.   
  160.         const  axis2_char_t *greeting = process_om_response(env, ret_node);  
  161.   
  162.         if  (greeting)  
  163.   
  164.             printf("/nReceived greeting: /"%s/" from service/n" , greeting);  
  165.   
  166.   
  167.   
  168.         axiom_node_free_tree(ret_node, env);  
  169.   
  170.         ret_node = NULL;  
  171.   
  172.     }  
  173.   
  174.     else   
  175.   
  176.     {  
  177.   
  178.         AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Stub invoke FAILED: Error code:"   
  179.   
  180.                 " %d :: %s" , env->error->error_number,  
  181.   
  182.                 AXIS2_ERROR_GET_MESSAGE(env->error));  
  183.   
  184.         printf("hello client invoke FAILED!/n" );  
  185.   
  186.     }  
  187.   
  188.   
  189.   
  190.     if  (svc_client)  
  191.   
  192.     {  
  193.   
  194.         axis2_svc_client_free(svc_client, env);  
  195.   
  196.         svc_client = NULL;  
  197.   
  198.     }  
  199.   
  200.   
  201.   
  202.     if  (env)  
  203.   
  204.     {  
  205.   
  206.         axutil_env_free((axutil_env_t *) env);  
  207.   
  208.         env = NULL;  
  209.   
  210.     }  
  211.   
  212.   
  213.   
  214.     return  0;  
  215.   
  216. }  
  217.   
  218.   
  219.   
  220. axiom_node_t *  
  221.   
  222. build_om_request(const  axutil_env_t *env)  
  223.   
  224. {  
  225.   
  226.     axiom_node_t* greet_om_node = NULL;  
  227.   
  228.     axiom_element_t * greet_om_ele = NULL;  
  229.   
  230.   
  231.   
  232.     greet_om_ele = axiom_element_create(env, NULL, "greet" , NULL, &greet_om_node);  
  233.   
  234.     axiom_element_set_text(greet_om_ele, env, "Hello Server!" , greet_om_node);  
  235.   
  236.   
  237.   
  238.     return  greet_om_node;  
  239.   
  240. }  
  241.   
  242.   
  243.   
  244. const  axis2_char_t *  
  245.   
  246. process_om_response(const  axutil_env_t *env,  
  247.   
  248.         axiom_node_t *node)  
  249.   
  250. {  
  251.   
  252.     axiom_node_t *service_greeting_node = NULL;  
  253.   
  254.     axiom_node_t *return_node = NULL;  
  255.   
  256.   
  257.   
  258.     if  (node)  
  259.   
  260.     {  
  261.   
  262.         service_greeting_node = axiom_node_get_first_child(node, env);  
  263.   
  264.         if  (service_greeting_node &&  
  265.   
  266.                 axiom_node_get_node_type(service_greeting_node, env) == AXIOM_TEXT)  
  267.   
  268.         {  
  269.   
  270.             axiom_text_t *greeting = (axiom_text_t *)axiom_node_get_data_element(service_greeting_node, env);  
  271.   
  272.             if  (greeting && axiom_text_get_value(greeting , env))  
  273.   
  274.             {  
  275.   
  276.                 return  axiom_text_get_value(greeting, env);  
  277.   
  278.             }  
  279.   
  280.         }  
  281.   
  282.     }  
  283.   
  284.     return  NULL;  
  285.   
  286. }  

 

to compile

cl.exe /nologo /D "WIN32" /D "_WINDOWS" /D "_MBCS" /I %AXIS2C_HOME%/include /c hello.c

to link,

link.exe /LIBPATH:%AXIS2C_HOME%/lib axutil.lib axiom.lib axis2_parser.lib axis2_engine.lib /OUT:hello.exe *.obj

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值