7. 样例
7.1 开发样例程序
创建一个EGL RichUI项目“RuiTesting”,选择“Web2.0 client application withservices”作为模板。
在RuiTesting项目里,创建一个EGL Record,源代码如下:
- record OrderItem
- itemId int{@Id};//{@Id, @GeneratedValue };
- //NAME string{@GeneratedValue};
- name string{ @Column { insertable=true } };
- end
- service HelloService
- function sayHello(name string in) returns(string)
- itemEntity OrderItem;
- //ds SQLDataSource? = new SQLDataSource("jdbc:derby:C:/databases/EGLDerbyR7;create=true");
- ds GAEDataSource? = new GAEDataSource();
- add itemEntity to ds;
- return("Hello World:" + name);
- end
- end
- function serviceButton_onClick(event Event in)
- call helloService.sayHello(nameField.text) returning to handleReturn onException Exceptionhandler;
- end
- function handleReturn(results string in)
- SysLib.writeStdout("Got service result:" + results);
- end
- private function Exceptionhandler( exception AnyException in)
- handleException(exception, null);
- end
- private function handleException( exception AnyException in, srvc string? in)
- SysLib.writeStdout(exception.message);
- end
- public String sayHello(String name) {
- OrderItem itemEntity = new OrderItem();
- GAEDataSource ds;
- ds = new GAEDataSource();
- com.google.appengine.api.datastore.DatastoreService datastore = com.google.appengine.api.datastore.DatastoreServiceFactory.getDatastoreService();
- com.google.appengine.api.datastore.Entity google_OrderItem = new com.google.appengine.api.datastore.Entity( "OrderItem" );
- google_OrderItem.setProperty("itemId",itemEntity.itemId);
- google_OrderItem.setProperty("name",itemEntity.name);
- datastore.put(google_OrderItem);;
- return (("Hello World:") + name);
- }
7.2 部署并在本地运行
Google App Engine的Eclipse插件提供了本地的运行环境。因此,我们在这里介绍一下如何将7.1中开发的样例程序进行部署并在本地运行。
使用EGL Deployment Descriptor Editor来打开RuiTesting/EGLSource/RuiTesting.egldd文件,在Resource Binding标签中,设置URI为http://localhost:8888/restservices/HelloService。
需要注意的是,由于GoogleApp Engine的Eclipse插件所提供的Web Application项目结构并非一个标准的Eclipse下的动态Web项目,为了EGL程序的部署需要,我们需要对GAE的项目结构做一些调整,使得它转换成一个标准的Eclipse动态Web项目。
首先创建一个Google App Engine的项目RuiTestingDeploy,在项目属性的Project Facet中设置DynamicWeb Module的版本为2.4。并设置WAR的目录为WebContent,如图所示。
其次在原有的war目录下,将Google App Engine的部署描述符appengine-web.xml和Log4J的配置文件logging.properties拷贝到WebContent目录下。并删除原有的war目录。
开始部署样例程序到RuiTestingDeploy中。部署完毕以后,修改下列文件:
- src/RuiTestingDeploy-uri.xml。将contextroot元素的值设置为/restservices(如果开发的是Dedicated Service,则忽略这一步)。
- WebContent/RuiTesting.html。将egl.initParams()函数的第一个参数设置为”.”。
在GAE环境中,运行RuiTesting.html,关于怎么在本地运行GAE程序,读者可以参考GAE的官方文档。
8. 下一步
本篇博客只是对EGL中的add语句在GAE平台上的扩展做了初步探讨,接下来需要读者实现对EGL里的其它抽象操作语言(replace, delete和get)的扩展实现,并使用扩展后的基于GAE的EGL实现一个针对数据库访问的应用(前台使用EGLRichUI,后台使用基于GAE的EGL服务),并且需要部署在Google App Engine上。
