01: /*
02: * Copyright 1999-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.naming.modules.fs;
18:
19: import java.util.Hashtable;
20:
21: import javax.naming.Context;
22: import javax.naming.Name;
23: import javax.naming.NamingException;
24: import javax.naming.spi.InitialContextFactory;
25: import javax.naming.spi.ObjectFactory;
26:
27: //import org.apache.naming.ContextBindings;
28:
29: /**
30: * Context factory for the "file:" namespace.
31: * <p>
32: * <b>Important note</b> : This factory MUST be associated with the "java" URL
33: * prefix, which can be done by either :
34: * <ul>
35: * <li>Adding a
36: * java.naming.factory.url.pkgs=org.apache.naming property to the JNDI properties file</li>
37: * <li>Setting an environment variable named Context.URL_PKG_PREFIXES with
38: * its value including the org.apache.naming package name.
39: * More detail about this can be found in the JNDI documentation :
40: * {@link javax.naming.spi.NamingManager#getURLContext(java.lang.String, java.util.Hashtable)}.</li>
41: * </ul>
42: *
43: * @author Remy Maucherat
44: */
45:
46: public class fsURLContextFactory implements ObjectFactory,
47: InitialContextFactory {
48: private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
49: .getLog(fsURLContextFactory.class);
50: /**
51: * Initial context.
52: */
53: protected static Context initialContext = null;
54:
55: // --------------------------------------------------------- Public Methods
56:
57: /**
58: * Crete a new Context's instance.
59: */
60: public Object getObjectInstance(Object obj, Name name,
61: Context nameCtx, Hashtable environment)
62: throws NamingException {
63: // Called with null/null/null if fs:/tmp/test
64: if (log.isDebugEnabled())
65: log.debug("getObjectInstance " + obj + " " + name + " "
66: + nameCtx + " " + environment);
67:
68: FileDirContext fc = new FileDirContext(environment);
69: fc.setDocBase("/");
70: fc.setURLPrefix("fs:");
71: return fc;
72: }
73:
74: /**
75: * Get a new (writable) initial context.
76: */
77: public Context getInitialContext(Hashtable environment)
78: throws NamingException {
79: // If the thread is not bound, return a shared writable context
80: if (initialContext == null) {
81: FileDirContext fc = new FileDirContext(environment);
82: fc.setDocBase("/");
83: fc.setURLPrefix("fs:");
84: initialContext = fc;
85: if (log.isDebugEnabled())
86: log.debug("Create initial fs context " + environment);
87: }
88: return initialContext;
89: }
90: }
|