Changeset 1165

Show
Ignore:
Timestamp:
12/20/07 11:13:17 (7 months ago)
Author:
richi
Message:

enhancement #115: [part I] implemented removal of service url in config file and added namespaces for generated stubs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/config/xml/json-rpc-service.xml

    r1156 r1165  
    33    xmlns:xj="http://xjconf.net/XJConf" 
    44    xmlns="http://stubbles.net/service/json-rpc"> 
    5   <service-url>${jsonrpc.service.url}</service-url> 
    65  <services> 
    76    <service name="MathService" className="org.stubbles.examples.service.MathService"/> 
  • trunk/src/main/javascript/stub-json-rpc-debug.js

    r1085 r1165  
    44stubbles.json = {}; 
    55stubbles.json.rpc = {}; 
    6 stubbles.json.rpc.appendToURL = null; 
     6stubbles.json.rpc.appendToUrl = null; 
     7stubbles.json.rpc.serviceUrl  = null; 
     8 
    79/** 
    810 * Stubbles JSON-RPC client 
     
    1517 * @author  Stephan Schmidt <schst@stubbles.net> 
    1618 */ 
    17 stubbles.json.rpc.Client = function(clientObj, serviceURL) { 
     19 
     20/** 
     21 * constuctor 
     22 */ 
     23stubbles.json.rpc.Client = function(clientObj) { 
    1824    var reqRespMapping = []; 
    1925 
    20     this.createId = function() { 
    21         var d = new Date(); 
    22         var id = d.getHours() +''+ d.getMinutes() +''+ d.getMilliseconds(); 
    23         return id; 
    24     }; 
    25  
    26     this.callback = { 
     26    var callback = { 
    2727        success: function(o) { 
    2828            var rpcObj = o.responseText.parseJSON(); 
     
    3232                        var classAndMethod = reqRespMapping[i].method.split("."); 
    3333                        console.info("Stubbles::REQID " + rpcObj.id +  " :: Calling callback method callback__" + classAndMethod[1] + "("+rpcObj.id+", "+rpcObj.result+", "+rpcObj.error+")"); 
     34                        var methodName = 'callback__' + classAndMethod[1]; 
    3435 
    3536                        // call callback method on clientObj 
    36                         var methodName = 'callback__' + classAndMethod[1]; 
    3737                        clientObj[methodName].call(clientObj, rpcObj.id, rpcObj.result, rpcObj.error); 
    3838                        return; 
     
    5252    }; 
    5353 
     54    this.createId = function() { 
     55        var d = new Date(); 
     56        var id = d.getHours() +''+ d.getMinutes() +''+ d.getMilliseconds(); 
     57        return id; 
     58    }; 
     59 
    5460    this.doCall = function(classAndMethod, args) { 
    5561        var id = this.createId(); 
     62 
     63        if(stubbles.json.rpc.serviceUrl === null) { 
     64            throw 'no service url set via js object \'stubbles.json.rpc.serviceUrl\''; 
     65        } else { 
     66            stubbles.json.rpc.serviceUrl = (stubbles.json.rpc.appendToUrl !== null) 
     67                                           ? stubbles.json.rpc.serviceUrl + '&' + stubbles.json.rpc.appendToUrl 
     68                                           : stubbles.json.rpc.serviceUrl + ''; 
     69        } 
    5670 
    5771        // because args is an array-like object and 
     
    6175        } 
    6276 
    63         var postUrl = serviceURL; 
    64         if (stubbles.json.rpc.appendToURL !== null) { 
    65             postUrl = postUrl + stubbles.json.rpc.appendToURL; 
    66         } 
    67          
    6877        var jsonRpcReq = { 
    6978            method: classAndMethod, 
     
    7180            id: id 
    7281        }; 
    73          
    74         YAHOO.util.Connect.asyncRequest('POST', postUrl, this.callback, jsonRpcReq.toJSONString()); 
     82 
     83        YAHOO.util.Connect.asyncRequest('POST', stubbles.json.rpc.serviceUrl, callback, jsonRpcReq.toJSONString()); 
    7584        console.info("Stubbles::REQID " + id +  " :: Calling method " + classAndMethod); 
    7685        reqRespMapping.push(jsonRpcReq); 
    7786        return id; 
    78   }; 
     87    }; 
    7988}; 
  • trunk/src/main/javascript/stub-json-rpc.js

    r1085 r1165  
    44stubbles.json = {}; 
    55stubbles.json.rpc = {}; 
    6 stubbles.json.rpc.appendToURL = null; 
     6stubbles.json.rpc.appendToUrl = null; 
     7stubbles.json.rpc.serviceUrl  = null; 
     8 
    79/** 
    810 * Stubbles JSON-RPC client 
     
    1517 * @author  Stephan Schmidt <schst@stubbles.net> 
    1618 */ 
    17 stubbles.json.rpc.Client = function(clientObj, serviceURL) { 
     19 
     20/** 
     21 * constuctor 
     22 */ 
     23stubbles.json.rpc.Client = function(clientObj) { 
    1824    var reqRespMapping = []; 
    1925 
    20     this.createId = function() { 
    21         var d = new Date(); 
    22         var id = d.getHours() +''+ d.getMinutes() +''+ d.getMilliseconds(); 
    23         return id; 
    24     }; 
    25  
    26     this.callback = { 
     26    var callback = { 
    2727        success: function(o) { 
    2828            var rpcObj = o.responseText.parseJSON(); 
     
    3232                        var classAndMethod = reqRespMapping[i].method.split("."); 
    3333                        var methodName = 'callback__' + classAndMethod[1]; 
    34                          
     34 
    3535                        // call callback method on clientObj 
    3636                        clientObj[methodName].call(clientObj, rpcObj.id, rpcObj.result, rpcObj.error); 
     
    4848    }; 
    4949 
     50    this.createId = function() { 
     51        var d = new Date(); 
     52        var id = d.getHours() +''+ d.getMinutes() +''+ d.getMilliseconds(); 
     53        return id; 
     54    }; 
     55 
    5056    this.doCall = function(classAndMethod, args) { 
    5157        var id = this.createId(); 
     58 
     59        if(stubbles.json.rpc.serviceUrl === null) { 
     60            throw 'no service url set via js object \'stubbles.json.rpc.serviceUrl\''; 
     61        } else { 
     62            stubbles.json.rpc.serviceUrl = (stubbles.json.rpc.appendToUrl !== null) 
     63                                           ? stubbles.json.rpc.serviceUrl + '&' + stubbles.json.rpc.appendToUrl 
     64                                           : stubbles.json.rpc.serviceUrl + ''; 
     65        } 
    5266 
    5367        // because args is an array-like object and 
     
    5771        } 
    5872 
    59         var postUrl = serviceURL; 
    60         if (stubbles.json.rpc.appendToURL !== null) { 
    61             postUrl = postUrl + stubbles.json.rpc.appendToURL; 
    62         } 
    63      
    6473        var jsonRpcReq = { 
    6574            method: classAndMethod, 
     
    6776            id: id 
    6877        }; 
    69      
    70         YAHOO.util.Connect.asyncRequest('POST', postUrl, this.callback, jsonRpcReq.toJSONString()); 
     78 
     79        YAHOO.util.Connect.asyncRequest('POST', stubbles.json.rpc.serviceUrl, callback, jsonRpcReq.toJSONString()); 
    7180        reqRespMapping.push(jsonRpcReq); 
    7281        return id; 
  • trunk/src/main/php/net/stubbles/service/jsonrpc/util/stubJsonRpcProxyGenerator.php

    r791 r1165  
    2020{ 
    2121    /** 
    22      * URL of the service 
    23      * 
    24      * @var  string 
    25      */ 
    26     protected $serviceURL; 
    27  
    28     /** 
    29      * create a new generator 
    30      * 
    31      * @param  string  $serviceURL 
    32      */ 
    33     public function __construct($serviceURL) 
    34     { 
    35         $this->serviceURL = $serviceURL; 
    36     } 
    37  
    38     /** 
    3922     * generate JS proxy for a specified class 
    4023     * 
     
    4427     * @throws  stubClassNotFoundException 
    4528     */ 
    46     public function generateJavascriptProxy($className, $jsClass = null
     29    public function generateJavascriptProxy($className, $jsClass = null, $jsNamespace = 'stubbles.json.proxy'
    4730    { 
    4831        $clazz = new stubReflectionClass($className); 
     
    5134        } 
    5235 
    53         $jsCode  = "function {$jsClass}(clientObj) {\n"; 
    54         $jsCode .= " this.dispatcher = new stubbles.json.rpc.Client(clientObj, '{$this->serviceURL}');\n"; 
    55         $jsCode .= "}\n"; 
     36        $jsCode  = "{$jsNamespace}.{$jsClass} = function(clientObj) {\n"; 
     37        $jsCode .= "    this.dispatcher = new stubbles.json.rpc.Client(clientObj);\n"; 
     38        $jsCode .= "};\n"; 
    5639 
    5740        foreach ($clazz->getMethods() as $method) { 
     
    6144 
    6245            $methodName = $method->getName(); 
    63             $jsCode .= "{$jsClass}.prototype.{$methodName} = function() {\n"; 
    64             $jsCode .= "  return this.dispatcher.doCall('{$jsClass}.{$methodName}', arguments);\n"; 
    65             $jsCode .= "}\n"; 
     46            $jsCode .= "{$jsNamespace}.{$jsClass}.prototype.{$methodName} = function() {\n"; 
     47            $jsCode .= "    return this.dispatcher.doCall('{$jsClass}.{$methodName}', arguments);\n"; 
     48            $jsCode .= "};\n"; 
    6649        } 
    67          
    6850        return $jsCode; 
    6951    } 
  • trunk/src/main/php/org/stubbles/phing/tasks/stubGenerateJsonRpcProxiesTask.php

    r1153 r1165  
    3535     */ 
    3636    protected $serviceFile; 
     37     
     38    /** 
     39     * Namespace for the generated javascript code 
     40     * 
     41     * @var string 
     42     */ 
     43    protected $javaScriptNamespace; 
    3744 
    3845    /** 
     
    5259    public function setServiceFile($serviceFile) { 
    5360        $this->serviceFile = $serviceFile; 
     61    } 
     62     
     63    /** 
     64     * Set the namespace for the generated javascript code 
     65     * (keeping the gloabl js namespace clean) 
     66     * 
     67     * @param string $namespace 
     68     */ 
     69    public function setJavaScriptNamespace($namespace) { 
     70        $this->javaScriptNamespace = $namespace; 
    5471    } 
    5572 
     
    7895 
    7996        $services = $xjconf->getConfigValue('services'); 
    80         $serviceURL = $xjconf->getConfigValue('service-url'); 
    81         if (empty($serviceURL)) { 
    82             $this->log('Cannot generate JSON-RPC clients.' , Project::MSG_ERR); 
    83             $this->log('No service URL specified in ' . stubConfig::getConfigPath() . '/xml/json-rpc-service.xml.' , Project::MSG_ERR); 
    84             throw new BuildException('No service URL specified in ' . stubConfig::getConfigPath() . '/xml/json-rpc-service.xml.'); 
    85         } 
    86         $generator = new stubJsonRpcProxyGenerator($serviceURL); 
    87         $this->log("Using service URL {$serviceURL}"); 
    88         $fullJsCode = ''; 
     97        $generator = new stubJsonRpcProxyGenerator(); 
     98         
     99        $fullJsCode = "{$this->javaScriptNamespace} = {};\n\n"; 
    89100        foreach ($services as $serviceConfig) { 
    90101            try { 
    91                 $jsCode = $generator->generateJavascriptProxy($serviceConfig['className'], $serviceConfig['name']); 
     102                $jsCode = $generator->generateJavascriptProxy($serviceConfig['className'], $serviceConfig['name'], $this->javaScriptNamespace); 
    92103            } catch (stubClassNotFoundException $e) { 
    93104                $this->log("Cannot generate proxy for {$serviceConfig['className']}, class does not exist.", Project::MSG_ERR); 
  • trunk/src/main/resources/phing/build-stubbles.xml

    r1160 r1165  
    6363        <echo>----------------------------------</echo> 
    6464        <input propertyname="jsonrpc.clients.dir" promptChar=":" defaultValue="genjs">Please supply the directory for generated clients</input> 
    65         <input propertyname="jsonrpc.service.url" promptChar=":" defaultValue="http://localhost/?processor=json-rpc">Please supply the service URL for generated clients</input> 
     65        <input propertyname="jsonrpc.clients.ns" promptChar=":" defaultValue="stubbles.json.proxy">Please supply the JavaScript namespace for the generated proxies (without '.' at the end)</input> 
    6666 
    6767        <append destFile="build.properties" text="jsonrpc.clients.dir=${jsonrpc.clients.dir}${line.separator}"/> 
     68        <append destFile="build.properties" text="jsonrpc.clients.ns=${jsonrpc.clients.ns}"/> 
    6869        <copy file="${stubbles.config.path}/xml/json-rpc-service-dist.xml" tofile="${stubbles.config.path}/xml/json-rpc-service.xml" overwrite="true"> 
    6970          <filterchain> 
     
    260261            <mkdir dir="${project.basedir}/docroot/${jsonrpc.clients.dir}"/> 
    261262            <stubGenerateJsonRpcProxies serviceFile="${project.basedir}/config/xml/json-rpc-service.xml" 
    262                                         targetFolder="${project.basedir}/docroot/${jsonrpc.clients.dir}"/> 
     263                                        targetFolder="${project.basedir}/docroot/${jsonrpc.clients.dir}" 
     264                                        javaScriptNamespace="${jsonrpc.clients.ns}"/> 
    263265          </then> 
    264266          <else> 
  • trunk/src/main/resources/xjconf/json-rpc-service.xml

    r1124 r1165  
    33  <namespace uri="http://stubbles.net/service/json-rpc"> 
    44    <tag name="services" type="array" /> 
    5     <tag name="service-url" type="string" /> 
    65    <tag name="service" type="array" keyAttribute="name"> 
    76      <attribute name="name" type="string" required="true"/> 
  • trunk/src/test/php/net/stubbles/service/jsonrpc/util/stubJsonRpcProxyGeneratorTestCase.php

    r1156 r1165  
    2222    public function testGeneration() 
    2323    { 
    24         $generator = new stubJsonRpcProxyGenerator('http://localhost/'); 
    25         $code = $generator->generateJavascriptProxy('org.stubbles.examples.service.MathService', 'MathService'); 
    26         $expected = "function MathService(clientObj) { 
    27  this.dispatcher = new stubbles.json.rpc.Client(clientObj, 'http://localhost/'); 
    28 } 
    29 MathService.prototype.add = function() { 
    30   return this.dispatcher.doCall('MathService.add', arguments); 
    31 } 
    32 MathService.prototype.throwException = function() { 
    33   return this.dispatcher.doCall('MathService.throwException', arguments); 
    34 } 
    35 "; 
     24        $generator = new stubJsonRpcProxyGenerator(); 
     25        $code = $generator->generateJavascriptProxy('org.stubbles.examples.service.MathService', 'MathService', 'my.js.ns'); 
     26        $expected = "my.js.ns.MathService = function(clientObj) { 
     27    this.dispatcher = new stubbles.json.rpc.Client(clientObj); 
     28}; 
     29my.js.ns.MathService.prototype.add = function() { 
     30    return this.dispatcher.doCall('MathService.add', arguments); 
     31}; 
     32my.js.ns.MathService.prototype.throwException = function() { 
     33    return this.dispatcher.doCall('MathService.throwException', arguments); 
     34}; 
     35";       
    3636        $this->assertEqual($expected, $code); 
    3737    }