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.users;
20
21
22 import java.util.Hashtable;
23
24 import javax.naming.Context;
25 import javax.naming.Name;
26 import javax.naming.RefAddr;
27 import javax.naming.Reference;
28 import javax.naming.spi.ObjectFactory;
29
30
31 /**
32 * <p>JNDI object creation factory for <code>MemoryUserDatabase</code>
33 * instances. This makes it convenient to configure a user database
34 * in the global JNDI resources associated with this Catalina instance,
35 * and then link to that resource for web applications that administer
36 * the contents of the user database.</p>
37 *
38 * <p>The <code>MemoryUserDatabase</code> instance is configured based
39 * on the following parameter values:</p>
40 * <ul>
41 * <li><strong>pathname</strong> - Absolute or relative (to the directory
42 * path specified by the <code>catalina.base</code> system property)
43 * pathname to the XML file from which our user information is loaded,
44 * and to which it is stored. [conf/tomcat-users.xml]</li>
45 * </ul>
46 *
47 * @author Craig R. McClanahan
48 * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
49 * @since 4.1
50 */
51
52 public class MemoryUserDatabaseFactory implements ObjectFactory {
53
54
55 // --------------------------------------------------------- Public Methods
56
57
58 /**
59 * <p>Create and return a new <code>MemoryUserDatabase</code> instance
60 * that has been configured according to the properties of the
61 * specified <code>Reference</code>. If you instance can be created,
62 * return <code>null</code> instead.</p>
63 *
64 * @param obj The possibly null object containing location or
65 * reference information that can be used in creating an object
66 * @param name The name of this object relative to <code>nameCtx</code>
67 * @param nameCtx The context relative to which the <code>name</code>
68 * parameter is specified, or <code>null</code> if <code>name</code>
69 * is relative to the default initial context
70 * @param environment The possibly null environment that is used in
71 * creating this object
72 */
73 public Object getObjectInstance(Object obj, Name name, Context nameCtx,
74 Hashtable environment)
75 throws Exception {
76
77 // We only know how to deal with <code>javax.naming.Reference</code>s
78 // that specify a class name of "org.apache.catalina.UserDatabase"
79 if ((obj == null) || !(obj instanceof Reference)) {
80 return (null);
81 }
82 Reference ref = (Reference) obj;
83 if (!"org.apache.catalina.UserDatabase".equals(ref.getClassName())) {
84 return (null);
85 }
86
87 // Create and configure a MemoryUserDatabase instance based on the
88 // RefAddr values associated with this Reference
89 MemoryUserDatabase database = new MemoryUserDatabase(name.toString());
90 RefAddr ra = null;
91
92 ra = ref.get("pathname");
93 if (ra != null) {
94 database.setPathname(ra.getContent().toString());
95 }
96
97 ra = ref.get("readonly");
98 if (ra != null) {
99 database.setReadonly(Boolean.valueOf(ra.getContent().toString()).booleanValue());
100 }
101
102 // Return the configured database instance
103 database.open();
104 database.save();
105 return (database);
106
107 }
108
109
110 }