KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import org.apache.activemq.jaas.GroupPrincipal;
29
30 import org.apache.activemq.broker.Broker;
31 import org.apache.activemq.broker.BrokerPlugin;
32
33 import java.util.Map JavaDoc;
34
35 /**
36  * A simple authentication plugin
37  *
38  * @org.apache.xbean.XBean element="simpleAuthenticationPlugin" description="Provides a simple authentication
39  * plugin configured with a map of user-passwords and a map of user-groups or a list of authentication users"
40  *
41  * @version $Revision: 496571 $
42  */

43 public class SimpleAuthenticationPlugin implements BrokerPlugin {
44     private Map JavaDoc userPasswords;
45     private Map JavaDoc userGroups;
46
47     public SimpleAuthenticationPlugin() {}
48
49     public SimpleAuthenticationPlugin(List JavaDoc users) {
50         setUsers(users);
51     }
52
53     public Broker installPlugin(Broker broker) {
54         return new SimpleAuthenticationBroker(broker, userPasswords, userGroups);
55     }
56
57     public Map JavaDoc getUserGroups() {
58         return userGroups;
59     }
60     
61     /**
62      * Sets individual users for authentication
63      *
64      * @org.apache.xbean.ElementType class="org.apache.activemq.security.AuthenticationUser"
65      */

66     public void setUsers(List JavaDoc users) {
67         userPasswords = new HashMap JavaDoc();
68         userGroups = new HashMap JavaDoc();
69         for (Iterator JavaDoc it = users.iterator(); it.hasNext();) {
70             AuthenticationUser user = (AuthenticationUser)it.next();
71             userPasswords.put(user.getUsername(), user.getPassword());
72             Set JavaDoc groups = new HashSet JavaDoc();
73             StringTokenizer JavaDoc iter = new StringTokenizer JavaDoc(user.getGroups(), ",");
74             while (iter.hasMoreTokens()) {
75                 String JavaDoc name = iter.nextToken().trim();
76                 groups.add(new GroupPrincipal(name));
77             }
78             userGroups.put(user.getUsername(), groups);
79         }
80     }
81
82     /**
83      * Sets the groups a user is in. The key is the user name and the value is a Set of groups
84      */

85     public void setUserGroups(Map JavaDoc userGroups) {
86         this.userGroups = userGroups;
87     }
88
89     public Map JavaDoc getUserPasswords() {
90         return userPasswords;
91     }
92
93     /**
94      * Sets the map indexed by user name with the value the password
95      */

96     public void setUserPasswords(Map JavaDoc userPasswords) {
97         this.userPasswords = userPasswords;
98     }
99
100 }
101
Popular Tags