01: /*
02: * Copyright 2002,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.catalina.users;
18:
19: import java.util.Hashtable;
20:
21: import javax.naming.Context;
22: import javax.naming.Name;
23: import javax.naming.RefAddr;
24: import javax.naming.Reference;
25: import javax.naming.spi.ObjectFactory;
26:
27: /**
28: * <p>JNDI object creation factory for <code>MemoryUserDatabase</code>
29: * instances. This makes it convenient to configure a user database
30: * in the global JNDI resources associated with this Catalina instance,
31: * and then link to that resource for web applications that administer
32: * the contents of the user database.</p>
33: *
34: * <p>The <code>MemoryUserDatabase</code> instance is configured based
35: * on the following parameter values:</p>
36: * <ul>
37: * <li><strong>pathname</strong> - Absolute or relative (to the directory
38: * path specified by the <code>catalina.base</code> system property)
39: * pathname to the XML file from which our user information is loaded,
40: * and to which it is stored. [conf/tomcat-users.xml]</li>
41: * </ul>
42: *
43: * @author Craig R. McClanahan
44: * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:50 $
45: * @since 4.1
46: */
47:
48: public class MemoryUserDatabaseFactory implements ObjectFactory {
49:
50: // --------------------------------------------------------- Public Methods
51:
52: /**
53: * <p>Create and return a new <code>MemoryUserDatabase</code> instance
54: * that has been configured according to the properties of the
55: * specified <code>Reference</code>. If you instance can be created,
56: * return <code>null</code> instead.</p>
57: *
58: * @param obj The possibly null object containing location or
59: * reference information that can be used in creating an object
60: * @param name The name of this object relative to <code>nameCtx</code>
61: * @param nameCtx The context relative to which the <code>name</code>
62: * parameter is specified, or <code>null</code> if <code>name</code>
63: * is relative to the default initial context
64: * @param environment The possibly null environment that is used in
65: * creating this object
66: */
67: public Object getObjectInstance(Object obj, Name name,
68: Context nameCtx, Hashtable environment) throws Exception {
69:
70: // We only know how to deal with <code>javax.naming.Reference</code>s
71: // that specify a class name of "org.apache.catalina.UserDatabase"
72: if ((obj == null) || !(obj instanceof Reference)) {
73: return (null);
74: }
75: Reference ref = (Reference) obj;
76: if (!"org.apache.catalina.UserDatabase".equals(ref
77: .getClassName())) {
78: return (null);
79: }
80:
81: // Create and configure a MemoryUserDatabase instance based on the
82: // RefAddr values associated with this Reference
83: MemoryUserDatabase database = new MemoryUserDatabase(name
84: .toString());
85: RefAddr ra = null;
86:
87: ra = ref.get("pathname");
88: if (ra != null) {
89: database.setPathname(ra.getContent().toString());
90: }
91:
92: // Return the configured database instance
93: database.open();
94: database.save();
95: return (database);
96:
97: }
98:
99: }
|