评论员写道:
Tomcat 中有一些不错的“大于号”代码。需要健康剂量的 (>>=)。
从 Apache Tomcat查看AuthenticatorBase.java 类时:
/**
* Enforce the security restrictions in the web application deployment
* descriptor of our associated Context.
*
* @param request Request to be processed
* @param response Response to be processed
*
* @exception IOException if an input/output error occurs
* @exception ServletException if thrown by a processing element
*/
@Override
public void invoke(Request request, Response response)
throws IOException, ServletException {
if (log.isDebugEnabled())
log.debug("Security checking request " +
request.getMethod() + " " + request.getRequestURI());
LoginConfig config = this.context.getLoginConfig();
// Have we got a cached authenticated Principal to record?
if (cache) {
Principal principal = request.getUserPrincipal();
if (principal == null) {
Session session = request.getSessionInternal(false);
if (session != null) {
principal = session.getPrincipal();
if (principal != null) {
if (log.isDebugEnabled())
log.debug("We have cached auth type " +
session.getAuthType() +
" for principal " +
session.getPrincipal());
request.setAuthType(session.getAuthType());
request.setUserPrincipal(principal);
}
}
}
}
我不得不承认,我错过了如何应用它。我知道有一种方法可以将 if-tree 重构为 monadic 绑定,但我不知道该怎么做。
假设:
- 这与语言无关,而与逻辑结构有关。你可以在 Haskell、Scala 或 Clojure 中表示这个 if-tree,它仍然会表示相同的 if-logic。
我的问题是:如何使用 Monadic Bind 简化这个 Apache Tomcat 代码?