Web Service Connector Factory

Waiting on Java programmers, so trying to be effective with my time. Figured I’d show some code snippets I’ve found useful over the past couple of weeks. Hopefully someone can gleen some use from ’em.

First on da platter is a factory function to create web service calls. I had some problems with the web service classes, so ended up using the WebServiceConnector on the majority of my work here at my job. However, as a physical component with a few parameters, I needed an easy way to create many, allow them to function indenpendently, but quickly get the code running so I could meet my Friday deadlines.

The below creates WebServiceConnectors, initializes them, fires off the web service call, and waits for the response. When there is a response, it calls whatever function is required, and then self-destructs. Who knows if garbage collection cleans up the mess… Make sure you have a WebServiceConnector in your library. Technically the return value should be WebServiceConnector.


function createWebService(WSDLURL,
                          result_str,
                          result_obj,
                          status_str,
                          status_obj,
                          operation,
                          params):Object
{
        if(wsID == null){
                wsID = -1;
        }
        wsID++;
        // create the web service connector
        var ws = attachMovie("WebServiceConnector", "ws" + wsID, getNextHighestDepth());
        ws.WSDLURL = WSDLURL;
        
        // create a responder object and set him up with needed supplies
        var r = {};
        // who is my parent?
        r.owner = this;
        // what web service?
        r.ws = ws;
        // what method do I call when done?
        r.result_str = result_str;
        // what scope is he in?
        r.result_obj = result_obj;
        // what method do I call when I flarge up?
        r.status_str = status_str;
        // what scope is he in?
        r.status_obj = status_obj;
        // define my result function
        r.result = function(result)
        {
                // call my callback, and pass the result
                this.result_obj[this.result_str](result);
                this.ws.removeMovieClip();
        };
        r.status = function(status)
        {
                // call my callback, and pass the status
                this.status_obj[this.status_str](status);
        };
        // add the responder as a listener
        ws.addEventListener("result", r);
        ws.addEventListener("status", r);
        // define the web service method name
        ws.operation = operation;
        // any web service arguments?
        // Remove the first 5 to check for leftovers...
        arguments.shift();
        arguments.shift();
        arguments.shift();
        arguments.shift();
        arguments.shift();
        if(arguments.length > 0){
                ws.params = arguments;
        }else{
                ws.params = [];
        }
        // start this mug!
        ws.trigger();
        // you want this?
        return ws;
}

Example usage:

var ws = createWebService("http://something.com/service?WSDL",
                          "result",
                          this,
                          "status",
                          this,
                          "doIt",
                          "JesterXL",
                          "mypassword");