serviceclient.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 832 行 · 第 1/3 页

JAVA
832
字号

    /**
     * Directly invoke an anonymous operation with an In-Out MEP without waiting
     * for a response. This method sends your supplied XML with response
     * notification to your callback handler. For more control, you can instead
     * create a client for the operation and use that client to execute the
     * exchange.
     *
     * @param elem the data to send (becomes the content of SOAP body)
     * @param callback a Callback which will be notified upon completion
     * @throws AxisFault in case of error
     * @see #createClient(QName)
     */
    public void sendReceiveNonBlocking(OMElement elem, AxisCallback callback)
            throws AxisFault {
        sendReceiveNonBlocking(ANON_OUT_IN_OP, elem, callback);
    }

    /**
     * Directly invoke a named operation with an In-Out MEP without waiting for
     * a response. This method sends your supplied XML with response
     * notification to your callback handler. For more control, you can instead
     * create a client for the operation and use that client to execute the
     * exchange.
     *
     * @param operation name of operation to be invoked (non-<code>null</code>)
     * @param elem the data to send (becomes the content of SOAP body)
     * @param callback a Callback which will be notified upon completion
     * @throws AxisFault in case of error
     * @see #createClient(QName)
     * @deprecated Please use the AxisCallback interface rather than Callback, which has been deprecated
     */
    public void sendReceiveNonBlocking(QName operation, OMElement elem, Callback callback)
            throws AxisFault {
        MessageContext mc = new MessageContext();
        fillSOAPEnvelope(mc, elem);
        OperationClient mepClient = createClient(operation);
        // here a blocking invocation happens in a new thread, so the
        // progamming model is non blocking
        mepClient.setCallback(callback);
        mepClient.addMessageContext(mc);
        mepClient.execute(false);
    }

    /**
     * Directly invoke a named operation with an In-Out MEP without waiting for
     * a response. This method sends your supplied XML with response
     * notification to your callback handler. For more control, you can instead
     * create a client for the operation and use that client to execute the
     * exchange.
     *
     * @param operation name of operation to be invoked (non-<code>null</code>)
     * @param elem the data to send (becomes the content of SOAP body)
     * @param callback a Callback which will be notified upon completion
     * @throws AxisFault in case of error
     * @see #createClient(QName)
     */
    public void sendReceiveNonBlocking(QName operation, OMElement elem, AxisCallback callback)
            throws AxisFault {
        MessageContext mc = new MessageContext();
        fillSOAPEnvelope(mc, elem);
        OperationClient mepClient = createClient(operation);
        // here a blocking invocation happens in a new thread, so the
        // progamming model is non blocking
        mepClient.setCallback(callback);
        mepClient.addMessageContext(mc);
        mepClient.execute(false);
    }

    /**
     * Create an operation client with the appropriate message exchange pattern
     * (MEP). This method creates a full-function MEP client which can be used
     * to exchange messages for a specific operation. It configures the
     * constructed operation client to use the current normal and override
     * options. This method is used internally, and also by generated client
     * stub code.
     *
     * @param operationQName qualified name of operation (local name is operation
     *                       name, namespace URI is just the empty string)
     * @return client configured to talk to the given operation
     * @throws AxisFault if the operation is not found
     */
    public OperationClient createClient(QName operationQName) throws AxisFault {
        AxisOperation axisOperation = axisService.getOperation(operationQName);
        if (axisOperation == null) {
            throw new AxisFault(Messages
                    .getMessage("operationnotfound", operationQName.getLocalPart()));
        }

        OperationClient operationClient = axisOperation.createClient(serviceContext, options);

        // if overide options have been set, that means we need to make sure
        // those options override the options of even the operation client. So,
        // what we do is switch the parents around to make that work.
        if (overrideOptions != null) {
            overrideOptions.setParent(operationClient.getOptions());
            operationClient.setOptions(overrideOptions);
        }
        return operationClient;
    }

    /**
     * Return the SOAP factory to use depending on what options have been set.
     * If the SOAP version can not be seen in the options, version 1.1 is the
     * default.
     *
     * @return the SOAP factory
     * @see Options#setSoapVersionURI(String)
     */
    private SOAPFactory getSOAPFactory() {
        String soapVersionURI = options.getSoapVersionURI();
        if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(soapVersionURI)) {
            return OMAbstractFactory.getSOAP12Factory();
        } else {
            // make the SOAP 1.1 the default SOAP version
            return OMAbstractFactory.getSOAP11Factory();
        }
    }

    /**
     * Prepare a SOAP envelope with the stuff to be sent.
     *
     * @param messageContext the message context to be filled
     * @param xmlPayload     the payload content
     * @throws AxisFault if something goes wrong
     */
    private void fillSOAPEnvelope(MessageContext messageContext, OMElement xmlPayload)
            throws AxisFault {
        messageContext.setServiceContext(serviceContext);
        SOAPFactory soapFactory = getSOAPFactory();
        SOAPEnvelope envelope = soapFactory.getDefaultEnvelope();
        if (xmlPayload != null) {
            envelope.getBody().addChild(xmlPayload);
        }
        addHeadersToEnvelope(envelope);
        messageContext.setEnvelope(envelope);
    }


    /**
     * Add all configured headers to a SOAP envelope.
     *
     * @param envelope the SOAPEnvelope in which to write the headers
     */
    public void addHeadersToEnvelope(SOAPEnvelope envelope) {
        if (headers != null) {
            SOAPHeader soapHeader = envelope.getHeader();
            for (int i = 0; i < headers.size(); i++) {
                soapHeader.addChild((OMElement) headers.get(i));
            }
        }
    }


    /**
     * Get the endpoint reference for this client using a particular transport.
     *
     * @param transport transport name (non-<code>null</code>)
     * @return local endpoint
     * @throws AxisFault in case of error
     */
    public EndpointReference getMyEPR(String transport) throws AxisFault {
        return serviceContext.getMyEPR(transport);
    }

    /**
     * Get the endpoint reference for the service.
     *
     * @return service endpoint
     */
    public EndpointReference getTargetEPR() {
        return serviceContext.getTargetEPR();
    }

    /**
     * Set the endpoint reference for the service.
     *
     * @param targetEpr the EPR this ServiceClient should target
     */
    public void setTargetEPR(EndpointReference targetEpr) {
        serviceContext.setTargetEPR(targetEpr);
        options.setTo(targetEpr);
    }

    /**
     * Gets the last OperationContext
     *
     * @return the last OperationContext that was invoked by this ServiceClient
     */
    public OperationContext getLastOperationContext() {
        return serviceContext.getLastOperationContext();
    }

    /**
     * Sets whether or not to cache the last OperationContext
     * @deprecated
     * @param cachingOpContext true if we should hold onto the last active OperationContext
     */
    public void setCachingOperationContext(boolean cachingOpContext) {
        serviceContext.setCachingOperationContext(cachingOpContext);
    }


    /**
     * Get the service context.
     *
     * @return context
     */
    public ServiceContext getServiceContext() {
        return serviceContext;
    }

    protected void finalize() throws Throwable {
        super.finalize();
        cleanup();
    }

    /**
     * Clean up configuration created with this client. Call this method when
     * you're done using the client, in order to discard any associated
     * resources.
     *
     * @throws AxisFault in case of error
     */
    public void cleanup() throws AxisFault {
        // if a configuration context was created for this client there'll also
        //  be a service group, so discard that
        if (!createConfigCtx) {
            String serviceGroupName = axisService.getAxisServiceGroup().getServiceGroupName();
            AxisConfiguration axisConfiguration = configContext.getAxisConfiguration();
            AxisServiceGroup asg = axisConfiguration.getServiceGroup(serviceGroupName);
            if (asg != null) {
                axisConfiguration.removeServiceGroup(serviceGroupName);
            }
        } else {
            configContext.terminate();
        }
    }

    public void cleanupTransport() throws AxisFault{
        if(getLastOperationContext()!=null){
            MessageContext outMessageContext =
                    getLastOperationContext().getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
            if (outMessageContext!=null) {
                outMessageContext.getTransportOut().getSender().cleanup(outMessageContext);
            }
        }
    }

    /**
     * Configure the ServiceClient to interact with the Web service described by
     * the specified AxisService object.
     *
     * @param axisService the AxisService that represents the new Web service.
     * @throws AxisFault if an error occurs while configuring the ServiceClient.
     */
    public void setAxisService(AxisService axisService) throws AxisFault {

        if (axisService == null) {
            // AxisFault?
            throw new IllegalArgumentException("AxisService is null");
        }

        axisConfig.removeService(this.axisService.getName());
        this.axisService = axisService;

        axisService.setClientSide(true);
        axisConfig.addService(axisService);

        AxisServiceGroup axisServiceGroup = axisService.getAxisServiceGroup();
        ServiceGroupContext serviceGroupContext =
                configContext.createServiceGroupContext(axisServiceGroup);
        this.serviceContext = serviceGroupContext.getServiceContext(axisService);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?