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.users;
019:
020: import java.util.Hashtable;
021:
022: import javax.naming.Context;
023: import javax.naming.Name;
024: import javax.naming.RefAddr;
025: import javax.naming.Reference;
026: import javax.naming.spi.ObjectFactory;
027:
028: /**
029: * <p>JNDI object creation factory for <code>MemoryUserDatabase</code>
030: * instances. This makes it convenient to configure a user database
031: * in the global JNDI resources associated with this Catalina instance,
032: * and then link to that resource for web applications that administer
033: * the contents of the user database.</p>
034: *
035: * <p>The <code>MemoryUserDatabase</code> instance is configured based
036: * on the following parameter values:</p>
037: * <ul>
038: * <li><strong>pathname</strong> - Absolute or relative (to the directory
039: * path specified by the <code>catalina.base</code> system property)
040: * pathname to the XML file from which our user information is loaded,
041: * and to which it is stored. [conf/tomcat-users.xml]</li>
042: * </ul>
043: *
044: * @author Craig R. McClanahan
045: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
046: * @since 4.1
047: */
048:
049: public class MemoryUserDatabaseFactory implements ObjectFactory {
050:
051: // --------------------------------------------------------- Public Methods
052:
053: /**
054: * <p>Create and return a new <code>MemoryUserDatabase</code> instance
055: * that has been configured according to the properties of the
056: * specified <code>Reference</code>. If you instance can be created,
057: * return <code>null</code> instead.</p>
058: *
059: * @param obj The possibly null object containing location or
060: * reference information that can be used in creating an object
061: * @param name The name of this object relative to <code>nameCtx</code>
062: * @param nameCtx The context relative to which the <code>name</code>
063: * parameter is specified, or <code>null</code> if <code>name</code>
064: * is relative to the default initial context
065: * @param environment The possibly null environment that is used in
066: * creating this object
067: */
068: public Object getObjectInstance(Object obj, Name name,
069: Context nameCtx, Hashtable environment) throws Exception {
070:
071: // We only know how to deal with <code>javax.naming.Reference</code>s
072: // that specify a class name of "org.apache.catalina.UserDatabase"
073: if ((obj == null) || !(obj instanceof Reference)) {
074: return (null);
075: }
076: Reference ref = (Reference) obj;
077: if (!"org.apache.catalina.UserDatabase".equals(ref
078: .getClassName())) {
079: return (null);
080: }
081:
082: // Create and configure a MemoryUserDatabase instance based on the
083: // RefAddr values associated with this Reference
084: MemoryUserDatabase database = new MemoryUserDatabase(name
085: .toString());
086: RefAddr ra = null;
087:
088: ra = ref.get("pathname");
089: if (ra != null) {
090: database.setPathname(ra.getContent().toString());
091: }
092:
093: ra = ref.get("readonly");
094: if (ra != null) {
095: database.setReadonly(Boolean.valueOf(
096: ra.getContent().toString()).booleanValue());
097: }
098:
099: // Return the configured database instance
100: database.open();
101: database.save();
102: return (database);
103:
104: }
105:
106: }
|