KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > PermissionName


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test;
8
9 import java.io.Serializable JavaDoc;
10 import java.security.BasicPermission JavaDoc;
11 import java.util.Comparator JavaDoc;
12 import java.util.Properties JavaDoc;
13 import javax.naming.CompoundName JavaDoc;
14 import javax.naming.Name JavaDoc;
15 import javax.naming.NamingException JavaDoc;
16
17 /** A javax.naming.Name based key class used as the name attribute
18 by NamespacePermissions.
19
20 @author Scott.Stark@jboss.org
21 @version $Revision: 1.4.6.2 $
22 */

23 public class PermissionName implements Comparable JavaDoc, Serializable JavaDoc
24 {
25    static final long serialVersionUID = 358449172612757607L;
26     /** The Properties used for the project directory heirarchical names */
27     static Name JavaDoc emptyName;
28     static Properties JavaDoc nameSyntax = new Properties JavaDoc();
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 JavaDoc("", nameSyntax);
36         }
37         catch(NamingException JavaDoc e)
38         {
39         }
40     }
41     private Name JavaDoc name;
42
43     /** An alternate PermissionName comparator that first orders names by
44         length(longer names before shorter names) to ensure that the most
45         precise names are seen first.
46     */

47     public static class NameLengthComparator implements Comparator JavaDoc
48     {
49         public int compare(Object JavaDoc o1, Object JavaDoc o2)
50         {
51             PermissionName p1 = (PermissionName) o1;
52             PermissionName p2 = (PermissionName) o2;
53             // if p1 is longer than p2, its < p2 -> < 0
54
int compare = p2.size() - p1.size();
55             if( compare == 0 )
56                 compare = p1.compareTo(p2);
57             return compare;
58         }
59     }
60
61     /** Creates new NamespacePermission */
62     public PermissionName(String JavaDoc name) throws IllegalArgumentException JavaDoc
63     {
64         try
65         {
66             this.name = new CompoundName JavaDoc(name, nameSyntax);
67         }
68         catch(NamingException JavaDoc e)
69         {
70             throw new IllegalArgumentException JavaDoc(e.toString(true));
71         }
72     }
73     public PermissionName(Name JavaDoc name)
74     {
75         this.name = name;
76     }
77
78     public int compareTo(Object JavaDoc obj)
79     {
80         PermissionName pn = (PermissionName) obj;
81         /* Each level must be compared. The first level to not be equals
82          determines the ordering of the names.
83         */

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 JavaDoc atom0 = name.get(n);
89             String JavaDoc atom1 = pn.name.get(n);
90             compare = atom0.compareTo(atom1);
91         }
92         return compare;
93     }
94
95     public boolean equals(Object JavaDoc 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 JavaDoc toString()
117     {
118         return name.toString();
119     }
120 }
121
Popular Tags