001: /*
002: * Copyright 1999-2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.realm;
018:
019: import org.apache.commons.digester.Digester;
020: import org.apache.commons.digester.Rule;
021: import org.apache.commons.digester.RuleSetBase;
022: import org.xml.sax.Attributes;
023:
024: /**
025: * <p><strong>RuleSet</strong> for recognizing the users defined in the
026: * XML file processed by <code>MemoryRealm</code>.</p>
027: *
028: * @author Craig R. McClanahan
029: * @version $Revision: 1.4 $ $Date: 2004/05/26 15:51:19 $
030: */
031:
032: public class MemoryRuleSet extends RuleSetBase {
033:
034: // ----------------------------------------------------- Instance Variables
035:
036: /**
037: * The matching pattern prefix to use for recognizing our elements.
038: */
039: protected String prefix = null;
040:
041: // ------------------------------------------------------------ Constructor
042:
043: /**
044: * Construct an instance of this <code>RuleSet</code> with the default
045: * matching pattern prefix.
046: */
047: public MemoryRuleSet() {
048:
049: this ("tomcat-users/");
050:
051: }
052:
053: /**
054: * Construct an instance of this <code>RuleSet</code> with the specified
055: * matching pattern prefix.
056: *
057: * @param prefix Prefix for matching pattern rules (including the
058: * trailing slash character)
059: */
060: public MemoryRuleSet(String prefix) {
061:
062: super ();
063: this .namespaceURI = null;
064: this .prefix = prefix;
065:
066: }
067:
068: // --------------------------------------------------------- Public Methods
069:
070: /**
071: * <p>Add the set of Rule instances defined in this RuleSet to the
072: * specified <code>Digester</code> instance, associating them with
073: * our namespace URI (if any). This method should only be called
074: * by a Digester instance.</p>
075: *
076: * @param digester Digester instance to which the new Rule instances
077: * should be added.
078: */
079: public void addRuleInstances(Digester digester) {
080:
081: digester.addRule(prefix + "user", new MemoryUserRule());
082:
083: }
084:
085: }
086:
087: /**
088: * Private class used when parsing the XML database file.
089: */
090: final class MemoryUserRule extends Rule {
091:
092: /**
093: * Construct a new instance of this <code>Rule</code>.
094: */
095: public MemoryUserRule() {
096: }
097:
098: /**
099: * Process a <code><user></code> element from the XML database file.
100: *
101: * @param attributes The attribute list for this element
102: */
103: public void begin(String namespace, String name,
104: Attributes attributes) throws Exception {
105:
106: String username = attributes.getValue("name");
107: if (username == null) {
108: username = attributes.getValue("username");
109: }
110: String password = attributes.getValue("password");
111: String roles = attributes.getValue("roles");
112:
113: MemoryRealm realm = (MemoryRealm) digester.peek(digester
114: .getCount() - 1);
115: realm.addUser(username, password, roles);
116:
117: }
118:
119: }
|