KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > realm > MemoryRuleSet


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

17
18
19 package org.apache.catalina.realm;
20
21
22 import org.apache.tomcat.util.digester.Digester;
23 import org.apache.tomcat.util.digester.Rule;
24 import org.apache.tomcat.util.digester.RuleSetBase;
25 import org.xml.sax.Attributes JavaDoc;
26
27
28 /**
29  * <p><strong>RuleSet</strong> for recognizing the users defined in the
30  * XML file processed by <code>MemoryRealm</code>.</p>
31  *
32  * @author Craig R. McClanahan
33  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
34  */

35
36 public class MemoryRuleSet extends RuleSetBase {
37
38
39     // ----------------------------------------------------- Instance Variables
40

41
42     /**
43      * The matching pattern prefix to use for recognizing our elements.
44      */

45     protected String JavaDoc prefix = null;
46
47
48     // ------------------------------------------------------------ Constructor
49

50
51     /**
52      * Construct an instance of this <code>RuleSet</code> with the default
53      * matching pattern prefix.
54      */

55     public MemoryRuleSet() {
56
57         this("tomcat-users/");
58
59     }
60
61
62     /**
63      * Construct an instance of this <code>RuleSet</code> with the specified
64      * matching pattern prefix.
65      *
66      * @param prefix Prefix for matching pattern rules (including the
67      * trailing slash character)
68      */

69     public MemoryRuleSet(String JavaDoc prefix) {
70
71         super();
72         this.namespaceURI = null;
73         this.prefix = prefix;
74
75     }
76
77
78     // --------------------------------------------------------- Public Methods
79

80
81     /**
82      * <p>Add the set of Rule instances defined in this RuleSet to the
83      * specified <code>Digester</code> instance, associating them with
84      * our namespace URI (if any). This method should only be called
85      * by a Digester instance.</p>
86      *
87      * @param digester Digester instance to which the new Rule instances
88      * should be added.
89      */

90     public void addRuleInstances(Digester digester) {
91
92         digester.addRule(prefix + "user", new MemoryUserRule());
93
94     }
95
96
97 }
98
99
100 /**
101  * Private class used when parsing the XML database file.
102  */

103 final class MemoryUserRule extends Rule {
104
105
106     /**
107      * Construct a new instance of this <code>Rule</code>.
108      */

109     public MemoryUserRule() {
110     }
111
112
113     /**
114      * Process a <code>&lt;user&gt;</code> element from the XML database file.
115      *
116      * @param attributes The attribute list for this element
117      */

118     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
119         throws Exception JavaDoc {
120
121         String JavaDoc username = attributes.getValue("name");
122         if (username == null) {
123             username = attributes.getValue("username");
124         }
125         String JavaDoc password = attributes.getValue("password");
126         String JavaDoc roles = attributes.getValue("roles");
127
128         MemoryRealm realm =
129             (MemoryRealm) digester.peek(digester.getCount() - 1);
130         realm.addUser(username, password, roles);
131
132     }
133
134
135 }
136
Popular Tags