KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > security > SecurityMetadata


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.mq.security;
23
24 import java.util.Set JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Attr JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35
36 import org.xml.sax.InputSource JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38
39 import org.jboss.security.SimplePrincipal;
40 import org.jboss.logging.Logger;
41 /**
42  * SecurityMetadata.java
43  *
44  *
45  * Created: Tue Feb 26 15:02:29 2002
46  *
47  * @author Peter
48  * @version
49  */

50
51 public class SecurityMetadata {
52    static Role DEFAULT_ROLE = new Role("guest", true, true, true);
53
54    static class Role {
55       String JavaDoc name;
56       boolean read= false;
57       boolean write = false;
58       boolean create = false;
59       public Role(String JavaDoc 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 JavaDoc toString() {
66          return "Role {name="+name+";read="+read+";write="+write+";create="+create+"}";
67       }
68
69    }
70
71
72    HashMap JavaDoc roles = new HashMap JavaDoc();
73    HashSet JavaDoc read = new HashSet JavaDoc();
74    HashSet JavaDoc write = new HashSet JavaDoc();
75    HashSet JavaDoc create = new HashSet JavaDoc();
76    static Logger log = Logger.getLogger(SecurityMetadata.class);
77    
78    public SecurityMetadata() {
79       addRole(DEFAULT_ROLE);
80    }
81    /**
82     * Create with given xml @see configure.
83     *
84     * If the configure script is null, a default role named guest will be
85     * created with read and write access, but no create access.
86     */

87    public SecurityMetadata(String JavaDoc conf)throws Exception JavaDoc {
88       configure(conf);
89    }
90    public SecurityMetadata(Element JavaDoc conf)throws Exception JavaDoc {
91       configure(conf);
92    }
93    /**
94     * Configure with an xml string.
95     *
96     * The format of the string is:
97     * <security>
98     * <role name="nameOfRole" read="true" write="true" create="false"/>
99     * </security>
100     *
101     * There may be one or more role elements.
102     */

103    public void configure(String JavaDoc conf) throws Exception JavaDoc {
104       Element JavaDoc sec = null;
105       if (conf != null) {
106          DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
107          DocumentBuilder JavaDoc parser = factory.newDocumentBuilder();
108          Document JavaDoc doc = parser.parse(new InputSource JavaDoc(new StringReader JavaDoc(conf)));
109          sec = doc.getDocumentElement();
110          
111       }
112       configure(sec);
113    }
114       
115    public void configure(Element JavaDoc sec) throws Exception JavaDoc {
116    
117       if (sec == null) {
118          addRole(DEFAULT_ROLE);
119       }else {
120
121          if (!sec.getTagName().equals("security"))
122             throw new SAXException JavaDoc("Configuration document not valid: root element must be security, not " + sec.getTagName());
123          
124          // Parse
125
NodeList JavaDoc list = sec.getElementsByTagName("role");
126          int l = list.getLength();
127          for(int i = 0; i<l;i++) {
128             Element JavaDoc role = (Element JavaDoc)list.item(i);
129             Attr JavaDoc na = role.getAttributeNode("name");
130             if (na == null)
131                throw new SAXException JavaDoc("There must exist a name attribute of role");
132             String JavaDoc 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 JavaDoc 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 JavaDoc getReadPrincipals() {
162       return read;
163    }
164    
165    public Set JavaDoc getWritePrincipals() {
166       return write;
167    }
168
169    public Set JavaDoc getCreatePrincipals() {
170       return create;
171    }
172 } // SecurityMetadata
173
Popular Tags