Poetlet 服务封装了portlet 中的可重用的商业和逻辑操作。Portlet 服务有利于将逻辑操作最小化,便于重用。另一方面,portlet 服务还有下面的好处:
内建的持久化框架有有利于将对象持久化到关系数据库中。
内建的访问控制权限有利于安全控制
集成测试框架
Spirit 服务接口借用了websphere 的接口。下面时服务的UML 图:
<!----><!----> <!---->
PortletServiceFactory 负责产生、初始化、销毁portlet 服务。 PortletService 定义了一个空的服务接口,而 PortletServiceProvider 定义了增加了生命周期,当服务初始化和销毁的时候调用init 和destroy 方法。服务描述文件定义了服务和初始化参数。通过 PortletServiceConfig 这个类可以得到初始化参数。
一个portlet 可以通过PortletContext 对象得到服务。getService() 方法返回一个服务对象PortletService 。
ExampleService = (ExampleService)getPortletConfig().getContext().getService(ExampleService.class);
用户服务时带有权限的服务,例如 UserMa nagerService 服务中的方法deleteUser(User user) 只有超级用户才能执行,其他用户才会抛出PortletServiceAuthorizationException 异常。
核心portlet 服务
Spirit 包含几个核心服务,这些服务有基本的portlet 所调用。仅有LoginService 不是用户服务。UserManagerService 和AccessControlService 都是用户服务。
Portlet 服务举例
下面列举一个股票报价的服务 StockQuoteService , 这个服务只有一个方法 getStockQuotes , 返回从quote.com 服务得到的股票报价列表。
定义服务
在WEB-INF/PortletServices.xml 文件中定义一个服务实体:
...
<service>
<name>Stock Quote Service</name>
<description>Provides Stock Quotes</description>
<interface>org.myorg.services.StockQuoteService</interface>
<implementation>org.myorg.services.impl.StockQuoteServiceImpl</implementation>
<service-config>
<param-name>quote_service</param-name>
<param-value>quote.com</param-value>
</service-config>
</service>
定义服务接口
package org.myorg.services.StockQuoteService;
import com.kuaff.spirit.portlet.service;
import java.util.List;
public interface StockQuoteService extends PortletService { <!----><!----> <!---->
public List getStockQuotes(); <!----><!----> <!---->
}
<!----><!----> <!----> |
StockQuoteService 必须继承PortletService |
<!----><!----> <!----> |
定义了一个方法:getStockQuotes |
定义服务实现
package org.myorg.services.impl.StockQuoteServiceImpl;
import org.myorg.services.StockQuoteService;
import com.kuaff.spirit.portlet.service.*;
import com.kuaff.spirit.portlet.service.spi.PortletServiceProvider;
import java.util.List;
public class StockQuoteServiceImpl implements PortletServiceProvider, StockQuoteService {
public void init(PortletServiceConfig config)
throws PortletServiceUnavailableException {
// 初始化
}
// 销毁
}
public List getStockQuotes() {
// 得到股票报价列表
}
}
Portlet 中访问服务
package org.myorg.portlets.StockQuotePortlet;
// 这里忽略了import
public class StockQuotePortlet extends AbstractPortlet {
public void actionPerformed(ActionEvent event) {
PortletContext ctx = getPortletConfi().getContext();
StockQuoteService quoteService = null;
try {
StockQuoteService quoteService = (StockQuoteService)ctx.getService(StockQuoteService.class);
} catch (PortletServiceException e) {
getPortletLog().error("Couldn't get quote service instance", e);
return;
}
// 得到股票报价列表,并放到request 中
List quotes = quoteService.getStockQuotes();
req.setAttribute("stocks", quotes);
}
public void doView(PortletRequest req, PortletResponse res) {
// 显示股票报价
List quotes = req.getAttribute("stocks");
...
}
}
用户服务举例
还是以上一个例子为例,假定只有管理员可以配置股票,这样 StockQuoteService 将变成了用户服务。
定义用户服务
仅仅增加了 user-required 元素
<service>
<name>Stock Quote Service</name>
<user-required>true</user-required>
<description>Provides Stock Quotes</description>
<interface>org.myorg.services.StockQuoteService</interface>
<implementation>org.myorg.services.impl.StockQuoteServiceImpl</implementation>
<service-config>
<param-name>quote_service</param-name>
<param-value>quote.com</param-value>
</service-config>
</service>
必须设置<user-required>true</user-required>
编辑用户接口
...
setQuoteService(String quoteService);
....
定义用户服务实现
package org.myorg.services.impl.StockQuoteServiceFactory;
import org.myorg.services.StockQuoteService;
import com.kuaff.spirit.portlet.service.*;
import com.kuaff.spirit.portlet.service.spi.PortletServiceProvider;
import java.util.List;
public class StockQuoteServiceFactory implements PortletServiceProvider, StockQuoteService {
// 产生单例
private instance = new StockQuoteFactory();
private StockQuoteServiceFactory() {}
public static StockQuoteServiceFactory getInstance() {
return instance;
}
public void init(PortletServiceConfig config)
throws PortletServiceUnavailableException {
// 初始化
}
public void destroy() {
// 销毁
}
public List getStockQuotes() {
// 得到股票列表
}
public void setStockQuoteService(String quoteService) {
// 设置股票服务
}
}
下一步就是对 StockQuoteServiceImpl 增加权限检查。
package org.myorg.services.impl.StockQuoteServiceImpl;
import org.myorg.services.StockQuoteService;
import com.kuaff.spirit.portlet.services.*;
import com.kuaff.spirit.portlet.services.spi.PortletServiceProvider;
import java.util.List;
public class StockQuoteServiceImpl implements PortletServiceProvider, StockQuoteService {
private StockQuoteServiceFactory quoteServiceFactory = null;
private PortletServiceAuthorizer authorizer = null;
public UserManagerServiceImpl(PortletServiceAuthorizer authorizer) {
this.authorizer = authorizer;
}
public void init(PortletServiceConfig config)
throws PortletServiceUnavailableException {
// 使用StockQuoteServiceFactory 配置
quoteServiceFactory = StockQuoteServiceFactory.getInstance();
quoteServiceFactory.init();
}
public void destroy() {
// 销毁
quoteServiceFactory.destroy();
}
public List getStockQuotes() {
quoteServiceFactory.getStockQuotes();
}
public void setStockQuoteService(String quoteService) { <!----><!----> <!---->
// 设置股票服务
// 检查权限
authorizer.authorizeSuperUser();
quoteServiceFactory.setStockQuoteService(quoteService);
}
}
Portlet 中访问用户服务
...
public void actionPerformed(ActionEvent event) {
PortletContext ctx = getPortletConfig().getContext();
PortletRequest req = event.getPortletRequest();
User user = req.getUser();
StockQuoteService quoteService = null;
try {
StockQuoteService quoteService = (StockQuoteService)ctx.getService(StockQuoteService.class, user);
} catch (PortletServiceException e) {
getPortletLog().error(" 不能得到服务实例", e);
return;
List quotes = quoteService.getStockQuotes();
req.setAttribute("stocks", quotes);
}