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);
用户服务时带有权限的服务,例如UserManagerService服务中的方法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 {
}
| 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);
}
....