1 17 18 package org.objectweb.jac.aspects.user; 19 20 import gnu.regexp.RE; 21 import gnu.regexp.REException; 22 import org.objectweb.jac.core.ACManager; 23 import org.objectweb.jac.core.rtti.MemberItem; 24 import org.objectweb.jac.core.rtti.MetaItem; 25 import org.objectweb.jac.util.Log; 26 import org.objectweb.jac.core.rtti.AbstractMethodItem; 27 28 34 35 public class Rule { 36 public static final boolean ALLOW = true; 37 public static final boolean DENY = false; 38 39 boolean allow; 40 String resourceExpression; 41 transient RE regexp; 42 43 49 public Rule(boolean allow, String resourceExpression) { 50 this.allow = allow; 51 this.resourceExpression = resourceExpression; 52 try { 53 this.regexp = new RE(resourceExpression); 54 } catch (REException e) { 55 throw new RuntimeException ("Caught regular expression exception: "+e); 56 } 57 } 58 59 64 public boolean getAllow() { 65 return allow; 66 } 67 68 74 public void setAllow(boolean allow) { 75 this.allow = allow; 76 invalidateCache(); 77 } 78 79 84 public void setResourceExpression(String resourceExpression) { 85 this.resourceExpression = resourceExpression; 86 invalidateCache(); 87 } 88 89 public String getResourceExpression() { 90 return resourceExpression; 91 } 92 93 96 protected RE getRegexp() { 97 if (regexp==null) 98 try { 99 regexp = new RE(resourceExpression==null?"":resourceExpression); 100 } catch (REException e) { 101 } 102 return regexp; 103 } 104 105 110 public boolean match(MetaItem item) { 111 String resourceDescr; 112 if (item instanceof AbstractMethodItem) { 113 AbstractMethodItem method = (AbstractMethodItem)item; 114 resourceDescr = method.getParent().getName()+ 115 "."+method.getFullName(); 116 } else if (item instanceof MemberItem) { 117 resourceDescr = ((MemberItem)item).getParent().getName()+ 118 "."+item.getName(); 119 } else { 120 resourceDescr = item.toString(); 121 } 122 boolean result = getRegexp().isMatch(resourceDescr); 123 if (!result && (item instanceof AbstractMethodItem)) { 124 resourceDescr = ((MemberItem)item).getParent().getName()+ 125 "."+item.getName(); 126 result = getRegexp().isMatch(resourceDescr); 127 } 128 Log.trace("profile.rule",resourceExpression+ 129 (result?" matches ":" does not match ")+resourceDescr); 130 return result; 131 } 132 133 141 protected void invalidateCache() { 142 UserAC ac = (UserAC)ACManager.getACM().getAC("user"); 143 if (ac!=null) 144 ac.invalidateCache(); 145 } 146 } 147 | Popular Tags |