📄 debugaccessguard.java
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- * * $RCSFile$ $Revision: 1.7 $ $Date: 2006/02/01 00:20:31 $ * * Copyright (c) 2004 Autonomy Corp. All Rights Reserved. * Permission to use, copy, modify, and distribute this file is hereby * granted without fee, provided that the above copyright notice appear * in all copies. */import java.util.*;/** * AccessGuard which generates debugging messages for each * security decision. * <p> * This sample file is useful for debugging your secure search * implementation. <code>GuardingSearchable</code> spawns * multiple Threads which invoke the <code>AccessGuard</code> * to make security decisions on <code>SearchResult</code>s. * <p> * This specialization of <code>AccessGuard</code> records * diagnostic messages for decision made. * <p> * To fetch the diagnostic messages, use <code>getDebugInfo</code>. * <p> * <blockquote><pre><code> * DebugAccessGuard userGuard = new DebugAccessGuard(); * ... * userGuard.clearDebugInfo(); * SearchResultList srl = secureSearchable.search(userQuery); * ... Display the visible search results ... * log("Here's the results of document access checks: " * + userGuard.getDebugInfo()); * </code></pre></blockquote> * * @since XPA2.2 * @see SSOSearchServlet.SearchRequest#showDebugServletSpecific */public class DebugAccessGuard extends SSOSearchServletAccessGuard{ private StringBuffer messages = new StringBuffer(2046); private long first_check_start = 0; private long last_check_finish = 0; private int results[] = {0,0,0}; public DebugAccessGuard(String[] hdrs) { super(hdrs); }; public void clearDebugInfo() { messages.setLength(0); first_check_start = 0; last_check_finish = 0; results[0] = results[1] = results[2] = 0; } public String getDebugInfo() { String msg = ""; if (last_check_finish != 0) { long elapsed = last_check_finish - first_check_start; msg = ("Security took: " + leftPad(elapsed,4) + "ms. " + getCacheSize() + " results cached. " + "Approve: " + leftPad(results[0],3) + " Deny: " + leftPad(results[1],3) + " Other: " + leftPad(results[2],3) + "\n"); } synchronized (messages) { return msg + messages.toString(); } } private void showMap(StringBuffer sb, Map map) { if (map==null) { sb.append("null map\n"); return; } Iterator i = map.entrySet().iterator(); while (i.hasNext()) { Map.Entry me = (Map.Entry) i.next(); sb.append( me.getKey().toString() ); sb.append( " = " ); sb.append( me.getValue().toString() ); sb.append( "\n" ); } } public void setHTTPHeaders(Map newHeaders) { int initialSize = getCacheSize(); super.setHTTPHeaders(newHeaders); int currentSize = getCacheSize(); if (currentSize==0 && messages!=null) { StringBuffer msg = new StringBuffer(1024); msg.append("setHTTPHeaders: cache had " ); msg.append(initialSize); msg.append(" entries before clear.\n"); msg.append("Current headers:\n" ); showMap(msg,getHTTPHeaders()); msg.append("\n"); synchronized (messages) { messages.append(msg.toString()); } } } public int getCacheSize() { return cacheMap().size(); } public Object getCachedResult(Object obj) { return cacheMap().get(getKey(obj)); } private String leftPad(long val, int width) { String res = "" + val; StringBuffer sb = new StringBuffer(width); while (res.length() + sb.length() < width) sb.append(' '); sb.append(res); return sb.toString(); } public void checkGuard(Object obj) throws SecurityException { String msg = " Unknown: "; long start_time = System.currentTimeMillis(); int result = 0; if (first_check_start == 0) { synchronized (this) { if (first_check_start == 0) first_check_start = start_time; } } String key = getKey(obj); boolean inCache = cacheMap().containsKey(key); try { try { super.checkGuard(obj); msg = " Approved: "; } catch (SecurityException exc) { msg = " Denied : " + exc.getMessage(); result = 1; throw exc; } catch (RuntimeException exc) { msg = " Other : " + exc; result = 2; throw exc; } } finally { long end_time = System.currentTimeMillis(); long elapsed = end_time - start_time; if (inCache && elapsed < 15) msg = " (c) " + msg + " " + key + "\n"; else msg = leftPad(elapsed,4) + "m" + msg + " " + key +"\n"; synchronized (messages) { messages.append(msg); results[result]++; if (last_check_finish < end_time) last_check_finish = end_time; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -