1 7 package org.jboss.test; 8 9 import java.io.Serializable ; 10 import java.security.BasicPermission ; 11 import java.util.Comparator ; 12 import java.util.Properties ; 13 import javax.naming.CompoundName ; 14 import javax.naming.Name ; 15 import javax.naming.NamingException ; 16 17 23 public class PermissionName implements Comparable , Serializable 24 { 25 static final long serialVersionUID = 358449172612757607L; 26 27 static Name emptyName; 28 static Properties nameSyntax = new Properties (); 29 static 30 { 31 nameSyntax.put("jndi.syntax.direction", "left_to_right"); 32 nameSyntax.put("jndi.syntax.separator", "/"); 33 try 34 { 35 emptyName = new CompoundName ("", nameSyntax); 36 } 37 catch(NamingException e) 38 { 39 } 40 } 41 private Name name; 42 43 47 public static class NameLengthComparator implements Comparator 48 { 49 public int compare(Object o1, Object o2) 50 { 51 PermissionName p1 = (PermissionName) o1; 52 PermissionName p2 = (PermissionName) o2; 53 int compare = p2.size() - p1.size(); 55 if( compare == 0 ) 56 compare = p1.compareTo(p2); 57 return compare; 58 } 59 } 60 61 62 public PermissionName(String name) throws IllegalArgumentException 63 { 64 try 65 { 66 this.name = new CompoundName (name, nameSyntax); 67 } 68 catch(NamingException e) 69 { 70 throw new IllegalArgumentException (e.toString(true)); 71 } 72 } 73 public PermissionName(Name name) 74 { 75 this.name = name; 76 } 77 78 public int compareTo(Object obj) 79 { 80 PermissionName pn = (PermissionName) obj; 81 84 int compare = name.size() - pn.name.size(); 85 int length = Math.min(name.size(), pn.name.size()); 86 for(int n = 0; compare == 0 && n < length; n ++) 87 { 88 String atom0 = name.get(n); 89 String atom1 = pn.name.get(n); 90 compare = atom0.compareTo(atom1); 91 } 92 return compare; 93 } 94 95 public boolean equals(Object obj) 96 { 97 return compareTo(obj) == 0; 98 } 99 100 public int hashCode() 101 { 102 return name.hashCode(); 103 } 104 105 public int size() 106 { 107 return name.size(); 108 } 109 110 public boolean isParent(PermissionName childName) 111 { 112 boolean isParent = childName.name.startsWith(name); 113 return isParent; 114 } 115 116 public String toString() 117 { 118 return name.toString(); 119 } 120 } 121 | Popular Tags |