1 13 package org.ejbca.core.model.approval; 14 15 import java.util.ArrayList ; 16 import java.util.StringTokenizer ; 17 18 import org.apache.log4j.Logger; 19 import org.ejbca.core.model.ca.caadmin.CAInfo; 20 21 67 public class ApprovalExecutorUtil { 68 69 private static final Logger log = Logger.getLogger(ApprovalExecutorUtil.class); 70 71 72 protected static String globallyAllowedString = "@approval.excludedClasses@"; 73 protected static ApprovalOveradableClassName[] globallyAllowed = null; 74 75 85 public static boolean requireApproval(ApprovalRequest req, ApprovalOveradableClassName[] overridableClassNames) { 86 if (req == null) return false; 87 if (log.isDebugEnabled()) { 88 log.debug(">requireApproval: "+req.getClass().getName()); 89 } 90 boolean ret = true; 91 if (req.getNumOfRequiredApprovals() > 0) { 92 ret = !isCalledByOveridableClassnames(getGloballyAllowed()); 93 if (ret && (overridableClassNames != null)) { 95 ret = !isCalledByOveridableClassnames(overridableClassNames); 96 } 97 if (ret && req.isAllowedTransition()) { 99 ret = false; 100 } 101 } else { 102 ret = false; 103 } 104 105 if (log.isDebugEnabled()) { 106 log.debug("<requireApproval: "+ret); 107 } 108 return ret; 109 } 110 111 private static ApprovalOveradableClassName[] getGloballyAllowed() { 112 if (globallyAllowed == null) { 113 ArrayList arr = new ArrayList (); 114 StringTokenizer tokenizer = new StringTokenizer (globallyAllowedString, ",", false); 115 while (tokenizer.hasMoreTokens()) { 116 String t = tokenizer.nextToken(); 117 ApprovalOveradableClassName o = new ApprovalOveradableClassName(t.trim(), null); 118 arr.add(o); 119 } 120 globallyAllowed = (ApprovalOveradableClassName[])arr.toArray(new ApprovalOveradableClassName[0]); 121 } 122 return globallyAllowed; 123 } 124 125 private static boolean isCalledByOveridableClassnames(ApprovalOveradableClassName[] overridableClassNames){ 126 boolean retval = false; 127 try{ 128 throw new Exception (); 129 }catch(Exception e){ 130 if(overridableClassNames != null){ 131 Throwable next = e; 132 while(next != null){ 133 for(int j=0;j< overridableClassNames.length;j++){ 134 retval = overridableClassNames[j].isInStackTrace(e.getStackTrace()); 135 if(retval == true){ 136 break; 137 } 138 } 139 if(retval == true){ 140 break; 141 } 142 next = e.getCause(); 143 } 144 } 145 } 146 147 return retval; 148 } 149 150 157 public static int getNumOfApprovalRequired(int action, CAInfo cainfo) { 158 int retval = 0; 159 if(cainfo.isApprovalRequired(action)){ 160 retval = cainfo.getNumOfReqApprovals(); 161 } 162 163 return retval; 164 } 165 } 166 | Popular Tags |