package com.yihu.hos.core.webservice;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;


/**
 * Created by hzp on 2016/5/23.
 * webservice调用通用类
 */
public class WebserviceUtil {


    /**
     * 获取SOAP协议ws
     */
    public static String request(String url,String method,Object[] objs) throws Exception{
        /*JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = factory.createClient(url);

        // 下面一段处理 WebService接口和实现类namespace不同的情况
        // CXF动态客户端在处理此问题时，会报No operation was found with the name的异常
        Endpoint endpoint = client.getEndpoint();
        QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), method);
        BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding();
        if (bindingInfo.getOperation(opName) == null) {
            for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) {
                if (method.equals(operationInfo.getName().getLocalPart())) {
                    opName = operationInfo.getName();
                    break;
                }
            }
        }*/

        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        Client client = factory.createClient(url);

        Object[] result = client.invoke(method, objs); // 按照方法的参数来提供值
        if (result != null && result.length > 0) {
            System.out.println(result[0]);
            return result[0].toString();
        }
        else{
            return "";
        }

    }


    public static void main(String[] args) throws Exception{
        //namespace是命名空间，methodName是方法名
        String sql = "select * from stde_adapter_metadata";
        //调用web Service//输出调用结果
        System.out.println(WebserviceUtil.request("http://192.168.131.6:8080/service/sql?wsdl","ExcuteSQL",new Object[]{"",sql}));

    }

}
