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