1 22 package org.jboss.mq.security; 23 24 import java.util.Set ; 25 import java.util.HashSet ; 26 import java.util.HashMap ; 27 import java.io.StringReader ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.DocumentBuilder ; 30 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.Attr ; 34 import org.w3c.dom.NodeList ; 35 36 import org.xml.sax.InputSource ; 37 import org.xml.sax.SAXException ; 38 39 import org.jboss.security.SimplePrincipal; 40 import org.jboss.logging.Logger; 41 50 51 public class SecurityMetadata { 52 static Role DEFAULT_ROLE = new Role("guest", true, true, true); 53 54 static class Role { 55 String name; 56 boolean read= false; 57 boolean write = false; 58 boolean create = false; 59 public Role(String name, boolean read, boolean write, boolean create) { 60 this.name = name; 61 this.read = read; 62 this.write = write; 63 this.create = create; 64 } 65 public String toString() { 66 return "Role {name="+name+";read="+read+";write="+write+";create="+create+"}"; 67 } 68 69 } 70 71 72 HashMap roles = new HashMap (); 73 HashSet read = new HashSet (); 74 HashSet write = new HashSet (); 75 HashSet create = new HashSet (); 76 static Logger log = Logger.getLogger(SecurityMetadata.class); 77 78 public SecurityMetadata() { 79 addRole(DEFAULT_ROLE); 80 } 81 87 public SecurityMetadata(String conf)throws Exception { 88 configure(conf); 89 } 90 public SecurityMetadata(Element conf)throws Exception { 91 configure(conf); 92 } 93 103 public void configure(String conf) throws Exception { 104 Element sec = null; 105 if (conf != null) { 106 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 107 DocumentBuilder parser = factory.newDocumentBuilder(); 108 Document doc = parser.parse(new InputSource (new StringReader (conf))); 109 sec = doc.getDocumentElement(); 110 111 } 112 configure(sec); 113 } 114 115 public void configure(Element sec) throws Exception { 116 117 if (sec == null) { 118 addRole(DEFAULT_ROLE); 119 }else { 120 121 if (!sec.getTagName().equals("security")) 122 throw new SAXException ("Configuration document not valid: root element must be security, not " + sec.getTagName()); 123 124 NodeList list = sec.getElementsByTagName("role"); 126 int l = list.getLength(); 127 for(int i = 0; i<l;i++) { 128 Element role = (Element )list.item(i); 129 Attr na = role.getAttributeNode("name"); 130 if (na == null) 131 throw new SAXException ("There must exist a name attribute of role"); 132 String n = na.getValue(); 133 boolean r = role.getAttributeNode("read") != null ? Boolean.valueOf( role.getAttributeNode("read").getValue() ).booleanValue() : false; 134 boolean w = role.getAttributeNode("write") != null ? Boolean.valueOf( role.getAttributeNode("write").getValue() ).booleanValue() : false; 135 boolean c = role.getAttributeNode("create") != null ? Boolean.valueOf( role.getAttributeNode("create").getValue() ).booleanValue() : false; 136 addRole(n,r,w,c); 137 138 } 139 } 140 } 141 142 public void addRole(String name, boolean read, boolean write, boolean create) { 143 Role r = new Role(name,read,write,create); 144 addRole(r); 145 } 146 147 public void addRole(Role r) { 148 if (log.isTraceEnabled()) 149 log.trace("Adding role: " + r.toString()); 150 151 roles.put(r.name,r); 152 SimplePrincipal p = new SimplePrincipal(r.name); 153 if(r.read == true) 154 read.add(p); 155 if(r.write == true) 156 write.add(p); 157 if (r.create == true) 158 create.add(p); 159 } 160 161 public Set getReadPrincipals() { 162 return read; 163 } 164 165 public Set getWritePrincipals() { 166 return write; 167 } 168 169 public Set getCreatePrincipals() { 170 return create; 171 } 172 } | Popular Tags |