Changeset 1165
- Timestamp:
- 12/20/07 11:13:17 (7 months ago)
- Files:
-
- trunk/config/xml/json-rpc-service.xml (modified) (1 diff)
- trunk/src/main/javascript/stub-json-rpc-debug.js (modified) (6 diffs)
- trunk/src/main/javascript/stub-json-rpc.js (modified) (6 diffs)
- trunk/src/main/php/net/stubbles/service/jsonrpc/util/stubJsonRpcProxyGenerator.php (modified) (4 diffs)
- trunk/src/main/php/org/stubbles/phing/tasks/stubGenerateJsonRpcProxiesTask.php (modified) (3 diffs)
- trunk/src/main/resources/phing/build-stubbles.xml (modified) (2 diffs)
- trunk/src/main/resources/xjconf/json-rpc-service.xml (modified) (1 diff)
- trunk/src/test/php/net/stubbles/service/jsonrpc/util/stubJsonRpcProxyGeneratorTestCase.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/config/xml/json-rpc-service.xml
r1156 r1165 3 3 xmlns:xj="http://xjconf.net/XJConf" 4 4 xmlns="http://stubbles.net/service/json-rpc"> 5 <service-url>${jsonrpc.service.url}</service-url>6 5 <services> 7 6 <service name="MathService" className="org.stubbles.examples.service.MathService"/> trunk/src/main/javascript/stub-json-rpc-debug.js
r1085 r1165 4 4 stubbles.json = {}; 5 5 stubbles.json.rpc = {}; 6 stubbles.json.rpc.appendToURL = null; 6 stubbles.json.rpc.appendToUrl = null; 7 stubbles.json.rpc.serviceUrl = null; 8 7 9 /** 8 10 * Stubbles JSON-RPC client … … 15 17 * @author Stephan Schmidt <schst@stubbles.net> 16 18 */ 17 stubbles.json.rpc.Client = function(clientObj, serviceURL) { 19 20 /** 21 * constuctor 22 */ 23 stubbles.json.rpc.Client = function(clientObj) { 18 24 var reqRespMapping = []; 19 25 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 = { 27 27 success: function(o) { 28 28 var rpcObj = o.responseText.parseJSON(); … … 32 32 var classAndMethod = reqRespMapping[i].method.split("."); 33 33 console.info("Stubbles::REQID " + rpcObj.id + " :: Calling callback method callback__" + classAndMethod[1] + "("+rpcObj.id+", "+rpcObj.result+", "+rpcObj.error+")"); 34 var methodName = 'callback__' + classAndMethod[1]; 34 35 35 36 // call callback method on clientObj 36 var methodName = 'callback__' + classAndMethod[1];37 37 clientObj[methodName].call(clientObj, rpcObj.id, rpcObj.result, rpcObj.error); 38 38 return; … … 52 52 }; 53 53 54 this.createId = function() { 55 var d = new Date(); 56 var id = d.getHours() +''+ d.getMinutes() +''+ d.getMilliseconds(); 57 return id; 58 }; 59 54 60 this.doCall = function(classAndMethod, args) { 55 61 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 } 56 70 57 71 // because args is an array-like object and … … 61 75 } 62 76 63 var postUrl = serviceURL;64 if (stubbles.json.rpc.appendToURL !== null) {65 postUrl = postUrl + stubbles.json.rpc.appendToURL;66 }67 68 77 var jsonRpcReq = { 69 78 method: classAndMethod, … … 71 80 id: id 72 81 }; 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()); 75 84 console.info("Stubbles::REQID " + id + " :: Calling method " + classAndMethod); 76 85 reqRespMapping.push(jsonRpcReq); 77 86 return id; 78 };87 }; 79 88 }; trunk/src/main/javascript/stub-json-rpc.js
r1085 r1165 4 4 stubbles.json = {}; 5 5 stubbles.json.rpc = {}; 6 stubbles.json.rpc.appendToURL = null; 6 stubbles.json.rpc.appendToUrl = null; 7 stubbles.json.rpc.serviceUrl = null; 8 7 9 /** 8 10 * Stubbles JSON-RPC client … … 15 17 * @author Stephan Schmidt <schst@stubbles.net> 16 18 */ 17 stubbles.json.rpc.Client = function(clientObj, serviceURL) { 19 20 /** 21 * constuctor 22 */ 23 stubbles.json.rpc.Client = function(clientObj) { 18 24 var reqRespMapping = []; 19 25 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 = { 27 27 success: function(o) { 28 28 var rpcObj = o.responseText.parseJSON(); … … 32 32 var classAndMethod = reqRespMapping[i].method.split("."); 33 33 var methodName = 'callback__' + classAndMethod[1]; 34 34 35 35 // call callback method on clientObj 36 36 clientObj[methodName].call(clientObj, rpcObj.id, rpcObj.result, rpcObj.error); … … 48 48 }; 49 49 50 this.createId = function() { 51 var d = new Date(); 52 var id = d.getHours() +''+ d.getMinutes() +''+ d.getMilliseconds(); 53 return id; 54 }; 55 50 56 this.doCall = function(classAndMethod, args) { 51 57 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 } 52 66 53 67 // because args is an array-like object and … … 57 71 } 58 72 59 var postUrl = serviceURL;60 if (stubbles.json.rpc.appendToURL !== null) {61 postUrl = postUrl + stubbles.json.rpc.appendToURL;62 }63 64 73 var jsonRpcReq = { 65 74 method: classAndMethod, … … 67 76 id: id 68 77 }; 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()); 71 80 reqRespMapping.push(jsonRpcReq); 72 81 return id; trunk/src/main/php/net/stubbles/service/jsonrpc/util/stubJsonRpcProxyGenerator.php
r791 r1165 20 20 { 21 21 /** 22 * URL of the service23 *24 * @var string25 */26 protected $serviceURL;27 28 /**29 * create a new generator30 *31 * @param string $serviceURL32 */33 public function __construct($serviceURL)34 {35 $this->serviceURL = $serviceURL;36 }37 38 /**39 22 * generate JS proxy for a specified class 40 23 * … … 44 27 * @throws stubClassNotFoundException 45 28 */ 46 public function generateJavascriptProxy($className, $jsClass = null )29 public function generateJavascriptProxy($className, $jsClass = null, $jsNamespace = 'stubbles.json.proxy') 47 30 { 48 31 $clazz = new stubReflectionClass($className); … … 51 34 } 52 35 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"; 56 39 57 40 foreach ($clazz->getMethods() as $method) { … … 61 44 62 45 $methodName = $method->getName(); 63 $jsCode .= "{$js Class}.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"; 66 49 } 67 68 50 return $jsCode; 69 51 } trunk/src/main/php/org/stubbles/phing/tasks/stubGenerateJsonRpcProxiesTask.php
r1153 r1165 35 35 */ 36 36 protected $serviceFile; 37 38 /** 39 * Namespace for the generated javascript code 40 * 41 * @var string 42 */ 43 protected $javaScriptNamespace; 37 44 38 45 /** … … 52 59 public function setServiceFile($serviceFile) { 53 60 $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; 54 71 } 55 72 … … 78 95 79 96 $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"; 89 100 foreach ($services as $serviceConfig) { 90 101 try { 91 $jsCode = $generator->generateJavascriptProxy($serviceConfig['className'], $serviceConfig['name'] );102 $jsCode = $generator->generateJavascriptProxy($serviceConfig['className'], $serviceConfig['name'], $this->javaScriptNamespace); 92 103 } catch (stubClassNotFoundException $e) { 93 104 $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 63 63 <echo>----------------------------------</echo> 64 64 <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> 66 66 67 67 <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}"/> 68 69 <copy file="${stubbles.config.path}/xml/json-rpc-service-dist.xml" tofile="${stubbles.config.path}/xml/json-rpc-service.xml" overwrite="true"> 69 70 <filterchain> … … 260 261 <mkdir dir="${project.basedir}/docroot/${jsonrpc.clients.dir}"/> 261 262 <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}"/> 263 265 </then> 264 266 <else> trunk/src/main/resources/xjconf/json-rpc-service.xml
r1124 r1165 3 3 <namespace uri="http://stubbles.net/service/json-rpc"> 4 4 <tag name="services" type="array" /> 5 <tag name="service-url" type="string" />6 5 <tag name="service" type="array" keyAttribute="name"> 7 6 <attribute name="name" type="string" required="true"/> trunk/src/test/php/net/stubbles/service/jsonrpc/util/stubJsonRpcProxyGeneratorTestCase.php
r1156 r1165 22 22 public function testGeneration() 23 23 { 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 }; 29 my.js.ns.MathService.prototype.add = function() { 30 return this.dispatcher.doCall('MathService.add', arguments); 31 }; 32 my.js.ns.MathService.prototype.throwException = function() { 33 return this.dispatcher.doCall('MathService.throwException', arguments); 34 }; 35 "; 36 36 $this->assertEqual($expected, $code); 37 37 }
