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.naming.java;
019:
020: import java.util.Hashtable;
021: import javax.naming.Name;
022: import javax.naming.Context;
023: import javax.naming.NamingException;
024: import javax.naming.spi.ObjectFactory;
025: import javax.naming.spi.InitialContextFactory;
026: import org.apache.naming.SelectorContext;
027: import org.apache.naming.NamingContext;
028: import org.apache.naming.ContextBindings;
029:
030: /**
031: * Context factory for the "java:" namespace.
032: * <p>
033: * <b>Important note</b> : This factory MUST be associated with the "java" URL
034: * prefix, which can be done by either :
035: * <ul>
036: * <li>Adding a
037: * java.naming.factory.url.pkgs=org.apache.catalina.util.naming property
038: * to the JNDI properties file</li>
039: * <li>Setting an environment variable named Context.URL_PKG_PREFIXES with
040: * its value including the org.apache.catalina.util.naming package name.
041: * More detail about this can be found in the JNDI documentation :
042: * {@link javax.naming.spi.NamingManager#getURLContext(java.lang.String, java.util.Hashtable)}.</li>
043: * </ul>
044: *
045: * @author Remy Maucherat
046: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
047: */
048:
049: public class javaURLContextFactory implements ObjectFactory,
050: InitialContextFactory {
051:
052: // ----------------------------------------------------------- Constructors
053:
054: // -------------------------------------------------------------- Constants
055:
056: public static final String MAIN = "initialContext";
057:
058: // ----------------------------------------------------- Instance Variables
059:
060: /**
061: * Initial context.
062: */
063: protected static Context initialContext = null;
064:
065: // --------------------------------------------------------- Public Methods
066:
067: // -------------------------------------------------- ObjectFactory Methods
068:
069: /**
070: * Crete a new Context's instance.
071: */
072: public Object getObjectInstance(Object obj, Name name,
073: Context nameCtx, Hashtable environment)
074: throws NamingException {
075: if ((ContextBindings.isThreadBound())
076: || (ContextBindings.isClassLoaderBound())) {
077: return new SelectorContext(environment);
078: } else {
079: return null;
080: }
081: }
082:
083: /**
084: * Get a new (writable) initial context.
085: */
086: public Context getInitialContext(Hashtable environment)
087: throws NamingException {
088: if (ContextBindings.isThreadBound()
089: || (ContextBindings.isClassLoaderBound())) {
090: // Redirect the request to the bound initial context
091: return new SelectorContext(environment, true);
092: } else {
093: // If the thread is not bound, return a shared writable context
094: if (initialContext == null)
095: initialContext = new NamingContext(environment, MAIN);
096: return initialContext;
097: }
098: }
099:
100: }
|