/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.ode.axis2.service;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.ode.utils.Namespaces;

import javax.xml.namespace.QName;

/**
 * Client utilities that can be used to invoke easily the deployment
 * and management services with Axis2.
 */
public class ServiceClientUtil {

    /**
     * Sends the provided message to an Axis2 deployed service.
     * @param msg the message OMElement that will be included in the body
     * @param url to send the message to
     * @return the response message
     * @throws AxisFault when a problem occured during the call
     */
    public OMElement send(OMElement msg, String url) throws AxisFault {
        Options options = new Options();
        EndpointReference target = new EndpointReference(url);
        options.setTo(target);

        ServiceClient serviceClient = new ServiceClient();
        serviceClient.setOptions(options);

        return serviceClient.sendReceive(msg);
    }

    /**
     * Builds a message for the deployment and management API using simple parameter
     * passing. Example: <br/>
     * <code>
     * buildMessage("listProcesses", new String[] {"filter", "orderKeys"},
     *          new String[] {"name=DynPartnerResponder namespace=http://ode/bpel/responder " +
     *                  "deployed>=" + notSoLongAgoStr, ""});
     * </code>
     * @param operation to call
     * @param params list of the parameters for the operation as defined in the WSDL document
     * @param values of the parameters
     * @return the message to send
     */
    public OMElement buildMessage(String operation, String[] params, Object[] values) {
        OMFactory _factory = OMAbstractFactory.getOMFactory();
        OMNamespace pmns = _factory.createOMNamespace(Namespaces.ODE_PMAPI, "pmapi");
        OMElement root = _factory.createOMElement(operation, pmns);
        for (int m = 0; m < params.length; m++) {
            OMElement omelmt = _factory.createOMElement(params[m], null);
            if (values[m] == null)
                omelmt.setText("");
            else if (values[m] instanceof String)
                omelmt.setText((String) values[m]);
            else if (values[m] instanceof QName)
                omelmt.setText((QName) values[m]);
            else if (values[m] instanceof OMElement)
                omelmt.addChild((OMElement) values[m]);
            else if (values[m] instanceof Object[]) {
                Object[] subarr = (Object[]) values[m];
                String elmtName = (String) subarr[0];
                for (int p = 1; p < subarr.length; p++) {
                    OMElement omarrelmt = _factory.createOMElement(elmtName, null);
                    omarrelmt.setText(subarr[p].toString());
                    omelmt.addChild(omarrelmt);
                }
            } else throw new UnsupportedOperationException("Type " + values[m].getClass() + "isn't supported as " +
                    "a parameter type (only String and QName are).");
            root.addChild(omelmt);
        }
        return root;
    }

}

@@ -1772,16 +1772,561 @@
 Fault %7B%0A
+        return send(msg, url, Options.DEFAULT_TIMEOUT_MILLISECONDS);%0A    %7D%0A%0A    /**%0A     * Sends the provided message to an Axis2 deployed service allowing to set a%0A     * specific timeout (in ms).%0A     * @param msg the message OMElement that will be included in the body%0A     * @param url to send the message to%0A     * @param timeout in milliseconds%0A     * @return the response message%0A     * @throws AxisFault when a problem occured during the call%0A     */%0A    public OMElement send(OMElement msg, String url, long timeout) throws AxisFault %7B%0A
         
@@ -2451,16 +2451,67 @@
 target);
+%0A        options.setTimeOutInMilliSeconds(timeout);
 %0A%0A      

Adding timeout option.