KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.test;
23
24 import java.io.Serializable JavaDoc;
25 import java.security.BasicPermission JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.Properties JavaDoc;
28 import javax.naming.CompoundName JavaDoc;
29 import javax.naming.Name JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31
32 /** A javax.naming.Name based key class used as the name attribute
33 by NamespacePermissions.
34
35 @author Scott.Stark@jboss.org
36 @version $Revision: 37406 $
37 */

38 public class PermissionName implements Comparable JavaDoc, Serializable JavaDoc
39 {
40     /** The Properties used for the project directory heirarchical names */
41     static Name JavaDoc emptyName;
42     static Properties JavaDoc nameSyntax = new Properties JavaDoc();
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 JavaDoc("", nameSyntax);
50         }
51         catch(NamingException JavaDoc e)
52         {
53         }
54     }
55     private Name JavaDoc name;
56
57     /** An alternate PermissionName comparator that first orders names by
58         length(longer names before shorter names) to ensure that the most
59         precise names are seen first.
60     */

61     public static class NameLengthComparator implements Comparator JavaDoc
62     {
63         public int compare(Object JavaDoc o1, Object JavaDoc o2)
64         {
65             PermissionName p1 = (PermissionName) o1;
66             PermissionName p2 = (PermissionName) o2;
67             // if p1 is longer than p2, its < p2 -> < 0
68
int compare = p2.size() - p1.size();
69             if( compare == 0 )
70                 compare = p1.compareTo(p2);
71             return compare;
72         }
73     }
74
75     /** Creates new NamespacePermission */
76     public PermissionName(String JavaDoc name) throws IllegalArgumentException JavaDoc
77     {
78         try
79         {
80             this.name = new CompoundName JavaDoc(name, nameSyntax);
81         }
82         catch(NamingException JavaDoc e)
83         {
84             throw new IllegalArgumentException JavaDoc(e.toString(true));
85         }
86     }
87     public PermissionName(Name JavaDoc name)
88     {
89         this.name = name;
90     }
91
92     public int compareTo(Object JavaDoc obj)
93     {
94         PermissionName pn = (PermissionName) obj;
95         /* Each level must be compared. The first level to not be equals
96          determines the ordering of the names.
97         */

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 JavaDoc atom0 = name.get(n);
103             String JavaDoc atom1 = pn.name.get(n);
104             compare = atom0.compareTo(atom1);
105         }
106         return compare;
107     }
108
109     public boolean equals(Object JavaDoc 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 JavaDoc toString()
131     {
132         return name.toString();
133     }
134 }
135
Popular Tags