当前页面: 开发资料首页 → J2EE 专题 → Petstore源码追踪记(3)-商业逻辑处理(四)
摘要:
public class SignOnEJB implements SessionBean {以下略...
private static final String USER_HOME_ENV_NAME =
"java:comp/env/ejb/local/User";
private InitialContext ic = null;
private UserLocalHome ulh = null;
public void ejbCreate() throws CreateException {
try {
ic = new InitialContext();
//取得UserLocalHome Reference,它是代表使用者基本资料的Local Entity Bean
ulh = (UserLocalHome) ic.lookup(USER_HOME_ENV_NAME);
} catch (NamingException ne) {
throw new EJBException("SignOnEJB Got naming exception! " +
ne.getMessage());
}
}
/**
*此函数由SignOnFilter呼叫,依使用者帐号找出对应的User实体
*(instance),然后呼叫User实体的密码比对函数-user.matchPassword()
* business method used to check if a user is allowed to sign on
*/
public boolean authenticate(String userName, String password) {
//请加入侦察程序代码,方便稍候程序验证
System.out.println("SignOnEJB执行authenticate()进行使用者验证
userName="+userName+", password="+password);
try {
UserLocal user = ulh.findByPrimaryKey(userName);
return user.matchPassword(password);
} catch (FinderException fe) {
return false; // User not found, so authentication failed.
}
}
public boolean matchPassword(String password) {
//请加入侦察程序代码,方便稍候程序验证
System.out.println("UserEJB执行matchPassword()进行密码比对");
return password.equals(getPassword());
}
boolean signedOn = false;
if (hreq.getSession().getAttribute(SIGNED_ON_USER) != null) {
signedOn
=((Boolean)hreq.getSession().getAttribute(SIGNED_ON_USER)).booleanValue();
//加入侦察码
System.out.println("signedOn="+signedOn);
} else {
hreq.getSession().setAttribute(SIGNED_ON_USER, new Boolean(false));
}
// jump to the resource if signed on
//若已登入过,则结束此Filter工作,进入Filter chain,以本例来说,它为
Filter chain中最后一个Filter,所以就是不做任何事,让使用者进入他的目的画面
if (signedOn) {
//加入侦察码
System.out.println("使用者已登入过!");
chain.doFilter(request,response);
return;
}
参考前面叙述重新编译部署后执行,可得下图预期结果:
图20 第三阶段程序验证结果
customer.do
到这里相信读者能对Petstore登入流程控管有更深入的了解,通过登入流程就到达了使用者基本数据浏览画面(customer.do),*.do与前二篇介绍的*.screen不同,*.screen代表的是一个画面,如main.screen代表首页,它可由多个.jsp所组成;*.do代表的是一个动作,customer.do代表对使用者基本资料相关动作,如新增、修改、删除,它会透过EJB tier与资料库互动,最后得到的结果也是要展现,运用*.screen的机制组成结果画面,所以我们可以这样想象*.screen是名词,*.do是动词,以本例来说,只是读取资料,虽然运用到*.do,但并没有任何动作,为了能让读者了解整个架构,还是在此稍事说明。
请开?deploytool,点选左边窗格Files > Applications > PetstoreEAR > PetstoreWAR > MainServlet,选择右边 Alias页,可发现处理*.do即是MainServlet
图21 *.do对应MainServlet
点选General页可找到实际对应类别,源码在
Petstore_home\src\waf\src\controller\com\sun\j2ee\blueprints\waf\controller\web\MainServlet.java
,在约79列可找到初始函数:
public void init(ServletConfig config) throws ServletException {
//读取预设语系,值为”en_US”
String defaultLocaleString = config.getInitParameter("default_locale");
defaultLocale = I18nUtil.getLocaleFromString(defaultLocaleString);
this.context = config.getServletContext();
String requestMappingsURL = null;
try {
//读取mapping.xml
requestMappingsURL =
context.getResource("/WEB-INF/mappings.xml").toString();
} catch (java.net.MalformedURLException ex) {
System.err.println("MainServlet: initializing ScreenFlowManager
malformed URL exception: " + ex);
}
//将mappings.xml转成HashMap类别并存入ServletContext
urlMappings = URLMappingsXmlDAO.loadRequestMappings(requestMappingsURL);
context.setAttribute(WebKeys.URL_MAPPINGS, urlMappings);
eventMappings = URLMappingsXmlDAO.loadEventMappings(requestMappingsURL);
context.setAttribute(WebKeys.EVENT_MAPPINGS, eventMappings);
//ScreenFlowManager初始化并存入ServletContext
getScreenFlowManager();
//RequestProcessor初始化并存入ServletContext
getRequestProcessor();
}
这一段卷标(tag)有三个参数:com.sun.j2ee.blueprints.petstore.controller.web.actions.CustomerHTMLAction