1 22 package org.jboss.test.security.test; 23 24 import java.io.Serializable ; 25 import java.security.BasicPermission ; 26 import java.util.Comparator ; 27 import java.util.Properties ; 28 import javax.naming.CompoundName ; 29 import javax.naming.Name ; 30 import javax.naming.NamingException ; 31 32 38 public class PermissionName implements Comparable , Serializable 39 { 40 41 static Name emptyName; 42 static Properties nameSyntax = new Properties (); 43 static 44 { 45 nameSyntax.put("jndi.syntax.direction", "left_to_right"); 46 nameSyntax.put("jndi.syntax.separator", "/"); 47 try 48 { 49 emptyName = new CompoundName ("", nameSyntax); 50 } 51 catch(NamingException e) 52 { 53 } 54 } 55 private Name name; 56 57 61 public static class NameLengthComparator implements Comparator 62 { 63 public int compare(Object o1, Object o2) 64 { 65 PermissionName p1 = (PermissionName) o1; 66 PermissionName p2 = (PermissionName) o2; 67 int compare = p2.size() - p1.size(); 69 if( compare == 0 ) 70 compare = p1.compareTo(p2); 71 return compare; 72 } 73 } 74 75 76 public PermissionName(String name) throws IllegalArgumentException 77 { 78 try 79 { 80 this.name = new CompoundName (name, nameSyntax); 81 } 82 catch(NamingException e) 83 { 84 throw new IllegalArgumentException (e.toString(true)); 85 } 86 } 87 public PermissionName(Name name) 88 { 89 this.name = name; 90 } 91 92 public int compareTo(Object obj) 93 { 94 PermissionName pn = (PermissionName) obj; 95 98 int compare = name.size() - pn.name.size(); 99 int length = Math.min(name.size(), pn.name.size()); 100 for(int n = 0; compare == 0 && n < length; n ++) 101 { 102 String atom0 = name.get(n); 103 String atom1 = pn.name.get(n); 104 compare = atom0.compareTo(atom1); 105 } 106 return compare; 107 } 108 109 public boolean equals(Object obj) 110 { 111 return compareTo(obj) == 0; 112 } 113 114 public int hashCode() 115 { 116 return name.hashCode(); 117 } 118 119 public int size() 120 { 121 return name.size(); 122 } 123 124 public boolean isParent(PermissionName childName) 125 { 126 boolean isParent = childName.name.startsWith(name); 127 return isParent; 128 } 129 130 public String toString() 131 { 132 return name.toString(); 133 } 134 } 135 | Popular Tags |