Restlet 2.0 边学边写(三)使用Component发布多个Application
很久没更新这篇博客了,今天继续。
上一次实践是一个Application绑定多个Resource。但是如果Resource多了以后使用一个Application来发布并不合适,而且绑定地址写在代码中也不便于修改。那么应该怎么办呢?
可以使用Component来发布多个Application,每个Application只负责发布自己的Resource。使用Component有两种方法:可以自定义Component类并在web.xml中配置;也可以使用restlet.xml来配置。
参考:http://ajaxcn.iteye.com/blog/416611
本次实践将创建一个Application:CustomerApplication;一个Component:OrderComponent,一个配置文件:restlet.xml。
1.Application
在com.sunny.restlet.order包中创建CustomerApplication类,代码如下:
package com.sunny.restlet.order;import org.restlet.Application;import org.restlet.Restlet;import org.restlet.routing.Router;public class CustomerApplication extends Application {@Overridepublic Restlet createRoot() {// TODO Auto-generated method stubRouter router = new Router(getContext());router.attach("", CustomerResource.class);return router;}}
package com.sunny.restlet.order;import org.restlet.Application;import org.restlet.Restlet;import org.restlet.routing.Router;public class OrderApplication extends Application {@Overridepublic synchronized Restlet createRoot() {Router router = new Router(getContext());router.attach("", OrderResource.class);return router;}}
package com.sunny.restlet.order;import org.restlet.Component;public class OrderComponent extends Component {public OrderComponent() {super();// TODO Auto-generated constructor stubgetDefaultHost().attach("/orders/{orderId}/{subOrderId}", new OrderApplication());getDefaultHost().attach("/customers/{custId}",new CustomerApplication());}}
package com.sunny.restlet.order;import org.restlet.Component;import org.restlet.data.Protocol;public class OrderMain {public static void main(String[] args) throws Exception {Component component = new OrderComponent();component.getServers().add(Protocol.HTTP, 8182);component.start();}}
<!-- OrderApplication --><init-param><param-name>org.restlet.component</param-name><param-value>com.sunny.restlet.order.OrderApplication</param-value></init-param>
<!-- OrderComponent --><init-param><param-name>org.restlet.component</param-name><param-value>com.sunny.restlet.order.OrderComponent</param-value></init-param>
<!-- OrderComponent --><init-param><param-name>org.restlet.component</param-name><param-value>com.sunny.restlet.order.OrderComponent</param-value></init-param>
protected Component createComponent(){ Component component = null; Client warClient = createWarClient(new Context(), getServletConfig()); Response response = warClient.handle(new Request(Method.GET, "war:///WEB-INF/restlet.xml")); if(response.getStatus().isSuccess() && response.isEntityAvailable()) component = new Component(response.getEntity()); if(component == null) { String componentClassName = getInitParameter("org.restlet.component", null); if(componentClassName != null)
<?xml version="1.0"?><component xmlns="http://www.restlet.org/schemas/1.1/Component"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.restlet.org/schemas/1.1/Component"><defaultHost><attach uriPattern="/customers/{custId}"target/><attach uriPattern="/orders/{orderId}/{subOrderId}"target/></defaultHost></component>
package com.sunny.restlet.order;import org.restlet.Application;import org.restlet.Restlet;import org.restlet.routing.Router;public class CustomerApplication extends Application {@Overridepublic Restlet createRoot() {// TODO Auto-generated method stubRouter router = new Router(getContext());router.attach("", CustomerResource.class);router.attach("/orders/{orderId}/{subOrderId}", CustomerResource.class);return router;}}
package com.sunny.restlet.order;import org.restlet.resource.Get;import org.restlet.resource.ResourceException;import org.restlet.resource.ServerResource;public class CustomerResource extends ServerResource {String customerId = "";String orderId = "";String subOrderId = "";@Overrideprotected void doInit() throws ResourceException {this.customerId = (String) getRequest().getAttributes().get("custId");this.orderId = (String) getRequest().getAttributes().get("orderId");this.subOrderId = (String) getRequest().getAttributes().get("subOrderId");}@Getpublic String represent() {return "get customer id :" + customerId + " and the order id is : " + orderId + " and the sub order id is : "+ subOrderId;}}