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.mq.security;
023:
024: import java.util.Set;
025: import java.util.HashSet;
026: import java.util.HashMap;
027: import java.io.StringReader;
028: import javax.xml.parsers.DocumentBuilderFactory;
029: import javax.xml.parsers.DocumentBuilder;
030:
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.Attr;
034: import org.w3c.dom.NodeList;
035:
036: import org.xml.sax.InputSource;
037: import org.xml.sax.SAXException;
038:
039: import org.jboss.security.SimplePrincipal;
040: import org.jboss.logging.Logger;
041:
042: /**
043: * SecurityMetadata.java
044: *
045: *
046: * Created: Tue Feb 26 15:02:29 2002
047: *
048: * @author Peter
049: * @version
050: */
051:
052: public class SecurityMetadata {
053: static Role DEFAULT_ROLE = new Role("guest", true, true, true);
054:
055: static class Role {
056: String name;
057: boolean read = false;
058: boolean write = false;
059: boolean create = false;
060:
061: public Role(String name, boolean read, boolean write,
062: boolean create) {
063: this .name = name;
064: this .read = read;
065: this .write = write;
066: this .create = create;
067: }
068:
069: public String toString() {
070: return "Role {name=" + name + ";read=" + read + ";write="
071: + write + ";create=" + create + "}";
072: }
073:
074: }
075:
076: HashMap roles = new HashMap();
077: HashSet read = new HashSet();
078: HashSet write = new HashSet();
079: HashSet create = new HashSet();
080: static Logger log = Logger.getLogger(SecurityMetadata.class);
081:
082: public SecurityMetadata() {
083: addRole(DEFAULT_ROLE);
084: }
085:
086: /**
087: * Create with given xml @see configure.
088: *
089: * If the configure script is null, a default role named guest will be
090: * created with read and write access, but no create access.
091: */
092: public SecurityMetadata(String conf) throws Exception {
093: configure(conf);
094: }
095:
096: public SecurityMetadata(Element conf) throws Exception {
097: configure(conf);
098: }
099:
100: /**
101: * Configure with an xml string.
102: *
103: * The format of the string is:
104: * <security>
105: * <role name="nameOfRole" read="true" write="true" create="false"/>
106: * </security>
107: *
108: * There may be one or more role elements.
109: */
110: public void configure(String conf) throws Exception {
111: Element sec = null;
112: if (conf != null) {
113: DocumentBuilderFactory factory = DocumentBuilderFactory
114: .newInstance();
115: DocumentBuilder parser = factory.newDocumentBuilder();
116: Document doc = parser.parse(new InputSource(
117: new StringReader(conf)));
118: sec = doc.getDocumentElement();
119:
120: }
121: configure(sec);
122: }
123:
124: public void configure(Element sec) throws Exception {
125:
126: if (sec == null) {
127: addRole(DEFAULT_ROLE);
128: } else {
129:
130: if (!sec.getTagName().equals("security"))
131: throw new SAXException(
132: "Configuration document not valid: root element must be security, not "
133: + sec.getTagName());
134:
135: // Parse
136: NodeList list = sec.getElementsByTagName("role");
137: int l = list.getLength();
138: for (int i = 0; i < l; i++) {
139: Element role = (Element) list.item(i);
140: Attr na = role.getAttributeNode("name");
141: if (na == null)
142: throw new SAXException(
143: "There must exist a name attribute of role");
144: String n = na.getValue();
145: boolean r = role.getAttributeNode("read") != null ? Boolean
146: .valueOf(
147: role.getAttributeNode("read")
148: .getValue()).booleanValue()
149: : false;
150: boolean w = role.getAttributeNode("write") != null ? Boolean
151: .valueOf(
152: role.getAttributeNode("write")
153: .getValue()).booleanValue()
154: : false;
155: boolean c = role.getAttributeNode("create") != null ? Boolean
156: .valueOf(
157: role.getAttributeNode("create")
158: .getValue()).booleanValue()
159: : false;
160: addRole(n, r, w, c);
161:
162: }
163: }
164: }
165:
166: public void addRole(String name, boolean read, boolean write,
167: boolean create) {
168: Role r = new Role(name, read, write, create);
169: addRole(r);
170: }
171:
172: public void addRole(Role r) {
173: if (log.isTraceEnabled())
174: log.trace("Adding role: " + r.toString());
175:
176: roles.put(r.name, r);
177: SimplePrincipal p = new SimplePrincipal(r.name);
178: if (r.read == true)
179: read.add(p);
180: if (r.write == true)
181: write.add(p);
182: if (r.create == true)
183: create.add(p);
184: }
185:
186: public Set getReadPrincipals() {
187: return read;
188: }
189:
190: public Set getWritePrincipals() {
191: return write;
192: }
193:
194: public Set getCreatePrincipals() {
195: return create;
196: }
197: } // SecurityMetadata
|