KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > security > AuthorizationEntry


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.security;
19
20 import org.apache.activemq.filter.DestinationMapEntry;
21
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 /**
30  * Represents an entry in a {@link DefaultAuthorizationMap} for assigning
31  * different operations (read, write, admin) of user roles to a specific
32  * destination or a hierarchical wildcard area of destinations.
33  *
34  * @org.apache.xbean.XBean
35  *
36  * @version $Revision: 486131 $
37  */

38 public class AuthorizationEntry extends DestinationMapEntry {
39
40     private Set JavaDoc readACLs = Collections.EMPTY_SET;
41     private Set JavaDoc writeACLs = Collections.EMPTY_SET;
42     private Set JavaDoc adminACLs = Collections.EMPTY_SET;
43     
44     private String JavaDoc adminRoles = null;
45     private String JavaDoc readRoles = null;
46     private String JavaDoc writeRoles = null;
47     
48     private String JavaDoc groupClass = "org.apache.activemq.jaas.GroupPrincipal";
49         
50     public String JavaDoc getGroupClass() {
51         return groupClass;
52     }
53      
54     public void setGroupClass(String JavaDoc groupClass) {
55         this.groupClass = groupClass;
56     }
57
58     public Set JavaDoc getAdminACLs() {
59         return adminACLs;
60     }
61
62     public void setAdminACLs(Set JavaDoc adminACLs) {
63         this.adminACLs = adminACLs;
64     }
65
66     public Set JavaDoc getReadACLs() {
67         return readACLs;
68     }
69
70     public void setReadACLs(Set JavaDoc readACLs) {
71         this.readACLs = readACLs;
72     }
73
74     public Set JavaDoc getWriteACLs() {
75         return writeACLs;
76     }
77
78     public void setWriteACLs(Set JavaDoc writeACLs) {
79         this.writeACLs = writeACLs;
80     }
81
82     // helper methods for easier configuration in Spring
83
// ACLs are already set in the afterPropertiesSet method to ensure that groupClass is set first before
84
// calling parceACLs() on any of the roles. We still need to add the call to parceACLs inside the helper
85
// methods for instances where we configure security programatically without using xbean
86
// -------------------------------------------------------------------------
87
public void setAdmin(String JavaDoc roles) throws Exception JavaDoc {
88         adminRoles = roles;
89         setAdminACLs(parseACLs(adminRoles));
90     }
91
92     public void setRead(String JavaDoc roles) throws Exception JavaDoc {
93         readRoles = roles;
94         setReadACLs(parseACLs(readRoles));
95     }
96
97     public void setWrite(String JavaDoc roles) throws Exception JavaDoc {
98         writeRoles = roles;
99         setWriteACLs(parseACLs(writeRoles));
100     }
101
102     protected Set JavaDoc parseACLs(String JavaDoc roles) throws Exception JavaDoc {
103         Set JavaDoc answer = new HashSet JavaDoc();
104         StringTokenizer JavaDoc iter = new StringTokenizer JavaDoc(roles, ",");
105         while (iter.hasMoreTokens()) {
106             String JavaDoc name = iter.nextToken().trim();
107             Class JavaDoc[] paramClass = new Class JavaDoc[1];
108             paramClass[0] = String JavaDoc.class;
109             
110             Object JavaDoc[] param = new Object JavaDoc[1];
111             param[0] = new String JavaDoc(name);
112
113             try {
114                 Class JavaDoc cls = Class.forName(groupClass);
115                 
116                 Constructor JavaDoc[] constructors = cls.getConstructors();
117                 int i;
118                 for (i=0; i<constructors.length; i++) {
119                     Class JavaDoc[] paramTypes = constructors[i].getParameterTypes();
120                     if (paramTypes.length!=0 && paramTypes[0].equals(paramClass[0])) break;
121                 }
122                 if (i < constructors.length) {
123                     Object JavaDoc instance = constructors[i].newInstance(param);
124                     answer.add(instance);
125                 }
126                 else {
127                     Object JavaDoc instance = cls.newInstance();
128                     Method JavaDoc[] methods = cls.getMethods();
129                     i=0;
130                     for (i=0; i<methods.length; i++) {
131                         Class JavaDoc[] paramTypes = methods[i].getParameterTypes();
132                         if (paramTypes.length!=0 && methods[i].getName().equals("setName") && paramTypes[0].equals(paramClass[0])) break;
133                     }
134                         
135                     if (i < methods.length) {
136                         methods[i].invoke(instance, param);
137                         answer.add(instance);
138                     }
139                     else throw new NoSuchMethodException JavaDoc();
140                 }
141             }
142             catch (Exception JavaDoc e) { throw e; }
143         }
144         return answer;
145     }
146     
147     public void afterPropertiesSet() throws Exception JavaDoc {
148         super.afterPropertiesSet();
149         
150         if(adminRoles!=null) {
151             setAdminACLs(parseACLs(adminRoles));
152         }
153
154         if(writeRoles!=null) {
155             setWriteACLs(parseACLs(writeRoles));
156         }
157         
158         if(readRoles!=null) {
159             setReadACLs(parseACLs(readRoles));
160         }
161         
162     }
163 }
164
Popular Tags