001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test;
023:
024: import java.io.Serializable;
025: import java.security.BasicPermission;
026: import java.util.Comparator;
027: import java.util.Properties;
028: import javax.naming.CompoundName;
029: import javax.naming.Name;
030: import javax.naming.NamingException;
031:
032: /** A javax.naming.Name based key class used as the name attribute
033: by NamespacePermissions.
034:
035: @author Scott.Stark@jboss.org
036: @version $Revision: 37390 $
037: */
038: public class PermissionName implements Comparable, Serializable {
039: static final long serialVersionUID = 358449172612757607L;
040: /** The Properties used for the project directory heirarchical names */
041: static Name emptyName;
042: static Properties nameSyntax = new Properties();
043: static {
044: nameSyntax.put("jndi.syntax.direction", "left_to_right");
045: nameSyntax.put("jndi.syntax.separator", "/");
046: try {
047: emptyName = new CompoundName("", nameSyntax);
048: } catch (NamingException e) {
049: }
050: }
051: private Name name;
052:
053: /** An alternate PermissionName comparator that first orders names by
054: length(longer names before shorter names) to ensure that the most
055: precise names are seen first.
056: */
057: public static class NameLengthComparator implements Comparator {
058: public int compare(Object o1, Object o2) {
059: PermissionName p1 = (PermissionName) o1;
060: PermissionName p2 = (PermissionName) o2;
061: // if p1 is longer than p2, its < p2 -> < 0
062: int compare = p2.size() - p1.size();
063: if (compare == 0)
064: compare = p1.compareTo(p2);
065: return compare;
066: }
067: }
068:
069: /** Creates new NamespacePermission */
070: public PermissionName(String name) throws IllegalArgumentException {
071: try {
072: this .name = new CompoundName(name, nameSyntax);
073: } catch (NamingException e) {
074: throw new IllegalArgumentException(e.toString(true));
075: }
076: }
077:
078: public PermissionName(Name name) {
079: this .name = name;
080: }
081:
082: public int compareTo(Object obj) {
083: PermissionName pn = (PermissionName) obj;
084: /* Each level must be compared. The first level to not be equals
085: determines the ordering of the names.
086: */
087: int compare = name.size() - pn.name.size();
088: int length = Math.min(name.size(), pn.name.size());
089: for (int n = 0; compare == 0 && n < length; n++) {
090: String atom0 = name.get(n);
091: String atom1 = pn.name.get(n);
092: compare = atom0.compareTo(atom1);
093: }
094: return compare;
095: }
096:
097: public boolean equals(Object obj) {
098: return compareTo(obj) == 0;
099: }
100:
101: public int hashCode() {
102: return name.hashCode();
103: }
104:
105: public int size() {
106: return name.size();
107: }
108:
109: public boolean isParent(PermissionName childName) {
110: boolean isParent = childName.name.startsWith(name);
111: return isParent;
112: }
113:
114: public String toString() {
115: return name.toString();
116: }
117: }
|